iNxmi

sdfsdfsdg

Jul 16th, 2026
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.49 KB | None | 0 0
  1. local VERSION = "1.0.0-alpha"
  2.  
  3. local MAXIMUM_MEMORY_USAGE = 0.90
  4. local AVAILABLE_MEMORY = 1000000 * MAXIMUM_MEMORY_USAGE
  5.  
  6. local SKIP_AMOUNT = 25
  7. local NEW_CHUNKS_PERCENTAGE = 0.7
  8.  
  9. local SPEAKER_BUFFER_SIZE = 64 * 1024
  10.  
  11. local SAMPLES_PER_SECOND = 48000
  12.  
  13. local function get_parser()
  14. local argparse = require "argparse__0_7_2"
  15.  
  16. local parser = argparse("script", "Example Description.")
  17.  
  18. function parser:error(message)
  19. error(message, 0)
  20. end
  21.  
  22. local parser_help = parser.help
  23. function parser:help(...)
  24. local help = parser_help(self, ...)
  25. error(help, 0)
  26. end
  27.  
  28. parser:flag("--print_arguments", "print debug argument info")
  29. parser:flag("--version", "print version")
  30.  
  31. parser:option("-a --address", "set server address", "127.0.0.1")
  32. parser:option("-p --port", "set server port", "8080")
  33.  
  34. local command_list = parser:command("list", "List music.")
  35.  
  36. local command_play = parser:command("play", "Play music.")
  37. command_play:argument("file", "File to play.")
  38. command_play:option("-c --chunk_size", "Chunk size (in bytes)", 128 * 1024)
  39.  
  40. return parser
  41. end
  42.  
  43. local function seconds()
  44. return os.epoch("utc") / 1000
  45. end
  46.  
  47. local function fetch(url)
  48. local response, err = http.get(url)
  49. if not response then
  50. error("HTTP request failed: " .. tostring(err))
  51. return nil
  52. end
  53.  
  54. local json_text, _ = response.readAll()
  55. local json = textutils.unserializeJSON(json_text)
  56.  
  57. return json
  58. end
  59.  
  60. local function fetch_list(http_url)
  61. local url = string.format("%s/list", http_url)
  62. return fetch(url)
  63. end
  64.  
  65. local function fetch_request(http_url, index, chunk_size)
  66. local url = string.format("%s/request?file=%s&chunkSizeInBytes=%d", http_url, index, chunk_size)
  67. return fetch(url)
  68. end
  69.  
  70. local function fetch_stream(http_url, hash, index)
  71. local url = string.format("%s/stream?hash=%s&chunk=%d", http_url, hash, index)
  72. return fetch(url)
  73. end
  74.  
  75. local function checksum(list)
  76. local sum = 0
  77. for _, value in ipairs(list) do
  78. sum = sum + value
  79. end
  80. return sum
  81. end
  82.  
  83. local raw_arguments = { ... }
  84.  
  85. local parser = get_parser()
  86. local arguments = parser:parse(raw_arguments)
  87.  
  88. local address = string.format("%s:%d", arguments.address, arguments.port)
  89. local http_url_default = string.format("http://%s", address)
  90.  
  91. local function command_list()
  92. local list = fetch_list(http_url_default)
  93. for index, file in ipairs(list) do
  94. local message = string.format("%s%d. %s", string.rep(" ", #tostring(#list) - #tostring(index - 1)), index - 1, file)
  95.  
  96. if index % 2 == 0 then
  97. term.setTextColour(colours.red)
  98. else
  99. term.setTextColour(colours.green)
  100. end
  101.  
  102. textutils.pagedPrint(message)
  103. end
  104. end
  105.  
  106. local function command_play()
  107. local speaker = peripheral.find("speaker")
  108. if not speaker then
  109. error("No speaker found.")
  110. return
  111. end
  112.  
  113. local json = fetch_request(http_url_default, arguments.file, arguments.chunk_size)
  114.  
  115. local is_running = true
  116. local is_paused = false
  117. local should_update = false
  118.  
  119. local time_last = 0
  120. local time_delta = 0
  121. local time_audio = 0
  122.  
  123. local volume = 1
  124.  
  125. local index_samples_last = 0
  126.  
  127. local chunks = {}
  128.  
  129. local function input()
  130. while true do
  131. local event = {os.pullEvent("key")}
  132. local hold = event[3]
  133. local key = event[2]
  134.  
  135. if event[1] ~= "key" then
  136. goto continue3
  137. end
  138.  
  139. if hold then
  140. goto continue3
  141. end
  142.  
  143. if key == keys.q then
  144. is_running = false
  145. speaker.stop()
  146. elseif key == keys.p or key == keys.space then
  147. is_paused = not is_paused
  148. if is_paused then
  149. speaker.stop()
  150. print("paused; time_audio="..time_audio)
  151. else
  152. should_update = true
  153. print("unpaused")
  154. end
  155. elseif key == keys.right then
  156. time_audio = time_audio + SKIP_AMOUNT
  157. speaker.stop()
  158. should_update = true
  159. print("time_audio="..time_audio)
  160. elseif key == keys.left then
  161. time_audio = time_audio - SKIP_AMOUNT
  162. speaker.stop()
  163. should_update = true
  164. print("time_audio="..time_audio)
  165. elseif key == keys.up then
  166. volume = volume + 0.1
  167. speaker.stop()
  168. should_update = true
  169. print("volume="..volume)
  170. elseif key == keys.down then
  171. volume = volume - 0.1
  172. speaker.stop()
  173. should_update = true
  174. print("volume="..volume)
  175. end
  176. ::continue3::
  177. sleep(0.1)
  178. end
  179. end
  180.  
  181. local fetch_queue = { 1 }
  182. local available_chunk_space_count = math.max(math.ceil(AVAILABLE_MEMORY / arguments.chunk_size), 1)
  183. local num_old_chunks = math.floor((1 - NEW_CHUNKS_PERCENTAGE) * available_chunk_space_count)
  184. local num_new_chunks = math.max(math.floor(NEW_CHUNKS_PERCENTAGE * available_chunk_space_count), 1)
  185. local function fetch_chunks()
  186. if #fetch_queue == 0 then
  187. return
  188. end
  189.  
  190. local chunk_to_fetch = table.remove(fetch_queue)
  191.  
  192. if chunks[chunk_to_fetch] == nil then
  193. local chunk = fetch_stream(http_url_default, json.hash, chunk_to_fetch - 1)
  194.  
  195. if not chunk then
  196. print("[ERROR] requested chunk not available")
  197. else
  198. chunks[chunk_to_fetch] = chunk
  199. end
  200. end
  201.  
  202. for i = chunk_to_fetch - 1, num_old_chunks, -1 do
  203. if i <= 1 then
  204. break
  205. end
  206.  
  207. if chunks[i] ~= nil then
  208. goto continue2
  209. end
  210.  
  211. local chunk = fetch_stream(http_url_default, json.hash, i - 1)
  212. if not chunk then
  213. print("[ERROR] requested chunk not available")
  214. else
  215. chunks[i] = chunk
  216. end
  217.  
  218. ::continue2::
  219. end
  220.  
  221. for i = chunk_to_fetch + 1, num_new_chunks, 1 do
  222. if i >= json.number_of_chunks then
  223. break
  224. end
  225.  
  226. if chunks[i] ~= nil then
  227. goto continue1
  228. end
  229.  
  230. local chunk = fetch_stream(http_url_default, json.hash, i - 1)
  231. if not chunk then
  232. print("[ERROR] requested chunk not available")
  233. else
  234. chunks[i] = chunk
  235. end
  236.  
  237. ::continue1::
  238. end
  239. end
  240.  
  241. local function thread_fetch_chunks()
  242. while true do
  243. fetch_chunks()
  244. sleep(0.1)
  245. end
  246. end
  247.  
  248. local function get_chunk(index)
  249. if chunks[index] == nil then
  250. table.insert(fetch_queue, index)
  251. end
  252.  
  253. while chunks[index] == nil do
  254. sleep(0.05)
  255. end
  256.  
  257. return chunks[index]
  258. end
  259.  
  260. local function get_samples(index_global_samples_start, index_global_samples_end)
  261. local result = {}
  262. local chunk_size = arguments.chunk_size
  263. local number_of_chunks = json.number_of_chunks
  264.  
  265. local first_chunk_index = math.floor(index_global_samples_start / chunk_size)
  266. local last_chunk_index = math.min(math.floor(index_global_samples_end / chunk_size), number_of_chunks - 1)
  267.  
  268. local write_position = 1
  269.  
  270. for chunk_index = first_chunk_index, last_chunk_index do
  271. local chunk = get_chunk(chunk_index + 1)
  272.  
  273. if chunk and chunk.samples then
  274. local chunk_start_global = chunk_index * chunk_size
  275.  
  276. local read_start = math.max(index_global_samples_start - chunk_start_global, 0) + 1
  277. local read_end = math.min(index_global_samples_end - chunk_start_global, chunk_size - 1) + 1
  278.  
  279. local number_of_samples = read_end - read_start + 1
  280.  
  281. if number_of_samples > 0 then
  282. table.move(chunk.samples, read_start, read_end, write_position, result)
  283. write_position = write_position + number_of_samples
  284. end
  285. end
  286. end
  287.  
  288. return result
  289. end
  290.  
  291. local function audio()
  292. if is_paused then
  293. return
  294. end
  295.  
  296. time_audio = time_audio + time_delta
  297.  
  298. --if #sampleBuffer <= 0 then
  299. -- is_running = not (download_index >= json.number_of_chunks - 1)
  300. -- return
  301. --end
  302.  
  303. if should_update then
  304. index_samples_last = time_audio * SAMPLES_PER_SECOND
  305. end
  306.  
  307. local index_samples_start = index_samples_last + 1
  308. local index_samples_end = math.min(index_samples_start + SPEAKER_BUFFER_SIZE - 1, json.number_of_samples)
  309.  
  310. local function map(array, func)
  311. local result = {}
  312. for i, value in ipairs(array) do
  313. result[i] = func(value)
  314. end
  315. return result
  316. end
  317.  
  318. local buffer = get_samples(index_samples_start, index_samples_end)
  319. local mapped = map(buffer, function(x) return math.max(math.min(x * volume, 127), -128) end)
  320.  
  321. local success = speaker.playAudio(mapped)
  322. if not success then
  323. return
  324. end
  325.  
  326. --os.pullEvent("speaker_audio_empty")
  327.  
  328. index_samples_last = index_samples_end
  329. should_update = false
  330. end
  331.  
  332. local function thread_audio()
  333. time_last = seconds()
  334. while is_running do
  335. local time_current = seconds()
  336. time_delta = time_current - time_last
  337. time_last = time_current
  338.  
  339. audio()
  340. sleep(0.05)
  341. end
  342. end
  343.  
  344. parallel.waitForAny(thread_audio, thread_fetch_chunks, input)
  345. end
  346.  
  347. local function get_command()
  348. if arguments.list then
  349. return command_list
  350. elseif arguments.play then
  351. return command_play
  352. end
  353. end
  354.  
  355. local function execute()
  356. if arguments.version then
  357. print(VERSION)
  358. end
  359.  
  360. if arguments.print_arguments then
  361. print(textutils.serialize(raw_arguments))
  362. print(textutils.serialize(arguments))
  363. end
  364.  
  365. local command = get_command()
  366. return command()
  367. end
  368.  
  369. local result = execute()
Advertisement
Add Comment
Please, Sign In to add comment