1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
uint_32 convertsample(uint_32 srcrate, uint_32 destrate, uint_32 pos)
{
return (uint_32)(((double)pos/(double)srcrate)*(double)destrate); //Give the position we use!
}
#ifndef sample_t
typedef struct
{
float l;
float r;
} sample_t; //The sample structure for 16-bits, stereo PCM!
#endif
void resample(sample_t *src, uint_32 srcrate, sample_t *dest, uint_32 destrate)
{
uint_32 i;
for (i=0;i<destrate;i++)
{
dest[i]->l = src[convertsample(srcrate,destrate,i)]->l; //SRC data L-channel!
dest[i]->r = src[convertsample(srcrate,destrate,i)]->r; //SRC data L-channel!
}
}
sample_t sample[2][44100]; //2 max 44KHz samples!
uint_32 samplerate[2] = {44100,22050}; //Sample rate of above samples, first is 44KHz, second is 22KHz!
sample_t resampled[2][44100]; //Resampled data!
void SoundRenderer(uint_32 hw_samplerate) //Render sample whatsample to data with hw_samplerate Hz
{
//Fill sample!
int i;
for (i=0;i<2;i++) //Process all samples!
{
resample(&sample[i],samplerate[i],&resampled[i],hw_samplerate); //Resample from buffer to PCM stream for hardware!
}
//Now just add and clip each sample!
//We're ready to process to the hardware!
}
//SoundRenderer is called by the hardware thread. Parameters are the buffer for the result, the hardware sample rate (44KHz(44100) most of the time) and the sample to use (0+, index into the sample(rate) arrays with data).
|