arduino-audio-tools
Loading...
Searching...
No Matches
ResampleStream Class Reference

Dynamic Resampling. We can use a variable factor to speed up or slow down the playback. More...

#include <ResampleStream.h>

Inheritance diagram for ResampleStream:
ReformatBaseStream ModifyingStream AudioStream AudioStream AudioInfoSource AudioInfoSupport BaseStream AudioInfoSource AudioInfoSupport BaseStream AudioInfoSource AudioInfoSupport BaseStream AudioInfoSource AudioInfoSupport BaseStream

Public Member Functions

 ResampleStream ()=default
 ResampleStream (AudioOutput &out)
 ResampleStream (AudioStream &io)
 ResampleStream (Print &out)
 Support for resampling via write.
 ResampleStream (Stream &io)
 Support for resampling via write and read.
virtual void addNotifyAudioChange (AudioInfoSupport &bi)
 Adds target to be notified about audio changes.
virtual void addNotifyAudioChange (AudioInfoSupport &bi)
 Adds target to be notified about audio changes.
virtual AudioInfo audioInfo () override
 provides the actual input AudioInfo
virtual AudioInfo audioInfo () override
 provides the actual input AudioInfo
AudioInfo audioInfoOut () override
int available () override
int availableForWrite () override
bool begin () override
bool begin (AudioInfo from, AudioInfo to)
bool begin (AudioInfo from, int toRate)
bool begin (AudioInfo from, sample_rate_t toRate)
virtual bool begin (AudioInfo info)
bool begin (AudioInfo info, float step)
bool begin (ResampleConfig cfg)
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
ResampleConfig defaultConfig ()
 Provides the default configuraiton.
void end () override
void flush ()
 When buffering is active, writes the buffered audio to the output.
float getByteFactor () override
virtual Print * getPrint ()
float getStepSize ()
 Returns the actual step size.
float getStepSize (float sampleRateFrom, float sampleRateTo)
virtual Stream * getStream ()
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
virtual operator bool ()
virtual operator bool ()
size_t readBytes (uint8_t *data, size_t len) override
virtual size_t readSilence (uint8_t *buffer, size_t length)
 Source to generate silence: just sets the buffer to 0.
virtual size_t readSilence (uint8_t *buffer, size_t length)
 Source to generate silence: just sets the buffer to 0.
virtual bool removeNotifyAudioChange (AudioInfoSupport &bi)
 Removes a target in order not to be notified about audio changes.
virtual bool removeNotifyAudioChange (AudioInfoSupport &bi)
 Removes a target in order not to be notified about audio changes.
void resizeReadResultQueue (int size)
virtual void setAudioInfo (AudioInfo newInfo) override
 Defines the input AudioInfo.
void setAudioInfo (AudioInfo newInfo) override
void setBuffered (bool active)
 Activates buffering to avoid small incremental writes.
void setMaxReadSize (int size)
 Defines the read buffer size for individual reads: same as transformationReader().setMaxReadSize(size).
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active).
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active).
virtual void setOutput (AudioOutput &print)
 Defines/Changes the output target and registers for audio change notifications.
virtual void setOutput (Print &print) override
 Defines/Changes the output target.
void setReadResultQueueSize (int size)
 same as resizeReadResultQueue(size)
void setStepSize (float step)
 influence the sample rate
virtual void setStream (AudioStream &stream)
 Defines/Changes the input & output and registers for audio change notifications.
virtual void setStream (Stream &stream) override
 Defines/Changes the input & output.
void setTargetSampleRate (int rate)
void setWriteBufferSize (int size)
void setWriteBufferSize (int size)
virtual TransformationReader< ReformatBaseStream > & transformationReader ()
 Provides access to the TransformationReader.
virtual size_t write (uint8_t ch) override
size_t write (const uint8_t *data, size_t len) override
virtual void writeSilence (size_t len)
 Writes len bytes of silence (=0).
virtual void writeSilence (size_t len)
 Writes len bytes of silence (=0).

Protected Member Functions

template<typename T>
T getValue (T *data, float frame_idx, int channel)
 get the interpolated value for indicated (float) index value
