iNxmi

ccaudio.lua

Jul 15th, 2026
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. local VERSION = "1.0.0-alpha"
  2.  
  3. function parser()
  4. local argparse = require "argparse__0_7_2"
  5.  
  6. local parser = argparse("script", "Example Description.")
  7.  
  8. function parser:error(message)
  9. error(message,0)
  10. end
  11.  
  12. local parser_help = parser.help
  13. function parser:help(...)
  14. local help = parser_help(self, ...)
  15. error(help, 0)
  16. end
  17.  
  18. parser:flag("--print_arguments", "print debug argument info")
  19. parser:flag("--version", "print version")
  20.  
  21. parser:option("-a --address", "set server address", "127.0.0.1")
  22. parser:option("-p --port", "set server port", "8080")
  23.  
  24. local command_list = parser:command("list", "List music.")
  25.  
  26. local command_play = parser:command("play", "Play music.")
  27. command_play:argument("file", "File to play.")
  28. command_play:option("-c --chunk_size", "Chunk size (in bytes)", 128 * 1024)
  29.  
  30. local command_echo = parser:command("echo", "Test the WebSocket with echo.")
  31. command_echo:argument("message", "Message to send the echo.")
  32.  
  33. return parser
  34. end
  35.  
  36. local raw_arguments = { ... }
  37.  
  38. local parser = parser()
  39. local arguments = parser:parse(raw_arguments)
  40.  
  41. local address = string.format("%s:%d", arguments.address, arguments.port)
  42. local http_url = string.format("http://%s", address)
  43. local websocket_url = string.format("ws://%s", address)
  44.  
  45. function get_list()
  46. local url = string.format("%s/list", http_url)
  47. local request = http.get(url)
  48. local json_text, _ = request.readAll()
  49. return textutils.unserializeJSON(json_text)
  50. end
  51.  
  52. function list()
  53. local list = get_list()
  54. for index, file in ipairs(list) do
  55. local message = string.format("%d. %s", index - 1, file)
  56. print(message)
  57. end
  58. end
  59.  
  60. function play()
  61. local speaker = peripheral.find("speaker")
  62.  
  63. if not speaker then
  64. error("No speaker found.")
  65. end
  66.  
  67. local initialUrl = string.format("%s/request?file=%s&chunkSizeInBytes=%d", http_url, arguments.file, arguments.chunk_size)
  68. local request, err = http.get(initialUrl)
  69. if not request then
  70. error("HTTP request failed: " .. tostring(err))
  71. return
  72. end
  73.  
  74. local json_text, _ = request.readAll()
  75. local json = textutils.unserializeJSON(json_text)
  76.  
  77. local chunk_size = json.chunk_size_in_bytes
  78. local chunk_count = json.number_of_chunks
  79. local hash = json.hash
  80.  
  81. local bufferedChunks = 0
  82. local MAX_CHUNKS = math.floor(1000000 / chunk_size)
  83. local dataBuffer = {}
  84. local nextDownloadIndex = 0
  85.  
  86. for i = 0, chunk_count - 1, 1 do
  87.  
  88. while ((bufferedChunks <= MAX_CHUNKS) and not (nextDownloadIndex >= chunk_count)) do
  89. local url = string.format("%s/stream?hash=%s&chunk=%d", http_url, hash, nextDownloadIndex)
  90. local res, _ = http.get(url, {}, true)
  91.  
  92. local jsonChunkText = res.readAll()
  93. local currentChunk = textutils.unserializeJSON(jsonChunkText)
  94.  
  95. nextDownloadIndex = nextDownloadIndex + 1
  96. table.insert(dataBuffer, currentChunk)
  97. bufferedChunks = bufferedChunks + 1
  98. res.close()
  99.  
  100. end
  101.  
  102. -- play the audio
  103. local tmpBuffer = table.remove(dataBuffer, 1)
  104. bufferedChunks = bufferedChunks - 1
  105.  
  106. if tmpBuffer then
  107. local audioBuffer = {}
  108.  
  109. local chunkSizeLimit = 128 * 1024
  110. for startIdx = 1, #tmpBuffer, chunkSizeLimit do
  111. local audioBuffer = {}
  112.  
  113. for j = startIdx, math.min(startIdx + chunkSizeLimit - 1, #tmpBuffer) do
  114. table.insert(audioBuffer, tmpBuffer[j])
  115. end
  116.  
  117. if #audioBuffer > 0 then
  118. while not speaker.playAudio(audioBuffer) do
  119. os.pullEvent("speaker_audio_empty")
  120. end
  121. end
  122. end
  123. end
  124. end
  125. end
  126.  
  127. function echo()
  128. local url = string.format("%s/echo", websocket_url)
  129. local socket = assert(http.websocket(url))
  130.  
  131. socket.send(arguments.message)
  132. local response, is_binary = socket.receive()
  133. socket.close()
  134.  
  135. print(response)
  136. end
  137.  
  138. function get_command()
  139. if arguments.list then
  140. return list
  141. elseif arguments.play then
  142. return play
  143. elseif arguments.echo then
  144. return echo
  145. end
  146. end
  147.  
  148. function execute()
  149. if arguments.version then
  150. print(VERSION)
  151. end
  152.  
  153. if arguments.print_arguments then
  154. print(textutils.serialize(raw_arguments))
  155. print(textutils.serialize(arguments))
  156. end
  157.  
  158. local command = get_command()
  159. return command()
  160. end
  161.  
  162. local result = execute()
Advertisement
Add Comment
Please, Sign In to add comment