ability to pause

This commit is contained in:
Kimapr 2024-05-27 20:57:14 +05:00
parent 0ad512608b
commit 4c2f464f85
3 changed files with 64 additions and 21 deletions

View file

@ -4,9 +4,14 @@ Experiment with sound generation in Lua.
![screenshot](screenshot.png) ![screenshot](screenshot.png)
# Usage ## Usage
- Copy `sound.lua.example` to `sound.lua` - Copy `sound.lua.example` to `sound.lua`
- Open `sound.lua` in your text editor (autosave on change recommended) - Open `sound.lua` in your text editor (autosave on change recommended)
- Run this with `love .` - Run this with `love .`
- Edit `sound.lua`, and watch (listen) the sound update in real time - Edit `sound.lua`, and watch (listen) the sound update in real time
## Keys
- `r`: restart sound from the beginning
- `space`: pause/unpause

View file

@ -44,6 +44,7 @@ local function init()
end end
end end
end end
local playing=false
function love.load() function love.load()
love.thread.newThread("thread.lua"):start() love.thread.newThread("thread.lua"):start()
init() init()
@ -54,29 +55,54 @@ function love.keypressed(key)
sour:pause() sour:pause()
sour:stop() sour:stop()
init() init()
elseif key == "space" then
playing = not playing
if playing then
sour:play()
else
sour:pause()
end
end end
end end
local lastc = love.timer.getTime()
local waitingc = false
function love.update(dt) function love.update(dt)
local tt = love.timer.getTime()
for n=1,sour:getFreeBufferCount()-offset do for n=1,sour:getFreeBufferCount()-offset do
push("snd",true) push("snd",true)
offset=offset+1 offset=offset+1
table.remove(chunkd,1) table.remove(chunkd,1)
end end
local tt1 = love.timer.getTime()
if tt1-lastc >= chunk*1.2 and not waitingc then
waitingc = true
push("slook",true)
end
local tt2 = love.timer.getTime()
while sndc:getCount()>0 do while sndc:getCount()>0 do
local c = sndc:demand() local c = sndc:demand()
local v = sndc:demand() local v = sndc:demand()
if c == "snd" then if c == "snd" then
sour:queue(samplify(v)) sour:queue(samplify(v))
sour:play() if playing then sour:play() end
offset=offset-1 offset=offset-1
elseif c == "pos" then elseif c == "pos" then
pos = v pos = v
elseif c == "look" then elseif c == "look" then
look = v look = v
lastc = love.timer.getTime()
waitingc = false
else else
error("unkc: "..tostring(c)) error("unkc: "..tostring(c))
end end
end end
local tt3 = love.timer.getTime()
if tt3-tt > 0.01 then
print("EE",tt3-tt)
print(" 1",tt1-tt)
print(" 2",tt2-tt1)
print(" 3",tt3-tt2)
end
end end
function love.draw() function love.draw()
local w,h = love.graphics.getDimensions() local w,h = love.graphics.getDimensions()

View file

@ -10,18 +10,22 @@ local function send(c,v)
sndc:push(c) sndc:push(c)
sndc:push(v) sndc:push(v)
end end
local function go() local function go(uu)
local t = love.timer.getTime() local t = love.timer.getTime()
local ok,err,x = xpcall(function() local ok,err,x = xpcall(function()
local snd,x = dofile("sound.lua") local snd,x = dofile("sound.lua")
snd = snd:phase(pos):clamp(0,chunk,true) snd = snd:phase(pos):clamp(0,chunk,true)
if not uu then
snd = assert(snd:compile(rate,bits),"no sound") snd = assert(snd:compile(rate,bits),"no sound")
assert(snd:typeOf("SoundData"),"fake sound") assert(snd:typeOf("SoundData"),"fake sound")
end
return snd,x return snd,x
end,debug.traceback) end,debug.traceback)
look = tonumber(x) or look look = tonumber(x) or look
local tt = love.timer.getTime()
sndc:performAtomic(function() sndc:performAtomic(function()
send("look",look) send("look",look)
if not uu then
if not ok then if not ok then
send("snd",emsnd) send("snd",emsnd)
print(err) print(err)
@ -29,8 +33,13 @@ local function go()
send("snd",err) send("snd",err)
end end
pos=pos+chunk pos=pos+chunk
end
send("pos",pos) send("pos",pos)
end) end)
if love.timer.getTime()-tt > 0.01 then
print("TIME TAKEN:",love.timer.getTime()-tt)
end
if not uu then
t=love.timer.getTime()-t t=love.timer.getTime()-t
local chnkcc=16 local chnkcc=16
local pgb=math.floor(t/chunk*chnkcc+.5) local pgb=math.floor(t/chunk*chnkcc+.5)
@ -41,6 +50,7 @@ local function go()
("."):rep(math.max(0,chnkcc-pgb)), ("."):rep(math.max(0,chnkcc-pgb)),
("!"):rep(math.max(0,-(chnkcc-pgb))) ("!"):rep(math.max(0,-(chnkcc-pgb)))
)) ))
end
end end
while true do while true do
local cmd = ctrl:demand() local cmd = ctrl:demand()
@ -59,6 +69,8 @@ while true do
bits = v bits = v
elseif cmd == "snd" then elseif cmd == "snd" then
go() go()
elseif cmd == "slook" then
go(true)
else else
error("unkc: "..tostring(cmd)) error("unkc: "..tostring(cmd))
end end