template<typename T>
T lookup (T *data, int frame, int channel)
 lookup value for indicated frame & channel: index starts with -1;
virtual int not_supported (int out, const char *msg="")
virtual int not_supported (int out, const char *msg="")
void notifyAudioChange (AudioInfo info)
void notifyAudioChange (AudioInfo info)
void refillReadBuffer ()
 Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read().
void refillReadBuffer ()
 Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read().
void setupLastSamples (AudioInfo cfg)
 Sets up the buffer for the rollover samples.
template<typename T>
void setupLastSamples (T *data, int frame)
 store last samples to provide values for index -1
void setupReader ()
template<typename T>
size_t write (const uint8_t *buffer, size_t bytes, size_t &written)
 Writes the buffer to defined output after resampling.

Protected Attributes

int _timeout = 10
int bytes_per_frame = 0
Vector< uint8_t > carry {0}
size_t carry_len = 0
Vector< uint8_t > combine_buffer {0}
float idx = 0
AudioInfo info
bool is_buffer_active = USE_RESAMPLE_BUFFER
bool is_first = true
bool is_notify_active = true
Vector< uint8_t > last_samples {0}
Vector< AudioInfoSupport * > notify_vector
SingleBuffer< uint8_t > out_buffer {0}
Stream * p_io = nullptr
Print * p_out = nullptr
TransformationReader< ReformatBaseStream > reader
float step_size = 1.0
RingBuffer< uint8_t > tmp_in {0}
RingBuffer< uint8_t > tmp_out {0}
int to_sample_rate = 0
int write_buffer_size = 8

Detailed Description

Dynamic Resampling. We can use a variable factor to speed up or slow down the playback.

Author
Phil Schatzmann
Template Parameters
T

Constructor & Destructor Documentation

◆ ResampleStream() [1/5]

ResampleStream ( )
default

◆ ResampleStream() [2/5]

ResampleStream ( Print & out)
inline

Support for resampling via write.

◆ ResampleStream() [3/5]

ResampleStream ( AudioOutput & out)
inline

Support for resampling via write. The audio information is copied from the io

◆ ResampleStream() [4/5]

ResampleStream ( Stream & io)
inline

Support for resampling via write and read.

◆ ResampleStream() [5/5]

ResampleStream ( AudioStream & io)
inline

Support for resampling via write and read. The audio information is copied from the io

Member Function Documentation

◆ addNotifyAudioChange() [1/2]

virtual void addNotifyAudioChange ( AudioInfoSupport & bi)
inlinevirtualinherited

Adds target to be notified about audio changes.

Reimplemented in CodecNOP, CodecNOP, EncodedAudioOutput, and EncodedAudioStream.

◆ addNotifyAudioChange() [2/2]

virtual void addNotifyAudioChange ( AudioInfoSupport & bi)
inlinevirtualinherited

Adds target to be notified about audio changes.

Reimplemented in CodecNOP, CodecNOP, EncodedAudioOutput, and EncodedAudioStream.

◆ audioInfo() [1/2]

◆ audioInfo() [2/2]

◆ audioInfoOut()

AudioInfo audioInfoOut ( )
inlineoverridevirtual

provides the actual output AudioInfo: this is usually the same as audioInfo() unless we use a transforming stream

Reimplemented from AudioInfoSupport.

◆ available()

int available ( )
inlineoverridevirtualinherited

Reimplemented from BaseStream.

◆ availableForWrite()

int availableForWrite ( )
inlineoverridevirtualinherited

Reimplemented from BaseStream.

◆ begin() [1/7]

bool begin ( )
inlineoverridevirtual

Reimplemented from BaseStream.

◆ begin() [2/7]

bool begin ( AudioInfo from,
AudioInfo to )
inline

◆ begin() [3/7]

bool begin ( AudioInfo from,
int toRate )
inline

◆ begin() [4/7]

bool begin ( AudioInfo from,
sample_rate_t toRate )
inline

◆ begin() [5/7]

virtual bool begin ( AudioInfo info)
inlinevirtual

