Skip to content

Commit

Permalink
Implement & exercise getKeyFrameMap in JNI
Browse files Browse the repository at this point in the history
  • Loading branch information
cannam committed Dec 14, 2023
1 parent e54fd1f commit dc745e3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
16 changes: 16 additions & 0 deletions com/breakfastquay/rubberband/RubberBandStretcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ License, or (at your option) any later version. See the file

package com.breakfastquay.rubberband;

import java.util.Map;
import java.util.Set;

public class RubberBandStretcher
{
public RubberBandStretcher(int sampleRate, int channels,
Expand Down Expand Up @@ -62,6 +65,19 @@ public RubberBandStretcher(int sampleRate, int channels,
public native int getSamplesRequired();

public native void setKeyFrameMap(long[] from, long[] to);
public void setKeyFrameMap(Map<Long, Long> m) {
Set<Long> keys = m.keySet();
int n = keys.size();
long[] from = new long[n];
long[] to = new long[n];
int i = 0;
for (Long k : keys) {
from[i] = k.longValue();
to[i] = m.get(k).longValue();
++i;
}
setKeyFrameMap(from, to);
}

public native void study(float[][] input, int offset, int n, boolean finalBlock);
public void study(float[][] input, boolean finalBlock) {
Expand Down
32 changes: 30 additions & 2 deletions com/breakfastquay/rubberband/test/RubberBandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import com.breakfastquay.rubberband.RubberBandStretcher;

import java.util.TreeMap;

public class RubberBandTest
{

Expand All @@ -15,7 +17,7 @@ public static void main(String[] args) {
(rate,
channels,
RubberBandStretcher.OptionEngineFiner +
RubberBandStretcher.OptionProcessRealTime,
RubberBandStretcher.OptionProcessOffline,
1.0,
1.0);

Expand All @@ -38,11 +40,16 @@ public static void main(String[] args) {
));

int blocksize = 1024;
int blocks = 200;
int blocks = 400;
double freq = 440.0;

stretcher.setMaxProcessSize(blocksize);

TreeMap<Long, Long> keyFrameMap = new TreeMap<Long, Long>();
keyFrameMap.put((long)(3 * rate), (long)(4 * rate));
keyFrameMap.put((long)(5 * rate), (long)(5 * rate));
stretcher.setKeyFrameMap(keyFrameMap);

float[][] buffer = new float[channels][blocksize];

int i0 = 0;
Expand All @@ -53,6 +60,27 @@ public static void main(String[] args) {
for (int i = 0; i < blocksize; ++i) {
buffer[c][i] = (float)Math.sin
((double)i0 * freq * Math.PI * 2.0 / (double)rate);
if (i0 % rate == 0) {
buffer[c][i] = 1.f;
}
++i0;
}
}

stretcher.study(buffer, block + 1 == blocks);
}

i0 = 0;

for (int block = 0; block < blocks; ++block) {

for (int c = 0; c < channels; ++c) {
for (int i = 0; i < blocksize; ++i) {
buffer[c][i] = (float)Math.sin
((double)i0 * freq * Math.PI * 2.0 / (double)rate);
if (i0 % rate == 0) {
buffer[c][i] = 1.f;
}
++i0;
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/jni/RubberBandStretcherJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ JNIEXPORT jint JNICALL Java_com_breakfastquay_rubberband_RubberBandStretcher_get
JNIEXPORT jint JNICALL Java_com_breakfastquay_rubberband_RubberBandStretcher_getSamplesRequired
(JNIEnv *, jobject);

/*
* Class: com_breakfastquay_rubberband_RubberBandStretcher
* Method: setKeyFrameMap
* Signature: ([J[J)V
*/
JNIEXPORT void JNICALL Java_com_breakfastquay_rubberband_RubberBandStretcher_setKeyFrameMap
(JNIEnv *, jobject, jlongArray, jlongArray);

/*
* Class: com_breakfastquay_rubberband_RubberBandStretcher
* Method: study
Expand Down Expand Up @@ -365,6 +373,22 @@ Java_com_breakfastquay_rubberband_RubberBandStretcher_getSamplesRequired(JNIEnv
return getStretcher(env, obj)->getSamplesRequired();
}

JNIEXPORT void JNICALL
Java_com_breakfastquay_rubberband_RubberBandStretcher_setKeyFrameMap(JNIEnv *env, jobject obj, jlongArray from, jlongArray to)
{
std::map<size_t, size_t> m;
int flen = env->GetArrayLength(from);
int tlen = env->GetArrayLength(to);
jlong *farr = env->GetLongArrayElements(from, 0);
jlong *tarr = env->GetLongArrayElements(to, 0);
for (int i = 0; i < flen && i < tlen; ++i) {
m[farr[i]] = tarr[i];
}
env->ReleaseLongArrayElements(from, farr, 0);
env->ReleaseLongArrayElements(to, tarr, 0);
getStretcher(env, obj)->setKeyFrameMap(m);
}

JNIEXPORT void JNICALL
Java_com_breakfastquay_rubberband_RubberBandStretcher_study(JNIEnv *env, jobject obj, jobjectArray data, jint offset, jint n, jboolean final)
{
Expand Down

0 comments on commit dc745e3

Please sign in to comment.