arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
LMSEchoCancellationStream< T > Class Template Reference

Echo cancellation with adaptive LMS filtering for microcontrollers. More...

#include <LMSEchoCancellationStream.h>

Inheritance diagram for LMSEchoCancellationStream< T >:
AudioStream BaseStream AudioInfoSupport AudioInfoSource Stream Print

Public Member Functions

 LMSEchoCancellationStream (Stream &in, size_t lag_samples=0, size_t buffer_size=512, size_t filter_len=32, float mu=0.001f)
 Constructor.
 
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
 
virtual bool begin ()
 
void cancel (const T *rec, const T *play, T *out, size_t len)
 Process echo cancellation on arrays of samples. Self-contained: feeds the playback reference and the recorded signal in lock-step, so it can be used directly without going through the Stream write()/readBytes() pair.
 
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
 
virtual void end ()
 
virtual void flush () override
 
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
 
virtual operator bool ()
 
size_t readBytes (uint8_t *buf, size_t len) override
 Read input and remove echo, using the playback signal previously buffered via write() as the reference for the adaptive filter.
 
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.
 
void reset ()
 Reset the internal buffer and lag state.
 
virtual void setAudioInfo (AudioInfo newInfo) override
 Defines the input AudioInfo.
 
void setFilterLen (size_t len)
 Set the filter length for the adaptive filter.
 
void setLag (size_t lag_samples)
 Set the lag (delay) in samples for echo cancellation.
 
void setMu (float mu)
 Set the adaptation rate (mu) for the LMS algorithm.
 
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active)
 
void setWriteBufferSize (int size)
 
size_t write (const uint8_t *buf, size_t len) override
 Store the output signal (sent to speaker). The samples are pushed into the internal delay line so that readBytes() can later use them as the echo reference.
 
virtual size_t write (uint8_t ch) override
 
virtual void writeSilence (size_t len)
 Writes len bytes of silence (=0).
 

Protected Member Functions

filterSample (T mic_sample)
 Estimate and subtract the echo for a single microphone sample, using the oldest filter_len samples currently buffered (i.e. the playback signal delayed by lag samples) as the adaptive filter's reference, then update the filter taps (NLMS-free, plain LMS).
 
virtual int not_supported (int out, const char *msg="")
 
void notifyAudioChange (AudioInfo info)
 
void pushPlayback (T sample)
 Push a playback (speaker) sample into the delay line, evicting the oldest sample once the window exceeds filter_len + lag so that the reference always reflects the most recent playback history.
 
void refillReadBuffer ()
 Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read()
 

Protected Attributes

int _timeout = 10
 
float adaptation_rate = 0.01f
 
size_t buffer_size
 
Vector< float > filter
 
size_t filter_len
 
AudioInfo info
 
bool is_notify_active = true
 
size_t lag
 
Vector< AudioInfoSupport * > notify_vector
 
Streamp_io = nullptr
 
Vector< T > ref_vec
 
RingBuffer< T > ring_buffer {0}
 
RingBuffer< uint8_t > tmp_in {0}
 
RingBuffer< uint8_t > tmp_out {0}
 
int write_buffer_size = MAX_SINGLE_CHARS
 

Detailed Description

template<typename T = int16_t>
class audio_tools::LMSEchoCancellationStream< T >

Echo cancellation with adaptive LMS filtering for microcontrollers.

This class implements echo cancellation using an adaptive FIR filter (LMS algorithm). It estimates the echo path and subtracts the estimated echo from the microphone input. write() buffers the playback (speaker) signal into a sliding delay line; readBytes() pulls mic samples from the wrapped Stream and filters each one against that delay line. cancel(rec, play, out, len) is a Stream-free alternative that feeds both signals explicitly in lock-step (useful for testing or non-duplex pipelines) and is verified to produce output identical to the write()/readBytes() path.

Strengths:

Weaknesses:

Constructor & Destructor Documentation

◆ LMSEchoCancellationStream()

template<typename T = int16_t>
LMSEchoCancellationStream ( Stream in,
size_t  lag_samples = 0,
size_t  buffer_size = 512,
size_t  filter_len = 32,
float  mu = 0.001f 
)
inline

Constructor.

Parameters
inReference to the input stream (microphone or audio input)
lag_samplesNumber of samples to delay the echo subtraction (default: 0)
buffer_sizeSize of the internal ring buffer (default: 512)

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

virtual bool begin ( )
inlinevirtualinherited

