Skip to content

Commit

Permalink
lock free manager compiles,
Browse files Browse the repository at this point in the history
and if it compiles it must run, right?
right???
  • Loading branch information
Shaji Khan committed Mar 7, 2024
1 parent d2525f0 commit 92cbedd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 30 deletions.
8 changes: 5 additions & 3 deletions app/src/main/cpp/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

Engine::Engine () {
assert(mOutputChannelCount == mInputChannelCount);
queueManager = new LockFreeQueueManager (this);
// queueManager = new LockFreeQueueManager ();
fileWriter = new FileWriter ();
queueManager.add_function (fileWriter.disk_write);
queueManager.add_function (
reinterpret_cast<int (*)(float *, unsigned long)>(fileWriter->disk_write));
// discoverPlugins();
// loadPlugins();
}
Expand Down Expand Up @@ -43,7 +44,8 @@ bool Engine::setEffectOn(bool isOn) {
}
*/

(mFullDuplexPass.queue) (float *, int) = queueManager.process ;
// mFullDuplexPass.queue = queueManager.process ;
mFullDuplexPass.lockFreeQueueManager = &queueManager ;
mFullDuplexPass.start();

fileWriter->setSampleRate (mSampleRate);
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/cpp/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#include "Plugin.h"
#include "FileWriter.h"
#include "Meter.h"
#include "LockFreeQueue.h"

