Skip to content

Commit

Permalink
latency tuner
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Feb 26, 2024
1 parent 51cbffb commit dbaf48f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 3 deletions.
23 changes: 22 additions & 1 deletion app/src/main/cpp/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ bool Engine::setEffectOn(bool isOn) {
}
*/
mFullDuplexPass.start();

fileWriter->setSampleRate (mSampleRate);

// char buffer [80];
Expand Down Expand Up @@ -132,7 +133,7 @@ oboe::Result Engine::openStreams() {

result = inBuilder.openStream(mRecordingStream);

oboe::LatencyTuner *latencyTuner = new oboe::LatencyTuner ( *mRecordingStream, 160);
latencyTuner = new oboe::LatencyTuner ( *mRecordingStream, 160);
latencyTuner->setMinimumBufferSize(160);
mRecordingStream->setBufferSizeInFrames(80);
latencyTuner->tune();
Expand Down Expand Up @@ -501,4 +502,24 @@ bool Engine::setPluginBuffer (float * buffer, int buffer_size, int plugin) {
mFullDuplexPass.bypass = false ;
OUT
return true ;
}

std::string Engine::tuneLatency () {
IN
if (! mIsEffectOn)
return std::string ("Turn on audio engine first") ;

latencyTuner->requestReset();
oboe::Result result = latencyTuner->tune() ;
char tmp [10];
// hello, old friend
sprintf (tmp, "%f", mRecordingStream->getXRunCount());
std::string str = std::string ("Tuner: " +
std::to_string(static_cast<double>(result)) +
", Buffer: " + std::to_string(mRecordingStream->getBufferSizeInFrames())
+ ", Xruns: " + std::string (tmp));

LOGD ("%s",str.c_str());
OUT
return str ;
}
2 changes: 2 additions & 0 deletions app/src/main/cpp/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Engine : public oboe::AudioStreamCallback {
public:
std::string LIBRARY_PATH ;
Engine() ;
std::string tuneLatency();
JavaVM * vm ;
bool mIsEffectOn = false;
bool setPluginBuffer (float * buffer, int buffer_size, int plugin) ;
Expand Down Expand Up @@ -75,6 +76,7 @@ class Engine : public oboe::AudioStreamCallback {
int32_t mRecordingDeviceId = oboe::kUnspecified;
int32_t mPlaybackDeviceId = oboe::kUnspecified;
oboe::AudioApi mAudioApi = oboe::AudioApi::AAudio;
oboe::LatencyTuner *latencyTuner;
oboe::Result openStreams();

void closeStreams();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/FullDuplexStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include <sys/types.h>

#include "oboe/Oboe.h"
#include <oboe/LatencyTuner.h>

class FullDuplexStream : public oboe::AudioStreamCallback {
public:

FullDuplexStream() {}
virtual ~FullDuplexStream() = default;

void setInputStream(std::shared_ptr<oboe::AudioStream> stream) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,10 @@ jint plugin
LOGI ("array of size %d", len);
int retval = engine -> setPluginBuffer (body, len, plugin);
env->ReleaseFloatArrayElements(data, body, 0);
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_shajikhan_ladspa_amprack_AudioEngine_tuneLatency(JNIEnv *env, jclass clazz) {
if (engine == NULL) return NULL ;
return env->NewStringUTF (engine -> tuneLatency ().c_str());
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class AudioEngine {

static native void testLV2 ();
static native void setPluginBuffer (float [] data, int plugin);
static native String tuneLatency ();

static void setDefaultStreamValues(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/com/shajikhan/ladspa/amprack/Rack.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;

import org.checkerframework.checker.units.qual.A;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
Expand Down Expand Up @@ -504,7 +505,16 @@ public boolean onMenuItemClick(MenuItem item) {
}
});

MenuItem exit_item = optionsMenu.getMenu().getItem(10);
MenuItem tuneLatency = optionsMenu.getMenu().getItem(10);
tuneLatency.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
MainActivity.toast(AudioEngine.tuneLatency());
return false;
}
});

MenuItem exit_item = optionsMenu.getMenu().getItem(11);
exit_item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_tune_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M3,17v2h6v-2L3,17zM3,5v2h10L13,5L3,5zM13,21v-2h8v-2h-8v-2h-2v6h2zM7,9v2L3,11v2h4v2h2L9,9L7,9zM21,13v-2L11,11v2h10zM15,9h2L17,7h4L21,5h-4L17,3h-2v6z"/>
</vector>
3 changes: 3 additions & 0 deletions app/src/main/res/menu/options_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<item android:id="@+id/load_collection"
android:title="Load presets from file"/>

<item android:id="@+id/menu_tune"
android:title="Latency Tuner"
android:icon="@drawable/baseline_tune_24"/>
<item android:id="@+id/menu_exit"
android:title="Exit"
android:icon="@drawable/ic_baseline_close_24"/>
Expand Down

0 comments on commit dbaf48f

Please sign in to comment.