← 返回文档
回调函数
结果回调
检测结果通过回调函数异步通知。
结果结构
typedef struct {
float time_sec; // 窗口起始时间(秒,从采集开始)
uint64_t timestamp_ms; // 绝对时间戳(毫秒)
float cry_prob; // 哭声概率 [0, 1]
int is_cry; // 二分类结果(cry_prob >= 0.5 → 1)
} cry_detect_result_t;回调类型
typedef void (*cry_detect_callback_t)(
const cry_detect_result_t *result,
void *user_data
);回调每 1 秒(处理步长间隔)触发一次。在回调中:
不要阻塞 — ——处理线程会等待回调返回
不要调用 `cry_detect_stop()` 或 `cry_detect_deinit()` — ——会导致死锁
• `result` 指针仅在回调调用期间有效——如需后续使用,请拷贝数据
示例
void my_callback(const cry_detect_result_t *result, void *user_data) {
my_context_t *ctx = (my_context_t *)user_data;
printf("[%.3f] cry_prob=%.4f is_cry=%d
",
result->time_sec, result->cry_prob, result->is_cry);
if (result->is_cry) {
ctx->cry_event_count++;
// 触发外部动作(LED、蜂鸣器、推送通知等)
}
}
// 注册回调
cry_detect_config_t config = {
.callback = my_callback,
.callback_data = &my_context,
};告警回调
告警策略将逐帧概率转换为更高级别的告警事件,减少抖动和误触发。
告警事件结构
typedef struct {
float start_time; // 事件起始时间(秒)
float end_time; // 事件结束时间(秒)
float confidence; // 段内平均概率
float max_confidence; // 段内最高概率
int frame_count; // 包含的帧数
int is_onset; // 1=哭声开始,0=哭声结束(或 flush)
} alarm_event_t;告警回调类型
typedef void (*alarm_callback_t)(
const alarm_event_t *event,
void *user_data
);示例
void my_alarm_callback(const alarm_event_t *event, void *user_data) {
if (event->is_onset) {
printf("告警开始 at %.1fs(置信度:%.2f)
",
event->start_time, event->confidence);
} else {
printf("告警结束 at %.1fs(持续:%.1fs,最高置信度:%.2f)
",
event->end_time,
event->end_time - event->start_time,
event->max_confidence);
}
}
// 注册告警回调
cry_detect_config_t config = {
.callback = my_callback,
.alarm_strategy = "asymmetric_envelope",
.alarm_params = "high_thr=0.85,low_thr=0.35,min_duration=1.0",
.alarm_callback = my_alarm_callback,
};告警策略
非对称包络(推荐)
"asymmetric_envelope" 策略采用快起慢放机制:
起音 — (上升沿):快速跟随概率上升
释放 — (下降沿):缓慢衰减,防止短暂下降结束告警
高阈值 — 触发告警开始(默认 0.85)
低阈值 — 触发告警结束(默认 0.35)
配置参数(通过 alarm_params 传入):
参数
默认值
说明
`attack_coef`
0.7
上升沿平滑系数
`release_coef`
0.92
下降沿保持系数
`high_thr`
0.85
触发阈值
`low_thr`
0.35
退出阈值
`min_duration`
1.0
最短持续秒数
告警策略也可独立使用:
alarm_strategy_t *s = alarm_strategy_create("asymmetric_envelope", NULL);
alarm_event_t event;
for each frame:
if (s->update(s, prob, timestamp, &event))
on_alarm_event(&event);
s->flush(s, &event); // 流结束
s->destroy(s);