◆ cancel()

template<typename T = int16_t>
void cancel ( const T *  rec,
const T *  play,
T *  out,
size_t  len 
)
inline

Process echo cancellation on arrays of samples. Self-contained: feeds the playback reference and the recorded signal in lock-step, so it can be used directly without going through the Stream write()/readBytes() pair.

Parameters
recPointer to received (microphone) signal samples
playPointer to playback (speaker) signal samples
outPointer to output buffer for echo-cancelled signal
lenNumber of samples to process

◆ clearNotifyAudioChange()

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

Deletes all change notify subscriptions.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ end()

virtual void end ( )
inlinevirtualinherited

◆ filterSample()

template<typename T = int16_t>
T filterSample ( mic_sample)
inlineprotected

Estimate and subtract the echo for a single microphone sample, using the oldest filter_len samples currently buffered (i.e. the playback signal delayed by lag samples) as the adaptive filter's reference, then update the filter taps (NLMS-free, plain LMS).

◆ flush()

virtual void flush ( )
inlineoverridevirtualinherited

◆ isNotifyActive()

bool isNotifyActive ( )
inlineinherited

Checks if the automatic AudioInfo update is active.

◆ not_supported()

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

◆ notifyAudioChange()

void notifyAudioChange ( AudioInfo  info)
inlineprotectedinherited

◆ operator bool()

virtual operator bool ( )
inlinevirtualinherited

◆ pushPlayback()

template<typename T = int16_t>
void pushPlayback ( sample)
inlineprotected

Push a playback (speaker) sample into the delay line, evicting the oldest sample once the window exceeds filter_len + lag so that the reference always reflects the most recent playback history.

◆ readBytes()

template<typename T = int16_t>
size_t readBytes ( uint8_t *  buf,
size_t  len 
)
inlineoverridevirtual

Read input and remove echo, using the playback signal previously buffered via write() as the reference for the adaptive filter.

Parameters
bufPointer to buffer to store processed input (T*)
lenNumber of bytes to read
Returns
Number of bytes read from input

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.

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

◆ reset()

template<typename T = int16_t>
void reset ( )
inline

Reset the internal buffer and lag state.

◆ setAudioInfo()

virtual void setAudioInfo ( AudioInfo  info)
inlineoverridevirtualinherited

◆ setFilterLen()

template<typename T = int16_t>
void setFilterLen ( size_t  len)
inline

Set the filter length for the adaptive filter.

Parameters
lenLength of the adaptive filter

◆ setLag()

template<typename T = int16_t>
void setLag ( size_t  lag_samples)
inline

Set the lag (delay) in samples for echo cancellation.

Parameters
lag_samplesNumber of samples to delay the echo subtraction

◆ setMu()

template<typename T = int16_t>
void setMu ( float  mu)
inline

Set the adaptation rate (mu) for the LMS algorithm.

Parameters
muAdaptation rate

◆ setNotifyActive()

void setNotifyActive ( bool  flag)
inlineinherited

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

◆ setWriteBufferSize()

void setWriteBufferSize ( int  size)
inlineinherited

◆ write() [1/2]

template<typename T = int16_t>
size_t write ( const uint8_t *  buf,
size_t  len 
)
inlineoverridevirtual

Store the output signal (sent to speaker). The samples are pushed into the internal delay line so that readBytes() can later use them as the echo reference.

Parameters
bufPointer to PCM data sent to the speaker (T*)
lenNumber of bytes in buf
Returns
Number of bytes processed

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

◆ adaptation_rate

template<typename T = int16_t>
float adaptation_rate = 0.01f
protected

◆ buffer_size

template<typename T = int16_t>
size_t buffer_size
protected

◆ filter

template<typename T = int16_t>
Vector<float> filter
protected

◆ filter_len

template<typename T = int16_t>
size_t filter_len
protected

◆ info

AudioInfo info
protectedinherited

◆ is_notify_active

bool is_notify_active = true
protectedinherited

◆ lag

template<typename T = int16_t>
size_t lag
protected

◆ notify_vector

Vector<AudioInfoSupport*> notify_vector
protectedinherited

◆ p_io

template<typename T = int16_t>
Stream* p_io = nullptr
protected

◆ ref_vec

template<typename T = int16_t>
Vector<T> ref_vec
protected

◆ ring_buffer

template<typename T = int16_t>
RingBuffer<T> ring_buffer {0}
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: