Advertisement
HappySunChild

MP3 Player

Mar 8th, 2022 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.68 KB | None | 0 0
  1. ---@diagnostic disable: undefined-field
  2. local speaker = peripheral.find("speaker")
  3.  
  4. term.clear()
  5. term.setCursorPos(1,1)
  6.  
  7. local function promptInput(promptText)
  8.     term.write(promptText)
  9.     return read()
  10. end
  11.  
  12. local volume = 1
  13. local pitch = 1
  14.  
  15. local function playSound(soundPath)
  16.     if soundPath and soundPath ~= "" then
  17.         speaker.playSound(soundPath,tonumber(volume) or 1,tonumber(pitch) or 1)
  18.     end
  19. end
  20.  
  21. local RECORDS = {
  22.     {"11",colors.brown},
  23.     {"13",colors.red},
  24.     {"blocks",colors.orange},
  25.     {"cat",colors.yellow},
  26.     {"chirp",colors.lime},
  27.     {"far",colors.green},
  28.     {"mall",colors.cyan},
  29.     {"mellohi",colors.lightBlue},
  30.     {"pigstep",colors.blue},
  31.     {"stal",colors.purple},
  32.     {"strad",colors.magenta},
  33.     {"wait",colors.pink},
  34.     {"ward",colors.red}
  35. }
  36.  
  37. local function isARecord(str)
  38.     for i,record in pairs(RECORDS) do
  39.         if str == record[1] then
  40.             return true, "music_disc." .. str
  41.         end
  42.     end
  43.  
  44.     return false
  45. end
  46.  
  47. local function printCenter(y, str, col)
  48.     term.setTextColor(col or colors.white)
  49.  
  50.     local x = math.floor((26 - string.len(str)) / 2)
  51.     term.setCursorPos(x,y)
  52.     term.clearLine()
  53.     term.write(str)
  54. end
  55.  
  56. local function resetTerm()
  57.     term.setCursorPos(1,1)
  58.     term.clear()
  59.     term.setTextColor(colors.white)
  60. end
  61.  
  62. function colors.random()
  63.     local rand = math.random(1,16)
  64.     local c = colors.white
  65.     local cols = {}
  66.  
  67.     for i, color in pairs(colors) do
  68.         if type(color) == "number" then
  69.             table.insert(cols, color)
  70.         end
  71.     end
  72.  
  73.     return cols[rand]
  74. end
  75.  
  76. local function createSaveFile()
  77.     if not fs.exists("saved.txt") then
  78.         local file = fs.open("saved.txt","w")
  79.         file.write("")
  80.         file.close()
  81.     end
  82. end
  83.  
  84. local function resetSaveFile()
  85.     if not fs.exists("saved.txt") then
  86.         createSaveFile()
  87.     else
  88.         local file = fs.open("saved.txt","w")
  89.         file.write("")
  90.         file.close()
  91.     end
  92. end
  93.  
  94. local function addJsonIdToSaveFile(id,name)
  95.     if not fs.exists("saved.txt") then
  96.         createSaveFile()
  97.         addJsonIdToSavedFile(id,name)
  98.     else
  99.         local file = fs.open("saved.txt","a")
  100.         file.write(id .. "\n" .. name .. "\n" .. colors.random() .. "\n")
  101.         file.close()
  102.     end
  103. end
  104.  
  105. local function readSaveFile()
  106.     if not fs.exists("saved.txt") then
  107.         createSaveFile()
  108.         readSavedFile()
  109.     else
  110.         local file = fs.open("saved.txt","r")
  111.         local saved = {}
  112.  
  113.         local i = 1
  114.         repeat
  115.             local id = file.readLine()
  116.             local name = file.readLine()
  117.             local color = file.readLine()
  118.             if id then
  119.                 saved[i] = {Id = id, Name = name, Color = color}
  120.             end
  121.             i = i + 1
  122.         until id == nil or name == nil or color == nil
  123.  
  124.         file.close()
  125.         return saved
  126.     end
  127. end
  128.  
  129. local function reconstructSaveFile(saveTable)
  130.     if not fs.exists("saved.txt") then
  131.         createSaveFile()
  132.     else
  133.         resetSaveFile()
  134.         local file = fs.open("saved.txt","a")
  135.        
  136.         for _,save in pairs(saveTable) do
  137.             file.write(save.Id .. "\n" .. save.Name .. "\n" .. save.Color .. "\n")
  138.         end
  139.  
  140.         file.close()
  141.     end
  142. end
  143.  
  144. local function List()
  145.     term.clear()
  146.  
  147.         printCenter(10,"Press up or down arrow")
  148.         printCenter(11,"to continue")
  149.        
  150.         local currentSelected = 1
  151.  
  152.         while true do
  153.             local event, key = os.pullEvent("key_up")
  154.             local name = keys.getName(key) or "not a key"
  155.  
  156.             if name == "down" then
  157.                 currentSelected = currentSelected + 1
  158.  
  159.                 if currentSelected > #RECORDS then
  160.                     currentSelected = 1
  161.                 end
  162.             elseif name == "up" then
  163.                 currentSelected = currentSelected - 1
  164.  
  165.                 if currentSelected < 1 then
  166.                     currentSelected = #RECORDS
  167.                 end
  168.             end
  169.  
  170.             if name == "down" or name == "up" then
  171.                 resetTerm()
  172.  
  173.                 printCenter(1,"All records: ")
  174.                
  175.                 for i,record in pairs(RECORDS) do
  176.                     if i == currentSelected then
  177.                         printCenter(i+2,"> " .. record[1] .. " <")
  178.                     else
  179.                         printCenter(i+2,record[1],record[2])
  180.                     end
  181.                 end
  182.  
  183.                 printCenter(19,"Press x to exit.")
  184.             elseif name == "enter" then
  185.                 speaker.playSound("block.comparator.click",1)
  186.                 sleep(0.2)
  187.                 playSound("music_disc." .. RECORDS[currentSelected][1])
  188.             elseif name == "x" then
  189.                 break
  190.             end
  191.         end
  192.  
  193.         term.clear()
  194.         term.setCursorPos(1,1)
  195. end
  196.  
  197. function Saves()
  198.     term.clear()
  199.  
  200.     local currentSelected = 1
  201.     local currentPlaying = 1
  202.  
  203.     local SAVED = readSaveFile()
  204.  
  205.     if #SAVED > 0 then
  206.         printCenter(9,"Press up or down arrow")
  207.         printCenter(10,"to continue")
  208.  
  209.         printCenter(13,"You have " .. #SAVED .. " saved song(s)")
  210.  
  211.         while true do
  212.             local event, key = os.pullEvent("key_up")
  213.             local name = keys.getName(key) or "not a key"
  214.  
  215.             if name == "down" then
  216.                 currentSelected = currentSelected + 1
  217.  
  218.                 if currentSelected > #SAVED then
  219.                     currentSelected = 1
  220.                 end
  221.             elseif name == "up" then
  222.                 currentSelected = currentSelected - 1
  223.  
  224.                 if currentSelected < 1 then
  225.                     currentSelected = #SAVED
  226.                 end
  227.             end
  228.  
  229.             if name == "down" or name == "up" then
  230.                 resetTerm()
  231.  
  232.                 local currentPlayingName = SAVED[currentPlaying] and SAVED[currentPlaying].Name or "null"
  233.                
  234.                 printCenter(1," All Saves",colors.green)
  235.                 printCenter(2," Playing: " .. currentPlayingName,colors.red)
  236.                
  237.                 for i,save in pairs(SAVED) do
  238.                     if i == currentSelected then
  239.                         printCenter(i+3,"> " .. save.Name .. " <",tonumber(save.Color))
  240.                     else
  241.                         printCenter(i+3,save.Name,tonumber(save.Color))
  242.                     end
  243.                 end
  244.  
  245.                 printCenter(19,"  Press x to exit")
  246.                 printCenter(18,"  Press r to remove")
  247.             elseif name == "enter" then
  248.                 speaker.playSound("block.comparator.click",0.4)
  249.                 sleep(0.2)
  250.  
  251.                 currentPlaying = currentSelected
  252.                 currentPlayingName = SAVED[currentPlaying] and SAVED[currentPlaying].Name or "null"
  253.                 printCenter(2," Playing: " .. currentPlayingName,colors.red)
  254.                 playSound(SAVED[currentSelected].Id)
  255.             elseif name == "r" then
  256.                 resetTerm()
  257.  
  258.                 printCenter(6," Delete?",colors.red)
  259.                 printCenter(10,'"' .. SAVED[currentSelected].Name .. '"',colors.white)
  260.                 printCenter(13,"No")
  261.                 printCenter(14,"Yeah")
  262.  
  263.                 local currentSelected2 = 1
  264.                 local options = {"No","Yeah"}
  265.  
  266.                 while true do
  267.                     local event, key = os.pullEvent("key_up")
  268.                     local name = keys.getName(key) or "not a key"
  269.  
  270.                     if name == "down" then
  271.                         currentSelected2 = currentSelected2 + 1
  272.  
  273.                         if currentSelected2 > #options then
  274.                             currentSelected2 = 1
  275.                         end
  276.                     elseif name == "up" then
  277.                         currentSelected2 = currentSelected2 - 1
  278.  
  279.                         if currentSelected2 < 1 then
  280.                             currentSelected2 = #options
  281.                         end
  282.                     end
  283.  
  284.                     if name == "down" or name == "up" then
  285.                         resetTerm()
  286.  
  287.                         printCenter(6, " Delete?",colors.red)
  288.                         printCenter(10,'"' .. SAVED[currentSelected].Name .. '"', colors.white)
  289.                         printCenter(13,"No")
  290.                         printCenter(14,"Yeah")
  291.  
  292.                         for i, option in pairs(options) do
  293.                             if i == currentSelected2 then
  294.                                 printCenter(i + 12,"> " .. option .. " <", colors.red)
  295.                             else
  296.                                 printCenter(i + 12,option)
  297.                             end
  298.                         end
  299.                     elseif name == "enter" then
  300.                         if currentSelected2 == 2 then
  301.                             local newSAVED = {}
  302.  
  303.                             for i, save in pairs(SAVED) do
  304.                                 if not (save.Id == SAVED[currentSelected].Id and save.Name == SAVED[currentSelected].Name) then
  305.                                     newSAVED[i] = save
  306.                                 end
  307.                             end
  308.  
  309.                             reconstructSaveFile(newSAVED)
  310.  
  311.                             resetTerm()
  312.  
  313.                             printCenter(10,"Deleted " .. SAVED[currentSelected].Name,colors.red)
  314.  
  315.                             speaker.playNote("bit",1,24)
  316.                             sleep(0.15)
  317.                             speaker.playNote("bit",2,20)
  318.                             sleep(0.1)
  319.                             speaker.playNote("bit",2,16)
  320.  
  321.                             sleep(1)
  322.  
  323.                             resetTerm()
  324.                             SAVED = newSAVED
  325.  
  326.                             printCenter(9,"Press up or down arrow")
  327.                             printCenter(10,"to continue")
  328.  
  329.                             printCenter(13,"You have " .. #SAVED .. " saved song(s)")
  330.  
  331.                             break
  332.                         else
  333.                             resetTerm()
  334.                             break
  335.                         end
  336.                     end
  337.                 end
  338.             elseif name == "x" then
  339.                 break
  340.             end
  341.         end
  342.     else
  343.         printCenter(10,"  You don't have any saves",colors.red)
  344.        
  345.         sleep(2)
  346.     end
  347.  
  348.     resetTerm()
  349. end
  350.  
  351. function Save()
  352.     resetTerm()
  353.  
  354.     local id = promptInput("Save id: ")
  355.     local name = promptInput("Save name: ")
  356.  
  357.     addJsonIdToSaveFile(id,name)
  358.  
  359.     play = ""
  360.    
  361.     resetTerm()
  362.  
  363.     printCenter(10,"Saved " .. id,colors.lime)
  364.     printCenter(11,"as " .. name, colors.lime)
  365.  
  366.     speaker.playNote("bit",1,16)
  367.     sleep(0.15)
  368.     speaker.playNote("bit",2,20)
  369.     sleep(0.1)
  370.     speaker.playNote("bit",2,24)
  371.  
  372.     sleep(1)
  373.    
  374.     resetTerm()
  375. end
  376.  
  377. local currentSelected = 1
  378. local options = {{Name = "Records",Color = colors.red},{Name = "Saves",Color = colors.orange},{Name = "Save a song",Color = colors.yellow},{Name = "Volume", Color = colors.green},{Name = "Pitch", Color = colors.lime},{Name = "Manual Input",Color = colors.blue},{Name = "Stop",Color = colors.lightBlue}}
  379.  
  380. printCenter(9,"If the screen is")
  381. printCenter(10,"just black then")
  382. printCenter(11,"press up or down")
  383.  
  384. while true do
  385.     local event, key = os.pullEvent("key_up")
  386.     local name = keys.getName(key) or "not a key"
  387.  
  388.     if name == "down" then
  389.         currentSelected = currentSelected + 1
  390.  
  391.         if currentSelected > #options then
  392.             currentSelected = 1
  393.         end
  394.     elseif name == "up" then
  395.         currentSelected = currentSelected - 1
  396.  
  397.         if currentSelected < 1 then
  398.             currentSelected = #options
  399.         end
  400.     end
  401.  
  402.     if name == "down" or name == "up" then
  403.         resetTerm()
  404.  
  405.         printCenter(1,"Player Options")
  406.  
  407.         for i,option in pairs(options) do
  408.             if i == currentSelected then
  409.  
  410.                 printCenter(i+2,"> " .. option.Name .. " <")
  411.             else
  412.                 printCenter(i+2,option.Name,option.Color)
  413.             end
  414.         end
  415.     elseif name == "enter" then
  416.         if currentSelected == 1 then
  417.             List()
  418.         elseif currentSelected == 2 then
  419.             Saves()
  420.         elseif currentSelected == 3 then
  421.             Save()
  422.         elseif currentSelected == 4 then
  423.             resetTerm()
  424.             volume = promptInput("Enter volume: ")
  425.             currentSelected = 0
  426.             resetTerm()
  427.         elseif currentSelected == 5 then
  428.             resetTerm()
  429.             pitch = promptInput("Enter pitch: ")
  430.             currentSelected = 0
  431.             resetTerm()
  432.         end
  433.     end
  434.  
  435.     playSound(play)
  436. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement