sgstream/main.lua

127 lines
2.6 KiB
Lua
Raw Permalink Normal View History

2024-05-27 13:06:41 +03:00
local rate,bits = 44100,16
local chunk = 1/4
local look = 1/4
local chunkc = 2
local sour,pos
local emsnd = require("soundgen")
.silence(0)
:clamp(0,chunk)
:compile(rate,bits)
local chunkd = {}
local function samplify(snd)
local chunk = {}
for i=0,snd:getSampleCount()-1 do
chunk[i] = snd:getSample(i,1)
end
chunkd[#chunkd+1] = chunk
return snd
end
local sndc = love.thread.getChannel("snd")
local ctrl = love.thread.getChannel("ctrl")
local function push(c,v)
ctrl:supply(c)
ctrl:push(v)
end
local offset = 0
local function init()
sour = love.audio.newQueueableSource(rate,bits,1,chunkc)
pos = 0
chunkd={}
for n=1,chunkc*2 do
samplify(emsnd)
end
push("chunk",chunk)
push("emsnd",emsnd)
push("pos",pos)
push("look",look)
push("rate",rate)
push("bits",bits)
while offset>0 do
local c = sndc:demand()
local v = sndc:demand()
if c == "snd" then
offset=offset-1
end
end
end
2024-05-27 18:57:14 +03:00
local playing=false
2024-05-27 13:06:41 +03:00
function love.load()
love.thread.newThread("thread.lua"):start()
init()
sour:play()
end
function love.keypressed(key)
if key == "r" then
sour:pause()
sour:stop()
init()
2024-05-27 18:57:14 +03:00
elseif key == "space" then
playing = not playing
if playing then
sour:play()
else
sour:pause()
end
2024-05-27 13:06:41 +03:00
end
end
2024-05-27 18:57:14 +03:00
local lastc = love.timer.getTime()
local waitingc = false
2024-05-27 13:06:41 +03:00
function love.update(dt)
2024-05-27 18:57:14 +03:00
local tt = love.timer.getTime()
2024-05-27 13:06:41 +03:00
for n=1,sour:getFreeBufferCount()-offset do
push("snd",true)
offset=offset+1
table.remove(chunkd,1)
end
2024-05-27 18:57:14 +03:00
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()
2024-05-27 13:06:41 +03:00
while sndc:getCount()>0 do
local c = sndc:demand()
local v = sndc:demand()
if c == "snd" then
sour:queue(samplify(v))
2024-05-27 18:57:14 +03:00
if playing then sour:play() end
2024-05-27 13:06:41 +03:00
offset=offset-1
elseif c == "pos" then
pos = v
elseif c == "look" then
look = v
2024-05-27 18:57:14 +03:00
lastc = love.timer.getTime()
waitingc = false
2024-05-27 13:06:41 +03:00
else
error("unkc: "..tostring(c))
end
end
2024-05-27 18:57:14 +03:00
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
2024-05-27 13:06:41 +03:00
end
function love.draw()
local w,h = love.graphics.getDimensions()
local line = {}
local sx = math.floor(((chunkc+sour:getFreeBufferCount()-offset)*chunk+sour:tell())*rate)
local rr = emsnd:getSampleCount()
for t=0,w do
local x = t/w
x=x*2-1
x=x*rate*look
x=sx+x
line[#line+1]=t
x=math.floor(x+.5)
local si,ii = math.floor(x/rr)+1,x%rr
line[#line+1]=(((chunkd[si]or{})[ii]or 0)*0.5+0.5)*h
end
love.graphics.setColor(1,1,1,0.1)
love.graphics.line(w/2,0,w/2,h)
love.graphics.setColor(1,1,1)
love.graphics.line(line)
end