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_rateThe desired sample rate in Hz
[in]channelsNumber 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]bufferPointer to the sample buffer to store the data. 16-bit channel data is stored consecutively, starting with ch0
[in]n_framesThe 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
trueAll samples requested from microphone are ready.
falseSamples 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_levelThe calculated sound level
[in]bufferBuffer to calculate sound level from. Must contain 16-bit samples, starting with channel 0
[in]n_framesNumber of audio frames to use when calculating sound level
[in]channelThe channel to get the sound level for
Return values
SL_STATUS_OKSuccess
SL_STATUS_INVALID_PARAMETERInvalid channel num
SL_STATUS_BUSYSample buffer is currently filling with samples