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

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

sl_mic_start()

sl_status_t sl_mic_start ( void )

Start the microphone.

Return values
SL_STATUS_OK

sl_mic_stop()

sl_status_t sl_mic_stop ( void )

Stop the microphone.

Return values
SL_STATUS_OK

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
SL_STATUS_BUSY Sample buffer is currently filling with samples