arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
AdaptiveResamplingStream Class Reference

An Audio Stream backed by a buffer (queue) which tries to correct jitter and automatically adjusts for the slightly different clock rates between an audio source and audio target. Use separate tasks to write and read the data. Also make sure that you protect the access with a mutex or provide a thread-safe buffer! More...

#include <AdaptiveResamplingStream.h>

Inheritance diagram for AdaptiveResamplingStream:
AudioStream BaseStream AudioInfoSupport AudioInfoSource Stream Print

Public Member Functions

 AdaptiveResamplingStream ()
 Construct a new AdaptiveResamplingStream object You need to call setBuffer() and setStepRangePercent() before begin()
 
 AdaptiveResamplingStream (BaseBuffer< uint8_t > &buffer, float stepRangePercent=5.0f)
 Construct a new AdaptiveResamplingStream object.
 
virtual void addNotifyAudioChange (AudioInfoSupport &bi)
 Adds target to be notified about audio changes.
 
virtual AudioInfo audioInfo () override
 provides the actual input AudioInfo
 
virtual AudioInfo audioInfoOut ()
 
virtual int available () override
 
virtual int availableForWrite () override
 
bool begin ()
 Initialize the stream and internal components.
 
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
 
void end ()
 End the stream and release resources.
 
virtual void flush () override
 
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
 
bool isPrimed ()
 
float levelPercent ()
 Get the fill level at the last calculation in percent.
 
float levelPercentActual ()
 Get the current actual buffer fill level in percent.
 
virtual operator bool ()
 
size_t readBytes (uint8_t *data, size_t len) override
 Read resampled audio data from the buffer.
 
virtual size_t readSilence (uint8_t *buffer, size_t length)
 Source to generate silence: just sets the buffer to 0.
 
float recalculate ()
 Recalculate the resampling step size based on buffer fill level.
 
virtual bool removeNotifyAudioChange (AudioInfoSupport &bi)
 Removes a target in order not to be notified about audio changes.
 
virtual void setAudioInfo (AudioInfo newInfo) override
 Defines the input AudioInfo.
 
void setBuffer (BaseBuffer< uint8_t > &buffer)
 Set the buffer used for audio data.
 
void setKalmanParameters (float process_noise, float measurement_noise)
 Set the Kalman filter parameters.
 
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active)
 
void setPIDParameters (float p_value, float i_value, float d_value)
 Set the PID controller parameters.
 
void setStartFillPercent (float percent)
 Defines the fill level (percent) that must be reached once, right after begin(), before any data is returned by readBytes(). Defaults to 50%; 0 disables priming. See AdaptiveResamplingBuffer::setStartFillPercent().
 
void setStepRangePercent (float rangePercent)
 Set the allowed resampling range as a percent.
 
void setWriteBufferSize (int size)
 
size_t write (const uint8_t *data, size_t len) override
 Write audio data to the buffer.
 
virtual size_t write (uint8_t ch) override
 
virtual void writeSilence (size_t len)
 Writes len bytes of silence (=0).
 

Protected Member Functions

virtual int not_supported (int out, const char *msg="")
 
void notifyAudioChange (AudioInfo info)
 
void refillReadBuffer ()
 Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read()
 

Protected Attributes

int _timeout = 10
 
AudioInfo info
 
bool is_notify_active = true
 
Vector< AudioInfoSupport * > notify_vector
 
AdaptiveResamplingBuffer resampling_buffer
 
RingBuffer< uint8_t > tmp_in {0}
 
RingBuffer< uint8_t > tmp_out {0}
 
int write_buffer_size = MAX_SINGLE_CHARS
 

Detailed Description

An Audio Stream backed by a buffer (queue) which tries to correct jitter and automatically adjusts for the slightly different clock rates between an audio source and audio target. Use separate tasks to write and read the data. Also make sure that you protect the access with a mutex or provide a thread-safe buffer!

This is a thin Stream adapter around AdaptiveResamplingBuffer, which implements the actual jitter-buffering/resampling logic behind the BaseBuffer<uint8_t> API. If you need a BaseBuffer (e.g. to plug into an API that expects one) rather than a Stream, use AdaptiveResamplingBuffer directly.

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ AdaptiveResamplingStream() [1/2]

Construct a new AdaptiveResamplingStream object You need to call setBuffer() and setStepRangePercent() before begin()

◆ AdaptiveResamplingStream() [2/2]

AdaptiveResamplingStream ( BaseBuffer< uint8_t > &  buffer,
float  stepRangePercent = 5.0f 
)
inline

Construct a new AdaptiveResamplingStream object.

Parameters
bufferReference to the buffer used for audio data
stepRangePercentAllowed resampling range in percent (default: 0.05)

Member Function Documentation

◆ addNotifyAudioChange()

virtual void addNotifyAudioChange ( AudioInfoSupport bi)
inlinevirtualinherited

◆ audioInfo()

virtual AudioInfo audioInfo ( )
inlineoverridevirtualinherited

◆ audioInfoOut()

virtual AudioInfo audioInfoOut ( )
inlinevirtualinherited

◆ available()

virtual int available ( )
inlineoverridevirtualinherited

◆ availableForWrite()

