#174015 - gelinlonlin - Wed May 12, 2010 8:30 pm
HI,I have looked at this,http://deku.rydia.net/program/sound2.html
Part2. Resampling
soundVars.curMixBuffer[i] = channel.data[ channel.pos>>12 ] * channel.vol >> 6;
First,why it need resampling?
and another is
I can not understand why it need >>12.
thanks for explaining.
#174016 - elhobbs - Thu May 13, 2010 12:14 am
Resampling would allow a sample recorded at one rate - say 44 kHz to playback at a different rate - say 22 kHz. The position may be stored in a fixed point format to allow increment the position by fractional amounts. The shift would convert the fixed point value to a whole number.
#174030 - Ruben - Thu May 13, 2010 10:51 pm
Well, what elhobbs said: channel.pos is in 12Q format (that is, a decimal number multiplied by 2^12 (1000h = 1.0, 800h = 0.5, 400h = 0.25, etc)).
This allows you to keep an accuracy of up to 4096 different values, with 0 being a whole number, or 4095 being something like 0.9999999...
The >>6 afterwards is merely done to divide by 64, as channel.vol's maximum value IS 64.
Resampling is required to allow one to play a sample with a different sampling rate than that of the mixer [for example, Deku's examples use a mixing rate of 18157Hz and 31536Hz, if I remember right. Most MOD samples use a rate of 8363Hz which is then modified to different frequencies to create an illusion of 'notes' in 1/12 intervals].