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.
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.
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.
typedef enum {
...
TKL_MEDIA_DETECT_TYPE_BABY_CRY, // baby cry
TKL_MEDIA_DETECT_TYPE_DB, // audio decibel
...
} TKL_MEDIA_DETECT_TYPE_E;
typedef struct {
INT32_T val;
CHAR_T res[8];
} TKL_AUDIO_DETECT_RESULT_T; // DETECT result
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);
/**
* @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);
/**
* @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);
// 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);
When the detection result is not needed, call the API to stop the detection to lower CPU usage.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback