Audio Feature Generator#

Extracts mel-filterbank features from an audio signal to use with machine learning audio classification applications.

Feature Generation#

The Mel scale replicates the behavior of the human ear, which has a higher resolution for lower frequencies and is less discriminative of the higher frequencies. To create a mel filterbank, a number of filters are applied to the signal, where the pass-band of the lower channel filters is narrow and increases towards higher frequencies.

The audio signal is split into short overlapping segments using a window function (Hamming). The Fast Fourier Transform (FFT) is applied to each segment to retrieve the frequency spectrum and then the power spectrum of the segment. The filterbank is created by applying a series of mel-scaled filters to the output. Finally, the log is applied to the output to increase the sensitivity between the lower channels.

         Audio Signal
              │
      ┌───────▼───────┐
      │   Windowing   │
      └───────┬───────┘
              │
      ┌───────▼───────┐
      │      FFT      │
      └───────┬───────┘
              │
      ┌───────▼───────┐
      │  Mel Filters  │
      └───────┬───────┘
              │
      ┌───────▼───────┐
      │      Log      │
      └───────┬───────┘
              │
              ▼
   Log-scaled Mel Filterbank

The feature array is generated by stacking filterbanks of sequential segments together to form a spectrogram. The array is sorted such that the first element is the first channel of the oldest filterbank.

The shared API is declared in sl_fe_audio.h. SiWx917 builds include sl_fe_audio_si91x.h, which pulls in this header and adds platform-only APIs. Both share the sl_fe_audio_config_t configuration exported by the fe_audio component.

Usage#

sl_ml_audio_feature_generation_init() initializes the frontend from a per-model sl_fe_audio_config_t. It also initializes and starts the microphone in streaming mode, which places the audio samples into a ring buffer sized from config->audio_buffer_size.

Frontend parameters come from the per-model configuration that the fe_audio component exports (typically sl_fe_audio_<instance>_cfg in sl_fe_audio_instances.h, autogenerated from the TensorFlow Lite flatbuffer metadata produced by the Silicon Labs converter toolchain). Pass &sl_fe_audio_<instance>_cfg so that the microfrontend, sample rate, ring buffer sizing, and quantization options match that model.

The features are generated when sl_ml_audio_feature_generation_update_features() is called. The feature generator then updates the features for as many new segments of audio as possible, starting from the last time the function was called up until the current time. The new features are appended to the feature buffer, replacing the oldest features such that the feature array always contains the most up to date features.

Note that if the audio buffer is not large enough to hold all audio samples required to generate features between calls to sl_ml_audio_feature_generation_update_features(), audio data will simply be overwritten. The generator will not return an error. The audio buffer must therefore be configured to be large enough to store all new sampled data between updating features.

To retrieve the generated features, either sl_ml_audio_feature_generation_get_features_raw() or sl_ml_audio_feature_generation_fill_tensor() must be called. SiWx917 additionally provides float getters, listed with the rest of the SiWx917 additions below.

Note that updating features and retrieving them can be performed independently. Updating features should be done often enough to avoid overwriting the audio buffer while retrieving them only needs to be done prior to inference.

Example#

When used with TensorFlow Lite Micro, the audio feature generator can be used to fill a tensor directly by using sl_ml_audio_feature_generation_fill_tensor(). However, the model has to be trained using the same feature generator configuration as used for inference, which is what the generated sl_fe_audio_config_t for the deployed .tflite provides.

#include "sl_tflite_micro_init.h"
#include "sl_fe_audio_instances.h"
#include "sl_fe_audio.h"          // or sl_fe_audio_si91x.h on SiWx917

void main(void)
{
  sl_ml_audio_feature_generation_init(&sl_fe_audio_my_model_cfg);

  while(1){
    sl_ml_audio_feature_generation_update_features();

    if(do_inference){
      sl_ml_audio_feature_generation_fill_tensor(sl_tflite_micro_get_input_tensor());
      sl_tflite_micro_get_interpreter()->Invoke();
    }

    ...

  }
}

Modules#

sl_fe_audio_config

Feature generation API#

Core feature generation API, available on both EFR32 Series 2 and SiWx917 (via sl_fe_audio_si91x.h) with the same signatures. The pipeline, configuration, and usage are described above.

