opus1 0.2.1

Pure-Rust implementation of the Opus audio codec (RFC 6716), following libopus 1.x
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! FARGAN, the vocoder of deep packet loss concealment (deep PLC): its layers
//! and the constants of its C header.
//!
//! Generated by `tools/dnn-gen` from libopus 1.6.1 `dnn/fargan_data.h` and
//! `dnn/fargan_data.c` (`init_fargan`). Do not edit: change the generator and
//! run it again (`tools/dnn-gen/README.md`).

use crate::Error;
use super::{Layer, LayerSpec, LinearSpec};
use crate::dnn::nnet::LinearLayer;
use crate::dnn::weights::WeightList;

/// The file of this model's weights in `dnn-weights/`.
pub const BLOB_FILE: &str = "fargan.bin";

// The constants of `dnn/fargan_data.h`, in the same order.

/// `COND_NET_PEMBED_OUT_SIZE`.
pub const COND_NET_PEMBED_OUT_SIZE: usize = 12;

/// `COND_NET_FDENSE1_OUT_SIZE`.
pub const COND_NET_FDENSE1_OUT_SIZE: usize = 64;

/// `COND_NET_FCONV1_OUT_SIZE`.
pub const COND_NET_FCONV1_OUT_SIZE: usize = 128;

/// `COND_NET_FCONV1_IN_SIZE`.
pub const COND_NET_FCONV1_IN_SIZE: usize = 64;

/// `COND_NET_FCONV1_STATE_SIZE`, `(64 * (2))` in C.
pub const COND_NET_FCONV1_STATE_SIZE: usize = 128;

/// `COND_NET_FCONV1_DELAY`.
pub const COND_NET_FCONV1_DELAY: usize = 1;

/// `COND_NET_FDENSE2_OUT_SIZE`.
pub const COND_NET_FDENSE2_OUT_SIZE: usize = 320;

/// `SIG_NET_COND_GAIN_DENSE_OUT_SIZE`.
pub const SIG_NET_COND_GAIN_DENSE_OUT_SIZE: usize = 1;

/// `SIG_NET_FWC0_CONV_OUT_SIZE`.
pub const SIG_NET_FWC0_CONV_OUT_SIZE: usize = 192;

/// `SIG_NET_FWC0_GLU_GATE_OUT_SIZE`.
pub const SIG_NET_FWC0_GLU_GATE_OUT_SIZE: usize = 192;

/// `SIG_NET_GRU1_OUT_SIZE`.
pub const SIG_NET_GRU1_OUT_SIZE: usize = 160;

/// `SIG_NET_GRU1_STATE_SIZE`.
pub const SIG_NET_GRU1_STATE_SIZE: usize = 160;

/// `SIG_NET_GRU2_OUT_SIZE`.
pub const SIG_NET_GRU2_OUT_SIZE: usize = 128;

/// `SIG_NET_GRU2_STATE_SIZE`.
pub const SIG_NET_GRU2_STATE_SIZE: usize = 128;

/// `SIG_NET_GRU3_OUT_SIZE`.
pub const SIG_NET_GRU3_OUT_SIZE: usize = 128;

/// `SIG_NET_GRU3_STATE_SIZE`.
pub const SIG_NET_GRU3_STATE_SIZE: usize = 128;

/// `SIG_NET_GRU1_GLU_GATE_OUT_SIZE`.
pub const SIG_NET_GRU1_GLU_GATE_OUT_SIZE: usize = 160;

/// `SIG_NET_GRU2_GLU_GATE_OUT_SIZE`.
pub const SIG_NET_GRU2_GLU_GATE_OUT_SIZE: usize = 128;

/// `SIG_NET_GRU3_GLU_GATE_OUT_SIZE`.
pub const SIG_NET_GRU3_GLU_GATE_OUT_SIZE: usize = 128;

/// `SIG_NET_SKIP_GLU_GATE_OUT_SIZE`.
pub const SIG_NET_SKIP_GLU_GATE_OUT_SIZE: usize = 128;

/// `SIG_NET_SKIP_DENSE_OUT_SIZE`.
pub const SIG_NET_SKIP_DENSE_OUT_SIZE: usize = 128;

/// `SIG_NET_SIG_DENSE_OUT_OUT_SIZE`.
pub const SIG_NET_SIG_DENSE_OUT_OUT_SIZE: usize = 40;

/// `SIG_NET_GAIN_DENSE_OUT_OUT_SIZE`.
pub const SIG_NET_GAIN_DENSE_OUT_OUT_SIZE: usize = 4;

