audio-codec 0.4.14

A collection of VoIP audio codecs in pure Rust, including G.711, G.722, G.729, and Opus.
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
#[cfg(feature = "std")]
use super::PcmBuf;
use super::{CodecError, Decoder, Encoder, Sample};
use crate::fade;
#[cfg(test)]
use crate::fade::RECOVER_MS;
// `Box` lives in the std prelude; under `no_std` it comes from `alloc`.
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
pub use opus_rs::Application as OpusApplication;
use opus_rs::{Application, OpusDecoder as OpusDecoderRaw, OpusEncoder as OpusEncoderRaw};

// Heap-free scratch-buffer caps (worst case: 48 kHz, stereo, 20 ms frame).
const OPUS_MAX_FRAME: usize = 960; // 20 ms @ 48 kHz per channel
const OPUS_MAX_CHANNELS: usize = 2;
const OPUS_MAX_SAMPLES: usize = OPUS_MAX_FRAME * OPUS_MAX_CHANNELS; // 1920
// RFC 6716: a single Opus packet carries at most 1276 bytes. Used by the
// std-only `encode` helper's scratch packet buffer.
#[cfg(feature = "std")]
const OPUS_MAX_PACKET: usize = 1276;

pub struct OpusDecoder {
    decoder: Box<OpusDecoderRaw>,
    sample_rate: u32,
    channels: u16,
    w_output_f32: [f32; OPUS_MAX_SAMPLES],
    w_pcm_i16: [i16; OPUS_MAX_SAMPLES],
    /// Opus's concealment carries on near the level of the audio before
    /// the loss, so it's faded here (see [`crate::fade`]).
    loss: fade::LossFade,
}

impl OpusDecoder {
    /// Create a new Opus decoder instance
    pub fn new(sample_rate: u32, channels: u16) -> Self {
        let decoder = Box::new(
            OpusDecoderRaw::new(sample_rate as i32, channels as usize)
                .expect("Failed to create Opus decoder"),
        );

        Self {
            decoder,
            sample_rate,
            channels,
            w_output_f32: [0.0; OPUS_MAX_SAMPLES],
            w_pcm_i16: [0; OPUS_MAX_SAMPLES],
            loss: fade::LossFade::default(),
        }
    }

    /// Create a default Opus decoder (48kHz, stereo)
    pub fn new_default() -> Self {
        Self::new(48000, 2)
    }

    /// Lower-level decode that does not perform the stereo→mono downmix.
    /// Used internally by both the trait impl and the back-comat `decode`.
    pub fn decode_into_raw(&mut self, data: &[u8], output: &mut [i16]) -> usize {
        if data.is_empty() {
            return 0;
        }

        // Detect the actual channel count from the Opus packet's TOC byte
        // (bit 2 is the stereo flag per RFC 6716). opus-rs 0.1.19+ rejects
        // decoding when the packet channel count doesn't match the decoder's,
        // so we adapt the decoder here to avoid returning empty PCM.
        let packet_channels = if data[0] & 0x04 != 0 { 2usize } else { 1 };
        if self.channels as usize != packet_channels {
            self.channels = packet_channels as u16;
            // Reuse the existing heap allocation (see `clippy::replace_box`).
            *self.decoder = OpusDecoderRaw::new(self.sample_rate as i32, packet_channels)
                .expect("Failed to create Opus decoder");
            self.loss.clear();
        }

        let channels = usize::from(self.channels);
        let frame_size = (self.sample_rate as usize * 20) / 1000;
        let max_samples = frame_size * channels;

        match self
            .decoder
            .decode(data, frame_size, &mut self.w_output_f32[..max_samples])
        {
            Ok(len) => {
                let total_samples = len * channels;
                if total_samples == 0 {
                    return 0;
                }

                for i in 0..total_samples {
                    self.w_pcm_i16[i] =
                        (self.w_output_f32[i] * 32768.0).clamp(-32768.0, 32767.0) as i16;
                }

                let n = total_samples.min(output.len());
                output[..n].copy_from_slice(&self.w_pcm_i16[..n]);
                // a DTX frame (the TOC byte alone) is the decoder's comfort
                // noise, not the end of a loss
                if data.len() > 1 {
                    self.loss
                        .end_loss(&mut output[..n], channels, self.sample_rate);
                }
                n
            }
            Err(_) => 0,
        }
    }
}

