|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2026 Intel Corporation. |
| 4 | +// |
| 5 | +// Real RNNoise backend for webrtc_ns2. |
| 6 | +// |
| 7 | +// RNNoise public API (rnnoise.h / denoise.c): |
| 8 | +// |
| 9 | +// int rnnoise_get_size(void); |
| 10 | +// Returns sizeof(DenoiseState). Use this instead of sizeof() so the |
| 11 | +// wrapper doesn't need to know the internal layout. |
| 12 | +// |
| 13 | +// int rnnoise_init(DenoiseState *st, const RNNModel *model); |
| 14 | +// Initialise state in-place. model=NULL uses the built-in weights |
| 15 | +// (rnnoise_model_orig compiled into rnnoise_tables.c). |
| 16 | +// |
| 17 | +// DenoiseState *rnnoise_create(const RNNModel *model); |
| 18 | +// Allocate + init. Calls malloc() internally. |
| 19 | +// |
| 20 | +// void rnnoise_destroy(DenoiseState *st); |
| 21 | +// Free. Calls free() internally. |
| 22 | +// |
| 23 | +// float rnnoise_process_frame(DenoiseState *st, float *out, const float *in); |
| 24 | +// Process one 480-sample (10 ms at 48 kHz) mono frame. |
| 25 | +// Samples are in full-scale float (not normalised to [-1,1]). |
| 26 | +// Returns speech probability in [0, 1]. |
| 27 | +// |
| 28 | +// Memory: ~30 KB per DenoiseState (including RNN state). |
| 29 | +// Dependencies: libm (expf, tanhf, sinf, cosf, sqrtf). |
| 30 | + |
| 31 | +#include <sof/audio/module_adapter/module/generic.h> |
| 32 | +#include <errno.h> |
| 33 | +#include "webrtc_ns2.h" |
| 34 | + |
| 35 | +#include <rnnoise.h> /* from cross-built rnnoise library */ |
| 36 | + |
| 37 | +LOG_MODULE_DECLARE(webrtc_ns2, CONFIG_SOF_LOG_LEVEL); |
| 38 | + |
| 39 | +/* RNNoise uses full-scale float (raw amplitude, not normalised). */ |
| 40 | +#define RNN_FULL_SCALE 32768.0f |
| 41 | + |
| 42 | +struct webrtc_ns2_rnn_data { |
| 43 | + DenoiseState *st[WEBRTC_NS2_CHANNELS_MAX]; |
| 44 | + int num_channels; |
| 45 | +}; |
| 46 | + |
| 47 | +/* Per-frame scratch buffer at full scale. */ |
| 48 | +static float rnn_in[WEBRTC_NS2_FRAME_SAMPLES]; |
| 49 | +static float rnn_out[WEBRTC_NS2_FRAME_SAMPLES]; |
| 50 | + |
| 51 | +static int webrtc_ns2_rnn_init(struct processing_module *mod) |
| 52 | +{ |
| 53 | + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); |
| 54 | + struct webrtc_ns2_rnn_data *rd; |
| 55 | + |
| 56 | + rd = mod_zalloc(mod, sizeof(*rd)); |
| 57 | + if (!rd) |
| 58 | + return -ENOMEM; |
| 59 | + |
| 60 | + cd->backend_data = rd; |
| 61 | + comp_info(mod->dev, "webrtc_ns2: RNNoise real backend, frame=%d rate=%d", |
| 62 | + WEBRTC_NS2_FRAME_SAMPLES, WEBRTC_NS2_SAMPLE_RATE); |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +static int webrtc_ns2_rnn_configure(struct processing_module *mod, int num_channels) |
| 67 | +{ |
| 68 | + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); |
| 69 | + struct webrtc_ns2_rnn_data *rd = cd->backend_data; |
| 70 | + int c; |
| 71 | + |
| 72 | + /* Free any previous instances from a re-prepare. */ |
| 73 | + for (c = 0; c < rd->num_channels; c++) { |
| 74 | + if (rd->st[c]) { |
| 75 | + rnnoise_destroy(rd->st[c]); |
| 76 | + rd->st[c] = NULL; |
| 77 | + } |
| 78 | + } |
| 79 | + rd->num_channels = 0; |
| 80 | + |
| 81 | + for (c = 0; c < num_channels; c++) { |
| 82 | + /* Pass NULL to use the built-in model weights. */ |
| 83 | + rd->st[c] = rnnoise_create(NULL); |
| 84 | + if (!rd->st[c]) { |
| 85 | + comp_err(mod->dev, |
| 86 | + "webrtc_ns2: rnnoise_create() failed ch%d", c); |
| 87 | + goto err; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + rd->num_channels = num_channels; |
| 92 | + comp_info(mod->dev, "webrtc_ns2: %d RNNoise instance(s) created", num_channels); |
| 93 | + return 0; |
| 94 | + |
| 95 | +err: |
| 96 | + for (c = 0; c < num_channels; c++) { |
| 97 | + if (rd->st[c]) { |
| 98 | + rnnoise_destroy(rd->st[c]); |
| 99 | + rd->st[c] = NULL; |
| 100 | + } |
| 101 | + } |
| 102 | + return -ENOMEM; |
| 103 | +} |
| 104 | + |
| 105 | +static float webrtc_ns2_rnn_process_ch(struct processing_module *mod, |
| 106 | + const float *in, float *out, int ch) |
| 107 | +{ |
| 108 | + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); |
| 109 | + struct webrtc_ns2_rnn_data *rd = cd->backend_data; |
| 110 | + float vad_prob; |
| 111 | + int i; |
| 112 | + |
| 113 | + /* |
| 114 | + * RNNoise expects full-scale float (raw PCM amplitude), but the SOF |
| 115 | + * glue normalises to [-1, +1]. Scale up before processing and back |
| 116 | + * down afterward. |
| 117 | + */ |
| 118 | + for (i = 0; i < WEBRTC_NS2_FRAME_SAMPLES; i++) |
| 119 | + rnn_in[i] = in[i] * RNN_FULL_SCALE; |
| 120 | + |
| 121 | + vad_prob = rnnoise_process_frame(rd->st[ch], rnn_out, rnn_in); |
| 122 | + |
| 123 | + for (i = 0; i < WEBRTC_NS2_FRAME_SAMPLES; i++) |
| 124 | + out[i] = rnn_out[i] / RNN_FULL_SCALE; |
| 125 | + |
| 126 | + return vad_prob; |
| 127 | +} |
| 128 | + |
| 129 | +static int webrtc_ns2_rnn_reset(struct processing_module *mod) |
| 130 | +{ |
| 131 | + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); |
| 132 | + struct webrtc_ns2_rnn_data *rd = cd->backend_data; |
| 133 | + int c; |
| 134 | + |
| 135 | + /* |
| 136 | + * RNNoise has no reset API; re-initialise each instance in-place |
| 137 | + * using rnnoise_init() to clear the GRU hidden state. |
| 138 | + */ |
| 139 | + for (c = 0; c < rd->num_channels; c++) { |
| 140 | + if (rd->st[c]) |
| 141 | + rnnoise_init(rd->st[c], NULL); |
| 142 | + } |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +static int webrtc_ns2_rnn_free(struct processing_module *mod) |
| 147 | +{ |
| 148 | + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); |
| 149 | + struct webrtc_ns2_rnn_data *rd = cd->backend_data; |
| 150 | + int c; |
| 151 | + |
| 152 | + if (!rd) |
| 153 | + return 0; |
| 154 | + |
| 155 | + for (c = 0; c < rd->num_channels; c++) { |
| 156 | + if (rd->st[c]) { |
| 157 | + rnnoise_destroy(rd->st[c]); |
| 158 | + rd->st[c] = NULL; |
| 159 | + } |
| 160 | + } |
| 161 | + mod_free(mod, rd); |
| 162 | + cd->backend_data = NULL; |
| 163 | + return 0; |
| 164 | +} |
| 165 | + |
| 166 | +const struct webrtc_ns2_backend webrtc_ns2_backend = { |
| 167 | + .name = "rnnoise", |
| 168 | + .init = webrtc_ns2_rnn_init, |
| 169 | + .configure = webrtc_ns2_rnn_configure, |
| 170 | + .process_ch = webrtc_ns2_rnn_process_ch, |
| 171 | + .reset = webrtc_ns2_rnn_reset, |
| 172 | + .free = webrtc_ns2_rnn_free, |
| 173 | +}; |
0 commit comments