← Back to Blog

Embedded Dog Bark Detection: Offline Bark Recognition SDK with Graded AlertsNEW

The Value of Bark Detection: Scenarios and Workflow

In the soundscape of a doorway or yard, barking carries the most information — and the most ambiguity. A dog barking at a stranger, barking at a passing car, and barking alone at home mean three completely different things. Detecting barking is only step one; telling which kind of barking apart is where product value lives.

The classic smart-doorbell problem: motion detection cannot distinguish "someone is at the door" from "a car passed by", so every trigger looks the same and users drown in alert fatigue. Adding bark recognition supplies the missing context:

Stranger at the door — — motion + alert barking + human presence → high-priority alert

Passing nuisance — — casual barking, no person → silent log

Unattended pet — — sustained barking while away → pet-wellbeing reminder

Doorway and yard scenario
Doorbells, outdoor cameras, pet care and community management in one SDK

The Signature of Dog Barks: Time and Frequency

Bark signature vs birdsong and cars
Bursts in groups vs high-frequency chirps vs sustained tones

Below is a real dog barking recording — waveform (top) and Mel spectrogram (bottom). The burst-series pulse structure is clearly visible:

Real dog bark sample: waveform and spectrum
Real dog bark sample: waveform (top) + Mel spectrogram (bottom)

Audio samples (real recordings — press play):

🔊 Dog bark (real sample)

🔊 Another bark recording

Bursts in groups — — a single bark lasts about 0.2–0.5 seconds and barks usually arrive in short series rather than isolated events

Mid-frequency energy — — the energy band sits between a few hundred Hz and a few kHz: above the rumble of traffic, below birdsong

Wide variation across breeds — — deep and resonant for large dogs, sharp and rapid for small ones; detection must handle "same event, different timbre"

How Recognition Works: Preprocessing → Features → Algorithms

The engine follows the standard pipeline (denoise → framing → time-frequency features), with bark-specific design choices on top:

1. Work in "series", not single barks — locate each burst, then verify the series structure: several bursts with barking-like intervals constitute a bark event. Isolated transients (a door slam, a falling object) are not lightly accepted

2. Generalize across breeds and near-neighbor classes — training covers different dog sizes while enforcing high discrimination against birdsong, cat sounds, speech and traffic

3. Outdoor robustness — event-based detection is inherently insensitive to sustained wind and rain noise

On-device architecture
Capture, processing, engine, output, application

Graded Alerts: Turning "A Dog Barked" into Useful Information

Graded alert flow
Human presence, duration checks, and three output levels

1. Bark event — series verification passed

2. Scene evaluation — is a person present? is the barking sustained?

3. Graded outputs — high-priority alert (push + recording + two-way talk) / silent log / pet status reminder

All decisions run on the device, and the grading policy is configurable per customer workflow.

The Hard Parts

Bark recognition must separate itself from a crowded field: birdsong (high-frequency), cat sounds (close neighbors), human speech and traffic. Attenuation over distance and outdoor wind/rain add physical difficulty. The countermeasures are multi-class negative training and event aggregation — the same approach that keeps false alarms below 2% in real yards.

Accuracy and Performance

Bark detection performance
Performance across distances and environments (illustrative)

From 0.5 m to 10 m — typical door distances — recognition stays stable in quiet and noisy conditions. The main false-alarm sources are "bark-like" speech and traffic; multi-class negative training keeps false alarms below 2%.

Item
Spec
Accuracy
96.0%
False alarm rate
<2%
Model size
0.2–1 MB (INT8)
Inference latency
Configurable to platform resources
Sample rate
16 kHz
Platforms
ARM Linux / MIPS / x86_64; SVP / Magik pre-adapted

Note: performance figures are based on internal test environments; actual results depend on hardware and deployment scenarios.

Applications

Smart doorbells (barking + human-presence linkage), outdoor cameras (yard monitoring and night-time anomaly assistance), pet care (alone-time barking logs), and community management (public-area barking events) all run the same engine with scenario-specific configurations.

Platform and Hardware Requirements

The model fits doorbell-class SoCs: 0.2–1 MB (INT8), CPU under 50 MHz, sharing the SoC with video encoding and networking.

C API and Embedded Integration

The dog bark detection library exposes a concise streaming C API: the caller just keeps feeding 16 kHz mono PCM; framing, Mel preprocessing and model inference run internally, and frame-level probabilities are aggregated by the alarm strategy into event callbacks.

Full interface declaration (bark_detect.h):

bark_detect.hc
/**
 * bark_detect.h — 狗叫声识别统一接口
 *
 * 封装 Mel 预处理 + 推理引擎 + 报警策略, 内部模型消费线程处理音频。
 * 与录音模块 (audio_capture.h) 相互独立: 调用者自行决定音频来源
 * (录音回调 / wav 文件 / 网络流), 通过 bark_detect_feed 送入, 数据任意大小。
 *
 * 用法 (实时录音模式):
 *   bark_detect_t *d = bark_detect_create(mgk_path, NULL, NULL);
 *   bark_detect_set_listener(d, on_frame, on_onset, on_offset, NULL);
 *   bark_detect_start(d);                          // 启动内部模型消费线程
 *   audio_capture_start(rec, capture_cb, d);        // 录音回调里调 bark_detect_feed
 *   ...
 *   bark_detect_stop(d);                           // 排空缓冲, 停止线程
 *   bark_detect_destroy(d);
 *
 * 用法 (wav 文件模式):
 *   bark_detect_t *d = bark_detect_create(mgk_path, NULL, NULL);
 *   bark_detect_set_listener(d, on_frame, on_onset, on_offset, NULL);
 *   bark_detect_start(d);
 *   循环读文件: bark_detect_feed(d, pcm, n);       // 任意数据大小
 *   bark_detect_stop(d);
 *   bark_detect_destroy(d);
 */

