How to set the frequency in sound?

I try to play a sound with a specific frequency. So I used the sound(parameter_table) function. But changing the StartFrequency does not change anything. Try it with the sample below. All other parameters change the sound, but not the StartFrequency. But using the dialog in the editor to change the settings, the frequency changes. What am I doing wrong?

Maybe StartFrequency is the wrong name, because I tried to encode the settings and it all gives me the same string although I change the frequency.


function setup()
    tap =  false
    iparameter("Waveform", 0, 3, 0)
    iparameter("StartFrequency", 1, 20000, 440)
    parameter("AttackTime", 0, 1, 0.3)
    parameter("SustainPunch", 0, 1, 0.4)
    parameter("DecayTime", 0, 1, 0.6)
end

function touched(touch)
    if touch.state == ENDED and touch.tapCount == 1 then
        tap = true
    end
end

function draw()
    if tap == true then
        instrument = {Waveform = Waveform, StartFrequency = StartFrequency, AttackTime = AttackTime, SustainPunch = SustainPunch, DecayTime = DecayTime}
        sound(instrument)

        --encStr = sound( ENCODE, instrument )
        --print(encStr)
        --sound(DATA, encStr)
        tap=false
    end
end

@KilamMalik

I haven’t played around with the sound function yet, but I don’t think the StartFrequency is what your expecting, ie 20-20,000 that the human ear is supposed to hear. Change the line of code for StartFrequency to be parameter instead of iparameter, and change the range to 0,2,1. Then run the program and try changing the StartFrequency towards 0.

Thanks @dave1707, now I get different frequencies :slight_smile:

I will have to find out how to calculcate this number from a frequency or better from a note. I have seen there is a music player called ABCplayer, maybe I’ll find some hints there.

Hi @KilamMalik,

Check out this thread for a discussion on what the parameter means: http://twolivesleft.com/Codea/Talk/discussion/comment/9384#Comment_9384

StartFrequency can be between 0 and 1.

I incorporated the algorithm in my ABCPlayerCodea project which you can find on GitHub, thread here: http://twolivesleft.com/Codea/Talk/discussion/225/abcplayercodea-play-music-in-your-project#Item_59

Thanks @Fred for the info. I tried doing a google search for SFXR and didn’t find anything useful. I was wondering if there was a formula to tie the StartFrequency to actual sound frequencies, and it’s there in the link. Also shows the StartFrequency to have a range of 0-1. When I was working on @KilamMalik’s program, I wasn’t sure if the frequency was changing above the value 1 or I just couldn’t hear the changes. I’m not playing with the sound functions yet, but it’s good to have the info if I start.

Thanks @Fred, that helped a lot. I have now written a function which calculates the half steps needed in the function you referenced in your ABCplayer. You can choose the note, the octave and the accidental with buttons, then play the sound and you see the calculation results on screen. I have checked the frequencies with this page and they are the same :slight_smile:

http://www.contrabass.com/pages/frequency.html

Looks like I can’t change this topic and type to “Example”, so I post the source in another discussion. Maybe its helpful for others too.