Downsampling pcm/wav audio from 22khz to 8khz
Asked Answered
C

2

0

My android application needs to convert PCM(22khz) to AMR , but the API AmrInputStream only supports with pcm of 8khz.

How can i downsample the pcm from 22 khz to 8 khz?

Cozmo answered 18/2, 2013 at 4:25 Comment(0)
J
1

The sample rate is hard coded in AmrInputStream.java.

// frame is 20 msec at 8.000 khz
private final static int SAMPLES_PER_FRAME = 8000 * 20 / 1000;

So you have to convert the PCM to AMR first.

InputStream inStream;
inStream = new FileInputStream(wavFilename);
AmrInputStream aStream = new AmrInputStream(inStream);

File file = new File(amrFilename);        
file.createNewFile();
OutputStream out = new FileOutputStream(file); 

//adding tag #!AMR\n
out.write(0x23);
out.write(0x21);
out.write(0x41);
out.write(0x4D);
out.write(0x52);
out.write(0x0A);    

byte[] x = new byte[1024];
int len;
while ((len=aStream.read(x)) > 0) {
    out.write(x,0,len);
}

out.close();

For Downsampling, You can try the Mary API.

Jughead answered 18/2, 2013 at 4:35 Comment(7)
i have done this already, what i want to do is downsample the pcm before converting to amr.Cozmo
Find a mary api to downsample audio file.Jughead
this api doesn't run on android. :(Cozmo
Yes, but I'm pretty sure there is no other API in Android that can downsample an audio file unless you implement one or use an existing library.Jughead
do you know any implementation of downsampling in c or c++?Cozmo
Yeah, here you are, and this link contains a guide to use ffmpeg. But do you want to use native code to do this in Android?Jughead
Androd allow you to use native library. see Android NDKJughead
I
0

I find a java downsample lib :https://github.com/hutm/JSSRC there is also a c version could be used by jni

Incoherent answered 18/8, 2015 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.