sl_status_t

Initialize audio feature generation using the default instance config.

sl_status_t
sl_ml_audio_feature_generation_init(const sl_fe_audio_config_t *config)

Set up the microphone as an audio source for feature generation and initialize the frontend for feature generation.

sl_status_t

Tear down the audio feature pipeline (mic, internal buffers, frontend state).

sl_status_t

Initialize the microfrontend using the default instance config.

sl_status_t
sl_ml_audio_feature_generation_frontend_init(const sl_fe_audio_config_t *config)

Initialize microfrontend and feature/audio ring buffers from config.

sl_status_t

Update the feature buffer with the missing feature slices since the last call to this function.

sl_status_t
sl_ml_audio_feature_generation_get_features_raw(uint16_t *buffer, size_t num_elements)

Retrieve the features as type uint16 and copy them to the provided buffer.

sl_status_t
sl_ml_audio_feature_generation_fill_tensor(TfLiteTensor *input_tensor)

Fill a TensorFlow tensor with feature data of type int8.

int

Return the number of new or unfetched feature slices that have been updated since the last call to sl_ml_audio_feature_generation_get_features_raw or sl_ml_audio_feature_generation_fill_tensor.

int
void

Reset the state of the audio feature generator.

bool

Load frontend settings from a TensorFlow Lite flatbuffer.

SiWx917 additions#

API available only on SiWx917, declared in sl_fe_audio_si91x.h: caller-provided audio buffers, additional feature getters, activity detection, and a microphone PCM callback. On SiWx917 the audio ring buffer is heap-allocated from config->audio_buffer_size unless the caller supplies one through sl_ml_audio_feature_generation_init_with_buffer.Single pipeline (multi-instance configs)The fe_audio component may define several sl_fe_audio_<instance>_cfg symbols (one per model), but only one pipeline may be active: this implementation uses one set of global buffers and one FrontendState at a time. After validating the new parameters, sl_ml_audio_feature_generation_init and sl_ml_audio_feature_generation_init_with_buffer tear down an existing pipeline (same as sl_ml_audio_feature_generation_deinit) and then bring up the new config, so callers can switch models with a single init call.

typedef void(*
sl_ml_audio_feature_generation_mic_callback_t)(void *arg, const int16_t *data, uint32_t n_frames)

Microphone callback function type for audio feature generation.

int16_t *

External audio buffer for audio feature generation (mic / DMA ring backing store).

sl_status_t
sl_ml_audio_feature_generation_init_with_buffer(int16_t *buffer, int n_frames, const sl_fe_audio_config_t *config)

Initialize audio feature generation with a user-provided buffer.

sl_status_t

Copy the active spectrogram ring to a float32 buffer.

sl_status_t
sl_ml_audio_feature_generation_get_features_scaled(float *buffer, size_t num_elements, float scaler)

Copy the active spectrogram to buffer with each sample multiplied by scaler.

sl_status_t

Mean- and standard-deviation-normalized copy of the active spectrogram into buffer.

sl_status_t

Query whether the activity-detection block has tripped since the last successful read.

bool

Return whether the runtime frontend config has activity detection enabled.

sl_status_t
sl_ml_audio_feature_generation_set_mic_callback(sl_ml_audio_feature_generation_mic_callback_t callback, void *arg)

Register the optional microphone PCM callback (or clear it).

Feature generation API Documentation#

sl_fe_audio_config_t#

typedef struct sl_fe_audio_config sl_fe_audio_config_t

sl_ml_audio_feature_generation_init#

sl_status_t sl_ml_audio_feature_generation_init (void )

Initialize audio feature generation using the default instance config.

Parameters
TypeDirectionArgument NameDescription
voidN/A

Compatibility wrapper for aiml 3.0.0 applications that called init with no arguments. Uses sl_fe_audio_get_default_config() internally. Declared in sl_ml_audio_feature_generation_config.h.

DeprecatedUse sl_ml_audio_feature_generation_init with &sl_fe_audio_<instance>_cfg instead.

Returns


sl_ml_audio_feature_generation_init#

sl_status_t sl_ml_audio_feature_generation_init (const sl_fe_audio_config_t * config)

Set up the microphone as an audio source for feature generation and initialize the frontend for feature generation.

