Gin
Loading...
Searching...
No Matches
Public Member Functions | List of all members
AudioFifo Class Reference

Lock-free audio FIFO buffer for single producer/consumer scenarios. More...

#include <gin_audiofifo.h>

Public Member Functions

 AudioFifo (int channels=2, int numSamples=128)
 Creates an AudioFifo with the specified number of channels and sample capacity.
 
void setSize (int numChannels, int numSamples)
 Resizes the FIFO to a new capacity.
 
int getNumChannels () const noexcept
 Returns the number of audio channels.
 
int getFreeSpace () const noexcept
 Returns the number of samples that can be written before the FIFO is full.
 
int getNumReady () const noexcept
 Returns the number of samples available to read.
 
void reset () noexcept
 Resets the FIFO to an empty state.
 
void ensureFreeSpace (int numSamples)
 Ensures at least the specified amount of free space by discarding oldest data if needed.
 
bool write (const juce::AudioSampleBuffer &src, int numSamples=-1)
 Writes audio data from an AudioSampleBuffer to the FIFO.
 
bool write (const float *const *data, int numSamples)
 Writes audio data from a multi-channel array to the FIFO.
 
bool writeSilence (int numSamples)
 Writes silence (zeros) to the FIFO.
 
bool writeMono (const float *data, int numSamples)
 Writes mono audio data to a single-channel FIFO.
 
bool peek (juce::AudioSampleBuffer &dest)
 Peeks at (reads without consuming) audio data from the FIFO.
 
bool peek (juce::AudioSampleBuffer &dest, int startSampleInDestBuffer, int numSamples)
 Peeks at (reads without consuming) audio data from the FIFO.
 
float peekSample (int channel, int sample)
 Peeks at a single sample without consuming it.
 
bool read (juce::AudioSampleBuffer &dest)
 Reads and removes audio data from the FIFO.
 
bool read (juce::AudioSampleBuffer &dest, int startSampleInDestBuffer, int numSamples)
 Reads and removes audio data from the FIFO.
 
bool readMono (float *data, int numSamples)
 Reads and removes mono audio data from a single-channel FIFO.
 
bool readAdding (juce::AudioSampleBuffer &dest)
 Reads and removes audio data from the FIFO, adding it to the destination buffer.
 
bool readAdding (juce::AudioSampleBuffer &dest, int startSampleInDestBuffer, int numSamples)
 Reads and removes audio data from the FIFO, adding it to the destination buffer.
 
bool pop (int numSamples)
 Removes (discards) samples from the FIFO without reading them.
 

Detailed Description

Lock-free audio FIFO buffer for single producer/consumer scenarios.

AudioFifo provides a thread-safe circular buffer for passing audio between threads, typically from an audio callback to a background processing thread or vice versa. It uses JUCE's AbstractFifo for lock-free operation with a single producer and single consumer thread.

Key Features:

Common Use Cases:

Thread Safety:

Usage:

AudioFifo fifo(2, 4096); // 2 channels, 4096 samples
// In audio thread (producer)
if (fifo.getFreeSpace() >= buffer.getNumSamples())
fifo.write(buffer);
// In background thread (consumer)
if (fifo.getNumReady() >= desiredSamples)
{
fifo.read(output);
// ... process output
}
Lock-free audio FIFO buffer for single producer/consumer scenarios.
Definition gin_audiofifo.h:66
A lightweight 2D point class for projects that don't use juce_graphics.
Definition gin_point.h:25
See also
MidiFifo, AudioMidiFifo, ResamplingFifo

Constructor & Destructor Documentation

◆ AudioFifo()

AudioFifo::AudioFifo ( int  channels = 2,
int  numSamples = 128 
)

Creates an AudioFifo with the specified number of channels and sample capacity.

Parameters
channelsNumber of audio channels (default: 2)
numSamplesMaximum number of samples the FIFO can hold (default: 128)

Member Function Documentation

◆ setSize()

void AudioFifo::setSize ( int  numChannels,
int  numSamples 
)

Resizes the FIFO to a new capacity.

Note: This is not thread-safe and should only be called when no other threads are accessing the FIFO.

Parameters
numChannelsNew number of audio channels
numSamplesNew maximum number of samples

Referenced by PluginWrapper::PluginWrapper(), and PluginWrapper::processBlock().

◆ getNumChannels()

int AudioFifo::getNumChannels ( ) const
noexcept

Returns the number of audio channels.

Referenced by PluginWrapper::processBlock().

◆ getFreeSpace()

int AudioFifo::getFreeSpace ( ) const
noexcept

Returns the number of samples that can be written before the FIFO is full.

Referenced by ensureFreeSpace(), PluginWrapper::processBlock(), write(), writeMono(), and writeSilence().

◆ getNumReady()

int AudioFifo::getNumReady ( ) const
noexcept

Returns the number of samples available to read.

Referenced by ensureFreeSpace(), peek(), peekSample(), pop(), read(), readAdding(), readMono(), and ResamplingFifo::samplesReady().

◆ reset()

void AudioFifo::reset ( )
noexcept

Resets the FIFO to an empty state.

Note: This is not thread-safe and should only be called when no other threads are accessing the FIFO.

◆ ensureFreeSpace()

void AudioFifo::ensureFreeSpace ( int  numSamples)

Ensures at least the specified amount of free space by discarding oldest data if needed.

If the requested free space is greater than currently available, this will discard the oldest samples to make room.