impl OpusDecoder {
    /// Conceal a lost frame of `samples` samples per channel with Opus's own
    /// packet loss concealment or, given `next`, rebuild it from the in-band
    /// FEC of the packet after it; without the stereo→mono downmix.
    fn conceal_into_raw(
        &mut self,
        next: Option<&[u8]>,
        samples: usize,
        out: &mut [Sample],
    ) -> Result<usize, CodecError> {
        let channels = usize::from(self.channels);
        let total = samples * channels;
        if total > OPUS_MAX_SAMPLES {
            return Err(CodecError::InvalidInput);
        }
        if out.len() < total {
            return Err(CodecError::BufferTooSmall);
        }
        let pcm = &mut self.w_output_f32[..total];
        let len = match next {
            Some(next) => self.decoder.decode_fec(next, samples, pcm),
            // opus-rs conceals a packet of 0 or 1 bytes, as libopus does
            // with `data == NULL`
            None => self.decoder.decode(&[], samples, pcm),
        }
        .map_err(|_| CodecError::DecodeFailed)?;
        let n = len * channels;
        for (o, f) in out[..n].iter_mut().zip(&self.w_output_f32[..n]) {
            *o = (f * 32768.0).clamp(-32768.0, 32767.0) as i16;
        }
        match next {
            // rebuilt from the sender's own data: audio like a decoded
            // packet's (where `next` carried no FEC, the frame is concealed
            // at full level, as the first of a loss would be)
            Some(_) => self
                .loss
                .end_loss(&mut out[..n], channels, self.sample_rate),
            None => self.loss.fade(&mut out[..n], channels, self.sample_rate),
        }
        Ok(n)
    }

    /// `conceal_into_raw` with the stereo→mono downmix `decode` does.
    #[cfg(feature = "std")]
    fn conceal_downmixed(&mut self, next: Option<&[u8]>, samples: usize) -> PcmBuf {
        let mut pcm = vec![0i16; samples * usize::from(self.channels)];
        match self.conceal_into_raw(next, samples, &mut pcm) {
            Ok(n) => pcm.truncate(n),
            Err(_) => return Vec::new(),
        }
        if usize::from(self.channels) == 2 {
            pcm = pcm
                .chunks_exact(2)
                .map(|chunk| ((chunk[0] as i32 + chunk[1] as i32) / 2) as i16)
                .collect();
        }
        pcm
    }
}

impl Decoder for OpusDecoder {
    /// Opus's concealment is part of the decoder: always on.
    fn enable_concealment(&mut self) -> bool {
        true
    }

    /// Conceals with Opus's own packet loss concealment, faded through a
    /// long loss as G.711 and G.722's is: full level for 10 ms, silence at
    /// 60 ms. `samples` must be an Opus frame size (a multiple of 2.5 ms, at
    /// most 20 ms); others fail with `DecodeFailed`.
    fn conceal_into(&mut self, samples: usize, out: &mut [Sample]) -> Result<usize, CodecError> {
        self.conceal_into_raw(None, samples, out)
    }

    /// Override the default to downmix stereo to mono, as `decode` does.
    #[cfg(feature = "std")]
    fn conceal(&mut self, samples: usize) -> PcmBuf {
        self.conceal_downmixed(None, samples)
    }

    /// Rebuilds the lost frame from `next`'s in-band FEC (libopus's
    /// `decode_fec`), concealing what it doesn't cover. `samples` as for
    /// `conceal_into`.
    fn recover_into(
        &mut self,
        next: &[u8],
        samples: usize,
        out: &mut [Sample],
    ) -> Result<usize, CodecError> {
        self.conceal_into_raw(Some(next), samples, out)
    }

    /// Override the default to downmix stereo to mono, as `decode` does.
    #[cfg(feature = "std")]
    fn recover(&mut self, next: &[u8], samples: usize) -> PcmBuf {
        self.conceal_downmixed(Some(next), samples)
    }

    fn decode_into(&mut self, data: &[u8], out: &mut [Sample]) -> Result<usize, CodecError> {
        if data.is_empty() {
            return Ok(0);
        }
        let n = self.decode_into_raw(data, out);
        if n == 0 {
            Err(CodecError::DecodeFailed)
        } else {
            Ok(n)
        }
    }