Parameters
TypeDirectionArgument NameDescription
const sl_fe_audio_config_t *[in]config

Frontend, buffer, and quantization parameters for this pipeline.

If a pipeline is already active, it is torn down first (same as sl_ml_audio_feature_generation_deinit). A non-OK status from that step is the return value from the microphone driver during deinit.

Note

Returns

  • SL_STATUS_OK for success SL_STATUS_INVALID_PARAMETER config is NULL or contains invalid frontend parameters SL_STATUS_ALLOCATION_FAILED the audio or feature ring could not be allocated SL_STATUS_FAIL coarse validation failed (sample rate, window step/size, segment length), or the microfrontend state could not be populated

Other non-OK statuses may come from the microphone driver during teardown or bring-up, in which case allocated resources are released before returning.


sl_ml_audio_feature_generation_deinit#

sl_status_t sl_ml_audio_feature_generation_deinit (void )

Tear down the audio feature pipeline (mic, internal buffers, frontend state).

Parameters
TypeDirectionArgument NameDescription
voidN/A

Idempotent: returns SL_STATUS_OK if the pipeline was already inactive. Frees the mic DMA buffer allocated by sl_ml_audio_feature_generation_init and the internal audio/feature ring buffers allocated by sl_ml_audio_feature_generation_frontend_init.

Teardown is best-effort: internal buffers and frontend state are always released after the microphone deinit is attempted. If the microphone returns a non-OK status, the pipeline is still marked inactive and FE memory is freed; the caller may need to handle or log the mic status separately.

Note

Returns

  • If the pipeline was active and the microphone deinit returns a value other than SL_STATUS_OK, that status is returned (feature-buffer and frontend teardown may still have completed).

Return values

  • SL_STATUS_OK: The microphone deinitialized cleanly, or the pipeline was already inactive.


sl_ml_audio_feature_generation_frontend_init#

sl_status_t sl_ml_audio_feature_generation_frontend_init (void )

Initialize the microfrontend using the default instance config.

Parameters
TypeDirectionArgument NameDescription
voidN/A

Compatibility wrapper for aiml 3.0.0 applications that called frontend init with no arguments. Uses sl_fe_audio_get_default_config() internally. Declared in sl_ml_audio_feature_generation_config.h.

DeprecatedUse sl_ml_audio_feature_generation_frontend_init with &sl_fe_audio_<instance>_cfg instead.

Returns


sl_ml_audio_feature_generation_frontend_init#

sl_status_t sl_ml_audio_feature_generation_frontend_init (const sl_fe_audio_config_t * config)

Initialize microfrontend and feature/audio ring buffers from config.

Parameters
TypeDirectionArgument NameDescription
const sl_fe_audio_config_t *[in]config

Frontend, buffer, and quantization parameters for this pipeline.

Note

  • On SiWx917 the audio ring must already be set up; the call returns SL_STATUS_NOT_INITIALIZED otherwise.

Returns

  • SL_STATUS_OK for success SL_STATUS_INVALID_PARAMETER config is NULL, zero audio_buffer_size, zero filterbank channels, or window sizing overflow SL_STATUS_ALLOCATION_FAILED the feature ring could not be allocated SL_STATUS_FAIL inconsistent sample rate / window / segment fields


sl_ml_audio_feature_generation_update_features#

sl_status_t sl_ml_audio_feature_generation_update_features ()

Update the feature buffer with the missing feature slices since the last call to this function.

To retrieve the features, call sl_ml_audio_feature_generation_get_features_raw or sl_ml_audio_feature_generation_fill_tensor.

Note

  • This function needs to be called often enough to ensure that the audio buffer isn't overwritten.

Returns

  • SL_STATUS_OK for success SL_STATUS_EMPTY No new slices were calculated SL_STATUS_NOT_INITIALIZED (SiWx917) audio ring, feature buffer, or frontend config is not ready


sl_ml_audio_feature_generation_get_features_raw#

sl_status_t sl_ml_audio_feature_generation_get_features_raw (uint16_t * buffer, size_t num_elements)

Retrieve the features as type uint16 and copy them to the provided buffer.

Parameters
TypeDirectionArgument NameDescription
uint16_t *[out]buffer

Pointer to the buffer to store the feature data

