Audio Detection

Last Updated on : 2023-08-09 09:25:02download

Audio detection is used to measure the decibel level and detect baby cry.

  • Decibel detection: Capture audio frames and calculate the decibel level using software algorithms.

  • Cry detection: Analyze audio frames to determine if there is a baby cry.

How it works

Decibel detection

  • Capture 200 milliseconds of audio data and pass it to the detection algorithm.

  • Average the amplitudes of the audio frames to get the decibel value.

  • The business logic layer reads the result in real time. It is recommended to read the result every 40 to 200 milliseconds and compare it with the threshold. If the result exceeds the threshold, trigger a sound alarm.

Cry detection

  • Pass each audio frame to the cry detection queue.

  • The cry detection thread detects the audio frames in real time and updates the result. If a cry is detected, the thread updates the result and sleeps for 1 second to prevent loss of results.

  • The business logic layer reads the result in real time. It is recommended to read the result every 40 to 500 milliseconds. If the result is 1, it indicates that a cry is detected.

API call process

Audio Detection

Data structure

Detection type

typedef enum {
...
    TKL_MEDIA_DETECT_TYPE_BABY_CRY,                                     // baby cry
    TKL_MEDIA_DETECT_TYPE_DB,                                           // audio decibel
...
} TKL_MEDIA_DETECT_TYPE_E;

Detection result

typedef struct {
    INT32_T val;
    CHAR_T res[8];
} TKL_AUDIO_DETECT_RESULT_T; // DETECT result

API description

Start detection

The IPC only supports 1-channel AI currently, so specify card as 0.

/**
* @brief audio input detect start
*
* @param[in] card: card number
* @param[in] type: detect type
*
* @return OPRT_OK on success. Others on error, please refer to tkl_error_code.h
*/
OPERATE_RET tkl_ai_detect_start(INT32_T card, TKL_MEDIA_DETECT_TYPE_E type);

Get the detection result

/**
* @brief audio detect get result
*
* @param[in] card: card number
* @param[in] type: detect type
* @param[out] presult: audio detect result
*
* @return OPRT_OK on success. Others on error, please refer to tkl_error_code.h
*/
OPERATE_RET tkl_ai_detect_get_result(INT32_T card, TKL_MEDIA_DETECT_TYPE_E type, TKL_AUDIO_DETECT_RESULT_T *presult);

Stop detection

/**
* @brief audio input detect stop
*
* @param[in] card: card number
* @param[in] type: detect type
*
* @return OPRT_OK on success. Others on error, please refer to tkl_error_code.h
*/
OPERATE_RET tkl_ai_detect_stop(INT32_T card, TKL_MEDIA_DETECT_TYPE_E type);

Example

// Decibel detection:
     ret = tkl_ai_detect_start(0, TKL_MEDIA_DETECT_TYPE_DB);
    ...
    ret = tkl_ai_detect_get_result(0, TKL_MEDIA_DETECT_TYPE_DB, &db_value);
    ...
        // Determine whether the decibel exceeds the threshold. db_value.val is the decibel value.
        if (db_value.val >= phdl->sensitivity) {
            phdl->db_flag = 1;
        } else {
            phdl->db_flag = 0;
        }
    ...
    ret = tkl_ai_detect_stop(0, TKL_MEDIA_DETECT_TYPE_DB);

// Crying detection:
    ret = tkl_ai_detect_start(0, TKL_MEDIA_DETECT_TYPE_BABY_CRY);
    ...
    ret = tkl_ai_detect_get_result(0, TKL_MEDIA_DETECT_TYPE_BABY_CRY, (VOID*)&result);
    if (result.val == 1) {      //Baby crying is detected.
        TYDEBUG("find baby cry\n");
        ...
    }
    ...
    ret = tkl_ai_detect_stop(0, TKL_MEDIA_DETECT_TYPE_BABY_CRY);

Things to note

When the detection result is not needed, call the API to stop the detection to lower CPU usage.

FAQs

How to set the threshold for decibel detection?

  • For high sensitivity, set the threshold to 45, corresponding to 60 to 65 dB.
  • For low sensitivity, set the threshold to 65, corresponding to 75 to 80 dB.