This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Fixed Sound Microphone Sample Size

Hi, I am trying to use the Sound service's microphone to turn on every 1000 ms, check if the audio input is above a certain decibel level and turns on the LED if it is and turns of the LED if it isn't. After this, I am looking to turn off the microphone after this check.

Right now, I perform both the conversion to decibels and check in the drv_mic_data_handler function in m_sound.c. In my main.c, I have a timer running to execute this function every 1000ms.

static void mic_in_handler(void * p_context)
{
    uint32_t err_code;

    err_code = drv_mic_start();
    APP_ERROR_CHECK(err_code);

    nrf_delay_ms(100);
    err_code = drv_mic_stop();
    APP_ERROR_CHECK(err_code);
}

If I remove the last 3 lines of the function, then the LED lights up whenever the audio input is loud enough and turns off whenever the audio input is quiet enough since the mic is constantly active, as expected. However, I want to only check the decibel levels every 1000ms, and when the last 3 lines are included, the LED lights do not light up even if I am feeding loud enough input constantly for 5000ms. Does anyone know how to fix this?

I'm wondering if there is any way to use a fixed sample size for each m_audio_frame_t's data. Perhaps this could help me solve this problem.

Any help would be appreciated. Thanks in advance!

  • Hi.

    Sorry about the delay.

    I assume that you are working with the Thingy:52 and the Thingy SKD?

    Are you having trouble implementing the timer to check the decibel level every 1000ms?
    Have you tried debugging? To see what happens in the code?

    I general I would not recommend using nrf_delay, as this halts the MCU.

    Best regards,
    Joakim

  • Yes, I am working with Thingy52 and the Thingy SDK.

    I am having trouble implementing the timer to check the decibel level every 1000ms. Whenever I include the line with drv_mic_stop() at the end of my mic_in_handler function, the LED does not light up at all when I am feeding loud enough noise into the microphone. I currently do not have a formal debugger set up but I have done some debugging using the LED toggle.

    mic_in_handler is running on a timer that activates every 1000ms. My code is as follows:

    main.c

    /**@brief Function for timer event handling of reading in mic input.
     */
    static void mic_in_handler(void * p_context)
    {
        int32_t err_code = NRF_SUCCESS;

        err_code = drv_mic_start();
        APP_ERROR_CHECK(err_code);

       
        err_code = drv_mic_stop();
        APP_ERROR_CHECK(err_code);


        /* check if audio input is >= 40 dB */
        if (db_global >= AUDIO_THRESHOLD_DB) {
            nrf_gpio_pin_set(ANA_DIG0); /* set pin (effectively LED) for debugging */
        } else {
            nrf_gpio_pin_clear(ANA_DIG0); /* clear pin (effectively LED) for debugging */
        }
    }

    m_sound.c

    static uint32_t drv_mic_data_handler(m_audio_frame_t * p_frame)
    {
        int i = 0, sum = 0;
        double avg = 0.0;

        /* get audio dB level */
        for (i = 0; i < p_frame->data_size; i++) {
            sum += p_frame->data[i];
        }

        avg = (double)sum / (double)p_frame->data_size;
        db_global = (double)20 * log10(avg);

        return NRF_SUCCESS;
    }

Related