What should I use in Android when porting C++ code written in libsndfiles?
I’m porting a small (< 10 classes) C++ project to Java. The project manipulates the sound file by using libsndfile in C++. The code includes the following:
const int channels = audioFileInfo.channels;
...
sf_readf_double( audioFile, inputBuffer, MAX_ECHO );
...
sf_writef_double( outputAudioFile, ¤tAudioBuffer[WINDOW_SIZE * channels], SEGMENTATION_LENGTH );
What is the best way to manipulate sound files at a low level in Java? I’m talking about normalization, adding echoes, etc.
Progress report
After some digging, I found javax.sound.sampled , looks like it can do the job.
Edit 2 double-checked, it won’t work (or at least not in any usable way) because it relies on the com.sun.sound
package
Edit 3 After more checking and experimentation, the com.sun.sound and
sun.misc
packages are released under GPLv2 under GNU, and I have downloaded them into my project. After renaming javax.sound.sampled to imp.javax.sound.sampled
, the project compiles and I can create an AudioFileFormat
object without exception and it is thrown, however.
I haven’t had a chance to play much yet, but I’ll keep you informed.
Edit 4 Ok, some things seem to work with javax.sound.sampled and some cannot. For example, the call is as follows:
AudioInputStream stream = AudioSystem.getAudioInputStream(waveFile));
Not working, but I can fix this by doing this :
WaveFileReader wfr = new WaveFileReader();
AudioInputStream stream = wfr.getAudioInputStream(waveFile);
In general, calling something like AudioSystem.getAudioFileTypes()
returns an empty list. I can dig into these packages and see that it’s provider-related, but I don’t know how to fix this. After getting my stream
object, it does report its encoding etc correctly, which is encouraging.
My big problem now is creating a Clip object. This needs to be created using a Line object, which is typically from the AudioSystem. Who can come up with a solution?
Solution
libsndfiles can be compiled for Android using the Native Development Kit. After compiling the library for Android, you should be able to access it from Java using JNI.