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?

Raneez Ahmed
  • 3,808
  • 3
  • 35
  • 58

2 Answers2

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.

StarPinkER
  • 14,081
  • 7
  • 55
  • 81
  • i have done this already, what i want to do is downsample the pcm before converting to amr. – Raneez Ahmed Feb 18 '13 at 05:46
  • Find a [mary api](http://elckerlyc.ewi.utwente.nl/browser/Elckerlyc/Shared/repository/MARYTTS/java/marytts/util/data/audio/AudioConverterUtils.java) to downsample audio file. – StarPinkER Feb 18 '13 at 06:17
  • this api doesn't run on android. :( – Raneez Ahmed Feb 18 '13 at 08:38
  • 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. – StarPinkER Feb 18 '13 at 08:41
  • do you know any implementation of downsampling in c or c++? – Raneez Ahmed Feb 18 '13 at 08:45
  • 1
    Yeah, [here](https://ccrma.stanford.edu/~jos/resample/Free_Resampling_Software.html) you are, and [this link](http://tdistler.com/2010/07/22/audio-resampling-using-ffmpeg-avcodec) contains a guide to use ffmpeg. But do you want to use native code to do this in Android? – StarPinkER Feb 18 '13 at 08:50
  • Androd allow you to use native library. see [Android NDK](http://developer.android.com/tools/sdk/ndk/index.html) – StarPinkER Feb 18 '13 at 09:04
0

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

max
  • 31
  • 2