Skip to content

Commit

Permalink
add resampleEx
Browse files Browse the repository at this point in the history
  • Loading branch information
ashqal committed May 25, 2018
1 parent 3d1764a commit 65da9e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/asha/libresample/RecActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void onRecvAudioData(ByteBuffer buffer) {
switch (state) {
case Recording:
if (cached.remaining() >= buffer.remaining()) {
int num = resample.resample(buffer, processed, buffer.remaining());
int num = resample.resampleEx(buffer, processed, buffer.remaining());
Log.w(TAG, String.format("input size:%d output size:%d", buffer.limit(), num));
cached.put(processed.array(), processed.arrayOffset(), num);
setText(String.format("正在录音:%d/%d", cached.position(), cached.limit()));
Expand Down
17 changes: 13 additions & 4 deletions libresample2/src/main/java/com/asha/libresample2/Resample.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public class Resample {

private long ptr;
private long ptr = -1;

private static final String RESAMPLE_LIB = "resample";

Expand All @@ -18,23 +18,32 @@ public class Resample {

public void create(int inputRate, int outputRate, int bufferSize, int channels) {
ptr = init(inputRate, outputRate, bufferSize, channels);
if (ptr == -1) {
throw new IllegalArgumentException("create failed");
}
}

public void destroy() {
close(ptr);
ptr = 0;
ptr = -1;
}

public double getFactor() {
return getFactor(ptr);
}

public int resample(ByteBuffer inputBuffer, ByteBuffer outputBuffer, int numSamples) {
return resample(getFactor(), inputBuffer, outputBuffer, numSamples);
public int resample(ByteBuffer inputBuffer, ByteBuffer outputBuffer, int byteLen) {
return resample(getFactor(), inputBuffer, outputBuffer, byteLen);
}

public int resampleEx(ByteBuffer inputBuffer, ByteBuffer outputBuffer, int byteLen) {
return resampleEx(ptr, inputBuffer, outputBuffer, byteLen);
}

private native int resample(double factor, ByteBuffer inputBuffer, ByteBuffer outputBuffer, int dataLen);

private native int resampleEx(long ptr, ByteBuffer inputBuffer, ByteBuffer outputBuffer, int dataLen);

private native long init(int inputRate, int outputRate, int bufferSize, int channels);

private native double getFactor(long ptr);
Expand Down
25 changes: 24 additions & 1 deletion libresample2/src/main/jni/resample_jni.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <jni.h>
#include <android/log.h>
#include <math.h>

#undef net_sourceforge_resample_Resample_DEFAULT_BUFFER_SIZE
#define net_sourceforge_resample_Resample_DEFAULT_BUFFER_SIZE 4096L
#undef net_sourceforge_resample_Resample_MAX_CHANNELS
#define net_sourceforge_resample_Resample_MAX_CHANNELS 2L
#define net_sourceforge_resample_Resample_MAX_CHANNELS 1L
#undef net_sourceforge_resample_Resample_CHANNEL_MONO
#define net_sourceforge_resample_Resample_CHANNEL_MONO 0L
#undef net_sourceforge_resample_Resample_CHANNEL_LEFT
Expand Down Expand Up @@ -32,6 +34,12 @@ Java_com_asha_libresample2_Resample_init__IIII(JNIEnv *env, jobject instance, ji
resample_data* data = malloc(sizeof(resample_data));
int i;
data->num_channels = channels;
if (data->num_channels > net_sourceforge_resample_Resample_MAX_CHANNELS) {
__android_log_print(ANDROID_LOG_DEBUG, "libresample.so",
"Resample supports stereo, mono only!");
return -1;
}

data->rs = calloc(data->num_channels, sizeof(struct rs_data *));
for (i = 0; i < data->num_channels; i++) {
data->rs[i] =
Expand Down Expand Up @@ -69,4 +77,19 @@ Java_com_asha_libresample2_Resample_resample__DLjava_nio_ByteBuffer_2Ljava_nio_B
int shortLen = byteLen / scale;
int num = resample_simple(factor, (short *) inData, (short *) outData, shortLen);
return num * scale;
}

JNIEXPORT jint JNICALL
Java_com_asha_libresample2_Resample_resampleEx(JNIEnv *env, jobject instance, jlong ptr,
jobject inputBuffer, jobject outputBuffer,
jint byteLen) {
int res;
resample_data* data = (resample_data *) ptr;
const char* inData = (char *) (*env)->GetDirectBufferAddress(env, inputBuffer);
const char* outData = (char *) (*env)->GetDirectBufferAddress(env, outputBuffer);
int scale = sizeof(short) / sizeof(char);
int shortLenIn = byteLen / scale;
int shortLenOut = (int) ceil(byteLen * 1.0f * data->rs[net_sourceforge_resample_Resample_CHANNEL_MONO]->factor / scale);
res = resample(data->rs[net_sourceforge_resample_Resample_CHANNEL_MONO], (short *) inData, shortLenIn, (short *) outData, shortLenOut, 1);
return res > 0 ? res * scale : -1;
}

0 comments on commit 65da9e6

Please sign in to comment.