    fn max_decode_samples(&self, n_bytes: usize) -> usize {
        // One 20ms frame per packet is the typical case; size for stereo
        // to cover the worst case (caller may downmix afterwards).
        let frame_size = (self.sample_rate as usize * 20) / 1000;
        let _ = n_bytes;
        frame_size * 2
    }

    fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    fn channels(&self) -> u16 {
        self.channels
    }

    /// Override the default to preserve back-comat stereo→mono downmix
    /// when the decoder was configured with `channels == 2`.
    #[cfg(feature = "std")]
    fn decode(&mut self, data: &[u8]) -> PcmBuf {
        if data.is_empty() {
            return Vec::new();
        }
        let packet_channels = if data[0] & 0x04 != 0 { 2usize } else { 1 };

        let frame_size = (self.sample_rate as usize * 20) / 1000;
        let max_samples = frame_size * packet_channels;
        let mut pcm = vec![0i16; max_samples];
        let n = self.decode_into_raw(data, &mut pcm);
        pcm.truncate(n);
        if usize::from(self.channels) == 2 {
            pcm = pcm
                .chunks_exact(2)
                .map(|chunk| ((chunk[0] as i32 + chunk[1] as i32) / 2) as i16)
                .collect();
        }
        pcm
    }
}

pub struct OpusEncoder {
    encoder: Box<OpusEncoderRaw>,
    sample_rate: u32,
    channels: u16,
    w_input_f32: [f32; OPUS_MAX_SAMPLES],
}

impl OpusEncoder {
    pub fn new_with_application(sample_rate: u32, channels: u16, application: Application) -> Self {
        let encoder = Box::new(
            OpusEncoderRaw::new(sample_rate as i32, channels as usize, application)
                .expect("Failed to create Opus encoder"),
        );

        Self {
            encoder,
            sample_rate,
            channels,
            w_input_f32: [0.0; OPUS_MAX_SAMPLES],
        }
    }

    /// Create a new Opus encoder instance.
    ///
    /// Keep backward-compatible defaults with pre-0.3.31 behavior:
    /// - VoIP application
    /// - caller can provide mono PCM even when encoder is configured as stereo;
    ///   `encode()` duplicates mono samples to stereo.
    pub fn new(sample_rate: u32, channels: u16) -> Self {
        let mut enc = Self::new_with_application(sample_rate, channels, Application::Voip);
        enc.encoder.bitrate_bps = if channels == 2 { 64000 } else { 48000 };
        enc.encoder.complexity = 5;
        enc.encoder.use_cbr = true;
        enc
    }

    /// Create a default Opus encoder (48kHz, stereo)
    pub fn new_default() -> Self {
        Self::new(48000, 2)
    }

    /// Set the encoder bitrate in bits per second.
    pub fn set_bitrate(&mut self, bitrate_bps: i32) {
        self.encoder.bitrate_bps = bitrate_bps;
    }

    /// Set the encoder complexity (0-10).
    pub fn set_complexity(&mut self, complexity: i32) {
        self.encoder.complexity = complexity;
    }

    /// Enable or disable constant bitrate (CBR) mode.
    pub fn set_cbr(&mut self, cbr: bool) {
        self.encoder.use_cbr = cbr;
    }

    /// Enable or disable in-band FEC: SILK frames also carry a low-bitrate
    /// copy of the previous frame, which a decoder can rebuild a lost packet
    /// from (`Decoder::recover_into`). Only coded when the expected packet
    /// loss (`set_packet_loss_perc`) is above zero and the bitrate leaves
    /// room for it (e.g. 32 kb/s for wideband speech).
    pub fn set_inband_fec(&mut self, enabled: bool) {
        self.encoder.use_inband_fec = enabled;
    }

    /// Set the packet loss the encoder expects, in percent (0-100). It
    /// trades bitrate for robustness, and gates in-band FEC.
    pub fn set_packet_loss_perc(&mut self, percent: i32) {
        self.encoder.packet_loss_perc = percent.clamp(0, 100);
    }

