Added info.json support and switched to event

This commit is contained in:
Casey 2024-09-15 03:31:42 +03:00
parent 3cb26f3b54
commit 5e1d23b4bb
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 23 additions and 17 deletions

View File

@ -12,6 +12,7 @@ local delay = 0
local loading_concurrency = 8 local loading_concurrency = 8
local buffer_size = 8192 local buffer_size = 8192
local wait_until_input = false local wait_until_input = false
local n_frames, video_url, audio_url_l, audio_url_r
while args[1] ~= nil and string.sub(args[1], 1, 1) == "-" do while args[1] ~= nil and string.sub(args[1], 1, 1) == "-" do
local k = table.remove(args, 1):sub(2) local k = table.remove(args, 1):sub(2)
@ -29,6 +30,15 @@ while args[1] ~= nil and string.sub(args[1], 1, 1) == "-" do
buffer_size = tonumber(table.remove(args, 1)) buffer_size = tonumber(table.remove(args, 1))
elseif k == "w" or k == "wait" then elseif k == "w" or k == "wait" then
wait_until_input = true wait_until_input = true
elseif k == "J" or k == "info-json" then
local req = assert(http.get(table.remove(args, 1)))
local info = textutils.unserializeJSON(req.readAll())
delay = info.frametime
n_frames = info.frame_count
video_url = info.video
audio_url_l = info.audio.l
audio_url_l = info.audio.r
req.close()
end end
end end
@ -42,9 +52,14 @@ if not speakers.l then
return return
end end
if #args < 3 then if #args < 3 and not n_frames and not video_url and not audio_url_l then
printError("Usage: video [-m MONITOR] [-l SPK_L] [-r SPK_R] [-d FRAME_T] <N_FRAMES> <VIDEO_TEMPLATE> <LEFT_CHANNEL> [RIGHT_CHANNEL]") printError("Usage: video [-w] [-b BUFSZ] [-t THREADS] [-J URL] [-m MONITOR] [-l SPK_L] [-r SPK_R] [-d FRAME_T] <N_FRAMES> <VIDEO_TEMPLATE> <LEFT_CHANNEL> [RIGHT_CHANNEL]")
return return
else
n_frames = tonumber(table.remove(args, 1))
video_url = table.remove(args, 1)
audio_url_l = table.remove(args, 1)
audio_url_r = #args > 0 and table.remove(args, 1) or nil
end end
print(string.format("Using monitor %s", peripheral.getName(monitor))) print(string.format("Using monitor %s", peripheral.getName(monitor)))
@ -54,11 +69,6 @@ else
print("Mono sound: " .. peripheral.getName(speakers.l)) print("Mono sound: " .. peripheral.getName(speakers.l))
end end
local n_frames = tonumber(table.remove(args, 1))
local video_url = table.remove(args, 1)
local audio_url_l = table.remove(args, 1)
local audio_url_r = #args > 0 and table.remove(args, 1) or nil
if not speakers.r and audio_url_r then if not speakers.r and audio_url_r then
printError("No right speaker found but right audio channel was specified") printError("No right speaker found but right audio channel was specified")
printError("Right channel will not be played") printError("Right channel will not be played")
@ -157,7 +167,6 @@ table.insert(subthreads, function()
print() print()
end) end)
local playback_locked = true
local playback_done = false local playback_done = false
table.insert(subthreads, function() table.insert(subthreads, function()
@ -170,22 +179,19 @@ table.insert(subthreads, function()
term.setCursorPos(1, ty - 1) term.setCursorPos(1, ty - 1)
term.setBackgroundColor(colors.gray) term.setBackgroundColor(colors.gray)
term.clearLine() term.clearLine()
term.write(string.format("Waiting for frames... (V:%d, A:%d) [ENTER to skip]", #frames, #audio_frames.l)) term.write(string.format("Waiting for frames... (V:%d, A:%d)", #frames, #audio_frames.l))
if #frames > 60 and #audio_frames.l >= n_audio_samples and not wait_until_input then if #frames > 60 and #audio_frames.l >= n_audio_samples and not wait_until_input then
break break
end end
end end
playback_locked = false os.queueEvent("playback_ready")
end)
table.insert(subthreads, function()
while playback_locked do os.sleep(0) end -- spin
end) end)
table.insert(subthreads, function() table.insert(subthreads, function()
local is_dfpwm = ({ audio_url_l:find("%.dfpwm") })[2] == #audio_url_l local is_dfpwm = ({ audio_url_l:find("%.dfpwm") })[2] == #audio_url_l
local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8 local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8
while playback_locked do os.sleep(0) end -- spin
os.pullEvent("playback_ready")
for i = 1, n_audio_samples do for i = 1, n_audio_samples do
local buffer = decode(audio_frames.l[i]) local buffer = decode(audio_frames.l[i])
@ -202,7 +208,7 @@ table.insert(subthreads, function()
local is_dfpwm = ({ audio_url_r:find("%.dfpwm") })[2] == #audio_url_r local is_dfpwm = ({ audio_url_r:find("%.dfpwm") })[2] == #audio_url_r
local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8 local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8
while playback_locked do os.sleep(0) end -- spin os.pullEvent("playback_ready")
for i = 1, n_audio_samples do for i = 1, n_audio_samples do
local buffer = decode(audio_frames.r[i]) local buffer = decode(audio_frames.r[i])
@ -214,7 +220,7 @@ table.insert(subthreads, function()
end) end)
table.insert(subthreads, function() table.insert(subthreads, function()
while playback_locked do os.sleep(0) end -- spin os.pullEvent("playback_ready")
local start_t = os.clock() local start_t = os.clock()
while not playback_done do while not playback_done do