Calling for a math guru,
would be nice if someone could explain this
I need to find the stepsize to get a nice sin wave
and maybe other waves in the future. but I'm confused
Anyone ?
You need to have a_nFrequency steps per second for a_nDuration seconds. The desired frequency of sine wave needs to be divided by a_nFrequency steps per second to get the increment to pass to the float that is input to the sin() function.
Let's say that a_nFrequency is 28,000 hertz, duration is 10 seconds, and you want a sin wave to produce concert A (440 hertz).
So you'd need 28,000 * 10 sample elements to put the sin values into, and the increment per sample element would be 440.0/28000.0 .
As Elias pointed out below, I forgot about the 2.0 * M_PI to get one complete cycle from sin().
Well, just look at a sine wave:
y = sin(x)
If x runs from 0 to 2 * pi, you get one complete wave. Now multiply by 2 * pi:
y = sin(x * 2 * pi)
Now the full wave goes from 0 to 1. Now assume x is our time in seconds, and you want a 440 Hz tone:
y = sin(x * 2 * pi * 440);
Now each seconds there is 440 complete sine waves.
Now, instead of having the time in seconds, x is a sample position, 48000 samples make up one second:
y = sin(x * 2 * pi * 440 / 48000);
In your code, a_nFrequency is the 48000. So a_nDuration/samples is your tone's frequency. I'm a bit unsure about those variable names, I'd say you can simplify that function a lot and still have it do the same thing.
Thnx,
I think this is what it should look like.
I'll try to make some effects using harmonics , see what can be done with this.
Allegro 5 should definitely get some "record sample" or "audio input" functions
Looks good to me now, assuming a_nDuration is in ms.