Well, I’m no expert on sound data management, but I do believe it should be pretty easy to upsample the data. In it’s simplest form, all you need to do is “stretch” the data much in the same way you’d stretch an image. It’s easy with standard multiples (i.e. 11hz, 22hz, 44h) since the sound is always even after conversion (i.e. 11hz to 44hz would just require that you quadruple each byte). With non-standard sample rates, it’s better to use a float to count out the streaching. For example:
double multiple = 2.5;
double count = 0;
byte[] olddata = getSoundData();
byte[] newdata = olddata * multiple;
for(int i=0, j=0; i<newdata.length; i++)
{
newdata[i] = olddata[j];
count++;
if(count > multiple)
{
j++;
count -= multiple;
}
}
Downsampling is just as easy. You just grab every so many bytes and throw away the rest. Of course, for more professional sounds (Whatcho’ talkin’ about? This is a game! I pitty the foo that get fancy! ;)) you’d probably want to “smooth” the sounds by performing averages on the resized data. For upsampling, this would mean that you would take the difference between the last byte and this one and add “in between” values. With downsampling, you’d average the X number of bytes to create the one byte for each sample. (i.e. 44hz to 11hz would require averaging 4 bytes of the original to produce one byte of the result.)
Does anyone else have any comments on this?