◆ begin() [6/7]

bool begin ( AudioInfo info,
float step )
inline

◆ begin() [7/7]

bool begin ( ResampleConfig cfg)
inline

◆ clearNotifyAudioChange() [1/2]

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

◆ clearNotifyAudioChange() [2/2]

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

◆ defaultConfig()

ResampleConfig defaultConfig ( )
inline

Provides the default configuraiton.

◆ end()

void end ( )
inlineoverridevirtualinherited

Reimplemented from BaseStream.

◆ flush()

void flush ( )
inlinevirtual

When buffering is active, writes the buffered audio to the output.

Reimplemented from ReformatBaseStream.

◆ getByteFactor()

float getByteFactor ( )
inlineoverridevirtual

Implements ReformatBaseStream.

◆ getPrint()

virtual Print * getPrint ( )
inlinevirtualinherited

◆ getStepSize() [1/2]

float getStepSize ( )
inline

Returns the actual step size.

◆ getStepSize() [2/2]

float getStepSize ( float sampleRateFrom,
float sampleRateTo )
inline

calculate the step size the sample rate: e.g. from 44200 to 22100 gives a step size of 2 in order to provide fewer samples

◆ getStream()

virtual Stream * getStream ( )
inlinevirtualinherited

◆ getValue()

template<typename T>
T getValue ( T * data,
float frame_idx,
int channel )
inlineprotected

get the interpolated value for indicated (float) index value

◆ isNotifyActive() [1/2]

bool isNotifyActive ( )
inlineinherited

Checks if the automatic AudioInfo update is active.

◆ isNotifyActive() [2/2]

bool isNotifyActive ( )
inlineinherited

Checks if the automatic AudioInfo update is active.

◆ lookup()

template<typename T>
T lookup ( T * data,
int frame,
int channel )
inlineprotected

lookup value for indicated frame & channel: index starts with -1;

◆ not_supported() [1/2]

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

◆ not_supported() [2/2]

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

◆ notifyAudioChange() [1/2]

void notifyAudioChange ( AudioInfo info)
inlineprotectedinherited

◆ notifyAudioChange() [2/2]

void notifyAudioChange ( AudioInfo info)
inlineprotectedinherited

◆ operator bool() [1/2]

◆ operator bool() [2/2]

◆ readBytes()

size_t readBytes ( uint8_t * data,
size_t len )
inlineoverridevirtualinherited

Reimplemented from AudioStream.

◆ readSilence() [1/2]

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

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

◆ readSilence() [2/2]

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

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

◆ refillReadBuffer() [1/2]

void refillReadBuffer ( )
inlineprotectedinherited

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

◆ refillReadBuffer() [2/2]

void refillReadBuffer ( )
inlineprotectedinherited

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

◆ removeNotifyAudioChange() [1/2]

virtual bool removeNotifyAudioChange ( AudioInfoSupport & bi)
inlinevirtualinherited

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

◆ removeNotifyAudioChange() [2/2]

virtual bool removeNotifyAudioChange ( AudioInfoSupport & bi)
inlinevirtualinherited

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

◆ resizeReadResultQueue()

void resizeReadResultQueue ( int size)
inlineinherited

Define the size of the interal read result queue: same as transformationReader().resizeResultQueue(size)

◆ setAudioInfo() [1/2]

◆ setAudioInfo() [2/2]

void setAudioInfo ( AudioInfo newInfo)
inlineoverride

◆ setBuffered()

void setBuffered ( bool active)
inline

Activates buffering to avoid small incremental writes.

◆ setMaxReadSize()

void setMaxReadSize ( int size)
inlineinherited

Defines the read buffer size for individual reads: same as transformationReader().setMaxReadSize(size).

◆ setNotifyActive() [1/2]

void setNotifyActive ( bool flag)
inlineinherited

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

◆ setNotifyActive() [2/2]

void setNotifyActive ( bool flag)
inlineinherited

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

◆ setOutput() [1/2]

virtual void setOutput ( AudioOutput & out)
inlinevirtualinherited

Defines/Changes the output target and registers for audio change notifications.

