Advertisement
Guest User

startup

a guest
Aug 8th, 2024
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.17 KB | None | 0 0
  1. local api_base_url = "https://ipod-2to6magyna-uc.a.run.app/"
  2.  
  3. local width, height = term.getSize()
  4.  
  5. local tab = 1
  6. local waiting_for_input = false
  7. local last_search = nil
  8. local last_search_url = nil
  9. local search_results = nil
  10. local search_error = false
  11. local in_fullscreen = 0
  12. local clicked_result = nil
  13.  
  14. local playing = false
  15. local queue = {}
  16. local now_playing = nil
  17. local looping = false
  18.  
  19. local playing_id = nil
  20. local last_download_url = nil
  21. local playing_status = 0
  22.  
  23. local player_handle = nil
  24. local start = nil
  25. local pcm = nil
  26. local size = nil
  27. local decoder = nil
  28. local needs_next_chunk = 0
  29. local buffer
  30.  
  31.  
  32. local speakers = { peripheral.find("speaker") }
  33.  
  34.  
  35.  
  36. local function playAudioOnSpeakers(buffer)
  37. for _, speakers in ipairs(speakers) do
  38. while not speakers.playAudio(buffer) do
  39. sleep(0.05) -- Wait before retrying
  40. end
  41. end
  42. end
  43.  
  44.  
  45.  
  46.  
  47.  
  48. local function redrawScreen()
  49.     if waiting_for_input == true then
  50.         return
  51.     end
  52.  
  53.     -- DRAWING
  54.     term.setBackgroundColor(colors.black)
  55.     term.clear()
  56.  
  57.     --tabs
  58.     term.setCursorPos(1,1)
  59.     term.setBackgroundColor(colors.gray)
  60.     term.clearLine()
  61.  
  62.     tabs = {" Now Playing ", " Search "}
  63.  
  64.     for i=1,2,1 do
  65.         if tab == i then
  66.             term.setTextColor(colors.black)
  67.             term.setBackgroundColor(colors.white)
  68.         else
  69.             term.setTextColor(colors.white)
  70.             term.setBackgroundColor(colors.gray)
  71.         end
  72.  
  73.         term.setCursorPos((math.floor((width/2)*(i-0.5)))-math.ceil(#tabs[i]/2)+1, 1)
  74.         term.write(tabs[i])
  75.     end
  76.  
  77.     --now playing tab
  78.     if tab == 1 then
  79.         if now_playing ~= nil then
  80.             term.setBackgroundColor(colors.black)
  81.             term.setTextColor(colors.white)
  82.             term.setCursorPos(2,3)
  83.             term.write(now_playing.name)
  84.             term.setTextColor(colors.lightGray)
  85.             term.setCursorPos(2,4)
  86.             term.write(now_playing.artist)
  87.         else
  88.             term.setBackgroundColor(colors.black)
  89.             term.setTextColor(colors.lightGray)
  90.             term.setCursorPos(2,3)
  91.             term.write("Not playing")
  92.         end
  93.  
  94.         term.setTextColor(colors.white)
  95.         term.setBackgroundColor(colors.gray)
  96.  
  97.         if playing then
  98.             term.setCursorPos(2, 6)
  99.             term.write(" Stop ")
  100.         else
  101.             if now_playing ~= nil or #queue > 0 then
  102.                 term.setTextColor(colors.white)
  103.                 term.setBackgroundColor(colors.gray)
  104.             else
  105.                 term.setTextColor(colors.lightGray)
  106.                 term.setBackgroundColor(colors.gray)
  107.             end
  108.             term.setCursorPos(2, 6)
  109.             term.write(" Play ")
  110.         end
  111.  
  112.         if now_playing ~= nil or #queue > 0 then
  113.             term.setTextColor(colors.white)
  114.             term.setBackgroundColor(colors.gray)
  115.         else
  116.             term.setTextColor(colors.lightGray)
  117.             term.setBackgroundColor(colors.gray)
  118.         end
  119.         term.setCursorPos(2 + 8, 6)
  120.         term.write(" Skip ")
  121.  
  122.         if looping then
  123.             term.setTextColor(colors.black)
  124.             term.setBackgroundColor(colors.white)
  125.         else
  126.             term.setTextColor(colors.white)
  127.             term.setBackgroundColor(colors.gray)
  128.         end
  129.         term.setCursorPos(2 + 8 + 8, 6)
  130.         term.write(" Loop ")
  131.  
  132.         --search results
  133.         if #queue > 0 then
  134.             term.setBackgroundColor(colors.black)
  135.             for i=1,#queue do
  136.                 term.setTextColor(colors.white)
  137.                 term.setCursorPos(2,8 + (i-1)*2)
  138.                 term.write(queue[i].name)
  139.                 term.setTextColor(colors.lightGray)
  140.                 term.setCursorPos(2,9 + (i-1)*2)
  141.                 term.write(queue[i].artist)
  142.             end
  143.         end
  144.     end
  145.  
  146.     --search tab
  147.     if tab == 2 then
  148.  
  149.         -- search bar
  150.         for a=3,5,1 do
  151.             term.setCursorPos(2,a)
  152.             term.setTextColor(colors.lightGray)
  153.             term.setBackgroundColor(colors.lightGray)
  154.             for i=1,width-2,1 do
  155.                 term.write(" ")
  156.             end
  157.         end
  158.         term.setCursorPos(3,4)
  159.         term.setTextColor(colors.black)
  160.         term.write(last_search or "Search...")
  161.  
  162.         --search results
  163.         if search_results ~= nil then
  164.             term.setBackgroundColor(colors.black)
  165.             for i=1,#search_results do
  166.                 term.setTextColor(colors.white)
  167.                 term.setCursorPos(2,7 + (i-1)*2)
  168.                 term.write(search_results[i].name)
  169.                 term.setTextColor(colors.lightGray)
  170.                 term.setCursorPos(2,8 + (i-1)*2)
  171.                 term.write(search_results[i].artist)
  172.             end
  173.         else
  174.             term.setCursorPos(2,7)
  175.             term.setBackgroundColor(colors.black)
  176.             if search_error == true then
  177.                 term.setTextColor(colors.red)
  178.                 term.write("Error")
  179.             else
  180.                 if last_search_url ~= nil then
  181.                     term.setTextColor(colors.white)
  182.                     term.write("Searching...")
  183.                 else
  184.  
  185.                 end
  186.             end
  187.         end
  188.  
  189.         --fullscreen song options
  190.         if in_fullscreen == 1 then
  191.             term.setBackgroundColor(colors.black)
  192.             term.clear()
  193.             term.setCursorPos(2,2)
  194.             term.setTextColor(colors.white)
  195.             term.write(search_results[clicked_result].name)
  196.             term.setCursorPos(2,3)
  197.             term.setTextColor(colors.lightGray)
  198.             term.write(search_results[clicked_result].artist)
  199.  
  200.             term.setBackgroundColor(colors.gray)
  201.             term.setTextColor(colors.white)
  202.  
  203.             term.setCursorPos(2,6)
  204.             term.clearLine()
  205.             term.write("Play now")
  206.  
  207.             term.setCursorPos(2,8)
  208.             term.clearLine()
  209.             term.write("Play next")
  210.  
  211.             term.setCursorPos(2,10)
  212.             term.clearLine()
  213.             term.write("Add to queue")
  214.  
  215.             term.setCursorPos(2,13)
  216.             term.clearLine()
  217.             term.write("Cancel")
  218.         end
  219.  
  220.     end
  221. end
  222.  
  223. local function searchInput()
  224.     while true do
  225.         if waiting_for_input == true then
  226.             for a=3,5,1 do
  227.                 term.setCursorPos(2,a)
  228.                 term.setTextColor(colors.white)
  229.                 term.setBackgroundColor(colors.white)
  230.                 for i=1,width-2,1 do
  231.                     term.write(" ")
  232.                 end
  233.             end
  234.             term.setCursorPos(3,4)
  235.             term.setTextColor(colors.black)
  236.             local input = read()
  237.             if string.len(input) > 0 then
  238.                 last_search = input
  239.                 last_search_url = api_base_url .. "?search=" .. textutils.urlEncode(input)
  240.                 http.request(last_search_url)
  241.                 search_results = nil
  242.                 search_error = false
  243.             else
  244.                 last_search = nil
  245.                 last_search_url = nil
  246.                 search_results = nil
  247.                 search_error = false
  248.             end
  249.  
  250.             waiting_for_input = false
  251.  
  252.             redrawScreen()
  253.         end
  254.  
  255.         sleep(0.1)
  256.     end
  257. end
  258.  
  259. local function mainLoop()
  260.     redrawScreen()
  261.  
  262.     while true do
  263.  
  264.         -- AUDIO
  265.         if playing and now_playing then
  266.             if playing_id ~= now_playing.id then
  267.                 playing_id = now_playing.id
  268.                 last_download_url = api_base_url .. "?id=" .. textutils.urlEncode(playing_id)
  269.                 playing_status = 0
  270.                 needs_next_chunk = 1
  271.  
  272.                 http.request({url = last_download_url, binary = true})
  273.  
  274.                 redrawScreen()
  275.             end
  276.             if playing_status == 1 and needs_next_chunk == 3 then
  277.                 needs_next_chunk = 1
  278.  
  279.                 while not playAudioOnSpeakers(buffer) do
  280.                     needs_next_chunk = 2
  281.                     break
  282.                 end
  283.             end
  284.             if playing_status == 1 and needs_next_chunk == 1 then
  285.  
  286.                 while true do
  287.                     local chunk = player_handle.read(size)
  288.                     if not chunk then
  289.                         if looping then
  290.                             playing_id = nil
  291.                         else
  292.                             if #queue > 0 then
  293.                                 now_playing = queue[1]
  294.                                 table.remove(queue, 1)
  295.                                 playing_id = nil
  296.                             else
  297.                                 now_playing = nil
  298.                                 playing = false
  299.                                 playing_id = nil
  300.                             end
  301.                         end
  302.  
  303.                         redrawScreen()
  304.  
  305.                         player_handle.close()
  306.                         needs_next_chunk = 0
  307.                         break
  308.                     else
  309.                         if start then
  310.                             chunk, start = start .. chunk, nil
  311.                             size = size + 4
  312.                         end
  313.  
  314.                         buffer = decoder(chunk)
  315.                          playAudioOnSpeakers(buffer)
  316.                             if needs_next_chunk == 2 then
  317.                             break
  318.                         end
  319.                     end
  320.                 end
  321.  
  322.             end
  323.         end
  324.  
  325.         -- EVENTS
  326.         local event, param1, param2, param3 = os.pullEvent()    
  327.  
  328.         -- CLICK EVENTS
  329.         if event == "mouse_click" and waiting_for_input == false then
  330.  
  331.             local button = param1
  332.             local x = param2
  333.             local y = param3
  334.  
  335.             -- tabs
  336.             if button == 1 and in_fullscreen == 0 then
  337.                 if y == 1 then
  338.                     if x < width/2 then
  339.                         tab = 1
  340.                     else
  341.                         tab = 2
  342.                     end
  343.                 end
  344.             end
  345.  
  346.             --fullscreen windows
  347.             if in_fullscreen == 1 then
  348.                 term.setBackgroundColor(colors.white)
  349.                 term.setTextColor(colors.black)
  350.  
  351.                 if y == 6 then
  352.                     term.setCursorPos(2,6)
  353.                     term.clearLine()
  354.                     term.write("Play now")
  355.                     sleep(0.2)
  356.                     in_fullscreen = 0
  357.                     now_playing = search_results[clicked_result]
  358.                     playing = true
  359.                     playing_id = nil
  360.                 end
  361.  
  362.                 if y == 8 then
  363.                     term.setCursorPos(2,8)
  364.                     term.clearLine()
  365.                     term.write("Play next")
  366.                     sleep(0.2)
  367.                     in_fullscreen = 0
  368.                     table.insert(queue, 1, search_results[clicked_result])
  369.                 end
  370.  
  371.                 if y == 10 then
  372.                     term.setCursorPos(2,10)
  373.                     term.clearLine()
  374.                     term.write("Add to queue")
  375.                     sleep(0.2)
  376.                     in_fullscreen = 0
  377.                     table.insert(queue, search_results[clicked_result])
  378.                 end
  379.  
  380.                 if y == 13 then
  381.                     term.setCursorPos(2,13)
  382.                     term.clearLine()
  383.                     term.write("Cancel")
  384.                     sleep(0.2)
  385.                     in_fullscreen = 0
  386.                 end
  387.             else
  388.  
  389.                 -- now playing tab
  390.                 if tab == 1 and button == 1 then
  391.                     if y == 6 then
  392.                         if x >= 2 and x <= 2 + 6 then
  393.                             local animate = false
  394.                             local was_playing = playing
  395.                             if playing then
  396.                                 playing = false
  397.                                 animate = true
  398.                                 for _, speakers in ipairs(speakers) do
  399.                                 speakers.stop()
  400.                                 end
  401.                                 playing_id = nil
  402.                             else
  403.                                 if now_playing ~= nil then
  404.                                     playing_id = nil
  405.                                     playing = true
  406.                                     animate = true
  407.                                 else
  408.                                     if #queue > 0 then
  409.                                         now_playing = queue[1]
  410.                                         table.remove(queue, 1)
  411.                                         playing_id = nil
  412.                                         playing = true
  413.                                         animate = true
  414.                                     end
  415.                                 end
  416.                             end
  417.                             if animate == true then
  418.                                 term.setBackgroundColor(colors.white)
  419.                                 term.setTextColor(colors.black)
  420.                                 term.setCursorPos(2, 6)
  421.                                 if was_playing then
  422.                                     term.write(" Stop ")
  423.                                 else
  424.                                     term.write(" Play ")
  425.                                 end
  426.                                 sleep(0.2)
  427.                             end
  428.                         end
  429.                         if x >= 2 + 8 and x <= 2 + 8 + 6 then
  430.                             local animate = false
  431.                             if playing then
  432.                                 for _, speakers in ipairs(speakers) do
  433.                                 speakers.stop()
  434.                                 end
  435.                             end
  436.                             if now_playing ~= nil or #queue > 0 then
  437.                                 if #queue > 0 then
  438.                                     now_playing = queue[1]
  439.                                     table.remove(queue, 1)
  440.                                     playing_id = nil
  441.                                 else
  442.                                     now_playing = nil
  443.                                     playing = false
  444.                                     playing_id = nil
  445.                                 end
  446.                                 animate = true
  447.                             end
  448.                             if animate == true then
  449.                                 term.setBackgroundColor(colors.white)
  450.                                 term.setTextColor(colors.black)
  451.                                 term.setCursorPos(2 + 8, 6)
  452.                                 term.write(" Skip ")
  453.                                 sleep(0.2)
  454.                             end
  455.                         end
  456.                         if x >= 2 + 8 + 8 and x <= 2 + 8 + 8 + 6 then
  457.                             if looping then
  458.                                 looping = false
  459.                             else
  460.                                 looping = true
  461.                             end
  462.                         end
  463.                     end
  464.                 end
  465.  
  466.                 -- search tab clicks
  467.                 if tab == 2 and button == 1 then
  468.                     -- search box click
  469.                     if y >= 3 and y <= 5 and x >= 1 and x <= width-1 then
  470.                         waiting_for_input = true
  471.                     end
  472.  
  473.                     -- search result click
  474.                     if search_results then
  475.                         for i=1,#search_results do
  476.                             if y == 7 + (i-1)*2 or y == 8 + (i-1)*2 then
  477.                                 term.setBackgroundColor(colors.white)
  478.                                 term.setTextColor(colors.black)
  479.                                 term.setCursorPos(2,7 + (i-1)*2)
  480.                                 term.clearLine()
  481.                                 term.write(search_results[i].name)
  482.                                 term.setTextColor(colors.gray)
  483.                                 term.setCursorPos(2,8 + (i-1)*2)
  484.                                 term.clearLine()
  485.                                 term.write(search_results[i].artist)
  486.                                 sleep(0.2)
  487.                                 in_fullscreen = 1
  488.                                 clicked_result = i
  489.                             end
  490.                         end
  491.                     end
  492.                 end
  493.             end
  494.  
  495.             redrawScreen()
  496.  
  497.         end
  498.  
  499.         -- HTTP EVENTS
  500.         if event == "http_success" then
  501.             local url = param1
  502.             local handle = param2
  503.  
  504.             if url == last_search_url then
  505.                 search_results = textutils.unserialiseJSON(handle.readAll())
  506.                 redrawScreen()
  507.             end
  508.             if url == last_download_url then
  509.                 player_handle = handle
  510.                 start = handle.read(4)
  511.                 size = 16 * 1024 - 4
  512.                 if start == "RIFF" then
  513.                     error("WAV not supported!")
  514.                 end
  515.                 playing_status = 1
  516.                 decoder = require "cc.audio.dfpwm".make_decoder()
  517.             end
  518.         end
  519.  
  520.         if event == "http_failure" then
  521.             local url = param1
  522.  
  523.             if url == last_search_url then
  524.                 search_error = true
  525.                 redrawScreen()
  526.             end
  527.             if url == last_download_url then
  528.                 if #queue > 0 then
  529.                     now_playing = queue[1]
  530.                     table.remove(queue, 1)
  531.                     playing_id = nil
  532.                 else
  533.                     now_playing = nil
  534.                     playing = false
  535.                     playing_id = nil
  536.                 end
  537.                 redrawScreen()
  538.             end
  539.         end
  540.  
  541.         if event == "timer" then
  542.             os.startTimer(1)
  543.         end
  544.  
  545.         if event == "speakers_audio_empty" then
  546.             for _, speaker in ipairs(speakers) do
  547.             if needs_next_chunk == 2 then
  548.                 needs_next_chunk = 3
  549.             end
  550.         end
  551.     end
  552.     end
  553.  
  554.     sleep(0.1)
  555. end
  556.  
  557. parallel.waitForAny(mainLoop, searchInput)
  558.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement