1

I want merge more WAV files in my app into one WAV file. But I don't want combine them consecutively (like File1-File2-File3...) but all files should start at the same time (concurrently/simultaneously).

Is this possible in Android without using some natives libraries (like FFmpeg)? I looked at the AudioTrack and SoundPool but I think it's not a solution for this problem.

Also I found this post Android: How to mix 2 audio files and reproduce them with soundPool but how can I combine bytes from more WAV files?

Community
  • 1
  • 1
Pepa Zapletal
  • 2,879
  • 3
  • 39
  • 69

1 Answers1

0

If you use AudioTrack or SoundPool, you won't be able to synchronize playing precisely, and most probably that will be easy to hear. Another problem is that your files can have different sound level, and thus the softer ones will get masked by the louder ones (think of playing some pop track on top of a piece of soft classical music). You will probably need to normalize the levels of the input sound files.

For precise merging and normalizing at the same time, you have to mix the sound contents of the files yourself, and then play the resulting output. You will need to read the .wav files yourself as binary streams, as Android lacks Java's AudioInputStream class which could make this easier. See this as an example of how to do it with basic IO classes:

https://thiscouldbebetter.wordpress.com/2011/08/14/reading-and-writing-a-wav-file-in-java/

Now you have your sound data as an array of short integers. This answer describes how to convert samples data from integer into floating point and merge two audio streams, while normalizing their sound level:

https://stackoverflow.com/a/32023546/4477684

In order to merge more that two sound inputs, start with merging two, and then merge the result with the third input, and so on.

Community
  • 1
  • 1
Mikhail Naganov
  • 6,643
  • 1
  • 26
  • 26
  • Thanks for your answer! I think some easy solution for this won't exists on Android. Do you know FFmpeg library? Is possible use this library (with some params) for this (merging and normalizing)? – Pepa Zapletal May 23 '16 at 18:41
  • First of all, ffmpeg is a native library and I would suggest thinking twice before adding native code to your app. On Android, if something can be done with Java, and performance is acceptable, it's better to stick with it. With ffmpeg (its libswresample part) it is definitely possible to merge audio streams, but it seems like a hammer too big. You can try using ffmpeg command-line interface to prototype your solution though. Here people are discussing how to merge multi-channel audio using ffmpeg, may be a good starting point: http://ffmpeg.gusari.org/viewtopic.php?f=11&t=2502 – Mikhail Naganov May 23 '16 at 23:51