sgstream/thread.lua
2024-05-27 20:26:12 +05:00

66 lines
1.4 KiB
Lua

require "love.sound"
require "love.thread"
require "love.math"
require "love.timer"
local ctrl = love.thread.getChannel("ctrl")
local sndc = love.thread.getChannel("snd")
local pos,look,chunk,rate,bits
local emsnd
local function send(c,v)
sndc:push(c)
sndc:push(v)
end
local function go()
local t = love.timer.getTime()
local ok,err,x = xpcall(function()
local snd,x = dofile("sound.lua")
snd = snd:phase(pos):clamp(0,chunk,true)
snd = assert(snd:compile(rate,bits),"no sound")
assert(snd:typeOf("SoundData"),"fake sound")
return snd,x
end,debug.traceback)
look = tonumber(x) or look
sndc:performAtomic(function()
send("look",look)
if not ok then
send("snd",emsnd)
print(err)
else
send("snd",err)
end
pos=pos+chunk
send("pos",pos)
end)
t=love.timer.getTime()-t
local chnkcc=16
local pgb=math.floor(t/chunk*chnkcc+.5)
print(("time: %.2f..%.2f\tload: %.2f%%\t[%s%s%s]"):format(
pos-chunk,pos,
math.floor(t/chunk*100+.5),
("#"):rep(math.min(pgb,chnkcc)),
("."):rep(math.max(0,chnkcc-pgb)),
("!"):rep(math.max(0,-(chnkcc-pgb)))
))
end
while true do
local cmd = ctrl:demand()
local v = ctrl:demand()
if cmd == "pos" then
pos = v
elseif cmd == "look" then
look = v
elseif cmd == "chunk" then
chunk = v
elseif cmd == "emsnd" then
emsnd = v
elseif cmd == "rate" then
rate = v
elseif cmd == "bits" then
bits = v
elseif cmd == "snd" then
go()
else
error("unkc: "..tostring(cmd))
end
end