Microphone#

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

   }

   ...

  } 

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.

Functions#

sl_status_t
sl_mic_init(uint32_t sample_rate, uint8_t channels)

Initialize the microphone.

sl_status_t

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

Start the microphone.

sl_status_t

Stop the microphone.

bool

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.

Typedef Documentation#

sl_mic_buffer_ready_callback_t#

typedef void(* sl_mic_buffer_ready_callback_t) (const void *buffer, uint32_t n_frames) )(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.


Definition at line 103 of file hardware/driver/mic/inc/sl_mic.h

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


Definition at line 118 of file hardware/driver/mic/inc/sl_mic.h

sl_mic_deinit#

sl_status_t sl_mic_deinit (void )

De-initialize the microphone.

Parameters
N/A

Definition at line 126 of file hardware/driver/mic/inc/sl_mic.h

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.

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

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.


Definition at line 148 of file hardware/driver/mic/inc/sl_mic.h

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.

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.

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.


Definition at line 178 of file hardware/driver/mic/inc/sl_mic.h

sl_mic_start#

sl_status_t sl_mic_start (void )

Start the microphone.

Parameters
N/A

Definition at line 188 of file hardware/driver/mic/inc/sl_mic.h

sl_mic_stop#

sl_status_t sl_mic_stop (void )

Stop the microphone.

Parameters
N/A

Definition at line 197 of file hardware/driver/mic/inc/sl_mic.h

sl_mic_sample_buffer_ready#

bool sl_mic_sample_buffer_ready (void )

Check whether the sample buffer is ready.

Parameters
N/A

Definition at line 206 of file hardware/driver/mic/inc/sl_mic.h

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


Definition at line 228 of file hardware/driver/mic/inc/sl_mic.h