From c4740b8bfd1b651f12058ef768dfa313351337f0 Mon Sep 17 00:00:00 2001 From: hkc Date: Mon, 16 Sep 2024 16:26:36 +0300 Subject: [PATCH] Added padding and fixed IPC problems --- video.lua | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/video.lua b/video.lua index ae4ab3d..d440913 100644 --- a/video.lua +++ b/video.lua @@ -2,6 +2,8 @@ local args = { ... } local dfpwm = require("cc.audio.dfpwm") local ccpi = require("ccpi") +local EV_NONCE = math.floor(0xFFFFFFFF * math.random()) + settings.define("video.speaker.left", { description = "Speaker ID for left audio channel", default = peripheral.getName(peripheral.find("speaker")), @@ -86,7 +88,8 @@ if not n_frames and not video_url and not audio_url_l then end end -print(string.format("Using monitor %s", peripheral.getName(monitor))) +local mon_w, mon_h = monitor.getSize() +print(string.format("Using monitor %s (%dx%d)", peripheral.getName(monitor), mon_w, mon_h)) if speakers.r then print(string.format("Stereo sound: L=%s R=%s", peripheral.getName(speakers.l), peripheral.getName(speakers.r))) else @@ -183,11 +186,11 @@ for i = 1, loading_concurrency do end table.insert(subthreads, function() - while #frames ~= n_frames or #audio_frames.l < n_audio_samples do + repeat draw_bar(ty - 3, colors.blue, colors.gray, #frames / n_frames, "Loading video [%5d / %5d]", #frames, n_frames) draw_bar(ty - 2, colors.red, colors.gray, #audio_frames.l / n_audio_samples, "Loading audio [%5d / %5d]", #audio_frames.l, n_audio_samples) os.sleep(0.25) - end + until #frames >= n_frames or #audio_frames.l >= n_audio_samples print() end) @@ -208,14 +211,16 @@ table.insert(subthreads, function() break end end - os.queueEvent("playback_ready") + os.queueEvent("playback_ready", EV_NONCE) end) table.insert(subthreads, function() local is_dfpwm = ({ audio_url_l:find("%.dfpwm") })[2] == #audio_url_l local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8 - os.pullEvent("playback_ready") + repeat + local _, nonce = os.pullEvent("playback_ready") + until nonce == EV_NONCE for i = 1, n_audio_samples do local buffer = decode(audio_frames.l[i]) @@ -232,7 +237,9 @@ table.insert(subthreads, function() local is_dfpwm = ({ audio_url_r:find("%.dfpwm") })[2] == #audio_url_r local decode = use_dfpwm and dfpwm.make_decoder() or decode_s8 - os.pullEvent("playback_ready") + repeat + local _, nonce = os.pullEvent("playback_ready") + until nonce == EV_NONCE for i = 1, n_audio_samples do local buffer = decode(audio_frames.r[i]) @@ -244,8 +251,9 @@ table.insert(subthreads, function() end) table.insert(subthreads, function() - os.pullEvent("playback_ready") - + repeat + local _, nonce = os.pullEvent("playback_ready") + until nonce == EV_NONCE local start_t = os.clock() while not playback_done do local frame = math.floor((os.clock() - start_t) / math.max(0.05, delay)) @@ -253,8 +261,11 @@ table.insert(subthreads, function() term.setBackgroundColor(frame >= #frames and colors.red or colors.gray) term.clearLine() term.write(string.format("Playing frame: %d/%d", frame + 1, #frames)) - if frames[frame + 1] then - ccpi.draw(frames[frame + 1], 1, 1, monitor) + local img = frames[frame + 1] + if img ~= nil then + local x = math.max(math.floor((mon_w - img.w) / 2), 0) + local y = math.max(math.floor((mon_h - img.h) / 2), 0) + ccpi.draw(img, x, y, monitor) end os.sleep(delay) end