    /// Encode into a caller-provided packet buffer.
    ///
    /// Returns `Some(bytes_written)` on success. Expects `samples` to match
    /// the encoder's channel count (interleaved stereo when `channels == 2`).
    pub fn encode_into_raw(&mut self, samples: &[Sample], output: &mut [u8]) -> Option<usize> {
        let channels = usize::from(self.channels);
        if samples.is_empty() || channels == 0 || !samples.len().is_multiple_of(channels) {
            return None;
        }
        // Fixed scratch cap (heap-free build).
        if samples.len() > OPUS_MAX_SAMPLES {
            return None;
        }

        let frame_size = samples.len() / channels;

        for (dst, &s) in self.w_input_f32[..samples.len()]
            .iter_mut()
            .zip(samples.iter())
        {
            *dst = s as f32 / 32768.0;
        }

        self.encoder
            .encode(&self.w_input_f32[..samples.len()], frame_size, output)
            .ok()
    }

    #[cfg(feature = "std")]
    fn encode_raw(&mut self, samples: &[Sample]) -> Vec<u8> {
        let channels = usize::from(self.channels);
        if samples.is_empty()
            || channels == 0
            || !samples.len().is_multiple_of(channels)
            || samples.len() > OPUS_MAX_SAMPLES
        {
            return Vec::new();
        }

        let frame_size = samples.len() / channels;

        for (dst, &s) in self.w_input_f32[..samples.len()]
            .iter_mut()
            .zip(samples.iter())
        {
            *dst = s as f32 / 32768.0;
        }

        let mut packet = [0u8; OPUS_MAX_PACKET];
        match self
            .encoder
            .encode(&self.w_input_f32[..samples.len()], frame_size, &mut packet)
        {
            Ok(len) => packet[..len].to_vec(),
            Err(_) => Vec::new(),
        }
    }
}

impl Encoder for OpusEncoder {
    fn encode_into(&mut self, samples: &[Sample], out: &mut [u8]) -> Result<usize, CodecError> {
        if samples.is_empty() {
            return Ok(0);
        }
        // Note: this expects `samples` to already match the encoder's channel
        // count (interleaved stereo if `channels == 2`). For the legacy
        // mono→stereo upmix behavior, use the std-only `encode` method.
        match self.encode_into_raw(samples, out) {
            Some(n) => Ok(n),
            None => Err(CodecError::EncodeFailed),
        }
    }

    fn max_encode_bytes(&self, _n_samples: usize) -> usize {
        // Per RFC 6716 the maximum Opus packet size is 1275 bytes.
        1275
    }

    fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    fn channels(&self) -> u16 {
        self.channels
    }

    /// Override the default to preserve back-comat mono→stereo upmix
    /// when the encoder was configured with `channels == 2`.
    #[cfg(feature = "std")]
    fn encode(&mut self, samples: &[Sample]) -> Vec<u8> {
        if self.channels == 2 {
            // mono→stereo upmix into a local scratch buffer (no allocation).
            if samples.len() > OPUS_MAX_FRAME {
                return Vec::new();
            }
            let mut stereo = [0i16; OPUS_MAX_SAMPLES];
            for (i, &sample) in samples.iter().enumerate() {
                stereo[2 * i] = sample;
                stereo[2 * i + 1] = sample;
            }
            return self.encode_raw(&stereo[..samples.len() * 2]);
        }
        self.encode_raw(samples)
    }
}

// The tests below exercise the std-only allocating `encode`/`decode`
// conveniences (including the stereo-downmix handling), so they only compile
// with `std`. The `*_into` trait API is covered portably by
// `test_opus_into_roundtrip` at the bottom, which also runs in `no_std`.
#[cfg(all(test, feature = "std"))]
mod tests {
    use super::*;

    fn make_mono_pcm_20ms() -> Vec<i16> {
        (0..960).map(|i| ((i * 100) % 32767) as i16).collect()
    }

    /// 20 ms frames of a continuous 220 Hz tone at 48 kHz.
    fn tone_frame(frame: usize) -> Vec<i16> {
        (0..960)
            .map(|i| {
                let t = (frame * 960 + i) as f32 / 48000.0;
                (8000.0 * (2.0 * core::f32::consts::PI * 220.0 * t).sin()) as i16
            })
            .collect()
    }

    fn rms(s: &[i16]) -> f64 {
        (s.iter().map(|x| (*x as f64).powi(2)).sum::<f64>() / s.len() as f64).sqrt()
    }