Parameters
numSamplesThe minimum number of free samples required

References getFreeSpace(), and getNumReady().

◆ write() [1/2]

bool AudioFifo::write ( const juce::AudioSampleBuffer &  src,
int  numSamples = -1 
)

Writes audio data from an AudioSampleBuffer to the FIFO.

Should only be called from the producer thread.

Parameters
srcThe source buffer to write from
numSamplesNumber of samples to write (default: -1 writes all samples from src)
Returns
true if successful, false if there wasn't enough space

References write().

Referenced by PluginWrapper::processBlock(), and write().

◆ write() [2/2]

bool AudioFifo::write ( const float *const data,
int  numSamples 
)

Writes audio data from a multi-channel array to the FIFO.

Should only be called from the producer thread.

Parameters
dataArray of channel pointers containing the audio data
numSamplesNumber of samples to write
Returns
true if successful, false if there wasn't enough space

References getFreeSpace().

◆ writeSilence()

bool AudioFifo::writeSilence ( int  numSamples)

Writes silence (zeros) to the FIFO.

Should only be called from the producer thread.

Parameters
numSamplesNumber of silent samples to write
Returns
true if successful, false if there wasn't enough space

References getFreeSpace().

◆ writeMono()

bool AudioFifo::writeMono ( const float data,
int  numSamples 
)

Writes mono audio data to a single-channel FIFO.

Should only be called from the producer thread. The FIFO must be configured for single-channel (mono) operation.

Parameters
dataPointer to the mono audio data
numSamplesNumber of samples to write
Returns
true if successful, false if there wasn't enough space

References getFreeSpace().

◆ peek() [1/2]

bool AudioFifo::peek ( juce::AudioSampleBuffer &  dest)

Peeks at (reads without consuming) audio data from the FIFO.

The data remains in the FIFO and can be read again. Should only be called from the consumer thread.

Parameters
destDestination buffer to copy the audio data into
Returns
true if successful, false if there weren't enough samples available

References peek().

Referenced by peek().

◆ peek() [2/2]

bool AudioFifo::peek ( juce::AudioSampleBuffer &  dest,
int  startSampleInDestBuffer,
int  numSamples 
)

Peeks at (reads without consuming) audio data from the FIFO.

The data remains in the FIFO and can be read again. Should only be called from the consumer thread.

Parameters
destDestination buffer to copy the audio data into
startSampleInDestBufferStarting position in the destination buffer
numSamplesNumber of samples to peek
Returns
true if successful, false if there weren't enough samples available

References getNumReady().

◆ peekSample()

float AudioFifo::peekSample ( int  channel,
int  sample 
)

Peeks at a single sample without consuming it.

Should only be called from the consumer thread.

Parameters
channelThe channel to peek from
sampleThe sample index to peek (0 = first available sample)
Returns
The sample value at the specified position

References getNumReady().

◆ read() [1/2]

bool AudioFifo::read ( juce::AudioSampleBuffer &  dest)

Reads and removes audio data from the FIFO.

Should only be called from the consumer thread.

Parameters
destDestination buffer to copy the audio data into
Returns
true if successful, false if there weren't enough samples available

References read().

Referenced by read().

◆ read() [2/2]

bool AudioFifo::read ( juce::AudioSampleBuffer &  dest,
int  startSampleInDestBuffer,
int  numSamples 
)

Reads and removes audio data from the FIFO.

Should only be called from the consumer thread.

Parameters
destDestination buffer to copy the audio data into
startSampleInDestBufferStarting position in the destination buffer
numSamplesNumber of samples to read
Returns
true if successful, false if there weren't enough samples available

References getNumReady().

◆ readMono()

bool AudioFifo::readMono ( float data,
int  numSamples 
)

Reads and removes mono audio data from a single-channel FIFO.

Should only be called from the consumer thread. The FIFO must be configured for single-channel (mono) operation.

Parameters
dataPointer to the destination array for the audio data
numSamplesNumber of samples to read
Returns
true if successful, false if there weren't enough samples available

References getNumReady().

◆ readAdding() [1/2]

bool AudioFifo::readAdding ( juce::AudioSampleBuffer &  dest)

Reads and removes audio data from the FIFO, adding it to the destination buffer.

Unlike read(), this adds the FIFO data to the destination rather than replacing it. Should only be called from the consumer thread.

Parameters
destDestination buffer to add the audio data to
Returns
true if successful, false if there weren't enough samples available

References readAdding().

Referenced by readAdding().

◆ readAdding() [2/2]

bool AudioFifo::readAdding ( juce::AudioSampleBuffer &  dest,
int  startSampleInDestBuffer,
int  numSamples 
)

Reads and removes audio data from the FIFO, adding it to the destination buffer.

Unlike read(), this adds the FIFO data to the destination rather than replacing it. Should only be called from the consumer thread.

Parameters
destDestination buffer to add the audio data to
startSampleInDestBufferStarting position in the destination buffer
numSamplesNumber of samples to read
Returns
true if successful, false if there weren't enough samples available

References getNumReady().

◆ pop()

bool AudioFifo::pop ( int  numSamples)

Removes (discards) samples from the FIFO without reading them.

This is useful after peek() operations when you want to consume the peeked data. Should only be called from the consumer thread.

Parameters
numSamplesNumber of samples to discard
Returns
true if successful, false if there weren't enough samples available

References getNumReady().


The documentation for this class was generated from the following file: