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 (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.
 
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 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 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 ()
 

Protected Attributes

int _timeout = 10
 
float d = 0.0001
 
float i = 0.00005
 
AudioInfo info
 
bool is_notify_active = true
 
KalmanFilter kalman_filter {0.01f, 0.1f}
 
float level_percent = 0.0
 
Vector< AudioInfoSupport * > notify_vector
 
float p = 0.005
 
BaseBuffer< uint8_t > * p_buffer = nullptr
 
PIDController pid
 
QueueStream< uint8_t > queue_stream
 
uint32_t read_count = 0
 
float resample_range = 0
 
ResampleStream resample_stream
 
float step_size = 1.0
 
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!

The resamping step size is calculated with the help of a PID controller. The fill level is smoothed using a Kalman filter.

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ AdaptiveResamplingStream()

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

Reimplemented from Print.

Reimplemented in ResampleStream, BufferedTaskStream, and BufferedStream.

◆ 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)

◆ 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.

◆ recalculate()

float recalculate ( )
inline

Recalculate the resampling step size based on buffer fill level.

Returns
float The new step size

◆ 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

◆ 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)

◆ 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

◆ 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%)

◆ 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

Reimplemented in BufferedTaskStream, and BufferedStream.


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