    #[test]
    fn test_opus_conceals_a_lost_frame_with_its_own_plc() {
        let mut enc = OpusEncoder::new_default();
        let mut dec = OpusDecoder::new_default();
        for f in 0..10 {
            dec.decode(&enc.encode(&tone_frame(f)));
        }
        // frame 10 is lost
        let concealed = dec.conceal(960);
        assert_eq!(concealed.len(), 960, "downmixed like decode");
        assert!(
            rms(&concealed) > 1000.0,
            "concealment is silent: rms {}",
            rms(&concealed)
        );
        // the stream carries on after the loss
        enc.encode(&tone_frame(10));
        let next = dec.decode(&enc.encode(&tone_frame(11)));
        assert_eq!(next.len(), 960);
        assert!(rms(&next) > 1000.0);
    }

    /// Voiced, speech-like 48 kHz mono: 8 harmonics of a pitch gliding
    /// 120 → 180 Hz under a slow envelope.
    fn voice(frames: usize) -> Vec<Vec<i16>> {
        let mut phase = 0f32;
        let pcm: Vec<i16> = (0..frames * 960)
            .map(|i| {
                let t = i as f32 / 48000.0;
                let f0 = 120.0 + 60.0 * (t / 0.6).min(1.0);
                phase += 2.0 * core::f32::consts::PI * f0 / 48000.0;
                let env = 0.6 + 0.4 * (2.0 * core::f32::consts::PI * 2.0 * t).sin();
                let v: f32 = (1..=8).map(|k| (k as f32 * phase).sin() / k as f32).sum();
                (6000.0 * env * v) as i16
            })
            .collect();
        pcm.chunks(960).map(<[i16]>::to_vec).collect()
    }

    #[test]
    fn test_opus_recovers_a_lost_frame_from_inband_fec() {
        // speech at 32 kb/s (SILK or Hybrid), told to expect loss so it codes
        // FEC; at 16 kb/s fullband Hybrid it codes none
        let mut enc = OpusEncoder::new(48000, 1);
        enc.set_bitrate(32000);
        enc.set_inband_fec(true);
        enc.set_packet_loss_perc(20);
        let packets: Vec<Vec<u8>> = voice(40).iter().map(|f| enc.encode(f)).collect();
        // SILK or Hybrid: FEC lives in the SILK layer (TOC configs 0-15)
        let has_silk = |p: &[u8]| p[0] >> 3 < 16;
        assert!(packets.iter().all(|p| has_silk(p)), "coded without SILK");

        let mut reference = OpusDecoder::new(48000, 1);
        let clean: Vec<Vec<i16>> = packets.iter().map(|p| reference.decode(p)).collect();
        let error_db = |out: &[i16], clean: &[i16]| {
            let err: f64 = out
                .iter()
                .zip(clean)
                .map(|(a, b)| (*a as f64 - *b as f64).powi(2))
                .sum();
            let sig: f64 = clean.iter().map(|b| (*b as f64).powi(2)).sum();
            10.0 * (err / sig).log10()
        };
        let (mut recovered_db, mut concealed_db) = (0f64, 0f64);
        let losses = [10, 17, 23, 31];
        for &lost in &losses {
            let mut recovering = OpusDecoder::new(48000, 1);
            let mut concealing = OpusDecoder::new(48000, 1);
            for p in &packets[..lost] {
                recovering.decode(p);
                concealing.decode(p);
            }
            let recovered = recovering.recover(&packets[lost + 1], 960);
            let concealed = concealing.conceal(960);
            assert_eq!(recovered.len(), 960);
            recovered_db += error_db(&recovered, &clean[lost]);
            concealed_db += error_db(&concealed, &clean[lost]);
            // `next` is not consumed: it decodes as usual afterwards
            assert_eq!(recovering.decode(&packets[lost + 1]).len(), 960);
        }
        let n = losses.len() as f64;
        let (recovered_db, concealed_db) = (recovered_db / n, concealed_db / n);
        println!(
            "lost frame error: recovered {recovered_db:.1} dB, concealed {concealed_db:.1} dB"
        );
        // measured: recovered -11.7 dB, concealed -5.0 dB
        assert!(
            recovered_db < concealed_db - 4.0,
            "FEC no better than concealment: {recovered_db:.1} vs {concealed_db:.1} dB"
        );
    }

