Reputation: 3941
I am generating a simple musical instrument using a ST board. basically I have a sensor which detects motion or spatial angle and generates sounds of differnt pitch and volume depending on the angle. I already have the driver for the audio codec so all I need to do is to generate sound samples and feed the samples to it. Now I am able to get the angle readings from the sensor but the challenging part is how to generate sound. From google search so far, I think I need to generate a sin function with frequency and amplitude(volume) set according to the reading from the sensor. But my codec assumes a sampling rate 0f 48KHz so how would I go about generating sinusoids with different frequencies for a fixed sampling rate?
So far I have done this:
samplingRate = 48000;
n = 0; // reset once there is a change in frequency
// this function is called 48000 times a second
int generateSineWave(float frequency, float volume)
{
int temp = volume*(sin(2*pi*frequency*n);
n = n + 1;
if (n == samplingRate) {
n = 0;
}
if (abs(temp) > MAXVAL) {
return ERROR_CODE;
}
return temp;
}
This seems to be working (I am hearing something),but I am not sure if it's generating the right frequency sinusoid. also the sound I am hearing is not very pleasant, how would I go about generating complex tones(like the ones in piano for example)? I guess also my control variables(spatial angles) needs to be low pass filtered. But apart from that any idea on how I could generate more audibly pleasant waves?
Upvotes: 1
Views: 1494
Reputation: 48310
Since you're sampling at 48 kHz (48,000 samples per second), it will take one second, or 48,000 samples, for a 1 Hz sinusoid to complete a single cycle. So you need to normalize, or divide, by the sampling rate:
temp = volume * sin(2 * pi * n * frequency / samplingRate);
Note that temp
must always be in the range from -volume
to volume
, so you need only check once that volume < MAXVAL
, and you can do that before you calculate temp
, which will detect an error (very slightly) more efficiently. You'll find pi
is already defined for you as M_PI
in math.h
.
You mentioned the tones aren't very pleasant. That's a sign that something may be wrong, because pure sinusoids sound, well, pure and are generally pleasing. One simple thing you might try is to start and stop your tones exactly at a zero-crossing; otherwise you'll hear clicks or distortions at the transitions. Alternatively, you can fade them in and out by multiplying by a "ramp" function, which you can create by decreasing a float
from 1 to 0 over some short length of time.
Some C language tips:
• You can replace n = n + 1
with ++n
.
• You can replace
if (abs(temp) > MAXVAL) {
return ERROR_CODE;
}
return temp;
with
return abs(temp) > MAXVAL ? ERROR_CODE : temp;
• If you choose to keep the if
, it's a good idea to use braces even for single-line bodies, because you may someday add another line and forget about the braces. That's a particularly hard bug to find because you'll read the code as you expect it to be, and not as it actually is. Trust me on this. :-)
Upvotes: 5