1

I am using ffmpeg (c++) to decode variety of audio files into 44100 stereo OR mono (according to the source). I have to keep this like this because I play them too. Meanwhile, I also need 11025 mono versions of these. So I am trying to find a very quick/easy way to convert 44100 stereo PCM into 11025 mono without ffmpeg to save cpu.

Is this possible in java (android) or c++ (ndk)?

I am already using java and c++ in the same project.

4
  • Are we talking about simple .wav files, or some other format of audio (e.g MP3, or some such)? To a large degree, converting from 44KHz to 11KHz is simply an averaging of the values. And I think stereo to mono is also just "combine the two values by averaging"... Jul 22, 2013 at 10:55
  • I convert MP3 or AAC into 44KHz Stereo/Mono PCM with ffmpeg.
    – frankish
    Jul 22, 2013 at 10:58
  • Right, so then "downsampling" that to 11K is just a case of taking 4 samples and average into one "new" sample. Jul 22, 2013 at 11:02
  • No it's not. What you are suggested is known as a moving average filter. It has appalling characteristics as downsampling filter and will sound terrible. What is required is a low pass filter whose corner frequency is 11.025kHz and good attenuation in the stop band above this frequency.
    – marko
    Jul 22, 2013 at 21:56

1 Answer 1

1

sample-by-sample averaging is correct for stereo to mono conversion, but for sample rate conversion, why not use a library like libsamplerate? If even that is too heavy, averaging is indeed fast, but it is not the correct method. Whether it is acceptable or not depends on your application. An alternative method is described in my answer to this SO post:

How to convert pcm samples in byte array as floating point numbers in the range -1.0 to 1.0 and back?

5
  • Would libsmaplerate work fast and use low cpu? I continuously pump bytes into AudioTrack in android which accepts PCM audio according to my setup (44100 and stereo or mono). If I use libsamplerate, I have to send these bytes into libsamplerate function while sending them to Android audio system (AudioTrack). In that case I think I need frame/window counts/boundaries, which I don't have. I just push bytes without knowing boundaries, audio system handles the rest. Any ideas?
    – frankish
    Jul 22, 2013 at 13:40
  • libsamplerate should be fast enough for android, and IIRC it gives you performance options. But, of course, it depends on what else is running. You will have to test yourself. Jul 22, 2013 at 13:53
  • How can I find out and trim audio data to find out frame count and audio window? Otherwise I may not provide correct input for libsamplerate functions.
    – frankish
    Jul 22, 2013 at 13:56
  • If you are having trouble using libsamplerate, please see the documentation and/or post a new question. Jul 22, 2013 at 14:00
  • I created a new question, thank you: stackoverflow.com/questions/17789917/…
    – frankish
    Jul 22, 2013 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.