tags:

views:

359

answers:

1

Hello I would like to downsample 16 bit pcm audio to 8 bit and then upsample the same from 8 bit back to 16 bit in android. I am using this which seems to work:

int tempint;
for (int i=1, j=0; i<tempBuffer.length; i+=2, j++)
{
    tempint = ((int) tempBuffer[i]) ^ 0x00000080;
    tempBuffer[j] = (byte) tempint; 
    Log.e("test","------------* COUNTER -> "+j+" BUFFER VALUE -> "+tempBuffer[j]+"*-----------");
} 

where tempbuffer is a short [] and tempint is an int. Can anyone tell me if this works fine because I am a starter, also I am using this to convert the byte [] back to a short []

for (int x=1, j=0; x<music.length; x+=2, j++)
{
    tempint = ((int) music[x])& 0xFF;
    music[j] = tempint.shortValue();
    Log.e("maxsap","------------*"+ music[j]+"*-----------");
}

which I am not sure it is working.

+1  A: 

Assuming both 8 bit and 16 bit audio are signed PCM:

16 bits to 8 bits:

sample_8 = sample_16 >> 8;

8 bits to 16 bits:

sample_16 = sample_8 << 8;

Ideally you would add dithering when converting from 8 bits up to 16 bits but you may not be able to afford the computational cost of doing this.

Note also that the terms "upsampling" and "downsampling" usually refer to changing sample rates. What you're doing here is not re-resampling, but changing the bit depth or re-quantizing the data.

Paul R