I am making an app in which I need to decode an mp3, perform some filtering, and then play it out using AudioTrack. Right now I am using Jlayer. My code looks something like this:
private void writeintoBuffer(){
Decoder decoder= new Decoder();
ByteArrayOutputStream os = new ByteArrayOutputStream();
while(playing){
try {
os.reset();
Header frameHeader = bitStream.readFrame();
if (frameHeader == null) {
playing=false;
}
//Jlayer stuff
SampleBuffer output = (SampleBuffer) decoder.decodeFrame(frameHeader, bitStream);
bitStream.closeFrame();
short[] pcm =output.getBuffer();
for (short s : pcm) {
os.write(s & 0xff);
os.write((s >> 8 ) & 0xff);
}
byte[] Buffer=os.toByteArray();
if(Filter){
//apply filter
Buffer=Tools.Filter(Buffer);
}
AudioInputBuffer.write(Buffer, 0, Buffer.length);//writing into buffer
}
catch (Exception E){
E.printStackTrace();
}
}*
The AudioInputBuffer is the AudioTrack variable.Bitstream has been initialized previously
Unfortunately, JLayer is very slow and it's really causing my app to lag now. I was hoping to get some help from NDK which according to this article would be around 20x faster than Pure Java. I have looked through NDK's audio project sample and I must admit, my C is not very good. Isn't there any way I can do with NDK what Jlayer did for me: I pass a bit of the mp3 file, it decodes it for me and gives it back to me ready to run my processes and then submit into buffer? Code would be more appreciated!