Generating simple notes (sine wave) using LÖVE

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Generating simple notes (sine wave) using LÖVE

Post 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?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Technicolour
Prole
Posts: 24
Joined: Sat Sep 24, 2011 12:22 am

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

Post by Technicolour »

Add this to your new audio source: setLooping( true )
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

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

Post 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
Shallow indentations.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post by Nixola »

Oh, that's quite a stupid error ^^'
Thanks Boolsheet (a sheet made out of trues and falses? ok, ignore it)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 15 guests