virtual int availableForWrite ( )
inlineoverridevirtualinherited

◆ begin()

bool begin ( )
inlinevirtual

Initialize the stream and internal components.

Returns
true if initialization was successful, false otherwise

Reimplemented from BaseStream.

◆ clearNotifyAudioChange()

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

Deletes all change notify subscriptions.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ end()

void end ( )
inlinevirtual

End the stream and release resources.

Reimplemented from BaseStream.

◆ flush()

virtual void flush ( )
inlineoverridevirtualinherited

◆ isNotifyActive()

bool isNotifyActive ( )
inlineinherited

Checks if the automatic AudioInfo update is active.

◆ isPrimed()

bool isPrimed ( )
inline

True once the buffer has reached setStartFillPercent() (or always true if priming is disabled).

◆ levelPercent()

float levelPercent ( )
inline

Get the fill level at the last calculation in percent.

Returns
float Last calculated fill level (0-100)

◆ levelPercentActual()

float levelPercentActual ( )
inline

Get the current actual buffer fill level in percent.

Returns
float Current fill level (0-100)

◆ not_supported()

virtual int not_supported ( int  out,
const char *  msg = "" 
)
inlineprotectedvirtualinherited

◆ notifyAudioChange()

void notifyAudioChange ( AudioInfo  info)
inlineprotectedinherited

◆ operator bool()

virtual operator bool ( )
inlinevirtualinherited

◆ readBytes()

size_t readBytes ( uint8_t *  data,
size_t  len 
)
inlineoverridevirtual

Read resampled audio data from the buffer.

Parameters
dataPointer to the buffer to fill
lenNumber of bytes to read
Returns
size_t Number of bytes actually read

Reimplemented from AudioStream.

◆ readSilence()

virtual size_t readSilence ( uint8_t *  buffer,
size_t  length 
)
inlinevirtualinherited

Source to generate silence: just sets the buffer to 0.

◆ recalculate()

float recalculate ( )
inline

Recalculate the resampling step size based on buffer fill level.

Returns
float The new step size

◆ refillReadBuffer()

void refillReadBuffer ( )
inlineprotectedinherited

Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read()

◆ removeNotifyAudioChange()

virtual bool removeNotifyAudioChange ( AudioInfoSupport bi)
inlinevirtualinherited

Removes a target in order not to be notified about audio changes.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ setAudioInfo()

virtual void setAudioInfo ( AudioInfo  info)
inlineoverridevirtualinherited

◆ setBuffer()

void setBuffer ( BaseBuffer< uint8_t > &  buffer)
inline

Set the buffer used for audio data.

Parameters
bufferReference to the buffer used for audio data

◆ setKalmanParameters()

void setKalmanParameters ( float  process_noise,
float  measurement_noise 
)
inline

Set the Kalman filter parameters.

Parameters
process_noiseProcess noise covariance (Q)
measurement_noiseMeasurement noise covariance (R)

◆ setNotifyActive()

void setNotifyActive ( bool  flag)
inlineinherited

Deactivate/Reactivate automatic AudioInfo updates: (default is active)

◆ setPIDParameters()

void setPIDParameters ( float  p_value,
float  i_value,
float  d_value 
)
inline

Set the PID controller parameters.

Parameters
p_valueProportional gain
i_valueIntegral gain
d_valueDerivative gain

◆ setStartFillPercent()

void setStartFillPercent ( float  percent)
inline

Defines the fill level (percent) that must be reached once, right after begin(), before any data is returned by readBytes(). Defaults to 50%; 0 disables priming. See AdaptiveResamplingBuffer::setStartFillPercent().

Parameters
percentFill level in percent (e.g. 50.0 for 50%)

◆ setStepRangePercent()

void setStepRangePercent ( float  rangePercent)
inline

Set the allowed resampling range as a percent.

Parameters
rangePercentAllowed range in percent (e.g., 5.0 for ± 5%)

◆ setWriteBufferSize()

void setWriteBufferSize ( int  size)
inlineinherited

◆ write() [1/2]

size_t write ( const uint8_t *  data,
size_t  len 
)
inlineoverridevirtual

Write audio data to the buffer.

Parameters
dataPointer to the data to write
lenNumber of bytes to write
Returns
size_t Number of bytes actually written

Reimplemented from AudioStream.

◆ write() [2/2]

virtual size_t write ( uint8_t  ch)
inlineoverridevirtualinherited

◆ writeSilence()

virtual void writeSilence ( size_t  len)
inlinevirtualinherited

Writes len bytes of silence (=0).

Member Data Documentation

◆ _timeout

int _timeout = 10
protectedinherited

◆ info

AudioInfo info
protectedinherited

◆ is_notify_active

bool is_notify_active = true
protectedinherited

◆ notify_vector

Vector<AudioInfoSupport*> notify_vector
protectedinherited

◆ resampling_buffer

AdaptiveResamplingBuffer resampling_buffer
protected

◆ tmp_in

RingBuffer<uint8_t> tmp_in {0}
protectedinherited

◆ tmp_out

RingBuffer<uint8_t> tmp_out {0}
protectedinherited

◆ write_buffer_size

int write_buffer_size = MAX_SINGLE_CHARS
protectedinherited

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