LeParzival

Untitled

Feb 4th, 2024 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local oldPull = os.pullEvent
  4. os.pullEvent = os.pullEventRaw
  5.  
  6. local dfpwm = require 'cc.audio.dfpwm'
  7. local dataSize = 16 * 1024
  8. local decoder = dfpwm.make_decoder()
  9.  
  10. local speaker, path, volume = nil, nil, 1
  11. if #args > 0 then
  12. local a, b, c = table.unpack(args)
  13. local _b, _c = tonumber(b), tonumber(c)
  14. if _c then
  15. speaker, path, volume = a, b, _c
  16. elseif not c and _b then
  17. path, volume = a, _b
  18. elseif b and not _b then
  19. speaker, path = a, b
  20. else
  21. path = a
  22. end
  23.  
  24. speaker = speaker and peripheral.wrap(speaker) or peripheral.find('speaker')
  25. else
  26. error('usage: speaker [side] <path> [volume]')
  27. end
  28.  
  29. assert(speaker, 'Speaker not found')
  30. local pType = peripheral.getType(speaker)
  31. assert(pType == 'speaker', 'Expected speaker, got ' .. pType)
  32.  
  33. local frames = ''
  34. local sub = path:sub(1, 7)
  35. if sub == 'http://' or sub == 'https:/' then
  36. print('Attempting HTTP')
  37.  
  38. local valid, err = http.checkURL(path)
  39. assert(valid, err)
  40.  
  41. local response, err = http.get(path, {}, true)
  42. assert(response, err)
  43. while true do
  44. local line = response.readLine(true)
  45. if not line then break end
  46. frames = frames .. line
  47. end
  48. else
  49. for chunk in io.lines((path:sub(1, 1) ~= '/' and shell.dir()..'/' or '')..path) do
  50. frames = frames .. chunk
  51. end
  52. end
  53.  
  54. print('Playing audio on speaker ' .. peripheral.getName(speaker))
  55. parallel.waitForAny(
  56. function()
  57. for pos = 1, #frames, dataSize do
  58. while not speaker.playAudio(decoder(frames:sub(pos, pos + dataSize - 1)), volume) do
  59. os.pullEvent('speaker_audio_empty')
  60. end
  61. end
  62. end,
  63. function()
  64. while true do
  65. if os.pullEventRaw('terminate') then
  66. print('Stopping')
  67. speaker.stop()
  68. break
  69. end
  70. end
  71. end
  72. )
  73.  
  74. print('Playback ended')
  75. os.pullEvent = oldPull
  76.  
Advertisement
Add Comment
Please, Sign In to add comment