#ifndef BARK_DETECT_H
#define BARK_DETECT_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/* 识别事件 (报警策略输出, 用于事件结束回调) */
typedef struct {
    float start_time;       /* 事件开始时间 (秒) */
    float end_time;         /* 事件结束时间 (秒) */
    float confidence;       /* 事件置信度 */
    float max_confidence;   /* 事件内最大帧置信度 */
    int   frame_count;      /* 事件持续帧数 */
} bark_detect_event_t;

/* 帧级回调: 每帧识别结果 (模型线程内执行) */
typedef void (*bark_detect_frame_cb_t)(float bark_prob, float timestamp,
                                        void *user_data);

/* 事件开始回调: 策略判定狗叫事件开始, 只有开始时间 */
typedef void (*bark_detect_onset_cb_t)(float start_time, void *user_data);

/* 事件结束回调: 策略判定狗叫事件结束 (或停止识别时未结束的事件), 完整事件信息 */
typedef void (*bark_detect_offset_cb_t)(const bark_detect_event_t *event,
                                         void *user_data);

typedef struct bark_detect_s bark_detect_t;

/* 创建/销毁; alarm_name/alarm_params 可传 NULL (用默认策略及参数) */
bark_detect_t *bark_detect_create(const char *mgk_path,      /* 模型文件路径 (必填) */
                                    const char *alarm_name,    /* 报警策略名, NULL=默认 */
                                    const char *alarm_params); /* 策略参数 key=val,key=val, NULL=默认 */
void bark_detect_destroy(bark_detect_t *det);

/* 设置事件回调 (create 后调用, 也可在运行中调整); 不需要的回调传 NULL */
void bark_detect_set_listener(bark_detect_t *det,
                               bark_detect_frame_cb_t  on_frame,
                               bark_detect_onset_cb_t  on_onset,
                               bark_detect_offset_cb_t on_offset,
                               void *user_data);

/* 启动/停止识别: 启动内部模型消费线程 / 排空缓冲后停止线程 */
int  bark_detect_start(bark_detect_t *det);
void bark_detect_stop(bark_detect_t *det);
int  bark_detect_is_running(bark_detect_t *det);

/* 设置事件识别策略 (可在运行中调整) */
int bark_detect_set_alarm(bark_detect_t *det, const char *alarm_name,
                           const char *alarm_params);

/* 送入 PCM 数据 (16bit 单声道 16kHz), 线程安全, 任意数据大小 */
int bark_detect_feed(bark_detect_t *det, const int16_t *pcm, int num_samples);

#ifdef __cplusplus
}
#endif

#endif /* BARK_DETECT_H */

A minimal WAV-inference demo (excerpt; the full file ships at src/barkDetect/c/bark_demo.c):

bark_demo.cc
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bark_detect.h"

#define DEFAULT_MGK  "bark_detect_v7.mgk"  /* 模型文件 */
#define DEFAULT_WAV  "test_bark.wav"       /* 16kHz 单声道 16bit PCM */
#define FEED_CHUNK   16000                  /* 每次送入 1 秒音频 */

/* 帧级回调: 每帧输出狗叫声概率 (约 1 秒一帧) */
static void on_frame(float bark_prob, float timestamp, void *user_data)
{
    (void)user_data;
    printf("%8.2fs  bark=%.4f\n", timestamp, bark_prob);
}

/* 事件开始回调: 策略判定狗叫事件开始 */
static void on_onset(float start_time, void *user_data)
{
    (void)user_data;
    printf("[EVENT] bark start at %.2fs\n", start_time);
}

/* 事件结束回调: 应用层可据此做后续统计或分级响应 */
static void on_offset(const bark_detect_event_t *ev, void *user_data)
{
    (void)user_data;
    printf("[EVENT] bark end at %.2fs (dur=%.2fs, conf=%.3f, frames=%d)\n",
           ev->end_time, ev->end_time - ev->start_time,
           ev->confidence, ev->frame_count);
}

static int read_wav_pcm(const char *path, int16_t **pcm, int *n, int *sr);  /* 完整实现见源文件 */

int main(void)
{
    bark_detect_t *det;
    int16_t *pcm = NULL;
    int num_samples = 0, sample_rate = 0;
    int pos;

    if (read_wav_pcm(DEFAULT_WAV, &pcm, &num_samples, &sample_rate) != 0)
        return 1;

    /* 1. 创建识别器: 模型文件 + 默认报警策略 (NULL) */
    det = bark_detect_create(DEFAULT_MGK, NULL, NULL);
    if (!det) return 1;

    /* 2. 注册回调 (均为可选) */
    bark_detect_set_listener(det, on_frame, on_onset, on_offset, NULL);

    /* 3. 启动内部模型消费线程 */
    bark_detect_start(det);

    /* 4. 分块送入 PCM; 实时录音时改在录音回调里 feed */
    for (pos = 0; pos < num_samples; pos += FEED_CHUNK) {
        int n = num_samples - pos;
        if (n > FEED_CHUNK) n = FEED_CHUNK;
        bark_detect_feed(det, pcm + pos, n);
    }

    /* 5. 停止并销毁 */
    bark_detect_stop(det);
    bark_detect_destroy(det);
    free(pcm);
    return 0;
}

Build and run:

buildbash
$(CC) bark_demo.c -I. -L. -lbarkdetect -lpthread -lm -o bark_demo
./bark_demo

Conclusion

The value of bark detection is not "detecting a bark" — it is understanding what this bark means. An online trial with full technical support is available.