/// The layers in the order of `init_fargan`: the arrays that each one reads,
/// and its shape. The names are the array names of the blob.
pub const LAYERS: [LayerSpec; 20] = [
    LayerSpec::Linear(LinearSpec {
        name: "cond_net_pembed",
        bias: Some("cond_net_pembed_bias"),
        subias: None,
        weights: None,
        float_weights: Some("cond_net_pembed_weights_float"),
        weights_idx: None,
        diag: None,
        scale: None,
        nb_inputs: 224,
        nb_outputs: 12,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "cond_net_fdense1",
        bias: Some("cond_net_fdense1_bias"),
        subias: None,
        weights: None,
        float_weights: Some("cond_net_fdense1_weights_float"),
        weights_idx: None,
        diag: None,
        scale: None,
        nb_inputs: 32,
        nb_outputs: 64,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "cond_net_fconv1",
        bias: Some("cond_net_fconv1_bias"),
        subias: Some("cond_net_fconv1_subias"),
        weights: Some("cond_net_fconv1_weights_int8"),
        float_weights: Some("cond_net_fconv1_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("cond_net_fconv1_scale"),
        nb_inputs: 192,
        nb_outputs: 128,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "cond_net_fdense2",
        bias: Some("cond_net_fdense2_bias"),
        subias: Some("cond_net_fdense2_subias"),
        weights: Some("cond_net_fdense2_weights_int8"),
        float_weights: Some("cond_net_fdense2_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("cond_net_fdense2_scale"),
        nb_inputs: 128,
        nb_outputs: 320,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_cond_gain_dense",
        bias: Some("sig_net_cond_gain_dense_bias"),
        subias: None,
        weights: None,
        float_weights: Some("sig_net_cond_gain_dense_weights_float"),
        weights_idx: None,
        diag: None,
        scale: None,
        nb_inputs: 80,
        nb_outputs: 1,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_fwc0_conv",
        bias: Some("sig_net_fwc0_conv_bias"),
        subias: Some("sig_net_fwc0_conv_subias"),
        weights: Some("sig_net_fwc0_conv_weights_int8"),
        float_weights: Some("sig_net_fwc0_conv_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_fwc0_conv_scale"),
        nb_inputs: 328,
        nb_outputs: 192,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_fwc0_glu_gate",
        bias: Some("sig_net_fwc0_glu_gate_bias"),
        subias: Some("sig_net_fwc0_glu_gate_subias"),
        weights: Some("sig_net_fwc0_glu_gate_weights_int8"),
        float_weights: Some("sig_net_fwc0_glu_gate_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_fwc0_glu_gate_scale"),
        nb_inputs: 192,
        nb_outputs: 192,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru1_input",
        bias: None,
        subias: Some("sig_net_gru1_input_subias"),
        weights: Some("sig_net_gru1_input_weights_int8"),
        float_weights: Some("sig_net_gru1_input_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru1_input_scale"),
        nb_inputs: 272,
        nb_outputs: 480,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru1_recurrent",
        bias: None,
        subias: Some("sig_net_gru1_recurrent_subias"),
        weights: Some("sig_net_gru1_recurrent_weights_int8"),
        float_weights: Some("sig_net_gru1_recurrent_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru1_recurrent_scale"),
        nb_inputs: 160,
        nb_outputs: 480,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru2_input",
        bias: None,
        subias: Some("sig_net_gru2_input_subias"),
        weights: Some("sig_net_gru2_input_weights_int8"),
        float_weights: Some("sig_net_gru2_input_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru2_input_scale"),
        nb_inputs: 240,
        nb_outputs: 384,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru2_recurrent",
        bias: None,
        subias: Some("sig_net_gru2_recurrent_subias"),
        weights: Some("sig_net_gru2_recurrent_weights_int8"),
        float_weights: Some("sig_net_gru2_recurrent_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru2_recurrent_scale"),
        nb_inputs: 128,
        nb_outputs: 384,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru3_input",
        bias: None,
        subias: Some("sig_net_gru3_input_subias"),
        weights: Some("sig_net_gru3_input_weights_int8"),
        float_weights: Some("sig_net_gru3_input_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru3_input_scale"),
        nb_inputs: 208,
        nb_outputs: 384,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru3_recurrent",
        bias: None,
        subias: Some("sig_net_gru3_recurrent_subias"),
        weights: Some("sig_net_gru3_recurrent_weights_int8"),
        float_weights: Some("sig_net_gru3_recurrent_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru3_recurrent_scale"),
        nb_inputs: 128,
        nb_outputs: 384,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru1_glu_gate",
        bias: Some("sig_net_gru1_glu_gate_bias"),
        subias: Some("sig_net_gru1_glu_gate_subias"),
        weights: Some("sig_net_gru1_glu_gate_weights_int8"),
        float_weights: Some("sig_net_gru1_glu_gate_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru1_glu_gate_scale"),
        nb_inputs: 160,
        nb_outputs: 160,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru2_glu_gate",
        bias: Some("sig_net_gru2_glu_gate_bias"),
        subias: Some("sig_net_gru2_glu_gate_subias"),
        weights: Some("sig_net_gru2_glu_gate_weights_int8"),
        float_weights: Some("sig_net_gru2_glu_gate_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru2_glu_gate_scale"),
        nb_inputs: 128,
        nb_outputs: 128,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gru3_glu_gate",
        bias: Some("sig_net_gru3_glu_gate_bias"),
        subias: Some("sig_net_gru3_glu_gate_subias"),
        weights: Some("sig_net_gru3_glu_gate_weights_int8"),
        float_weights: Some("sig_net_gru3_glu_gate_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_gru3_glu_gate_scale"),
        nb_inputs: 128,
        nb_outputs: 128,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_skip_glu_gate",
        bias: Some("sig_net_skip_glu_gate_bias"),
        subias: Some("sig_net_skip_glu_gate_subias"),
        weights: Some("sig_net_skip_glu_gate_weights_int8"),
        float_weights: Some("sig_net_skip_glu_gate_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_skip_glu_gate_scale"),
        nb_inputs: 128,
        nb_outputs: 128,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_skip_dense",
        bias: Some("sig_net_skip_dense_bias"),
        subias: Some("sig_net_skip_dense_subias"),
        weights: Some("sig_net_skip_dense_weights_int8"),
        float_weights: Some("sig_net_skip_dense_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_skip_dense_scale"),
        nb_inputs: 688,
        nb_outputs: 128,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_sig_dense_out",
        bias: Some("sig_net_sig_dense_out_bias"),
        subias: Some("sig_net_sig_dense_out_subias"),
        weights: Some("sig_net_sig_dense_out_weights_int8"),
        float_weights: Some("sig_net_sig_dense_out_weights_float"),
        weights_idx: None,
        diag: None,
        scale: Some("sig_net_sig_dense_out_scale"),
        nb_inputs: 128,
        nb_outputs: 40,
    }),
    LayerSpec::Linear(LinearSpec {
        name: "sig_net_gain_dense_out",
        bias: Some("sig_net_gain_dense_out_bias"),
        subias: None,
        weights: None,
        float_weights: Some("sig_net_gain_dense_out_weights_float"),
        weights_idx: None,
        diag: None,
        scale: None,
        nb_inputs: 192,
        nb_outputs: 4,
    }),
];

/// libopus `FARGAN` (`dnn/fargan_data.h`): the layers of the model, in the
/// order of the C struct. Each layer borrows its arrays from a weight list.
#[derive(Clone, Copy, Default)]
pub struct Fargan<'w> {
    /// `cond_net_pembed`: float weights, 224 inputs, 12 outputs.
    pub cond_net_pembed: LinearLayer<'w>,
    /// `cond_net_fdense1`: float weights, 32 inputs, 64 outputs.
    pub cond_net_fdense1: LinearLayer<'w>,
    /// `cond_net_fconv1`: int8 weights, 192 inputs, 128 outputs.
    pub cond_net_fconv1: LinearLayer<'w>,
    /// `cond_net_fdense2`: int8 weights, 128 inputs, 320 outputs.
    pub cond_net_fdense2: LinearLayer<'w>,
    /// `sig_net_cond_gain_dense`: float weights, 80 inputs, 1 output.
    pub sig_net_cond_gain_dense: LinearLayer<'w>,
    /// `sig_net_fwc0_conv`: int8 weights, 328 inputs, 192 outputs.
    pub sig_net_fwc0_conv: LinearLayer<'w>,
    /// `sig_net_fwc0_glu_gate`: int8 weights, 192 inputs, 192 outputs.
    pub sig_net_fwc0_glu_gate: LinearLayer<'w>,
    /// `sig_net_gru1_input`: int8 weights, 272 inputs, 480 outputs.
    pub sig_net_gru1_input: LinearLayer<'w>,
    /// `sig_net_gru1_recurrent`: int8 weights, 160 inputs, 480 outputs.
    pub sig_net_gru1_recurrent: LinearLayer<'w>,
    /// `sig_net_gru2_input`: int8 weights, 240 inputs, 384 outputs.
    pub sig_net_gru2_input: LinearLayer<'w>,
    /// `sig_net_gru2_recurrent`: int8 weights, 128 inputs, 384 outputs.
    pub sig_net_gru2_recurrent: LinearLayer<'w>,
    /// `sig_net_gru3_input`: int8 weights, 208 inputs, 384 outputs.
    pub sig_net_gru3_input: LinearLayer<'w>,
    /// `sig_net_gru3_recurrent`: int8 weights, 128 inputs, 384 outputs.
    pub sig_net_gru3_recurrent: LinearLayer<'w>,
    /// `sig_net_gru1_glu_gate`: int8 weights, 160 inputs, 160 outputs.
    pub sig_net_gru1_glu_gate: LinearLayer<'w>,
    /// `sig_net_gru2_glu_gate`: int8 weights, 128 inputs, 128 outputs.
    pub sig_net_gru2_glu_gate: LinearLayer<'w>,
    /// `sig_net_gru3_glu_gate`: int8 weights, 128 inputs, 128 outputs.
    pub sig_net_gru3_glu_gate: LinearLayer<'w>,
    /// `sig_net_skip_glu_gate`: int8 weights, 128 inputs, 128 outputs.
    pub sig_net_skip_glu_gate: LinearLayer<'w>,
    /// `sig_net_skip_dense`: int8 weights, 688 inputs, 128 outputs.
    pub sig_net_skip_dense: LinearLayer<'w>,
    /// `sig_net_sig_dense_out`: int8 weights, 128 inputs, 40 outputs.
    pub sig_net_sig_dense_out: LinearLayer<'w>,
    /// `sig_net_gain_dense_out`: float weights, 192 inputs, 4 outputs.
    pub sig_net_gain_dense_out: LinearLayer<'w>,
}

impl<'w> Fargan<'w> {
    /// libopus `init_fargan`: builds every layer from the arrays of `list`, in
    /// the order of `LAYERS`. Fails at the first layer whose arrays are
    /// missing, have the wrong size or are malformed; then nothing is built.
    pub fn init(list: &WeightList<'w>) -> Result<Self, Error> {
        Ok(Fargan {
            cond_net_pembed: LAYERS[0].linear(list)?,
            cond_net_fdense1: LAYERS[1].linear(list)?,
            cond_net_fconv1: LAYERS[2].linear(list)?,
            cond_net_fdense2: LAYERS[3].linear(list)?,
            sig_net_cond_gain_dense: LAYERS[4].linear(list)?,
            sig_net_fwc0_conv: LAYERS[5].linear(list)?,
            sig_net_fwc0_glu_gate: LAYERS[6].linear(list)?,
            sig_net_gru1_input: LAYERS[7].linear(list)?,
            sig_net_gru1_recurrent: LAYERS[8].linear(list)?,
            sig_net_gru2_input: LAYERS[9].linear(list)?,
            sig_net_gru2_recurrent: LAYERS[10].linear(list)?,
            sig_net_gru3_input: LAYERS[11].linear(list)?,
            sig_net_gru3_recurrent: LAYERS[12].linear(list)?,
            sig_net_gru1_glu_gate: LAYERS[13].linear(list)?,
            sig_net_gru2_glu_gate: LAYERS[14].linear(list)?,
            sig_net_gru3_glu_gate: LAYERS[15].linear(list)?,
            sig_net_skip_glu_gate: LAYERS[16].linear(list)?,
            sig_net_skip_dense: LAYERS[17].linear(list)?,
            sig_net_sig_dense_out: LAYERS[18].linear(list)?,
            sig_net_gain_dense_out: LAYERS[19].linear(list)?,
        })
    }

    /// Every layer, in the order of `LAYERS`.
    pub fn layers(&self) -> [Layer<'_, 'w>; 20] {
        [
            Layer::Linear(&self.cond_net_pembed),
            Layer::Linear(&self.cond_net_fdense1),
            Layer::Linear(&self.cond_net_fconv1),
            Layer::Linear(&self.cond_net_fdense2),
            Layer::Linear(&self.sig_net_cond_gain_dense),
            Layer::Linear(&self.sig_net_fwc0_conv),
            Layer::Linear(&self.sig_net_fwc0_glu_gate),
            Layer::Linear(&self.sig_net_gru1_input),
            Layer::Linear(&self.sig_net_gru1_recurrent),
            Layer::Linear(&self.sig_net_gru2_input),
            Layer::Linear(&self.sig_net_gru2_recurrent),
            Layer::Linear(&self.sig_net_gru3_input),
            Layer::Linear(&self.sig_net_gru3_recurrent),
            Layer::Linear(&self.sig_net_gru1_glu_gate),
            Layer::Linear(&self.sig_net_gru2_glu_gate),
            Layer::Linear(&self.sig_net_gru3_glu_gate),
            Layer::Linear(&self.sig_net_skip_glu_gate),
            Layer::Linear(&self.sig_net_skip_dense),
            Layer::Linear(&self.sig_net_sig_dense_out),
            Layer::Linear(&self.sig_net_gain_dense_out),
        ]
    }
}