I have a waveform 64 samples long. If the sampling rate is 44100 hz, how can I play(loop) this waveform so that it plays arbitrary frequencies?
frequency = samplerate / waveform duration in samples
Therefore the frequency should be 689hz(44100/64). If I wanted it to be say, 65.41hz(C-2), I would have to do this:
65.41 = 44100 / x
Solving for x yields aprox. 674.208. So I need to figure out what speed to play the waveform at to get this frequency. So we can solve this equation:
64 * x = 674.208
and get about 10.5. So the waveform needs to be played at 10.5% of its original speed.
Here is my code:
double smp_index = 0;
double freq = .105;
void callback(void *data, Uint8 *buf, int len){
int i;
s8 *out;
out = (s8*) buf;
if(smp_index < waveform_length){
for(i = 0; i < len; i ++){
out[i] = smpdata[(int)smp_index];
smp_index +=freq;
if(smp_index >= waveform_length)
smp_index = 0;
}
}
}
So the resulting audio should be about the note C-2, but its more of a D-2. Is the cast
(int)smp_index
causing the problem? I couldn't see any other way to accomplish this...