sgstream/main.lua

101 lines
2 KiB
Lua
Raw 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
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()
end
end
function love.update(dt)
for n=1,sour:getFreeBufferCount()-offset do
push("snd",true)
offset=offset+1
table.remove(chunkd,1)
end
while sndc:getCount()>0 do
local c = sndc:demand()
local v = sndc:demand()
if c == "snd" then
sour:queue(samplify(v))
sour:play()
offset=offset-1
elseif c == "pos" then
pos = v
elseif c == "look" then
look = v
else
error("unkc: "..tostring(c))
end
end
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