soundtest/main.lua

97 lines
2.3 KiB
Lua
Raw Normal View History

2024-05-26 15:27:13 +03:00
soundgen=require "soundgen"
love.window.setMode(800,600,{resizable=true})
function znoise(...)
return love.math.noise(...)*2-1
end
local sdata
local source
function love.keypressed(k)
local p="sounds/"..k..".lua"
if love.filesystem.getInfo(p) and k~="0" and k~="space" then
if source then
source:stop()
source=nil
sdata=nil
end
package.loaded.soundgen=nil
soundgen=require"soundgen"
local ok,err=xpcall(function()return dofile("sounds/"..k..".lua")end,debug.traceback)
if ok and err then
print("booooom")
sdata=err
source=love.audio.newSource(sdata)
source:setLooping(true)
source:play()
else
print("bad",ok,source,err)
end
elseif k=="0" then
if source then
source:stop()
source=nil
sdata=nil
end
elseif k=="space" then
if source then
local _ = source:isPlaying() and {source:pause()} or source:play()
end
end
end
local l1={}
local l2={}
local res=4096
local chanc=0
local upd=nil
local pitch=1
function love.mousemoved(x,y,dx,dy)
local w,h=love.graphics.getDimensions()
local dy=-dy/h*2*(math.abs(pitch)+.1)
if love.mouse.isDown(1) then
pitch=math.min(10,math.max(-10,pitch+dy))
end
end
function love.update(dt)
local w,h=love.graphics.getDimensions()
local rate=sdata and sdata:getSampleRate() or 0
chanc=sdata and sdata:getChannelCount() or 1
local s2i=chanc==2
if source then
source:setPitch(math.max(0.001,pitch))
if pitch<0.001 then
source:seek(math.max(0,source:tell()+pitch*dt))
end
end
local ma=sdata and sdata:getSampleCount() or 1
for n=1,res do
local sami=(source and source:tell("samples") or 0)-math.floor(rate*((n-1)/(res-1))*0.05)
sami=sami%ma
l1[(n-1)*2+1],l1[(n-1)*2+2]
=w-(n-1)/(res-1)*w,(sdata and (-sdata:getSample(s2i and sami*2 or sami)*0.5*0.5+0.5) or 0.5)*h
if s2i then
l2[(n-1)*2+1],l2[(n-1)*2+2]
=w-(n-1)/(res-1)*w,(sdata and (-sdata:getSample(sami*2+1)*0.5*0.5+0.5) or 0.5)*h
end
end
upd=true
end
function love.draw()
love.graphics.print(pitch)
love.graphics.push("all")
love.graphics.setLineJoin("none")
love.graphics.setLineWidth(1)
love.graphics.setLineStyle("rough")
if chanc==1 then
love.graphics.setColor(1,1,1)
love.graphics.line(l1)
elseif chanc==2 then
love.graphics.setColor(0.8,0.8,1)
love.graphics.line(l1)
love.graphics.setColor(1,0.8,0.8)
love.graphics.line(l2)
end
love.graphics.pop()
end