From add41b5b786b429ee24f9bb13a712429bc477fa0 Mon Sep 17 00:00:00 2001 From: Shaji Khan Date: Sun, 25 Feb 2024 20:20:11 +0530 Subject: [PATCH] Resample works! --- .../ladspa/amprack/AudioDecoder.java | 31 ++++++++++++++++++- .../ladspa/amprack/MainActivity.java | 17 +--------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/shajikhan/ladspa/amprack/AudioDecoder.java b/app/src/main/java/com/shajikhan/ladspa/amprack/AudioDecoder.java index 404e5d54..d57dae03 100644 --- a/app/src/main/java/com/shajikhan/ladspa/amprack/AudioDecoder.java +++ b/app/src/main/java/com/shajikhan/ladspa/amprack/AudioDecoder.java @@ -16,11 +16,13 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.FloatBuffer; import java.util.Arrays; import java.util.HashMap; public class AudioDecoder { MainActivity mainActivity ; + long MAX_BUFFER = 512 ; String TAG = "Moffin Decoder yeah" ; int sampleRate = 48000 ; AudioDecoder (MainActivity _MainActivity) { @@ -114,7 +116,16 @@ float[] decode (FileDescriptor fileDescriptor, String mimeType, int _sampleRate) boolean sawOutputEOS = false; int noOutputCounter = 0; boolean reconfigure = true ; + long bufferCount = 0 ; while (!sawOutputEOS && noOutputCounter < 50) { + bufferCount ++ ; +// Log.d(TAG, String.format ("buffer count %d: maxBuffer %d", bufferCount, MAX_BUFFER)); + if (bufferCount > MAX_BUFFER) { + sawInputEOS = true ; + sawOutputEOS = true ; + break ; + } + noOutputCounter++; if (!sawInputEOS) { int inputBufIndex = codec.dequeueInputBuffer(kTimeOutUs); @@ -191,6 +202,24 @@ float[] decode (FileDescriptor fileDescriptor, String mimeType, int _sampleRate) codec.release(); Log.i(TAG, "decode: returning " + String.format("%d audio samples", decoded.length)); - return decoded; + + if (sampleRate == 48000) + return decoded; + + ByteBuffer bb = ByteBuffer.allocateDirect(decoded.length * 4); + ByteBuffer bb2 = ByteBuffer.allocateDirect(decoded.length * 10); + FloatBuffer floatBuffer = bb.asFloatBuffer(), resampled = bb2.asFloatBuffer(); + floatBuffer.put(decoded); + floatBuffer.position(0); + + Resampler resampler = new Resampler(true,0.1,30); + boolean result = resampler.process((double)48000.0/sampleRate,floatBuffer,true,resampled); + float [] res = new float[resampled.limit()]; +// resampled.position(0); + for (int i = 0 ; i < resampled.limit(); i ++) { + res [i] = resampled.get(i); + } + + return res; } } diff --git a/app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java b/app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java index 0800ce6c..a7716250 100644 --- a/app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java +++ b/app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java @@ -1578,22 +1578,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { try { float [] samples = audioDecoder.decode(data.getData(), null, -1); - - ByteBuffer bb = ByteBuffer.allocateDirect(samples.length * 4); - ByteBuffer bb2 = ByteBuffer.allocateDirect(samples.length * 10); - FloatBuffer floatBuffer = bb.asFloatBuffer(), resampled = bb2.asFloatBuffer(); - floatBuffer.put(samples); - floatBuffer.position(0); - - Resampler resampler = new Resampler(true,0.1,30); - boolean result = resampler.process((double)48000.0/22050.0,floatBuffer,true,resampled); - float [] res = new float[resampled.limit()]; -// resampled.position(0); - for (int i = 0 ; i < resampled.limit(); i ++) { - res [i] = resampled.get(i); - } - - AudioEngine.setPluginBuffer(res, plugin); + AudioEngine.setPluginBuffer(samples, plugin); } catch (IOException e) { toast(e.getMessage()); Log.e(TAG, "onActivityResult: ", e);