Skip to content

Commit

Permalink
Resample works!
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Feb 25, 2024
1 parent d073900 commit add41b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
31 changes: 30 additions & 1 deletion app/src/main/java/com/shajikhan/ladspa/amprack/AudioDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
17 changes: 1 addition & 16 deletions app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit add41b5

Please sign in to comment.