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

Executes FFT using audio data privded by write() and/or an inverse FFT where the samples are made available via readBytes(). The Driver which is passed in the constructor selects a specifc FFT implementation. More...

#include <AudioFFT.h>

Inheritance diagram for AudioFFTBase:
AudioStream BaseStream AudioInfoSupport AudioInfoSource BaseStream AudioInfoSupport AudioInfoSource Stream Stream Stream Stream Print Print Print Print Print Print Print Print AudioCmsisFFT AudioESP32FFT AudioEspressifFFT AudioFixedFFT< CalcT > AudioKissFFT AudioRealFFT

Classes

struct  MelFilter
 One triangular mel filter: weights for bins [start_bin, start_bin+count). More...

Public Member Functions

 AudioFFTBase (const AudioFFTBase &)=delete
 AudioFFTBase (FFTDriver *driver)
 ~AudioFFTBase ()
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
virtual AudioInfo audioInfoOut ()
virtual AudioInfo audioInfoOut ()
int available () override
 Data available for reverse fft.
int availableForWrite () override
 We try to fill the buffer at once.
bool begin () override
 starts the processing
bool begin (AudioFFTConfig info)
 starts the processing
void clearBins ()
 clears the fft data
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
AudioFFTConfig & config ()
 Provides the actual configuration.
AudioFFTConfig defaultConfig (RxTxMode mode=TX_MODE)
 Provides the default configuration.
FFTDriver * driver ()
void end () override
 Release the allocated memory.
virtual void flush () override
virtual void flush () override
float frequency (int bin)
 Determines the frequency of the indicated bin.
int frequencyToBin (int freq)
 Determine the bin number from the frequency.
bool fromMEL (float *values, int n_bins, float min_freq=0.0f, float max_freq=0.0f)
 Convert MEL spectrum back to linear frequency spectrum, using the same (cached) filterbank as toMEL().
bool getBin (int pos, FFTBin &bin)
 gets the value of a bin
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
int length ()
 The number of samples.
float magnitude (int bin)
float magnitudeFast (int bin)
float * magnitudes ()
float * magnitudesFast ()
 operator bool () override
AudioFFTBase & operator= (const AudioFFTBase &)=delete
float phase (int bin)
 calculates the phase
size_t readBytes (uint8_t *data, size_t len) override
 Provides the result of a reverse FFT.
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.
template<typename T>
T & reference ()
 Provides the reference pointer.
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 reset ()
 Just resets the current_pos e.g. to start a new cycle.
AudioFFTResult result ()
 Determines the result values in the max magnitude bin.
template<int N>
void resultArray (AudioFFTResult(&result)[N])
 Determines the N biggest result values.
unsigned long resultTime ()
unsigned long resultTimeBegin ()
 time before the fft
virtual void setAudioInfo (AudioInfo newInfo) override
 Defines the input AudioInfo.
void setAudioInfo (AudioInfo info) override
 Notify change of audio information.
bool setBin (int idx, float real, float img)
 sets the value of a bin
bool setBin (int pos, FFTBin &bin)
 sets the value of a bin
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active).
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active).
void setWriteBufferSize (int size)
void setWriteBufferSize (int size)
int size ()
 The number of bins used by the FFT which are relevant for the result.
float * toMEL (int n_bins, float min_freq=0.0f, float max_freq=0.0f, bool power=false)
virtual size_t write (uint8_t ch) override
size_t write (const uint8_t *data, size_t len) override
 Provide the audio data as FFT input.
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

int bytesPerSample ()
template<typename T>
void fft ()
template<int N>
void insertSorted (AudioFFTResult(&result)[N], AudioFFTResult tmp)
 make sure that we do not reuse already found results
bool isPowerOfTwo (uint16_t x)
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)
template<typename T>
void processSamples (const void *data, size_t count)
template<typename T>
void processStrideBuffer ()
 FFT of the full stride buffer, then drop stride samples.
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 rfft ()
 reverse fft
void rfftWriteData (BaseBuffer< uint8_t > &data)
 write reverse fft result to buffer to make it available for readBytes
bool updateMelFilterbank (int n_bins, float min_freq, float max_freq)
 (Re)builds the cached sparse filterbank if any parameter changed
template<typename T>
T windowedSample (T sample, int pos)
template<typename T>
void writeIFFT (float *data, int len)
bool writeStrideBuffer (uint8_t *buffer, size_t len)

Static Protected Member Functions

static float hzToMel (float hz)
static float melToHz (float mel)

Protected Attributes

