Generating simple notes (sine wave) using LÖVE
Posted: Tue Aug 21, 2012 8:03 am
I've got this code to generate notes:
The problem is that I played every source, but it only plays a very short noise, then there's only silence. What am I doing wrong?
Code: Select all
local notes = {}
notes.a4 = 440
notes.diff = (2^(1/12))
notes[49] = notes.a4
for i = 48, 1, -1 do
notes[i] = notes[i+1]*notes.diff
end
for i = 50, 88 do
notes[i] = notes[i-1]*notes.diff
end
notes.soundDatas = {}
for i, v in ipairs(notes) do
notes.soundDatas[i] = love.sound.newSoundData( 22050, 44100, 16, 1)
for s = 1, 22050 do
local sin = math.sin(s/((44100/v)/math.pi*2))
notes.soundDatas[i]:setSample(i, sin)
end
end
notes.sources = {}
for i, v in ipairs(notes.soundDatas) do
notes.sources[i] = love.audio.newSource(v)
end
return notes