Skip to content

Commit

Permalink
add separate lockfree queue managerso that we run only one thread to …
Browse files Browse the repository at this point in the history
…get data out of the realtime thread
  • Loading branch information
Shaji Khan committed Mar 7, 2024
1 parent 671b929 commit 685201d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/src/main/cpp/LockFreeQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
//

#include "LockFreeQueue.h"

LockFreeQueue<AudioBuffer *, LOCK_FREE_SIZE> LockFreeQueueManager::lockFreeQueue;



21 changes: 21 additions & 0 deletions app/src/main/cpp/LockFreeQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <cstdint>
#include <atomic>
#include "Engine.h"

/**
* A lock-free queue for single consumer, single producer. Not thread-safe when using multiple
Expand Down Expand Up @@ -153,4 +154,24 @@ class LockFreeQueue {

};

typedef struct audio_buffer {
int overruns;
int pos;
// float data[];
float *data;
int size ;
} AudioBuffer;

#define LOCK_FREE_SIZE 4096

class LockFreeQueueManager {
static LockFreeQueue<AudioBuffer *, LOCK_FREE_SIZE> lockFreeQueue ;
AudioBuffer ** pAudioBuffer ;
Engine * engine ;
int buffer_size ;

LockFreeQueueManager (Engine * _engine) {
engine = _engine ;
}
};
#endif //AMP_RACK_LOCKFREEQUEUE_H
38 changes: 38 additions & 0 deletions app/src/main/cpp/Meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
vringbuffer_t * Meter::vringbuffer ;
vringbuffer_t * Meter::vringbufferOutput ;
Meter::buffer_t *Meter::current_buffer;
LockFreeQueue<Meter::buffer_t*, LOCK_FREE_SIZE> Meter::lockFreeQueue;
int Meter::bufferUsed = 0;
bool Meter::tunerEnabled = true;
int Meter::bufferUsedOutput = 0;
Expand Down Expand Up @@ -114,6 +115,33 @@ Meter::Meter(JavaVM *pVm) {
vringbuffer_set_receiver_callback(vringbuffer,meter_callback);
vringbuffer_set_receiver_callback(vringbufferOutput,meter_callback_output);

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!");
}

setMixerMeter = env->GetStaticMethodID(mainActivity, "setMixerMeterSwitch",
"(FZ)V");
if (setMixerMeter == nullptr) {
LOGF("cannot find method!");
}
OUT
}

Expand Down Expand Up @@ -249,3 +277,13 @@ void Meter::process (int nframes, const float * data, bool isInput) {
}
}

void Meter::writeLoop () {
buffer_t *buffer ;
while (enabled) {
if (lockFreeQueue.pop(buffer)) {
disk_write(buffer->data, buffer->pos);
}

// std::this_thread::sleep_for(std::chrono::microseconds (10));
}
}
3 changes: 3 additions & 0 deletions app/src/main/cpp/Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ extern "C" {
#include "vringbuffer.h"
}
#endif
#include "LockFreeQueue.h"

//#include "FileWriter.h"
JNIEnv* getEnv() ;
#define LOCK_FREE_SIZE 4096

class Meter {
typedef struct buffer_t{
Expand Down Expand Up @@ -52,6 +54,7 @@ class Meter {
static staticBuffer_t buffers [1024] ;
static int MAX_STATIC_BUFFER ;
static JavaVM *vm ;
static LockFreeQueue<buffer_t *, LOCK_FREE_SIZE> lockFreeQueue ;

public:
Meter(JavaVM *pVm);
Expand Down

0 comments on commit 685201d

Please sign in to comment.