Page 1 of 1

Generating simple notes (sine wave) using LÖVE

Posted: Tue Aug 21, 2012 8:03 am
by Nixola
I've got this code to generate notes:

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
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?

Re: Generating simple notes (sine wave) using LÖVE

Posted: Tue Aug 21, 2012 11:06 am
by Technicolour
Add this to your new audio source: setLooping( true )

Re: Generating simple notes (sine wave) using LÖVE

Posted: Tue Aug 21, 2012 11:10 am
by Nixola
That's not the problem, the notes are half a second long... Anyway I tried, just to be sure, and the only result I achieved is a short looping noise

Re: Generating simple notes (sine wave) using LÖVE

Posted: Tue Aug 21, 2012 12:04 pm
by Boolsheet
The samples you set with setSample actually start at 0. The code maps it more or less directly to the array.

New SoundData are uninitialized and may contain random data.

You always set sample 1. You always set the the sample with the index of the SoundData (the variable 'i').

Code: Select all

for i, v in ipairs(notes) do
   notes.soundDatas[i] = love.sound.newSoundData( 22050, 44100, 16, 1)
   for s = 0, 22050-1 do
      local sin = math.sin(s/((44100/v)/math.pi*2))
      notes.soundDatas[i]:setSample(s, sin)
   end
end

Re: Generating simple notes (sine wave) using LÖVE

Posted: Tue Aug 21, 2012 12:09 pm
by Nixola
Oh, that's quite a stupid error ^^'
Thanks Boolsheet (a sheet made out of trues and falses? ok, ignore it)