Microphone
Description
Sound level driver for PDM and I2S microphones.
Microphone example code
Basic example for looping measurement of sound level:
#include "sl_mic.h" #define MIC_SAMPLE_RATE 44100 #define MIC_SAMPLE_BUFFER_SIZE 1024 #define MIC_N_CHANNELS 2 static int16_t buffer[MIC_SAMPLE_BUFFER_SIZE * MIC_N_CHANNELS]; int main( void ) { ... float sound_level_0; float sound_level_1; uint32_t n_samples = 1024; // Initialize microphone with sample rate and number of channels sl_mic_init(MIC_SAMPLE_RATE, MIC_N_CHANNELS); while(true){ // Read samples from the microphone sl_mic_get_n_samples(buffer, n_samples); while (!sl_mic_sample_buffer_ready()) { // Wait until sample buffer ready } // Calculate sound level sl_mic_calculate_sound_level(&sound_level_0, buffer, n_samples, 0); sl_mic_calculate_sound_level(&sound_level_1, buffer, n_samples, 1); } ... }
Functions |
|
sl_status_t | sl_mic_init (uint32_t sample_rate, uint8_t channels) |
Initialize the microphone.
|
|
sl_status_t | sl_mic_deinit (void) |
De-initialize the microphone.
|
|
sl_status_t | sl_mic_get_n_samples (void *buffer, uint32_t n_frames) |
Read samples from the microphone into a sample buffer.
|
|
sl_status_t | sl_mic_start_streaming (void *buffer, uint32_t n_frames, sl_mic_buffer_ready_callback_t callback) |
Read samples from the microphone into a sample buffer continuously.
|
|
sl_status_t | sl_mic_start (void) |
Start the microphone.
|
|
sl_status_t | sl_mic_stop (void) |
Stop the microphone.
|
|
bool | sl_mic_sample_buffer_ready (void) |
Check whether the sample buffer is ready.
|
|
sl_status_t | sl_mic_calculate_sound_level (float *sound_level, const int16_t *buffer, uint32_t n_frames, uint8_t channel) |
Calculate the dBSPL value for a channel from a sample buffer.
|
|
Typedefs |
|
typedef void(* | sl_mic_buffer_ready_callback_t ) (const void *buffer, uint32_t n_frames) |
Callback function indicating that the sample buffer is ready.
|
|
Function Documentation
◆ sl_mic_init()
sl_status_t sl_mic_init | ( | uint32_t |
sample_rate,
|
uint8_t |
channels
|
||
) |
Initialize the microphone.
- Parameters
-
[in] sample_rate
The desired sample rate in Hz [in] channels
Number of audio channels (1 or 2)
- Returns
- Returns SL_STATUS_OK on success, non-zero otherwise
◆ sl_mic_deinit()
sl_status_t sl_mic_deinit | ( | void |
|
) |
De-initialize the microphone.
- Return values
-
SL_STATUS_OK
◆ sl_mic_get_n_samples()
sl_status_t sl_mic_get_n_samples | ( | void * |
buffer,
|
uint32_t |
n_frames
|
||
) |
Read samples from the microphone into a sample buffer.
This function starts the microphone sampling and stops the sampling after reading the desired number of samples. Call sl_mic_sample_buffer_ready to check when the samples are ready in the buffer.
- Parameters
-
[in] buffer
Pointer to the sample buffer to store the data. 16-bit channel data is stored consecutively, starting with ch0 [in] n_frames
The number of the audio frames to get
- Return values
-
SL_STATUS_OK
Success. SL_STATUS_NOT_INITIALIZED
Not initialized. SL_STATUS_INVALID_STATE
Sampling is already in progress.
◆ sl_mic_start_streaming()
sl_status_t sl_mic_start_streaming | ( | void * |
buffer,
|
uint32_t |
n_frames,
|
||
sl_mic_buffer_ready_callback_t |
callback
|
||
) |
Read samples from the microphone into a sample buffer continuously.
This function starts the microphone sampling and stops only upon calling sl_mic_stop or sl_mic_deinit . The buffer is used in a "ping-pong" manner meaning that one half of the buffer is used for sampling while the other half is being processed.
- Parameters
-
[in] buffer
Pointer to the sample buffer to store the data. 16-bit channel data is stored consecutively, starting with ch0. This buffer shall be big enough to hold twice the n_frames because of the ping-pong operation. [in] n_frames
The number of audio frames to receive before the callback is called. Maximum value limited by DMADRV_MAX_XFER_COUNT. [in] callback
Callback is called when n_frames in the sample buffer is ready.
- Return values
-
SL_STATUS_OK
Success. SL_STATUS_NOT_INITIALIZED
Not initialized. SL_STATUS_INVALID_STATE
Sampling is already in progress. SL_STATUS_INVALID_PARAMETER
n_frames too large.
◆ sl_mic_start()
sl_status_t sl_mic_start | ( | void |
|
) |
Start the microphone.
- Return values
-
SL_STATUS_OK
Success. SL_STATUS_NOT_INITIALIZED
Not initialized. SL_STATUS_INVALID_STATE
Microphone is already running.
◆ sl_mic_stop()
sl_status_t sl_mic_stop | ( | void |
|
) |
Stop the microphone.
- Return values
-
SL_STATUS_OK
Success. SL_STATUS_INVALID_STATE
Microphone is not running.
◆ sl_mic_sample_buffer_ready()
bool sl_mic_sample_buffer_ready | ( | void |
|
) |
Check whether the sample buffer is ready.
- Return values
-
true
All samples requested from microphone are ready. false
Samples are still being read into the buffer.
◆ sl_mic_calculate_sound_level()
sl_status_t sl_mic_calculate_sound_level | ( | float * |
sound_level,
|
const int16_t * |
buffer,
|
||
uint32_t |
n_frames,
|
||
uint8_t |
channel
|
||
) |
Calculate the dBSPL value for a channel from a sample buffer.
- Parameters
-
[out] sound_level
The calculated sound level [in] buffer
Buffer to calculate sound level from. Must contain 16-bit samples, starting with channel 0 [in] n_frames
Number of audio frames to use when calculating sound level [in] channel
The channel to get the sound level for
- Return values
-
SL_STATUS_OK
Success SL_STATUS_INVALID_PARAMETER
Invalid channel num
Typedef Documentation
◆ sl_mic_buffer_ready_callback_t
typedef void(* sl_mic_buffer_ready_callback_t) (const void *buffer, uint32_t n_frames) |
Callback function indicating that the sample buffer is ready.
- Parameters
-
[in] buffer
Pointer to the sample buffer. [in] n_frames
Number of audio frames in the sample buffer.
- Returns
- None.