Downsample 16-bit pcm audio to 8-bit in android

3

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.

android
audio
pcm
asked on Stack Overflow Mar 1, 2010 by maxsap • edited May 4, 2014 by BenMorel

1 Answer

5

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;

If the 8 bit PCM is "offset binary" however (an unsigned format with an implicit bias) then the conversions would be:

16 bits to 8 bits:

sample_8 = (sample_16 >> 8) + 128;

8 bits to 16 bits:

sample_16 = (sample_8 - 128) << 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.

answered on Stack Overflow Mar 1, 2010 by Paul R • edited May 29, 2014 by Paul R

User contributions licensed under CC BY-SA 3.0