In working on a project I came across the need to generate various waves, accurately. I thought that a simple sine wave would be the easiest to begin with, but it appears that I am mistaken. I made a simple program that generates a vector
of samples and then plays those samples back so that the user hears the wave, as a test. Here is the relevant code:
vector<short> genSineWaveSample(int nsamples, float freq, float amp) {
vector<short> samples;
for(float i = 0; i <= nsamples; i++) {
samples.push_back(amp * sinx15(freq*i));
}
return samples;
}
I'm not sure what the issue with this is. I understand that there could be some issue with the vector being made of short
s, but that's what my audio framework wants, and I am inexperienced with that kind of library and so do not know what to expect.
The symptoms are as follows:
- frequency not correct
- ie: given freq=440, A4 is not the note played back
- strange distortion
- Most frequencies do not generate a clean wave. 220, 440, 880 are all clean, most others are distorted
- Most frequencies are shifted upwards considerably
Can anyone give advice as to what I may be doing wrong?
Here's what I've tried so far:
- Making my own sine function, for greater accuracy.
- I used a 15th degree Taylor Series expansion for sin(x)
- Changed the sample rate, anything from 256 to 44100, no change can be heard given the above errors, the waves are simply more distorted.
Thank you. If there is any information that can help you, I'd be obliged to provide it.
i
to typeint
, most probably it will not fix anything, but will work faster – Slavasinx15
do? – Ed Healsinx15
is my 15th degree Taylor polynomial sine approximation. – PyroAVR