    #[test]
    fn test_opus_concealment_fades_through_a_long_loss() {
        let mut enc = OpusEncoder::new(48000, 1);
        let packets: Vec<Vec<u8>> = voice(12).iter().map(|f| enc.encode(f)).collect();
        let mut dec = OpusDecoder::new(48000, 1);
        for p in &packets[..10] {
            dec.decode(p);
        }
        let levels: Vec<f64> = (0..5).map(|_| rms(&dec.conceal(960))).collect();
        // full for 10 ms, then 20% per 10 ms: silent from 60 ms
        assert!(
            levels[0] > 500.0,
            "first frame of a loss is audible: {levels:?}"
        );
        assert!(
            levels[1] < levels[0] && levels[2] < levels[1],
            "not fading: {levels:?}"
        );
        assert_eq!(levels[3..], [0.0, 0.0], "not silent from 60 ms: {levels:?}");
    }

    #[test]
    fn test_opus_decoding_after_a_faded_loss_ramps_in() {
        let mut enc = OpusEncoder::new(48000, 1);
        let packets: Vec<Vec<u8>> = voice(12).iter().map(|f| enc.encode(f)).collect();
        let mut dec = OpusDecoder::new(48000, 1);
        // opus-rs on its own, given the same calls
        let mut raw = OpusDecoderRaw::new(48000, 1).unwrap();
        let mut raw_out = [0f32; 960];
        for p in &packets[..10] {
            dec.decode(p);
            raw.decode(p, 960, &mut raw_out).unwrap();
        }
        for _ in 0..5 {
            dec.conceal(960);
            raw.decode(&[], 960, &mut raw_out).unwrap();
        }
        let resumed = dec.decode(&packets[10]);
        raw.decode(&packets[10], 960, &mut raw_out).unwrap();
        let expected: Vec<i16> = raw_out
            .iter()
            .map(|f| (f * 32768.0).clamp(-32768.0, 32767.0) as i16)
            .collect();
        // from silence over 4 ms, then as decoded
        let ramp = 48000 * RECOVER_MS as usize / 1000;
        assert!(resumed[0].unsigned_abs() <= expected[0].unsigned_abs() / 100 + 1);
        assert_eq!(resumed[ramp..], expected[ramp..]);
        // the loss is over: the next one starts at full level
        dec.conceal(960);
        assert_eq!(dec.loss.concealed(), 960);
    }

    #[test]
    fn test_opus_recover_without_fec_conceals() {
        let mut enc = OpusEncoder::new(48000, 1);
        let packets: Vec<Vec<u8>> = voice(12).iter().map(|f| enc.encode(f)).collect();
        let mut dec = OpusDecoder::new(48000, 1);
        for p in &packets[..10] {
            dec.decode(p);
        }
        // no FEC coded: the lost frame is concealed, still audible
        let recovered = dec.recover(&packets[11], 960);
        assert_eq!(recovered.len(), 960);
        assert!(rms(&recovered) > 500.0, "rms {}", rms(&recovered));
    }

    #[test]
    fn test_opus_conceal_into_is_interleaved_and_checks_the_buffer() {
        let mut enc = OpusEncoder::new_default();
        let mut dec = OpusDecoder::new_default();
        dec.decode(&enc.encode(&tone_frame(0)));
        let mut out = [0i16; 1920];
        // stereo decoder: 960 samples per channel, interleaved
        assert_eq!(dec.conceal_into(960, &mut out), Ok(1920));
        let mut small = [0i16; 100];
        assert_eq!(
            dec.conceal_into(960, &mut small),
            Err(CodecError::BufferTooSmall)
        );
    }

    #[test]
    fn test_opus_encode_decode_20ms_produces_960_mono_samples() {
        let mut enc = OpusEncoder::new_default();
        let pcm = make_mono_pcm_20ms();
        let opus_pkt = enc.encode(&pcm);
        assert!(!opus_pkt.is_empty(), "encoder should produce output");

        // TOC byte bit 2 should be set (stereo flag from mono→stereo upmix)
        assert!(opus_pkt[0] & 0x04 != 0, "packet should be stereo");

        let mut dec = OpusDecoder::new_default();
        let decoded = dec.decode(&opus_pkt);

        // 20ms at 48kHz mono = 960 samples
        assert_eq!(
            decoded.len(),
            960,
            "decoder should downmix stereo→mono and output 960 samples, got {}",
            decoded.len()
        );
    }