Reimplemented from ModifyingStream.

Reimplemented in EncodedAudioStream.

◆ setOutput() [2/2]

virtual void setOutput ( Print & out)
inlineoverridevirtualinherited

Defines/Changes the output target.

Implements ModifyingStream.

Reimplemented in EncodedAudioStream, EncodedAudioStream, and FormatConverterStream.

◆ setReadResultQueueSize()

void setReadResultQueueSize ( int size)
inlineinherited

same as resizeReadResultQueue(size)

◆ setStepSize()

void setStepSize ( float step)
inline

influence the sample rate

◆ setStream() [1/2]

virtual void setStream ( AudioStream & io)
inlinevirtualinherited

Defines/Changes the input & output and registers for audio change notifications.

Reimplemented from ModifyingStream.

Reimplemented in EncodedAudioStream.

◆ setStream() [2/2]

virtual void setStream ( Stream & in)
inlineoverridevirtualinherited

Defines/Changes the input & output.

Implements ModifyingStream.

Reimplemented in EncodedAudioStream, EncodedAudioStream, and FormatConverterStream.

◆ setTargetSampleRate()

void setTargetSampleRate ( int rate)
inline

◆ setupLastSamples() [1/2]

void setupLastSamples ( AudioInfo cfg)
inlineprotected

Sets up the buffer for the rollover samples.

◆ setupLastSamples() [2/2]

template<typename T>
void setupLastSamples ( T * data,
int frame )
inlineprotected

store last samples to provide values for index -1

◆ setupReader()

void setupReader ( )
inlineprotectedinherited

◆ setWriteBufferSize() [1/2]

void setWriteBufferSize ( int size)
inlineinherited

◆ setWriteBufferSize() [2/2]

void setWriteBufferSize ( int size)
inlineinherited

◆ transformationReader()

virtual TransformationReader< ReformatBaseStream > & transformationReader ( )
inlinevirtualinherited

Provides access to the TransformationReader.

◆ write() [1/3]

◆ write() [2/3]

template<typename T>
size_t write ( const uint8_t * buffer,
size_t bytes,
size_t & written )
inlineprotected

Writes the buffer to defined output after resampling.

◆ write() [3/3]

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

Reimplemented from AudioStream.

◆ writeSilence() [1/2]

virtual void writeSilence ( size_t len)
inlinevirtualinherited

Writes len bytes of silence (=0).

◆ writeSilence() [2/2]

virtual void writeSilence ( size_t len)
inlinevirtualinherited

Writes len bytes of silence (=0).

Member Data Documentation

◆ _timeout

int _timeout = 10
protectedinherited

◆ bytes_per_frame

int bytes_per_frame = 0
protected

◆ carry

Vector<uint8_t> carry {0}
protected

◆ carry_len

size_t carry_len = 0
protected

◆ combine_buffer

Vector<uint8_t> combine_buffer {0}
protected

◆ idx

float idx = 0
protected

◆ info

AudioInfo info
protectedinherited

◆ is_buffer_active

bool is_buffer_active = USE_RESAMPLE_BUFFER
protected

◆ is_first

bool is_first = true
protected

◆ is_notify_active

bool is_notify_active = true
protectedinherited

◆ last_samples

Vector<uint8_t> last_samples {0}
protected

◆ notify_vector

Vector< AudioInfoSupport * > notify_vector
protectedinherited

◆ out_buffer

SingleBuffer<uint8_t> out_buffer {0}
protected

◆ p_io

Stream* p_io = nullptr
protectedinherited

◆ p_out

Print* p_out = nullptr
protectedinherited

◆ reader

TransformationReader<ReformatBaseStream> reader
protectedinherited

◆ step_size

float step_size = 1.0
protected

◆ tmp_in

RingBuffer< uint8_t > tmp_in {0}
protectedinherited

◆ tmp_out

RingBuffer< uint8_t > tmp_out {0}
protectedinherited

◆ to_sample_rate

int to_sample_rate = 0
protected

◆ write_buffer_size

int write_buffer_size = 8
protectedinherited

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