Skip to content

Commit

Permalink
output meter linked to LockFreeQueue.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Mar 8, 2024
1 parent e3af1ef commit b434ab6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 13 deletions.
3 changes: 3 additions & 0 deletions app/src/main/cpp/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Engine::Engine () {
// queueManager = new LockFreeQueueManager ();
fileWriter = new FileWriter ();
queueManager.add_function (fileWriter->disk_write);
queueManager.add_function (meter->updateMeterOutput);
// discoverPlugins();
// loadPlugins();
}
Expand Down Expand Up @@ -85,6 +86,8 @@ bool Engine::setEffectOn(bool isOn) {
fileWriter->stopRecording() ;
}

meter->stop();

mFullDuplexPass.stop();
closeStreams();
mIsEffectOn = isOn;
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/cpp/FullDuplexPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ class FullDuplexPass : public FullDuplexStream {
*outputFloats++ = inSamples [i];
}

if (meterEnabled) {
Meter::process (samplesToProcess, inSamples, false);
}
// if (meterEnabled) {
// Meter::process (samplesToProcess, inSamples, false);
// }

lockFreeQueueManager->process(inSamples, samplesToProcess) ;

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/cpp/LockFreeQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void LockFreeQueueManager::init (int _buffer_size) {

ready = true ;
fileWriteThread = std::thread (&LockFreeQueueManager::main, this);
LOGD("[LockFreeQueue thread id] %d", gettid ());

OUT
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/LockFreeQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <atomic>
#include <thread>
#include "logging_macros.h"
#include <unistd.h>
//#include "Engine.h"

/**
Expand Down
71 changes: 61 additions & 10 deletions app/src/main/cpp/Meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,53 @@ int Meter::autoincrease_callback(vringbuffer_t *vrb, bool first_call, int readin
return 0 ;
}

int Meter::updateMeterOutput (float * data, int samples) {
if (! enabled)
return 0;

if (envOutput == nullptr) {
LOGD("MeterOutput thread id: %d", gettid ());
envOutput = getEnv();
if (envOutput == nullptr)
LOGF("envOutput is null");
mainActivityOutput = findClassWithEnv(envOutput, "com/shajikhan/ladspa/amprack/MainActivity");
if (mainActivityOutput == nullptr) {
HERE
LOGF("cannot find class mainactivityOutput!");
}

setMixerMeterOutput = envOutput->GetStaticMethodID(mainActivityOutput, "setMixerMeterSwitch",
"(FZ)V");

setTuner = envOutput->GetStaticMethodID(mainActivityOutput, "setTuner",
"([F)V");
if (setMixerMeterOutput == nullptr) {
LOGF("cannot find method!");
}

if (setTuner == nullptr) {
LOGF("cannot find setTuner method!");
}
}

float max = 0 ;
for (int i = 0 ; i < samples; i ++) {
if (data [i] > max)
max = data [i] ;
}

envOutput->CallStaticVoidMethod(mainActivityOutput, setMixerMeterOutput, (jfloat) max, false);
return 0;
}

void Meter::stop () {
envOutput = nullptr ;
}

Meter::Meter(JavaVM *pVm) {
IN
vm = pVm;
envOutput = nullptr ;
// sane defaults
jack_samplerate = 48000 ;
block_size = 384 ;
Expand All @@ -97,23 +140,23 @@ Meter::Meter(JavaVM *pVm) {
return ;
}

vringbufferOutput = vringbuffer_create(JC_MAX(4,seconds_to_buffers(1)),
JC_MAX(4,seconds_to_buffers(40)),
(size_t) buffer_size_in_bytes);
// vringbufferOutput = vringbuffer_create(JC_MAX(4,seconds_to_buffers(1)),
// JC_MAX(4,seconds_to_buffers(40)),
// (size_t) buffer_size_in_bytes);

if(vringbufferOutput == NULL){
HERE LOGF ("Unable to create ringbuffer output!") ;
OUT
return ;
}
// if(vringbufferOutput == NULL){
// HERE LOGF ("Unable to create ringbuffer output!") ;
// OUT
// return ;
// }

/// TODO: Free this memory!
vringbuffer_set_autoincrease_callback(vringbuffer,autoincrease_callback,0);
vringbuffer_set_autoincrease_callback(vringbufferOutput,autoincrease_callback,0);
// vringbuffer_set_autoincrease_callback(vringbufferOutput,autoincrease_callback,0);
current_buffer = static_cast<buffer_t *>(vringbuffer_get_writing(vringbuffer));
empty_buffer = static_cast<float *>(my_calloc(sizeof(float), block_size * 1));
vringbuffer_set_receiver_callback(vringbuffer,meter_callback);
vringbuffer_set_receiver_callback(vringbufferOutput,meter_callback_output);
// vringbuffer_set_receiver_callback(vringbufferOutput,meter_callback_output);

/*
envOutput = getEnv();
Expand Down Expand Up @@ -147,6 +190,10 @@ Meter::Meter(JavaVM *pVm) {
OUT
}

void Meter::disable () {
enabled = false ;
}

void Meter::enable () {
IN
if (enabled) {
Expand Down Expand Up @@ -260,6 +307,9 @@ void Meter::process (int nframes, const float * data, bool isInput) {

vringbuffer_trigger_autoincrease_callback(vringbuffer);
}

return;
/*
else {
if (bufferUsedOutput < MAX_STATIC_BUFFER) {
for (int i = 0; i < nframes; i++) {
Expand All @@ -277,4 +327,5 @@ void Meter::process (int nframes, const float * data, bool isInput) {
vringbuffer_trigger_autoincrease_callback(vringbufferOutput);
}
*/
}
6 changes: 6 additions & 0 deletions app/src/main/cpp/Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ class Meter {

static float tunerBuffer [1024*4];
static int tunerIndex;

static int updateMeterOutput(float *data, int samples);

void stop();

void disable();
};

#endif //AMP_RACK_METER_H
4 changes: 4 additions & 0 deletions app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ Java_com_shajikhan_ladspa_amprack_AudioEngine_toggleMixer(JNIEnv *env, jclass cl
// TODO: implement toggleMixer()
if (engine == NULL) return ;
engine->mFullDuplexPass.meterEnabled = toggle ;
if (toggle)
engine->meter->enable();
else
engine->meter->disable();
LOGD("setting mixer to %d", toggle);
}
extern "C"
Expand Down

0 comments on commit b434ab6

Please sign in to comment.