size_t[in]num_elements

The number of elements corresponding to the size of the buffer; If this is not large enough to store the entire feature buffer the function will return with an error. On SiWx917 it must equal sl_ml_audio_feature_generation_get_feature_buffer_size exactly.

Note

Returns

  • SL_STATUS_OK for success SL_STATUS_INVALID_PARAMETER num_elements too small SL_STATUS_NOT_INITIALIZED (SiWx917) feature buffer is not allocated


sl_ml_audio_feature_generation_fill_tensor#

sl_status_t sl_ml_audio_feature_generation_fill_tensor (TfLiteTensor * input_tensor)

Fill a TensorFlow tensor with feature data of type int8.

Parameters
TypeDirectionArgument NameDescription
TfLiteTensor *[in]input_tensor

The input tensor to fill with features.

The int8 values are derived by quantizing the microfrontend output, expected to be in the range 0 to 670, to signed integer numbers in -128 to 127 range.

Note

  • This function overwrites the entire input tensor.

  • Supports tensors of type kTfLiteInt8. SiWx917 additionally supports kTfLiteUInt16 (raw spectrogram) and kTfLiteFloat32 (raw, scaled, or mean/std-normalized float, per the active sl_fe_audio_config_t).

Returns

  • SL_STATUS_OK for success SL_STATUS_INVALID_PARAMETER Tensor type or size does not correspond with configuration SL_STATUS_NOT_INITIALIZED (SiWx917) feature buffer is not allocated


sl_ml_audio_feature_generation_get_new_feature_slice_count#

int sl_ml_audio_feature_generation_get_new_feature_slice_count ()

Return the number of new or unfetched feature slices that have been updated since the last call to sl_ml_audio_feature_generation_get_features_raw or sl_ml_audio_feature_generation_fill_tensor.

Returns

  • The number of unfetched feature slices, or 0 if none are pending or the pipeline is not initialized.


sl_ml_audio_feature_generation_get_feature_buffer_size#

int sl_ml_audio_feature_generation_get_feature_buffer_size ()

Return the feature buffer size.

Returns

  • Number of uint16 feature elements in the active spectrogram ring, or 0 if the feature buffer is not initialized.


sl_ml_audio_feature_generation_reset#

void sl_ml_audio_feature_generation_reset ()

Reset the state of the audio feature generator.


sl_ml_audio_feature_generation_load_model_settings#

bool sl_ml_audio_feature_generation_load_model_settings (const void * tflite_flatbuffer)

Load frontend settings from a TensorFlow Lite flatbuffer.

Parameters
TypeDirectionArgument NameDescription
const void *[in]tflite_flatbuffer

Pointer to a serialized model flatbuffer (ignored by current replacements).

Legacy helper for aiml 3.0.0 applications. Frontend parameters are now supplied by the fe_audio component via sl_fe_audio_<instance>_config.h. Declared in sl_ml_audio_feature_generation_config.h.

DeprecatedRemove calls to this function. Use SL_FE_* macros in sl_fe_audio_<instance>_config.h instead. For activity detection, call sl_ml_audio_feature_generation_is_activity_detection_enabled rather than reading SL_ML_FRONTEND_ACTIVITY_DETECTION_ENABLE.

Returns

  • Implementation-defined status for legacy callers.


SiWx917 additions Documentation#

sl_ml_audio_feature_generation_mic_callback_t#

typedef void(* sl_ml_audio_feature_generation_mic_callback_t) (void *arg, const int16_t *data, uint32_t n_frames) )(void *arg, const int16_t *data, uint32_t n_frames)

Microphone callback function type for audio feature generation.

Parameters
TypeDirectionArgument NameDescription
[in]arg

User-defined argument passed to the callback.

[in]data

Pointer to the audio data buffer.

[in]n_frames

Number of audio frames in the buffer.


sl_ml_audio_feature_generation_audio_buffer#

int16_t* sl_ml_audio_feature_generation_audio_buffer

External audio buffer for audio feature generation (mic / DMA ring backing store).


sl_ml_audio_feature_generation_init_with_buffer#

sl_status_t sl_ml_audio_feature_generation_init_with_buffer (int16_t * buffer, int n_frames, const sl_fe_audio_config_t * config)

