Skip to content

Commit

Permalink
Input RingQueue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Oct 24, 2024
1 parent f10d489 commit 6be01ee
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
62 changes: 45 additions & 17 deletions source/include/signalflow/buffer/ringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*--------------------------------------------------------------------------------*/

#include <math.h>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -20,27 +21,46 @@ template <class T>
class RingBuffer
{
public:
RingBuffer(int size);
RingBuffer(unsigned int capacity);
~RingBuffer();

void append(T value);
void extend(T *ptr, int count);
void extend(T *ptr, unsigned int count);
T get(double index);
T operator[](double index) { return this->get(index); }

private:
protected:
T *data = nullptr;
int size;
int position;
unsigned int capacity;
unsigned int write_position;
signalflow_interpolation_mode_t interpolation_mode;
};

template <class T>
RingBuffer<T>::RingBuffer(int size)
class RingQueue : public RingBuffer<T>
{
this->data = new T[size]();
this->position = 0;
this->size = size;
public:
RingQueue(unsigned int capacity)
: RingBuffer<T>(capacity)
{
read_position = capacity - 256;
}
T pop();

private:
unsigned int read_position;
};

template <class T>
RingBuffer<T>::RingBuffer(unsigned int capacity)
{
if (capacity == 0)
{
throw std::runtime_error("RingBuffer must have a capacity greater than zero");
}
this->data = new T[capacity]();
this->write_position = 0;
this->capacity = capacity;
}

template <class T>
Expand All @@ -52,12 +72,12 @@ RingBuffer<T>::~RingBuffer()
template <class T>
void RingBuffer<T>::append(T value)
{
this->data[this->position] = value;
this->position = (this->position + 1) % this->size;
this->data[this->write_position] = value;
this->write_position = (this->write_position + 1) % this->capacity;
}

template <class T>
void RingBuffer<T>::extend(T *ptr, int count)
void RingBuffer<T>::extend(T *ptr, unsigned int count)
{
for (int i = 0; i < count; i++)
this->append(ptr[i]);
Expand All @@ -66,19 +86,27 @@ void RingBuffer<T>::extend(T *ptr, int count)
template <class T>
T RingBuffer<T>::get(double index)
{
double frame = index + this->position;
double frame = index + this->write_position;
while (frame < 0)
{
frame += this->size;
frame += this->capacity;
}
frame = fmod(frame, this->size);
frame = fmod(frame, this->capacity);

double frame_frac = (frame - (int) frame);
int frame_index = (int) frame;
int next_frame_index = ((int) ceil(frame)) % size;
int next_frame_index = ((int) ceil(frame)) % this->capacity;

T rv = ((1.0 - frame_frac) * this->data[frame_index]) + (frame_frac * this->data[next_frame_index]);

T rv = ((1.0 - frame_frac) * data[frame_index]) + (frame_frac * data[next_frame_index]);
return rv;
}

template <class T>
T RingQueue<T>::pop()
{
T rv = this->data[this->read_position];
this->read_position = (this->read_position + 1) % this->capacity;
return rv;
}

Expand Down
1 change: 1 addition & 0 deletions source/include/signalflow/core/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef float sample;
typedef sample *frame;

typedef RingBuffer<sample> SampleRingBuffer;
typedef RingQueue<sample> SampleRingQueue;

#if defined(__APPLE__) && !defined(FFT_FFTW)
#define FFT_ACCELERATE
Expand Down
15 changes: 14 additions & 1 deletion source/src/node/io/input/miniaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace signalflow
{

AudioIn *shared_in;
std::vector<SampleRingQueue *> input_queue;

void read_callback(ma_device *pDevice,
void *pOutput,
Expand All @@ -33,7 +34,7 @@ void read_callback(ma_device *pDevice,
{
for (int channel = 0; channel < num_channels; channel++)
{
input_node->out[channel][frame] = input_samples[frame * num_channels + channel];
input_queue[channel]->append(input_samples[frame * num_channels + channel]);
}
}

Expand Down Expand Up @@ -124,6 +125,11 @@ void AudioIn::init()
<< "buffer size " << device.capture.internalPeriodSizeInFrames << " samples, " << device.capture.internalChannels << " channel" << s << ")"
<< std::endl;

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

this->start();
}

Expand Down Expand Up @@ -157,6 +163,13 @@ void AudioIn::destroy()

void AudioIn::process(Buffer &out, int num_samples)
{
for (int channel = 0; channel < this->num_output_channels; channel++)
{
for (int frame = 0; frame < num_samples; frame++)
{
out[channel][frame] = input_queue[channel]->pop();
}
}
}

}

0 comments on commit 6be01ee

Please sign in to comment.