|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2026 Intel Corporation. |
| 4 | +// |
| 5 | +// libfvad backend for the webrtc_vad module. |
| 6 | +// |
| 7 | +// libfvad is a standalone pure-C BSD-3 extraction of the WebRTC GMM Voice |
| 8 | +// Activity Detection algorithm. It operates in Q15 fixed-point and requires |
| 9 | +// no FPU, making it well suited to Xtensa DSPs and Cortex-M class targets. |
| 10 | +// |
| 11 | +// The backend wraps the following minimal libfvad API: |
| 12 | +// |
| 13 | +// fvad_new() — allocate a FvadState |
| 14 | +// fvad_set_mode() — aggressiveness 0..3 |
| 15 | +// fvad_set_sample_rate() — 8000/16000/32000/48000 Hz |
| 16 | +// fvad_process() — classify num_samples of int16 mono audio |
| 17 | +// fvad_free() — release FvadState |
| 18 | +// |
| 19 | +// Requires cross-compiled libfvad static library and headers (west module |
| 20 | +// modules/audio/libfvad; see webrtc_vad.cmake for the cross-build recipe). |
| 21 | +// Only compiled when CONFIG_COMP_WEBRTC_VAD_STUB is not selected. |
| 22 | + |
| 23 | +#include <sof/audio/module_adapter/module/generic.h> |
| 24 | +#include <errno.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include "webrtc_vad.h" |
| 27 | + |
| 28 | +#include <fvad.h> /* from cross-built libfvad install */ |
| 29 | + |
| 30 | +LOG_MODULE_DECLARE(webrtc_vad, CONFIG_SOF_LOG_LEVEL); |
| 31 | + |
| 32 | +static int webrtc_vad_fvad_init(struct processing_module *mod) |
| 33 | +{ |
| 34 | + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); |
| 35 | + Fvad *fvad; |
| 36 | + |
| 37 | + fvad = fvad_new(); |
| 38 | + if (!fvad) { |
| 39 | + comp_err(mod->dev, "webrtc_vad: fvad_new() failed"); |
| 40 | + return -ENOMEM; |
| 41 | + } |
| 42 | + |
| 43 | + cd->backend_data = fvad; |
| 44 | + comp_info(mod->dev, "webrtc_vad: libfvad backend initialised"); |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +static int webrtc_vad_fvad_configure(struct processing_module *mod, |
| 49 | + int sample_rate_hz, int mode) |
| 50 | +{ |
| 51 | + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); |
| 52 | + Fvad *fvad = cd->backend_data; |
| 53 | + int ret; |
| 54 | + |
| 55 | + ret = fvad_set_sample_rate(fvad, sample_rate_hz); |
| 56 | + if (ret < 0) { |
| 57 | + comp_err(mod->dev, "webrtc_vad: fvad_set_sample_rate(%d) failed %d", |
| 58 | + sample_rate_hz, ret); |
| 59 | + return -EINVAL; |
| 60 | + } |
| 61 | + |
| 62 | + ret = fvad_set_mode(fvad, mode); |
| 63 | + if (ret < 0) { |
| 64 | + comp_err(mod->dev, "webrtc_vad: fvad_set_mode(%d) failed %d", |
| 65 | + mode, ret); |
| 66 | + return -EINVAL; |
| 67 | + } |
| 68 | + |
| 69 | + comp_info(mod->dev, "webrtc_vad: libfvad rate=%d mode=%d", |
| 70 | + sample_rate_hz, mode); |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +static int webrtc_vad_fvad_classify(struct processing_module *mod, |
| 75 | + const int16_t *samples, int num_samples) |
| 76 | +{ |
| 77 | + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); |
| 78 | + Fvad *fvad = cd->backend_data; |
| 79 | + int decision; |
| 80 | + |
| 81 | + /* fvad_process returns 1 (speech), 0 (non-speech), or <0 on error. */ |
| 82 | + decision = fvad_process(fvad, samples, num_samples); |
| 83 | + if (decision < 0) { |
| 84 | + comp_err(mod->dev, "webrtc_vad: fvad_process error %d", decision); |
| 85 | + return -EIO; |
| 86 | + } |
| 87 | + |
| 88 | + return decision; |
| 89 | +} |
| 90 | + |
| 91 | +static int webrtc_vad_fvad_reset(struct processing_module *mod) |
| 92 | +{ |
| 93 | + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); |
| 94 | + Fvad *fvad = cd->backend_data; |
| 95 | + |
| 96 | + /* libfvad has no explicit reset API; re-init the state via free+new. */ |
| 97 | + int rate = cd->sample_rate; |
| 98 | + int mode = CONFIG_WEBRTC_VAD_MODE; |
| 99 | + |
| 100 | + fvad_free(fvad); |
| 101 | + fvad = fvad_new(); |
| 102 | + if (!fvad) |
| 103 | + return -ENOMEM; |
| 104 | + |
| 105 | + cd->backend_data = fvad; |
| 106 | + return webrtc_vad_fvad_configure(mod, rate, mode); |
| 107 | +} |
| 108 | + |
| 109 | +static int webrtc_vad_fvad_free(struct processing_module *mod) |
| 110 | +{ |
| 111 | + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); |
| 112 | + |
| 113 | + if (cd->backend_data) { |
| 114 | + fvad_free(cd->backend_data); |
| 115 | + cd->backend_data = NULL; |
| 116 | + } |
| 117 | + return 0; |
| 118 | +} |
| 119 | + |
| 120 | +const struct webrtc_vad_backend webrtc_vad_backend = { |
| 121 | + .name = "fvad", |
| 122 | + .init = webrtc_vad_fvad_init, |
| 123 | + .configure = webrtc_vad_fvad_configure, |
| 124 | + .classify = webrtc_vad_fvad_classify, |
| 125 | + .reset = webrtc_vad_fvad_reset, |
| 126 | + .free = webrtc_vad_fvad_free, |
| 127 | +}; |
0 commit comments