Skip to content

Commit

Permalink
miniaudio: Fix RingBuffer glitching on Linux with mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Oct 24, 2024
1 parent 6be01ee commit 3f290ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/audio-through-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def main():
#--------------------------------------------------------------------------------
# Add some delay, and play
#--------------------------------------------------------------------------------
output = audio_in + CombDelay(audio_in, 0.2, 0.8) * 0.3
output = audio_in
stereo = StereoPanner(output)

graph.play(stereo)
Expand Down
21 changes: 20 additions & 1 deletion source/include/signalflow/buffer/ringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mutex>

enum signalflow_interpolation_mode_t : unsigned int;

Expand Down Expand Up @@ -43,12 +44,18 @@ class RingQueue : public RingBuffer<T>
RingQueue(unsigned int capacity)
: RingBuffer<T>(capacity)
{
read_position = capacity - 256;
this->read_position = this->capacity - 826;
this->filled_count = 0;
}
T pop();

int get_filled_count() { return this->filled_count; }
void append(T value);

private:
unsigned int read_position;
int filled_count;
std::mutex mutex;
};

template <class T>
Expand Down Expand Up @@ -105,9 +112,21 @@ T RingBuffer<T>::get(double index)
template <class T>
T RingQueue<T>::pop()
{
mutex.lock();
T rv = this->data[this->read_position];
this->read_position = (this->read_position + 1) % this->capacity;
this->filled_count--;
mutex.unlock();
return rv;
}

template <class T>
void RingQueue<T>::append(T value)
{
mutex.lock();
this->RingBuffer<T>::append(value);
this->filled_count++;
mutex.unlock();
}

}
2 changes: 1 addition & 1 deletion source/src/node/io/input/miniaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void AudioIn::init()

for (int channel = 0; channel < device.capture.internalChannels; channel++)
{
input_queue.push_back(new SampleRingQueue(device.capture.internalPeriodSizeInFrames * 4));
input_queue.push_back(new SampleRingQueue(device.capture.internalPeriodSizeInFrames * 8));
}

this->start();
Expand Down

0 comments on commit 3f290ad

Please sign in to comment.