int _timeout = 10
int bins = 0
AudioFFTConfig cfg
int current_pos = 0
bool has_rfft_data = false
AudioInfo info
bool is_notify_active = true
Vector< float > l_magnitudes {0}
Vector< float > mel_bins {0}
Vector< MelFilter > mel_filters {0}
int mel_length = 0
float mel_max_freq = -1.0f
float mel_min_freq = -1.0f
int mel_n_bins = 0
int mel_sample_rate = 0
Vector< float > mel_weights {0}
Vector< AudioInfoSupport * > notify_vector
FFTDriver * p_driver = nullptr
FFTInverseOverlapAdder rfft_add {0}
RingBuffer< uint8_t > rfft_data {0}
Vector< float > step_data {0}
SingleBuffer< uint8_t > stride_buffer {0}
unsigned long timestamp = 0l
unsigned long timestamp_begin = 0l
RingBuffer< uint8_t > tmp_in {0}
RingBuffer< uint8_t > tmp_out {0}
int write_buffer_size = 8

Detailed Description

Executes FFT using audio data privded by write() and/or an inverse FFT where the samples are made available via readBytes(). The Driver which is passed in the constructor selects a specifc FFT implementation.

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ AudioFFTBase() [1/2]

AudioFFTBase ( FFTDriver * driver)
inline

Default Constructor. The len needs to be of the power of 2 (e.g. 512, 1024, 2048, 4096, 8192) Takes ownership of the driver (allocated with new), which is deleted together with this object

◆ AudioFFTBase() [2/2]

AudioFFTBase ( const AudioFFTBase & )
delete

◆ ~AudioFFTBase()

~AudioFFTBase ( )
inline

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() [1/2]

◆ audioInfoOut() [2/2]

◆ available()

int available ( )
inlineoverridevirtual

Data available for reverse fft.

Reimplemented from BaseStream.

◆ availableForWrite()

int availableForWrite ( )
inlineoverridevirtual

We try to fill the buffer at once.

Reimplemented from BaseStream.

◆ begin() [1/2]

bool begin ( )
inlineoverridevirtual

starts the processing

Reimplemented from BaseStream.

◆ begin() [2/2]

bool begin ( AudioFFTConfig info)
inline

starts the processing

◆ bytesPerSample()

int bytesPerSample ( )
inlineprotected

bytes per sample in the audio data: 24 bit samples use the int24_t representation (4 bytes unless USE_3BYTE_INT24 is defined)

◆ clearBins()

void clearBins ( )
inline

clears the fft data

◆ clearNotifyAudioChange() [1/2]

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

◆ clearNotifyAudioChange() [2/2]

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

◆ config()

AudioFFTConfig & config ( )
inline

Provides the actual configuration.

◆ defaultConfig()

AudioFFTConfig defaultConfig ( RxTxMode mode = TX_MODE)
inline

Provides the default configuration.

◆ driver()

FFTDriver * driver ( )
inline

provides access to the FFTDriver which implements the basic FFT functionality

◆ end()

void end ( )
inlineoverridevirtual

Release the allocated memory.

Reimplemented from BaseStream.

◆ fft()

template<typename T>
void fft ( )
inlineprotected

◆ flush() [1/2]

◆ flush() [2/2]

◆ frequency()

float frequency ( int bin)
inline

Determines the frequency of the indicated bin.

◆ frequencyToBin()

int frequencyToBin ( int freq)
inline

Determine the bin number from the frequency.

◆ fromMEL()

bool fromMEL ( float * values,
int n_bins,
float min_freq = 0.0f,
float max_freq = 0.0f )
inline

Convert MEL spectrum back to linear frequency spectrum, using the same (cached) filterbank as toMEL().

Parameters
valuesPointer to MEL spectrum values
n_binsNumber of MEL bins
Returns
bool Success status

◆ getBin()

bool getBin ( int pos,
FFTBin & bin )
inline

gets the value of a bin

◆ hzToMel()

float hzToMel ( float hz)
inlinestaticprotected

◆ insertSorted()

template<int N>
void insertSorted ( AudioFFTResult(&) result[N],
AudioFFTResult tmp )
inlineprotected

make sure that we do not reuse already found results

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

◆ isPowerOfTwo()

bool isPowerOfTwo ( uint16_t x)
inlineprotected

◆ length()

int length ( )
inline

The number of samples.

◆ magnitude()

float magnitude ( int bin)
inline

Calculates the magnitude of the fft result to determine the max value (bin is 0 to size())

◆ magnitudeFast()

float magnitudeFast ( int bin)
inline

◆ magnitudes()

float * magnitudes ( )
inline

Provides the magnitudes as array of size size(). Please note that this method is allocating additinal memory!

◆ magnitudesFast()

float * magnitudesFast ( )
inline

Provides the magnitudes w/o calling the square root function as array of size size(). Please note that this method is allocating additinal memory!

◆ melToHz()

float melToHz ( float mel)
inlinestaticprotected

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

operator bool ( )
inlineoverridevirtual

Reimplemented from AudioStream.

◆ operator=()

AudioFFTBase & operator= ( const AudioFFTBase & )
delete

◆ phase()

float phase ( int bin)
inline

calculates the phase

◆ processSamples()

template<typename T>
void processSamples ( const void * data,
size_t count )
inlineprotected