    #[test]
    fn test_opus_consecutive_frames_all_produce_960() {
        let mut enc = OpusEncoder::new_default();
        let pcm = make_mono_pcm_20ms();
        let opus_pkt = enc.encode(&pcm);

        let mut dec = OpusDecoder::new_default();
        for i in 0..5 {
            let decoded = dec.decode(&opus_pkt);
            assert_eq!(
                decoded.len(),
                960,
                "frame {} should produce 960 mono samples, got {}",
                i,
                decoded.len()
            );
        }
    }

    #[test]
    fn test_opus_decoder_output_has_reasonable_energy() {
        let mut enc = OpusEncoder::new_default();
        // 440Hz sine at 48kHz, 20ms
        let pcm: Vec<i16> = (0..960)
            .map(|i| {
                let t = i as f64 / 48000.0;
                (16384.0 * (2.0 * std::f64::consts::PI * 440.0 * t).sin()) as i16
            })
            .collect();
        let opus_pkt = enc.encode(&pcm);

        let mut dec = OpusDecoder::new_default();
        let decoded = dec.decode(&opus_pkt);
        assert_eq!(decoded.len(), 960);

        let energy: f64 =
            decoded.iter().map(|&s| (s as f64).powi(2)).sum::<f64>() / decoded.len() as f64;
        let rms = energy.sqrt();
        assert!(
            rms > 100.0 && rms < 20000.0,
            "decoded audio RMS {} should be in reasonable range",
            rms
        );
    }

    #[test]
    fn test_opus_decoder_handles_mono_packet_gracefully() {
        // Create a mono Opus encoder, encode PCM → mono Opus packet
        let mut mono_enc = OpusEncoder::new(48000, 1);
        let pcm = make_mono_pcm_20ms();
        let mono_pkt = mono_enc.encode(&pcm);
        assert!(!mono_pkt.is_empty());
        // Mono packet TOC bit 2 should be 0
        assert!(
            mono_pkt[0] & 0x04 == 0,
            "mono encoder should produce mono packet"
        );

        // Decode with default stereo decoder — should handle mono→stereo transition
        let mut dec = OpusDecoder::new_default();
        let decoded = dec.decode(&mono_pkt);
        assert_eq!(
            decoded.len(),
            960,
            "first mono packet should produce exactly 960 mono samples, got {}",
            decoded.len()
        );

        // Second mono packet should also be 960
        let decoded2 = dec.decode(&mono_pkt);
        assert_eq!(
            decoded2.len(),
            960,
            "second mono packet should also produce 960 mono samples, got {}",
            decoded2.len()
        );
    }

    #[test]
    fn test_opus_stereo_packet_downmix_produces_960() {
        let mut enc = OpusEncoder::new_default();
        let pcm = make_mono_pcm_20ms();
        let stereo_pkt = enc.encode(&pcm);
        assert!(stereo_pkt[0] & 0x04 != 0);

        let mut dec = OpusDecoder::new_default();
        let decoded = dec.decode(&stereo_pkt);
        assert_eq!(
            decoded.len(),
            960,
            "stereo packet should downmix to 960 mono samples"
        );
    }
}

/// Portable roundtrip through the trait `encode_into`/`decode_into` API
/// (compiles with or without `std`; the no_std opus path).
#[cfg(all(test, feature = "opus"))]
mod no_std_tests {
    use super::*;
    use alloc::vec::Vec;

    #[test]
    fn test_opus_into_roundtrip() {
        let mut enc = OpusEncoder::new_default();
        let mut dec = OpusDecoder::new(48000, 1);
        let pcm: Vec<i16> = (0..960).map(|i| ((i * 100) % 32767) as i16).collect();

        let mut packet = alloc::vec![0u8; enc.max_encode_bytes(pcm.len())];
        let n = enc
            .encode_into(&pcm, &mut packet)
            .expect("packet sized by max_encode_bytes");
        packet.truncate(n);
        assert!(!packet.is_empty(), "encoder should produce output");

        let mut out = alloc::vec![0i16; dec.max_decode_samples(packet.len())];
        let samples = dec
            .decode_into(&packet, &mut out)
            .expect("out sized by max_decode_samples");
        assert_eq!(samples, 960, "20 ms at 48 kHz must decode to 960 samples");
        let rms: f64 = out[..samples]
            .iter()
            .map(|&s| (s as f64) * (s as f64))
            .sum::<f64>()
            / samples as f64;
        assert!(rms.sqrt() > 1000.0, "decoded signal should keep energy");
    }
}