|
arduino-audio-tools
|
Acoustic echo canceller using MDF algorithm. More...
#include <MDFEchoCancellation.h>
Public Types | |
| using | Mem = Word32 |
| using | Num = typename SampleType::float_t |
| using | State = EchoState< T, SampleType > |
| using | Word16 = typename SampleType::word16_t |
| using | Word32 = typename SampleType::word32_t |
Protected Member Functions | |
| template<typename A > | |
| A * | echoAlloc (size_t count) |
| Allocate a zero-initialized array of type A using the configured Allocator (see AudioTools/CoreAudio/AudioBasic/Collections/Allocator.h) | |
| void | echoCancellationImpl (State *st, const T *in, const T *far_end, T *out) |
| Core echo cancellation implementation. | |
| void | echoFatal (const char *str) |
| template<typename A > | |
| void | echoFree (A *ptr, size_t count) |
| Deallocate an array previously returned by echoAlloc() | |
| State * | echoStateInitMc (int frame_size, int filter_length, int nb_mic, int nb_speakers) |
| Initialize multi-channel echo canceller state. | |
| void | echoWarning (const char *str) |
| void | ensureInitialized () |
| Ensure echo canceller is initialized (lazy initialization) | |
| void | filterDcNotch16 (const T *in, Word16 radius, Word16 *out, int len, Mem *mem, int stride) |
| Apply DC notch filter to remove DC offset. | |
| void | mdfAdjustProp (const Word32 *W, int N, int M, int P, Word16 *prop) |
| Adjust proportional adaptation weights. | |
| Word32 | mdfInnerProd (const Word16 *x, const Word16 *y, int len) |
| Compute inner product of two vectors. | |
| virtual int | not_supported (int out, const char *msg="") |
| void | notifyAudioChange (AudioInfo info) |
| void | powerSpectrum (const Word16 *X, Word32 *ps, int N) |
| Compute power spectrum from FFT output. | |
| void | powerSpectrumAccum (const Word16 *X, Word32 *ps, int N) |
| Accumulate power spectrum from FFT output. | |
| void | refillReadBuffer () |
| Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read() | |
| T | saturateToSample (Num x) const |
| Rounds and saturates a de-emphasized output sample to T's representable range. Replaces the original fixed int16_t-only WORD2INT macro. Uses NumberConverter::minValueT<T>()/maxValueT<T>() (symmetric +-maxValue(bits) – a harmless 1-LSB narrower on the negative side than T's native asymmetric two's-complement minimum, e.g. -32767 vs -32768 for int16_t). | |
| void | spectralMulAccum (const Word16 *X, const Word32 *Y, Word16 *acc, int N, int M) |
| Accumulate spectral multiplication across multiple frames. | |
| float | spxCos (float x) |
| Compute cosine function. | |
| float | spxExp (float x) |
| Compute exponential function. | |
| echo_int16_t | spxIlog2 (echo_uint32_t x) |
| template<typename A > | |
| A | spxSqrt (A x) |
| void | weightedSpectralMulConj (const Num *w, const Num p, const Word16 *X, const Word16 *Y, Word32 *prod, int N) |
| Compute weighted spectral multiplication with conjugate. | |
Protected Attributes | |
| int | _timeout = 10 |
| Allocator & | allocator |
| AudioFFTBase * | fft_driver |
| int | filter_length |
| AudioInfo | info |
| bool | initialized = false |
| bool | is_notify_active = true |
| int | nb_mic |
| int | nb_speakers |
| Vector< AudioInfoSupport * > | notify_vector |
| State * | state = nullptr |
| RingBuffer< uint8_t > | tmp_in {0} |
| RingBuffer< uint8_t > | tmp_out {0} |
| int | write_buffer_size = MAX_SINGLE_CHARS |
Acoustic echo canceller using MDF algorithm.
High-performance echo cancellation implementation based on the Multi-Delay block Frequency adaptive filter (MDF) algorithm. Supports both single and multi-channel configurations with customizable memory allocation. This is the underlying algorithm used by MDFEchoCancellationStream – use this class directly (via capture()/playback(), or the self-contained cancel()) if you don't want the Stream-based wrapper.
The echo canceller operates by:
Features:
Example usage:
Strengths (see MDFEchoCancellationStream for the fuller writeup):
| SampleType,MDFFloat | by default) chosen when you construct the object, e.g. MDFEchoCancellation<int16_t, MDFFixedPoint> – see MDFFloat/MDFFixedPoint's docs above for the tradeoff. |
| T | The PCM sample type used by capture()/playback() (e.g. int16_t, int32_t). Defaults to int16_t. Must match the sample type of the audio actually flowing through the canceller – see saturateToSample(). |
| SampleType | MDFFloat (default) or MDFFixedPoint. |
| using State = EchoState<T, SampleType> |
|
inline |
Initialize echo canceller with single channel (mono)
| filterLength | Length of echo cancellation filter in samples |
| fftDriver | FFT driver instance from AudioFFT |
| alloc | Allocator used for the echo-state buffers (optional, defaults to the shared DefaultAllocator) |
|
inline |
Initialize echo canceller with multiple channels
| filterLength | Length of echo cancellation filter in samples |
| nbMic | Number of microphone channels |
| nbSpeakers | Number of speaker channels |
| fftDriver | FFT driver instance from AudioFFT |
| alloc | Allocator used for the echo-state buffers (optional, defaults to the shared DefaultAllocator) |
|
inline |
Destructor
|
inlinevirtualinherited |
Adds target to be notified about audio changes.
Reimplemented in CodecNOP, EncodedAudioOutput, EncodedAudioStream, AACDecoderFDK, DecoderBasic, CodecChain, MP3DecoderHelix, MP3DecoderMAD, OggContainerDecoder, RTSPClient< TcpClient, UdpSocket >, Pipeline, and Pipeline::ModifyingStreamAdapter.
provides the actual input AudioInfo
Implements AudioInfoSupport.
Reimplemented in JupyterAudioT< T >, MozziStream, TimerCallbackAudioStream, EncodedAudioStream, PureDataStream, AdapterAudioOutputToAudioStream, GeneratedSoundStream< T >, GeneratedSoundStream< int16_t >, and InputMerge< T >.
provides the actual output AudioInfo: this is usually the same as audioInfo() unless we use a transforming stream
Reimplemented in MP3EncoderShine, PureDataStream, PWMAudioOutput< PWMDriverT >, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, FormatConverterStream, Pipeline, ResampleStream, and ResampleStreamT< TInterpolator >.
Reimplemented from Stream.
Reimplemented in AudioKitStream, AudioLoRa, URLStreamBufferedT< T >, URLStreamBufferedT< ICYStream >, VBANStream, AudioStreamWrapper, FaustStream< DSP >, AudioMP34DT05, I2SCodecStream, MiniAudioStream, StdioStream, TfLiteAudioStream, A2DPStream, ESPNowStream, HammingFEC< bytecount, block_t >, HLSStreamT< URLStream >, HttpRequest, ICYStreamT< T >, URLStream, BufferedTaskStream, NRF24Stream, ReedSolomonFEC< bytecount, additional_bytes >, SPIAudioSlave, UDPStream, USBAudioDeviceBase, AnalogAudioStream, AudioEffectStreamT< T >, AudioEffectStream, Equalizer3Bands, Equalizer3BandsPerChannel, I2SStream, ReformatBaseStream, TimedStream, MemoryStream, RingBufferStream, GeneratedSoundStream< T >, GeneratedSoundStream< int16_t >, BufferedStream, ConverterStream< T >, MeasuringStream, ProgressStream, Throttle, InputMerge< T >, CallbackStream, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, CatStream, QueueStream< T >, QueueStream< uint8_t >, DynamicMemoryStream, FadeStream, FrequencyDetectorAutoCorrelation, FrequencyDetectorZeroCrossing, Pipeline, Pipeline::ModifyingStreamAdapter, QualityAnalysisStream, VolumeStream, FileLoopT< FileType >, FileLoopT< File >, AudioFFTBase, AudioBLEClient, AudioBLEClient, AudioBLEServer, and AudioBLEServer.
Reimplemented from Print.
Reimplemented in AudioLoRa, UDPStream, VBANStream, AudioStreamWrapper, ConverterStream< T >, EncodedAudioStream, FaustStream< DSP >, I2SCodecStream, MiniAudioStream, StdioStream, A2DPStream, ESPNowStream, HammingFEC< bytecount, block_t >, AbstractURLStream, URLStreamESP32, NRF24Stream, ReedSolomonFEC< bytecount, additional_bytes >, SPIAudioSlave, USBAudioDeviceBase, AnalogAudioStream, AudioEffectStreamT< T >, AudioEffectStream, Equalizer3Bands, Equalizer3BandsPerChannel, I2SStream, ReformatBaseStream, TimedStream, MemoryStream, RingBufferStream, MeasuringStream, ProgressStream, Throttle, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, QueueStream< T >, QueueStream< uint8_t >, DynamicMemoryStream, FadeStream, FrequencyDetectorAutoCorrelation, FrequencyDetectorZeroCrossing, Pipeline, QualityAnalysisStream, VolumeStream, AudioFFTBase, AudioBLEClient, AudioBLEClient, AudioBLEServer, and AudioBLEServer.
Reimplemented in AudioOutputWithCallback, AudioMP34DT05, I2SCodecStream, MozziStream, PureDataStream, SPDIFOutput, VS1053Stream, WM8960Stream, AdaptiveResamplingStream, AudioLoRa, ESPNowStream, USBAudioDeviceBase, VBANStream, AudioEffectStreamT< T >, AudioEffectStream, Equalizer3Bands, Equalizer3BandsPerChannel, I2SStream, AudioStreamWrapper, TimerCallbackAudioStream, FrequencyDetectorAutoCorrelation, GoertzelStream, ResampleStreamT< TInterpolator >, FileLoopT< FileType >, FileLoopT< File >, EncodedAudioStream, AudioBoardStream, AudioKitStream, MiniAudioStream, PortAudioStream, StdioStream, HLSStreamT< URLStream >, NRF24Stream, AnalogAudioStream, AdapterAudioOutputToAudioStream, TimedStream, MemoryStream, GeneratedSoundStream< T >, GeneratedSoundStream< int16_t >, MeasuringStream, ProgressStream, Throttle, InputMerge< T >, CallbackStream, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, VolumeMeter, AudioInputMonitor, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, FormatConverterStream, CatStream, QueueStream< T >, QueueStream< uint8_t >, DynamicMemoryStream, Pipeline, Pipeline::ModifyingStreamAdapter, QualityAnalysisStream, ResampleStream, VolumeStream, AudioFFTBase, and EqualizerNBands< SampleT, AccT, NUM_TAPS, NUM_BANDS >.
Perform echo cancellation
| rec | Recorded signal (with echo) |
| play | Playback signal (reference) |
| out | Output signal (echo removed) |
Process captured audio with buffered playback
| rec | Recorded signal |
| out | Output signal (echo removed) |
Deletes all change notify subscriptions.
Reimplemented in RTSPClient< TcpClient, UdpSocket >.
Control/query echo canceller parameters
| request | Request type (ECHO_GET_FRAME_SIZE, etc.) |
| ptr | Pointer to parameter value |
|
inlineprotected |
Allocate a zero-initialized array of type A using the configured Allocator (see AudioTools/CoreAudio/AudioBasic/Collections/Allocator.h)
| A | Type of objects to allocate |
| count | Number of objects to allocate |
Core echo cancellation implementation.
Implements the complete MDF echo cancellation algorithm including:
| st | Echo state structure containing all internal state |
| in | Input signal from microphones (interleaved if multi-channel) |
| far_end | Reference signal from speakers (interleaved if multi-channel) |
| out | Output signal with echo removed (interleaved if multi-channel) |
Deallocate an array previously returned by echoAlloc()
| A | Type of objects to deallocate |
| ptr | Pointer to memory to deallocate |
| count | Number of objects originally allocated (must match the echoAlloc() call that produced ptr) |
|
inlineprotected |
Initialize multi-channel echo canceller state.
| frame_size | Number of samples per frame |
| filter_length | Total filter length in samples |
| nb_mic | Number of microphone channels |
| nb_speakers | Number of speaker channels |
Reimplemented in FaustStream< DSP >, AudioMP34DT05, I2SCodecStream, MozziStream, PureDataStream, SPDIFOutput, VS1053Stream, WM8960Stream, AdaptiveResamplingStream, AudioLoRa, ESPNowStream, BufferedTaskStream, URLStreamBufferedT< T >, URLStreamBufferedT< ICYStream >, USBAudioDeviceBase, Equalizer3Bands, I2SStream, AudioStreamWrapper, TimerCallbackAudioStream, FormatConverterStream, GoertzelStream, FileLoopT< FileType >, FileLoopT< File >, EqualizerNBands< SampleT, AccT, NUM_TAPS, NUM_BANDS >, EncodedAudioStream, AudioKitStream, MiniAudioStream, PortAudioStream, StdioStream, A2DPStream, HLSStreamT< URLStream >, HttpRequest, ICYStreamT< T >, URLStream, URLStreamESP32, NRF24Stream, SPIAudioSlave, AnalogAudioStream, AudioEffectStreamT< T >, AudioEffectStream, Equalizer3BandsPerChannel, ReformatBaseStream, AdapterAudioOutputToAudioStream, MemoryStream, GeneratedSoundStream< T >, GeneratedSoundStream< int16_t >, InputMixer< T, SumT >, InputMerge< T >, CallbackStream, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, ChannelFormatConverterStream, NumberFormatConverterStream, CatStream, QueueStream< T >, QueueStream< uint8_t >, DynamicMemoryStream, Pipeline, Pipeline::ModifyingStreamAdapter, VolumeStream, AudioFFTBase, AudioBLEClient, AudioBLEClient, AudioBLEServer, AudioBLEServer, AbstractURLStream, and AudioBLEStream.
|
inlineprotected |
Ensure echo canceller is initialized (lazy initialization)
Reimplemented from Print.
Reimplemented in PureDataStream, URLStreamBufferedT< T >, URLStreamBufferedT< ICYStream >, ReformatBaseStream, AudioStreamWrapper, ResampleStream, EncodedAudioStream, URLStream, BufferedTaskStream, I2SStream, MemoryStream, RingBufferStream, GeneratedSoundStream< T >, GeneratedSoundStream< int16_t >, and BufferedStream.
|
inline |
Get filter length
|
inline |
Get frame size
|
inline |
Get impulse response
| response | Buffer to store impulse response (must be pre-allocated) |
|
inline |
Get impulse response size
|
inline |
Get number of microphone channels
|
inline |
Get sampling rate
|
inline |
Get number of speaker channels
|
inline |
Get underlying state (for advanced usage)
Adjust proportional adaptation weights.
| W | Filter weights in frequency domain |
| N | FFT size |
| M | Number of filter blocks |
| P | Number of channels |
| prop | Output proportional weights (normalized) |
Compute inner product of two vectors.
| x | First input vector |
| y | Second input vector |
| len | Length of vectors |
Buffer playback signal for later processing
| play | Playback signal to buffer |
Compute power spectrum from FFT output.
| X | FFT output in packed format |
| ps | Output power spectrum |
| N | FFT size |
Accumulate power spectrum from FFT output.
| X | FFT output in packed format |
| ps | Accumulated power spectrum (updated in place) |
| N | FFT size |
Implements BaseStream.
Reimplemented in LMSEchoCancellationStream< T >, MDFEchoCancellationStream< T, SampleType >, USBAudioDeviceBase, MozziStream, PureDataStream, AudioLoRa, URLStreamBufferedT< T >, URLStreamBufferedT< ICYStream >, AudioStreamWrapper, EncodedAudioStream, FaustStream< DSP >, AudioKitStream, AudioMP34DT05, I2SCodecStream, MiniAudioStream, PortAudioStream, TfLiteAudioStream, A2DPStream, AdaptiveResamplingStream, HLSStreamT< URLStream >, ICYStreamT< T >, URLStream, BufferedTaskStream, URLStreamESP32, NRF24Stream, SPIAudioSlave, VBANStream, AnalogAudioStream, AudioEffectStreamT< T >, AudioEffectStream, Equalizer3Bands, Equalizer3BandsPerChannel, I2SStream, ReformatBaseStream, TimedStream, MemoryStream, RingBufferStream, GeneratedSoundStream< T >, GeneratedSoundStream< int16_t >, BufferedStream, ConverterStream< T >, MeasuringStream, ProgressStream, Throttle, InputMixer< T, SumT >, InputMerge< T >, CallbackStream, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, VolumeMeter, AudioInputMonitor, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, FadeStream, FrequencyDetectorAutoCorrelation, FrequencyDetectorZeroCrossing, GoertzelStream, Pipeline, QualityAnalysisStream, VolumeStream, AudioFFTBase, AudioBLEClient, AudioBLEClient, AudioBLEServer, AudioBLEServer, EqualizerNBands< SampleT, AccT, NUM_TAPS, NUM_BANDS >, WM8960Stream, AnalogDriverESP32V1::IO16Bit, and AnalogDriverESP32V2::IO16Bit.
Source to generate silence: just sets the buffer to 0.
|
inlineprotectedinherited |
Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read()
|
inlinevirtualinherited |
Removes a target in order not to be notified about audio changes.
Reimplemented in RTSPClient< TcpClient, UdpSocket >.
|
inline |
Reset echo canceller state
Rounds and saturates a de-emphasized output sample to T's representable range. Replaces the original fixed int16_t-only WORD2INT macro. Uses NumberConverter::minValueT<T>()/maxValueT<T>() (symmetric +-maxValue(bits) – a harmless 1-LSB narrower on the negative side than T's native asymmetric two's-complement minimum, e.g. -32767 vs -32768 for int16_t).
Defines the input AudioInfo.
Implements AudioInfoSupport.
Reimplemented in VS1053Stream, WM8960Stream, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, VolumeStream, MiniAudioStream, PortAudioStream, I2SCodecStream, MozziStream, SPDIFOutput, AudioLoRa, AnalogAudioStream, I2SStream, TimerCallbackAudioStream, AudioBLEStream, MDFEchoCancellationStream< T, SampleType >, AudioKitStream, NRF24Stream, SPIAudioSlave, USBAudioDeviceBase, VBANStream, Equalizer3Bands, Equalizer3BandsPerChannel, AdapterAudioOutputToAudioStream, TimedStream, MeasuringStream, ProgressStream, CallbackStream, VolumeMeter, AudioInputMonitor, FormatConverterStream, FadeStream, GoertzelStream, Pipeline::ModifyingStreamAdapter, QualityAnalysisStream, AudioFFTBase, EncodedAudioStream, PureDataStream, GeneratedSoundStream< T >, GeneratedSoundStream< int16_t >, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, Pipeline, ResampleStream, and ResampleStreamT< TInterpolator >.
|
inline |
Set FFT driver (must be called before first use)
| fftDriver | Reference to FFT driver instance |
Set filter length (must be called before first use)
| len | Length of echo cancellation filter in samples |
Set number of microphone channels (must be called before first use)
| num | Number of microphone channels |
|
inlineinherited |
Deactivate/Reactivate automatic AudioInfo updates: (default is active)
Set number of speaker channels (must be called before first use)
| num | Number of speaker channels |
|
inlineinherited |
|
inlineprotected |
Accumulate spectral multiplication across multiple frames.
| X | Input spectrum (M frames) |
| Y | Filter weights in frequency domain |
| acc | Output accumulated result |
| N | FFT size |
| M | Number of frames to accumulate |
Compute cosine function.
| x | Input angle in radians |
Compute exponential function.
| x | Input value |
|
inlineprotected |
|
inlineprotected |
Compute weighted spectral multiplication with conjugate.
| w | Weight array for each frequency bin |
| p | Global scaling factor |
| X | First input spectrum |
| Y | Second input spectrum (conjugated) |
| prod | Output product |
| N | FFT size |
Implements BaseStream.
Reimplemented in URLStream, LMSEchoCancellationStream< T >, MDFEchoCancellationStream< T, SampleType >, AudioBLEServer, ESP3288AudioOutput, I2SCodecStream, MozziStream, PureDataStream, SPDIFOutput, AudioLoRa, USBAudioDeviceBase, I2SStream, AudioStreamWrapper, ConverterStream< T >, EncodedAudioStream, FaustStream< DSP >, AudioKitStream, MiniAudioStream, PortAudioStream, TfLiteAudioStream, VS1053Stream, A2DPStream, AdaptiveResamplingStream, BufferedTaskStream, URLStreamESP32, NRF24Stream, SPIAudioSlave, VBANStream, AnalogAudioStream, AudioEffectStreamT< T >, AudioEffectStream, Equalizer3Bands, Equalizer3BandsPerChannel, AdapterAudioOutputToAudioStream, TimedStream, MemoryStream, RingBufferStream, BufferedStream, MeasuringStream, ProgressStream, Throttle, CallbackStream, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, VolumeMeter, AudioInputMonitor, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, FormatConverterStream, FadeStream, FrequencyDetectorAutoCorrelation, FrequencyDetectorZeroCrossing, GoertzelStream, Pipeline, Pipeline::ModifyingStreamAdapter, QualityAnalysisStream, ResampleStream, ResampleStreamT< TInterpolator >, VolumeStream, AudioFFTBase, AudioBLEClient, AudioBLEClient, AudioBLEServer, EqualizerNBands< SampleT, AccT, NUM_TAPS, NUM_BANDS >, TfLiteAudioStreamBase, WM8960Stream, AnalogDriverESP32V1::IO16Bit, and AnalogDriverESP32V2::IO16Bit.
Reimplemented in MemoryStream, AudioStreamWrapper, BufferedTaskStream, RingBufferStream, BufferedStream, and URLStream.
|
protectedinherited |
|
protected |
Pointer to FFT driver instance
|
protected |
Length of echo cancellation filter
|
protectedinherited |
Lazy initialization flag
|
protected |
Number of microphone channels
|
protected |
Number of speaker channels
|
protectedinherited |
Pointer to internal echo canceller state
|
protectedinherited |
|
protectedinherited |
|
protectedinherited |