diff --git a/android/jni/Android.mk b/android/jni/Android.mk index c113c85..676b606 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -6,6 +6,7 @@ LOCAL_CPP_EXTENSION := .cc LOCAL_SRC_FILES := android_glue.cc \ dx7note.cc \ env.cc \ + exp2.cc \ fm_core.cc \ fm_op_kernel.cc \ freqlut.cc \ diff --git a/android/src/com/google/synthesizer/android/stats/JitterStats.java b/android/src/com/google/synthesizer/android/stats/JitterStats.java new file mode 100644 index 0000000..58e395d --- /dev/null +++ b/android/src/com/google/synthesizer/android/stats/JitterStats.java @@ -0,0 +1,44 @@ +package com.google.synthesizer.android.stats; + +import android.util.Log; + +public class JitterStats { + public JitterStats() { + startTime_ = new double[N_STATS]; + endTime_ = new double[N_STATS]; + + } + + // Takes stats buf in format from android_glue + public void aggregate(byte[] statsBuf) { + String[] lines = new String(statsBuf).split("\n"); + for (int i = 0; i < lines.length; i++) { + String[] toks = lines[i].split(" "); + if (toks.length == 3 && "ts".equals(toks[0])) { + double startTime = Double.parseDouble(toks[1]); + double endTime = Double.parseDouble(toks[2]); + startTime_[bufIx_] = startTime; + endTime_[bufIx_] = endTime; + bufIx_ = (bufIx_ + 1) % N_STATS; + double cbTime = endTime - startTime; + meanCbTime_ += (cbTime - meanCbTime_) * .01; + } + } + } + + public String report() { + double maxCbTime = 0.0; + for (int i = 0; i < N_STATS; i++) { + double cbTime = endTime_[i] - startTime_[i]; + maxCbTime = Math.max(maxCbTime, cbTime); + } + return "max cb = " + Double.toString(maxCbTime * 1000) + "ms"; + } + + static final int N_STATS = 2000; + double meanCbTime_; + double startTime_[]; + double endTime_[]; + int bufIx_ = 0; + +} diff --git a/android/src/com/google/synthesizer/android/ui/PianoActivity2.java b/android/src/com/google/synthesizer/android/ui/PianoActivity2.java index ab17491..69eaaa2 100644 --- a/android/src/com/google/synthesizer/android/ui/PianoActivity2.java +++ b/android/src/com/google/synthesizer/android/ui/PianoActivity2.java @@ -18,7 +18,6 @@ package com.google.synthesizer.android.ui; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; @@ -46,6 +45,7 @@ import android.widget.TextView; import com.google.synthesizer.R; import com.google.synthesizer.android.AndroidGlue; +import com.google.synthesizer.android.stats.JitterStats; import com.google.synthesizer.android.widgets.knob.KnobListener; import com.google.synthesizer.android.widgets.knob.KnobView; import com.google.synthesizer.android.widgets.piano.PianoView; @@ -120,6 +120,7 @@ public class PianoActivity2 extends Activity { tryConnectUsb(); } + jitterStats_ = new JitterStats(); statusHandler_ = new Handler(); statusRunnable_ = new Runnable() { public void run() { @@ -127,15 +128,11 @@ public class PianoActivity2 extends Activity { if (n > 0) { byte[] buf = new byte[n]; androidGlue_.readStatsBytes(buf, 0, n); + jitterStats_.aggregate(buf); TextView statusTextView = (TextView)findViewById(R.id.status); - String statusString = new String(buf); - int nlIndex = statusString.indexOf('\n'); - if (nlIndex >= 0) { - statusString = statusString.substring(0, nlIndex); - } - statusTextView.setText(statusString); + statusTextView.setText(jitterStats_.report()); } - statusHandler_.postDelayed(statusRunnable_, 10); + statusHandler_.postDelayed(statusRunnable_, 100); } }; statusRunnable_.run(); @@ -252,4 +249,5 @@ public class PianoActivity2 extends Activity { private Spinner presetSpinner_; private Handler statusHandler_; private Runnable statusRunnable_; + private JitterStats jitterStats_; } diff --git a/cpp/src/android_glue.cc b/cpp/src/android_glue.cc index 0a3781f..c43ce70 100644 --- a/cpp/src/android_glue.cc +++ b/cpp/src/android_glue.cc @@ -27,6 +27,7 @@ #include "synth.h" #include "freqlut.h" +#include "exp2.h" #include "sin.h" #include "synth_unit.h" @@ -137,6 +138,7 @@ Java_com_google_synthesizer_android_AndroidGlue_start(JNIEnv *env, buffer_size = buf_size; Freqlut::init(sample_rate); + Exp2::init(); Sin::init(); ring_buffer = new RingBuffer(); stats_ring_buffer = new RingBuffer(); diff --git a/cpp/src/dx7note.cc b/cpp/src/dx7note.cc index 2cb4a0d..9e775a7 100644 --- a/cpp/src/dx7note.cc +++ b/cpp/src/dx7note.cc @@ -21,6 +21,7 @@ #include "synth.h" #include "freqlut.h" #include "patch.h" +#include "exp2.h" #include "dx7note.h" using namespace std; @@ -171,8 +172,8 @@ void Dx7Note::compute(int32_t *buf) { for (int op = 0; op < 6; op++) { params_[op].gain[0] = params_[op].gain[1]; int32_t level = env_[op].getsample(); - // TODO: replace pow with faster calculation - int32_t gain = pow(2, 10 + level * (1.0 / (1 << 24))); + int32_t gain = Exp2::lookup(level - (14 * (1 << 24))); + //int32_t gain = pow(2, 10 + level * (1.0 / (1 << 24))); params_[op].gain[1] = gain; } core_.compute(buf, params_, algorithm_, fb_buf_, fb_shift_);