private static ByteBuffer resampleTo16BitPcm(ByteBuffer buffer,int offset,int size,int sourceEncoding,ByteBuffer out){
  int resampledSize;
switch (sourceEncoding) {
case C.ENCODING_PCM_8BIT:
    resampledSize=size * 2;
  break;
case C.ENCODING_PCM_24BIT:
resampledSize=(size / 3) * 2;
break;
case C.ENCODING_PCM_32BIT:
resampledSize=size / 2;
break;
default :
throw new IllegalStateException();
}
ByteBuffer resampledBuffer=out;
if (resampledBuffer == null || resampledBuffer.capacity() < resampledSize) {
resampledBuffer=ByteBuffer.allocateDirect(resampledSize);
}
resampledBuffer.position(0);
resampledBuffer.limit(resampledSize);
int limit=offset + size;
switch (sourceEncoding) {
case C.ENCODING_PCM_8BIT:
for (int i=offset; i < limit; i++) {
resampledBuffer.put((byte)0);
resampledBuffer.put((byte)((buffer.get(i) & 0xFF) - 128));
}
break;
case C.ENCODING_PCM_24BIT:
for (int i=offset; i < limit; i+=3) {
resampledBuffer.put(buffer.get(i + 1));
resampledBuffer.put(buffer.get(i + 2));
}
break;
case C.ENCODING_PCM_32BIT:
for (int i=offset; i < limit; i+=4) {
resampledBuffer.put(buffer.get(i + 2));
resampledBuffer.put(buffer.get(i + 3));
}
break;
default :
throw new IllegalStateException();
}
resampledBuffer.position(0);
return resampledBuffer;
}

private static ByteBuffer resampleTo16BitPcm ( ByteBuffer buffer , int offset , int size , int sourceEncoding , ByteBuffer out ) { int resampledSize ; switch ( sourceEncoding ) { case C . ENCODING_PCM_8BIT : resampledSize = size * 2 ; break ; case C . ENCODING_PCM_24BIT : resampledSize = ( size / 3 ) * 2 ; break ; case C . ENCODING_PCM_32BIT : resampledSize = size / 2 ; break ; default : throw new IllegalStateException ( ) ; } ByteBuffer resampledBuffer = out ; if ( resampledBuffer == null || resampledBuffer . capacity ( ) < resampledSize ) { resampledBuffer = ByteBuffer . allocateDirect ( resampledSize ) ; } resampledBuffer . position ( 0 ) ; resampledBuffer . limit ( resampledSize ) ; int limit = offset + size ; switch ( sourceEncoding ) { case C . ENCODING_PCM_8BIT : for ( int i = offset ; i < limit ; i ++ ) { resampledBuffer . put ( ( byte ) 0 ) ; resampledBuffer . put ( ( byte ) ( ( buffer . get ( i ) & 0xFF ) - 128 ) ) ; } break ; case C . ENCODING_PCM_24BIT : for ( int i = offset ; i < limit ; i += 3 ) { resampledBuffer . put ( buffer . get ( i + 1 ) ) ; resampledBuffer . put ( buffer . get ( i + 2 ) ) ; } break ; case C . ENCODING_PCM_32BIT : for ( int i = offset ; i < limit ; i += 4 ) { resampledBuffer . put ( buffer . get ( i + 2 ) ) ; resampledBuffer . put ( buffer . get ( i + 3 ) ) ; } break ; default : throw new IllegalStateException ( ) ; } resampledBuffer . position ( 0 ) ; return resampledBuffer ; }
converts the provided buffer into 16 - bit pcm .