◆ processStrideBuffer()

template<typename T>
void processStrideBuffer ( )
inlineprotected

FFT of the full stride buffer, then drop stride samples.

◆ readBytes()

size_t readBytes ( uint8_t * data,
size_t len )
inlineoverridevirtual

Provides the result of a reverse FFT.

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.

◆ reference()

template<typename T>
T & reference ( )
inline

Provides the reference pointer.

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

◆ reset()

void reset ( )
inline

Just resets the current_pos e.g. to start a new cycle.

◆ result()

AudioFFTResult result ( )
inline

Determines the result values in the max magnitude bin.

◆ resultArray()

template<int N>
void resultArray ( AudioFFTResult(&) result[N])
inline

Determines the N biggest result values.

◆ resultTime()

unsigned long resultTime ( )
inline

time after the fft: time when the last result was provided - you can poll this to check if we have a new result

◆ resultTimeBegin()

unsigned long resultTimeBegin ( )
inline

time before the fft

◆ rfft()

void rfft ( )
inlineprotected

reverse fft

◆ rfftWriteData()

void rfftWriteData ( BaseBuffer< uint8_t > & data)
inlineprotected

write reverse fft result to buffer to make it available for readBytes

◆ setAudioInfo() [1/2]

◆ setAudioInfo() [2/2]

void setAudioInfo ( AudioInfo info)
inlineoverride

Notify change of audio information.

◆ setBin() [1/2]

bool setBin ( int idx,
float real,
float img )
inline

sets the value of a bin

◆ setBin() [2/2]

bool setBin ( int pos,
FFTBin & bin )
inline

sets the value of a bin

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

◆ setWriteBufferSize() [1/2]

void setWriteBufferSize ( int size)
inlineinherited

◆ setWriteBufferSize() [2/2]

void setWriteBufferSize ( int size)
inlineinherited

◆ size()

int size ( )
inline

The number of bins used by the FFT which are relevant for the result.

◆ toMEL()

float * toMEL ( int n_bins,
float min_freq = 0.0f,
float max_freq = 0.0f,
bool power = false )
inline

Convert the FFT result to MEL spectrum. The triangular filterbank uses fractional (HTK style) weights, so no filter collapses to zero width, and it is computed once and cached until the parameters change.

Parameters
n_binsnumber of mel bands (<= 0: size())
min_freqlower edge in Hz (<= 0: frequency(0))
max_frequpper edge in Hz (<= 0: frequency(size() - 1))
poweruse the power spectrum |X|^2 instead of the magnitude |X|

◆ updateMelFilterbank()

bool updateMelFilterbank ( int n_bins,
float min_freq,
float max_freq )
inlineprotected

(Re)builds the cached sparse filterbank if any parameter changed

◆ windowedSample()

template<typename T>
T windowedSample ( T sample,
int pos )
inlineprotected

◆ write() [1/2]

◆ write() [2/2]

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

Provide the audio data as FFT input.

Reimplemented from AudioStream.

◆ writeIFFT()

template<typename T>
void writeIFFT ( float * data,
int len )
inlineprotected

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

◆ writeStrideBuffer()

bool writeStrideBuffer ( uint8_t * buffer,
size_t len )
inlineprotected

Member Data Documentation

◆ _timeout

int _timeout = 10
protectedinherited

◆ bins

int bins = 0
protected

◆ cfg

AudioFFTConfig cfg
protected

◆ current_pos

int current_pos = 0
protected

◆ has_rfft_data

bool has_rfft_data = false
protected

◆ info

AudioInfo info
protectedinherited

◆ is_notify_active

bool is_notify_active = true
protectedinherited

◆ l_magnitudes

Vector<float> l_magnitudes {0}
protected

◆ mel_bins

Vector<float> mel_bins {0}
protected

◆ mel_filters

Vector<MelFilter> mel_filters {0}
protected

◆ mel_length

int mel_length = 0
protected

◆ mel_max_freq

float mel_max_freq = -1.0f
protected

◆ mel_min_freq

float mel_min_freq = -1.0f
protected

◆ mel_n_bins

int mel_n_bins = 0
protected

◆ mel_sample_rate

int mel_sample_rate = 0
protected

◆ mel_weights

Vector<float> mel_weights {0}
protected

◆ notify_vector

Vector< AudioInfoSupport * > notify_vector
protectedinherited

◆ p_driver

FFTDriver* p_driver = nullptr
protected

◆ rfft_add

FFTInverseOverlapAdder rfft_add {0}
protected

◆ rfft_data

RingBuffer<uint8_t> rfft_data {0}
protected

◆ step_data

Vector<float> step_data {0}
protected

◆ stride_buffer

SingleBuffer<uint8_t> stride_buffer {0}
protected

◆ timestamp

unsigned long timestamp = 0l
protected

◆ timestamp_begin

unsigned long timestamp_begin = 0l
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 = 8
protectedinherited

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