Initialize audio feature generation with a user-provided buffer.

Parameters
TypeDirectionArgument NameDescription
int16_t *[in]buffer

Pointer to the audio buffer to use for feature generation.

int[in]n_frames

Number of int16 audio samples in the buffer; must equal config->audio_buffer_size.

const sl_fe_audio_config_t *[in]config

Per-model frontend and buffer parameters; must not be NULL.

If a pipeline is already active, it is torn down first (same as sl_ml_audio_feature_generation_deinit). A non-OK status from that step is the return value from sl_ml_mic_deinit() during deinit.

After the frontend is initialized, failures from sl_ml_mic_init, sl_ml_mic_start_streaming, or sl_ml_mic_process_action return that driver sl_status_t after cleaning up allocated resources.

Returns

  • Other non-OK sl_status_t values may be returned from the microphone / I2S driver during teardown or bring-up.

Return values

  • SL_STATUS_OK: Success.

  • SL_STATUS_INVALID_PARAMETER: config

  • SL_STATUS_FAIL: Coarse validation failed (sample rate, window step/size, segment length), or microfrontend state cannot be populated.

  • SL_STATUS_ALLOCATION_FAILED: The feature ring buffer could not be allocated.


sl_ml_audio_feature_generation_get_features_raw_float32#

sl_status_t sl_ml_audio_feature_generation_get_features_raw_float32 (float * buffer, size_t num_elements)

Copy the active spectrogram ring to a float32 buffer.

Parameters
TypeDirectionArgument NameDescription
float *[out]buffer

Pointer to the buffer to store the feature data.

size_t[in]num_elements

Must equal the active feature element count (sl_ml_audio_feature_generation_get_feature_buffer_size).

Note

  • Overwrites the entire buffer.


sl_ml_audio_feature_generation_get_features_scaled#

sl_status_t sl_ml_audio_feature_generation_get_features_scaled (float * buffer, size_t num_elements, float scaler)

Copy the active spectrogram to buffer with each sample multiplied by scaler.

Parameters
TypeDirectionArgument NameDescription
float *[out]buffer

Pointer to the buffer to store the scaled feature data.

size_t[in]num_elements

Must equal the active feature element count (sl_ml_audio_feature_generation_get_feature_buffer_size).

float[in]scaler

Scaling factor applied to each feature value (must be non-zero).

Computes buffer[i] = (float)feature[i] * scaler for each element in the ring.

Note

  • Overwrites the entire buffer.


sl_ml_audio_feature_generation_get_features_mean_std_normalized#

sl_status_t sl_ml_audio_feature_generation_get_features_mean_std_normalized (float * buffer, size_t num_elements)

Mean- and standard-deviation-normalized copy of the active spectrogram into buffer.

Parameters
TypeDirectionArgument NameDescription
float *[out]buffer

Pointer to the buffer to store the normalized feature data.

size_t[in]num_elements

Must equal the active feature element count (sl_ml_audio_feature_generation_get_feature_buffer_size).

For each element: (float(value) - mean) / std. Near-constant input yields zeros in buffer.

Note

  • Overwrites the entire buffer.


sl_ml_audio_feature_generation_activity_detected#

sl_status_t sl_ml_audio_feature_generation_activity_detected ()

Query whether the activity-detection block has tripped since the last successful read.

Call sl_ml_audio_feature_generation_update_features periodically so the detector advances on new audio.

Note


sl_ml_audio_feature_generation_is_activity_detection_enabled#

bool sl_ml_audio_feature_generation_is_activity_detection_enabled (void )

Return whether the runtime frontend config has activity detection enabled.

Parameters
TypeDirectionArgument NameDescription
voidN/A

After sl_ml_audio_feature_generation_init, reflects the active sl_fe_audio_config_t.


sl_ml_audio_feature_generation_set_mic_callback#

sl_status_t sl_ml_audio_feature_generation_set_mic_callback (sl_ml_audio_feature_generation_mic_callback_t callback, void * arg)

Register the optional microphone PCM callback (or clear it).

Parameters
TypeDirectionArgument NameDescription
sl_ml_audio_feature_generation_mic_callback_t[in]callback

Function pointer (may be NULL to clear).

void *[in]arg

User argument passed to callback.