Sound synthesis and strange error from tutorial.

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
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Sound synthesis and strange error from tutorial.

Post by Pangit »

Back in the day there were some great books for the Atari and commodore about making your own sounds for games. I realised after a quick check that love has the potential to do this also. What I am getting stuck on is that the example tutorial is pretty basic and seems to not work.

http://www.headchant.com/2011/11/01/sou ... ine-waves/

When I type in the listing - I changed the

Code: Select all

function Oscillator(freq)
    local phase = 0
    return function()
        phase = phase + 2*math.pi/rate            
        if phase >= 2*math.pi then
            phase = phase - 2*math.pi
        end
        return math.sin(freq*phase)
    end
end
To...

Code: Select all

function Oscillator(freq)
    local phase = 0
    return function()
        phase = phase + 2*math.pi/rate            
        if phase >= 2*math.pi then
            phase = phase - 2*math.pi
        end
        return math.sin(freq*phase)
    end
end
But this still produces attempt to set out of range sample.

So I am guessing that something is up with the way that the Oscillator is checking the phase?

Is there something wrong with the code snippet? I checked the documentation and there does not seem to be any depreciated library functions being called. It should just work right - with the exception of the >.

Lol I just want to get my tone playing then I can start to update the old school commodore books to lua.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Sound synthesis and strange error from tutorial.

Post by Nixola »

The issue is when calling setSample, which we can't see. Could you provide the whole code?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: Sound synthesis and strange error from tutorial.

Post by Pangit »

Sure

Code: Select all


function Oscillator(freq)
    local phase = 0
    return function()
        phase = phase + 2*math.pi/rate           
        if phase >= 2*math.pi then
            phase = phase - 2*math.pi
        end
        return math.sin(freq*phase)
    end
end

function love.load()
    len = 2
    rate = 44100
    bits = 16
    channel = 1

    soundData = love.sound.newSoundData(len*rate,rate,bits,channel)

    osc = Oscillator(440)
    amplitude = 0.2

    for i=1,len*rate do
        sample = osc() * amplitude
        soundData:setSample(i, sample)
    end

    source = love.audio.newSource(soundData)
    love.audio.play(source)
end

User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Sound synthesis and strange error from tutorial.

Post by Nixola »

Sounddatas' samples go from 0 to length-1, you should adjust your loop accordingly
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: Sound synthesis and strange error from tutorial.

Post by Pangit »

Thank you for your help Nixola much appreciated.

Code: Select all

function Oscillator(freq)
    local phase = 0
    return function()
        phase = phase + 2*math.pi/rate            
        if phase >= 2*math.pi then
            phase = phase - 2*math.pi
        end
        return math.sin(freq*phase)
    end
end


function love.load()
    len = 2
    rate = 44100
    bits = 16
    channel = 1

    soundData = love.sound.newSoundData(len*rate,rate,bits,channel)

    osc = Oscillator(440)
    amplitude = 0.2

    for i=0,len*rate-1 do
        sample = osc() * amplitude
        soundData:setSample(i, sample)
    end

    source = love.audio.newSource(soundData)
    love.audio.play(source)
end
Found the following really useful also.

https://github.com/superzazu/denver.lua ... denver.lua

Was able to use the oscillators in my little program. Now if only I could get the other types of noise working (grey, blue, violet) :ultraglee:
Post Reply

Who is online

Users browsing this forum: No registered users and 208 guests