class Engine : public oboe::AudioStreamCallback {
public:
static LockFreeQueueManager queueManager ;
LockFreeQueueManager queueManager ;
static oboe::DataCallbackResult outputCapture (
std::shared_ptr<oboe::AudioStream> inputStream,
const void *inputData,
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/cpp/FullDuplexPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "FullDuplexStream.h"
#include "FileWriter.h"
#include "Meter.h"
#include "LockFreeQueue.h"

class FullDuplexPass : public FullDuplexStream {
//| TODO: Limit number of plugins active in free version?
Expand All @@ -33,6 +34,7 @@ class FullDuplexPass : public FullDuplexStream {

// Lock Free Queue Manager
void (queue)(float * data, int frames) ;
LockFreeQueueManager * lockFreeQueueManager;

void (*connect_port [MAX_PLUGINS])(LADSPA_Handle Instance,
unsigned long Port,
Expand Down Expand Up @@ -104,7 +106,7 @@ class FullDuplexPass : public FullDuplexStream {
Meter::process (samplesToProcess, inSamples, false);
}

queue (inSamples, samplesToProcess) ;
lockFreeQueueManager->process(inSamples, samplesToProcess) ;

// for (int32_t i = 0; i < samplesToProcess; i++) {
// *outputFloats++ = *inputFloats++ * outputVolume; // do some arbitrary processing
Expand Down
25 changes: 13 additions & 12 deletions app/src/main/cpp/LockFreeQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@
LockFreeQueue<AudioBuffer *, LOCK_FREE_SIZE> LockFreeQueueManager::lockFreeQueue;
std::thread LockFreeQueueManager::fileWriteThread ;

LockFreeQueueManager::init (int _buffer_size) {
void LockFreeQueueManager::init (int _buffer_size) {
buffer_size = _buffer_size ;
pAudioBuffer = calloc (SPARE_BUFFERS, sizeof (AudioBuffer));
// pAudioBuffer = static_cast<AudioBuffer *>(calloc(SPARE_BUFFERS, sizeof(AudioBuffer)));
for (int i = 0; i < SPARE_BUFFERS; i ++) {
pAudioBuffer [i] -> data = malloc (buffer_size * sizeof (float)) ;
pAudioBuffer [i] -> data = static_cast<float *>(malloc(buffer_size * sizeof(float)));
pAudioBuffer [i] -> pos = 0 ;
}

buffer_counter = 0 ;
functions_counter = 0 ;
functions_count = 0 ;

ready = true ;
fileWriteThread = std::thread (&LockFreeQueueManager::main, this);
}

LockFreeQueueManager::add_function (int (* f) (float *, int)) {
if (functions_counter > MAX_FUNCTIONS) {
void LockFreeQueueManager::add_function (int (* f) (float *, unsigned long)) {
if (functions_count > MAX_FUNCTIONS) {
HERE LOGE ("already have %d functions added to queue, cannot add any more!", MAX_FUNCTIONS);
return ;
}

functions [functions_counter] = f ;
functions_counter ++ ;
functions [functions_count] = reinterpret_cast<void (*)(float *, int)>(f);
functions_count ++ ;
}

LockFreeQueueManager::process (float * data, int samplesToProcess) {
void LockFreeQueueManager::process (float * data, int samplesToProcess) {
if (! ready)
return ;

for (int i = 0 ; i < samplesToProcess ; i ++) {
pAudioBuffer [buffer_counter][i] = data [i] ;
pAudioBuffer [buffer_counter]->data [i] = data [i] ;
}

pAudioBuffer [buffer_counter]->pos = samplesToProcess;
lockFreeQueue.push (pAudioBuffer [buffer_counter]);

buffer_counter ++ ;
Expand All @@ -48,7 +49,7 @@ LockFreeQueueManager::process (float * data, int samplesToProcess) {
}
}

LockFreeQueueManager::main () {
void LockFreeQueueManager::main () {
AudioBuffer * buffer ;
while (lockFreeQueue.pop (buffer)) {
for (int i = 0 ; i < MAX_FUNCTIONS ; i ++) {
Expand All @@ -57,7 +58,7 @@ LockFreeQueueManager::main () {
}
}

LockFreeQueueManager::quit () {
void LockFreeQueueManager::quit () {
fileWriteThread.join();
for (int i = 0; i < SPARE_BUFFERS; i ++) {
free (pAudioBuffer [i] -> data) ;
Expand Down
24 changes: 12 additions & 12 deletions app/src/main/cpp/LockFreeQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
#include <cstdint>
#include <atomic>
#include <thread>
#include "Engine.h"
#include "logging_macros.h"
//#include "Engine.h"

/**
* A lock-free queue for single consumer, single producer. Not thread-safe when using multiple
Expand Down Expand Up @@ -168,26 +169,25 @@ typedef struct audio_buffer {

class LockFreeQueueManager {
static LockFreeQueue<AudioBuffer *, LOCK_FREE_SIZE> lockFreeQueue ;
AudioBuffer ** pAudioBuffer ;
Engine * engine ;
AudioBuffer * pAudioBuffer [SPARE_BUFFERS];
int buffer_size ;
int buffer_counter ;
bool ready = false ;

#define MAX_FUNCTIONS 10
void * functions [MAX_FUNCTIONS] ;
void (* functions [MAX_FUNCTIONS])(float *, int) ;
int functions_count ;

static std::thread fileWriteThread ;

LockFreeQueueManager (Engine * _engine) {
engine = _engine ;
}
public:
void init (int _buffer_size) ;
void add_function(int (*f)(float *, unsigned long));
void process (float * data, int samplesToProcess) ;
void main () ;
void quit () ;

LockFreeQueueManager::init (int _buffer_size) ;
LockFreeQueueManager::add_function (void (* f) (float *, int)) ;
LockFreeQueueManager::process (float * data, int samplesToProcess) ;
LockFreeQueueManager::main () ;
LockFreeQueueManager::quit () ;
LockFreeQueueManager () {
}
};
#endif //AMP_RACK_LOCKFREEQUEUE_H
2 changes: 1 addition & 1 deletion app/src/main/cpp/Meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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;
//LockFreeQueue<Meter::buffer_t*, LOCK_FREE_SIZE> Meter::lockFreeQueue;
int Meter::bufferUsed = 0;
bool Meter::tunerEnabled = true;
int Meter::bufferUsedOutput = 0;
Expand Down

0 comments on commit 92cbedd

Please sign in to comment.