glint-audio 0.12.0

Audio codec suite — encodes MP3, AAC-LC and Opus, decodes those plus Ogg-Vorbis, FLAC and WAV. Safe Rust wrapper, no system codec libraries.
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
//! Safe Rust wrapper for the glint MP3 encoder.

use glint_sys::*;
use std::slice;

/// A safe wrapper around the glint MP3 encoder.
pub struct Encoder {
    handle: glint_t,
    samples_per_frame: usize,
    channels: usize,
}

// SAFETY: The glint encoder uses no thread-local state and all access
// goes through &mut self, so it is safe to send across threads.
unsafe impl Send for Encoder {}

impl Encoder {
    /// Create a new MP3 encoder.
    ///
    /// Returns an error if the configuration is invalid (e.g. unsupported
    /// sample rate or bitrate).
    pub fn new(sample_rate: u32, channels: u32, bitrate: u32) -> Result<Self, &'static str> {
        unsafe {
            let rc = glint_check_config(sample_rate as i32, bitrate as i32);
            if rc != 0 {
                return Err("unsupported sample rate or bitrate");
            }

            let mode = if channels == 1 { 0 } else { 2 }; // MONO or JOINT
            let cfg = glint_config {
                sample_rate: sample_rate as i32,
                num_channels: channels as i32,
                mode,
                bitrate: bitrate as i32,
                path: 0,    // default
                simd: 0,    // auto
                quality: 1, // NORMAL
                vbr: 0,
                vbr_quality: 0,
            };

            let handle = glint_create(&cfg);
            if handle.is_null() {
                return Err("glint_create returned null");
            }

            let spf = glint_samples_per_frame(handle) as usize;

            Ok(Encoder {
                handle,
                samples_per_frame: spf,
                channels: channels as usize,
            })
        }
    }

    /// Create an MP3 encoder with explicit knobs. `mode`: 0 mono, 1 dual,
    /// 2 joint, 3 stereo. `quality`: 0 speed / 1 normal / 2 best. `path`:
    /// 0 default, 1 double, 2 fixed. `vbr_quality` Some(0..9) selects VBR.
    pub fn with_options(sample_rate: u32, channels: u32, bitrate: u32,
        mode: u32, quality: u32, path: u32, vbr_quality: Option<u32>)
        -> Result<Self, &'static str> {
        unsafe {
            let cfg = glint_config {
                sample_rate: sample_rate as i32,
                num_channels: channels as i32,
                mode: mode as i32,
                bitrate: bitrate as i32,
                path: path as i32,
                simd: 0,
                quality: quality as i32,
                vbr: if vbr_quality.is_some() { 1 } else { 0 },
                vbr_quality: vbr_quality.unwrap_or(0) as i32,
            };
            let handle = glint_create(&cfg);
            if handle.is_null() {
                return Err("glint_create returned null");
            }
            let spf = glint_samples_per_frame(handle) as usize;
            Ok(Encoder { handle, samples_per_frame: spf,
                channels: channels as usize })
        }
    }

    /// Number of samples per channel expected by each call to `encode`.
    pub fn samples_per_frame(&self) -> usize {
        self.samples_per_frame
    }

    /// Number of channels this encoder was configured with.
    pub fn channels(&self) -> usize {
        self.channels
    }

    /// Encode one frame of interleaved 16-bit PCM audio.
    ///
    /// `pcm` should contain `samples_per_frame * channels` samples in
    /// interleaved order. If fewer samples are provided, the remainder
    /// is zero-padded.
    pub fn encode(&mut self, pcm: &[i16]) -> Vec<u8> {
        let spf = self.samples_per_frame;
        let ch = self.channels;

        // De-interleave into per-channel buffers
        let mut channel_bufs: Vec<Vec<i16>> = (0..ch).map(|_| vec![0i16; spf]).collect();
        for (i, &sample) in pcm.iter().enumerate() {
            let c = i % ch;
            let s = i / ch;
            if s < spf {
                channel_bufs[c][s] = sample;
            }
        }

        let ptrs: Vec<*const i16> = channel_bufs.iter().map(|b| b.as_ptr()).collect();
        let mut out_size: i32 = 0;

        unsafe {
            let data = glint_encode(self.handle, ptrs.as_ptr(), &mut out_size);
            if data.is_null() || out_size <= 0 {
                return Vec::new();
            }
            slice::from_raw_parts(data, out_size as usize).to_vec()
        }
    }

    /// Encode one frame of interleaved 32-bit float PCM audio.
    ///
    /// Samples should be in the range [-1.0, 1.0]. Layout is the same
    /// as `encode`.
    pub fn encode_float(&mut self, pcm: &[f32]) -> Vec<u8> {
        let spf = self.samples_per_frame;
        let ch = self.channels;

        let mut channel_bufs: Vec<Vec<f32>> = (0..ch).map(|_| vec![0.0f32; spf]).collect();
        for (i, &sample) in pcm.iter().enumerate() {
            let c = i % ch;
            let s = i / ch;
            if s < spf {
                channel_bufs[c][s] = sample;
            }
        }

        let ptrs: Vec<*const f32> = channel_bufs.iter().map(|b| b.as_ptr()).collect();
        let mut out_size: i32 = 0;

        unsafe {
            let data = glint_encode_float(self.handle, ptrs.as_ptr(), &mut out_size);
            if data.is_null() || out_size <= 0 {
                return Vec::new();
            }
            slice::from_raw_parts(data, out_size as usize).to_vec()
        }
    }

    /// Flush the encoder, returning any remaining MP3 data.
    pub fn flush(&mut self) -> Vec<u8> {
        let mut out_size: i32 = 0;
        unsafe {
            let data = glint_flush(self.handle, &mut out_size);
            if data.is_null() || out_size <= 0 {
                return Vec::new();
            }
            slice::from_raw_parts(data, out_size as usize).to_vec()
        }
    }
}

impl Drop for Encoder {
    fn drop(&mut self) {
        unsafe {
            glint_destroy(self.handle);
        }
    }
}

/// AAC-LC encoder (ADTS output). Same interleaved-PCM conventions as
/// [`Encoder`]; one `encode` call consumes `samples_per_frame()` (1024)
/// samples per channel and returns one ADTS frame. `flush` returns the two
/// tail frames and must be called at end of stream (encoder delay: 2048
/// samples; the first frame is a silence priming frame).
pub struct AacEncoder {
    handle: glint_t,
    samples_per_frame: usize,
    channels: usize,
}

unsafe impl Send for AacEncoder {}

impl AacEncoder {
    /// quality: 0 = speed, 1 = normal (default), 2 = best.
    pub fn new(
        sample_rate: u32,
        channels: u32,
        bitrate: u32,
        quality: u32,
    ) -> Result<Self, &'static str> {
        unsafe {
            let cfg = glint_aac_config {
                sample_rate: sample_rate as i32,
                num_channels: channels as i32,
                bitrate: bitrate as i32,
                quality: quality as i32,
                vbr: 0,
                vbr_quality: 0,
                reserved: [0; 4],
            };
            let handle = glint_aac_create(&cfg);
            if handle.is_null() {
                return Err("glint_aac_create returned null");
            }
            let spf = glint_aac_samples_per_frame(handle) as usize;
            Ok(AacEncoder { handle, samples_per_frame: spf, channels: channels as usize })
        }
    }

    /// Like [`AacEncoder::new`] but with `vbr_quality` Some(0..9) to select
    /// constant-quality VBR (None = CBR at `bitrate`).
    pub fn with_options(sample_rate: u32, channels: u32, bitrate: u32,
        quality: u32, vbr_quality: Option<u32>) -> Result<Self, &'static str> {
        unsafe {
            let cfg = glint_aac_config {
                sample_rate: sample_rate as i32,
                num_channels: channels as i32,
                bitrate: bitrate as i32,
                quality: quality as i32,
                vbr: if vbr_quality.is_some() { 1 } else { 0 },
                vbr_quality: vbr_quality.unwrap_or(0) as i32,
                reserved: [0; 4],
            };
            let handle = glint_aac_create(&cfg);
            if handle.is_null() {
                return Err("glint_aac_create returned null");
            }
            let spf = glint_aac_samples_per_frame(handle) as usize;
            Ok(AacEncoder { handle, samples_per_frame: spf, channels: channels as usize })
        }
    }

    pub fn samples_per_frame(&self) -> usize {
        self.samples_per_frame
    }

    pub fn channels(&self) -> usize {
        self.channels
    }

    /// Encode one frame of interleaved 16-bit PCM (zero-padded if short).
    pub fn encode(&mut self, pcm: &[i16]) -> Vec<u8> {
        let spf = self.samples_per_frame;
        let ch = self.channels;
        let mut channel_bufs: Vec<Vec<i16>> = (0..ch).map(|_| vec![0i16; spf]).collect();
        for (i, &sample) in pcm.iter().enumerate() {
            let c = i % ch;
            let s = i / ch;
            if s < spf {
                channel_bufs[c][s] = sample;
            }
        }
        let ptrs: Vec<*const i16> = channel_bufs.iter().map(|b| b.as_ptr()).collect();
        let mut out_size: i32 = 0;
        unsafe {
            let data = glint_aac_encode(self.handle, ptrs.as_ptr(), &mut out_size);
            if data.is_null() || out_size <= 0 {
                return Vec::new();
            }
            slice::from_raw_parts(data, out_size as usize).to_vec()
        }
    }

    /// Flush the encoder, returning the two tail ADTS frames.
    pub fn flush(&mut self) -> Vec<u8> {
        let mut out_size: i32 = 0;
        unsafe {
            let data = glint_aac_flush(self.handle, &mut out_size);
            if data.is_null() || out_size <= 0 {
                return Vec::new();
            }
            slice::from_raw_parts(data, out_size as usize).to_vec()
        }
    }
}

impl Drop for AacEncoder {
    fn drop(&mut self) {
        unsafe {
            glint_aac_destroy(self.handle);
        }
    }
}

/// Convenience function: encode an entire buffer of interleaved 16-bit PCM
/// to AAC (ADTS) in one call. quality: 0 speed / 1 normal / 2 best.
pub fn encode_pcm_aac(
    pcm: &[i16],
    sample_rate: u32,
    channels: u32,
    bitrate: u32,
    quality: u32,
) -> Vec<u8> {
    let mut enc = match AacEncoder::new(sample_rate, channels, bitrate, quality) {
        Ok(e) => e,
        Err(_) => return Vec::new(),
    };
    let frame_len = enc.samples_per_frame() * enc.channels();
    let mut aac = Vec::new();
    for chunk in pcm.chunks(frame_len) {
        aac.extend(enc.encode(chunk));
    }
    aac.extend(enc.flush());
    aac
}

/// Convenience function: encode an entire buffer of interleaved 16-bit PCM
/// to MP3 in one call.
pub fn encode_pcm(pcm: &[i16], sample_rate: u32, channels: u32, bitrate: u32) -> Vec<u8> {
    let mut enc = match Encoder::new(sample_rate, channels, bitrate) {
        Ok(e) => e,
        Err(_) => return Vec::new(),
    };

    let frame_len = enc.samples_per_frame() * enc.channels();
    let mut mp3 = Vec::new();

    for chunk in pcm.chunks(frame_len) {
        mp3.extend(enc.encode(chunk));
    }
    mp3.extend(enc.flush());
    mp3
}


/// CELT-only Opus encoder: 48 kHz interleaved f32 PCM in, complete Opus
/// packets out. Frame sizes 120/240/480/960 samples per channel.
pub struct OpusEncoder {
    handle: glint_sys::glint_t,
    channels: usize,
}

unsafe impl Send for OpusEncoder {}

impl OpusEncoder {
    pub fn new(channels: u32, bitrate: u32, vbr: bool) -> Result<Self, &'static str> {
        let handle = unsafe {
            glint_sys::glint_opus_enc_create(channels as i32, bitrate as i32, vbr as i32)
        };
        if handle.is_null() {
            return Err("invalid Opus encoder config");
        }
        Ok(OpusEncoder { handle, channels: channels as usize })
    }

    /// Encode one frame of interleaved f32 PCM (len = frame_size * channels,
    /// frame_size in {120, 240, 480, 960}). Returns the packet.
    pub fn encode(&mut self, pcm: &[f32]) -> Result<Vec<u8>, &'static str> {
        let frame = pcm.len() / self.channels;
        let mut out = vec![0u8; 1500];
        let n = unsafe {
            glint_sys::glint_opus_encode(
                self.handle, pcm.as_ptr(), frame as i32, out.as_mut_ptr(), out.len() as i32)
        };
        if n < 0 {
            return Err("opus encode failed (frame must be 120/240/480/960)");
        }
        out.truncate(n as usize);
        Ok(out)
    }

    pub fn final_range(&self) -> u32 {
        unsafe { glint_sys::glint_opus_enc_final_range(self.handle) }
    }
}

impl Drop for OpusEncoder {
    fn drop(&mut self) {
        unsafe { glint_sys::glint_opus_enc_destroy(self.handle) };
    }
}

/// Opus decoder (SILK/CELT/hybrid, PLC, SILK in-band FEC), output rates
/// 8/12/16/24/48 kHz, interleaved f32 PCM out.
pub struct OpusDecoder {
    handle: glint_sys::glint_t,
    channels: usize,
}

unsafe impl Send for OpusDecoder {}

impl OpusDecoder {
    pub fn new(channels: u32, sample_rate: u32) -> Result<Self, &'static str> {
        let handle = unsafe {
            glint_sys::glint_opus_dec_create(channels as i32, sample_rate as i32)
        };
        if handle.is_null() {
            return Err("invalid Opus decoder config");
        }
        Ok(OpusDecoder { handle, channels: channels as usize })
    }

    /// Decode one packet to interleaved f32 PCM.
    pub fn decode(&mut self, packet: &[u8]) -> Result<Vec<f32>, &'static str> {
        let mut pcm = vec![0f32; 2 * 5760];
        let n = unsafe {
            glint_sys::glint_opus_decode(
                self.handle, packet.as_ptr(), packet.len() as i32, pcm.as_mut_ptr(), 5760)
        };
        if n < 0 {
            return Err("opus decode failed");
        }
        pcm.truncate(n as usize * self.channels);
        Ok(pcm)
    }

    /// Conceal a LOST packet of `frame_size` samples per channel;
    /// `next_packet` (the one after the loss) supplies SILK in-band FEC
    /// when present.
    pub fn decode_lost(
        &mut self,
        frame_size: usize,
        next_packet: Option<&[u8]>,
    ) -> Result<Vec<f32>, &'static str> {
        let mut pcm = vec![0f32; 2 * 5760];
        let n = unsafe {
            match next_packet {
                Some(p) => glint_sys::glint_opus_decode_fec(
                    self.handle, p.as_ptr(), p.len() as i32, pcm.as_mut_ptr(),
                    frame_size as i32),
                None => glint_sys::glint_opus_decode_fec(
                    self.handle, std::ptr::null(), 0, pcm.as_mut_ptr(),
                    frame_size as i32),
            }
        };
        if n < 0 {
            return Err("opus concealment failed");
        }
        pcm.truncate(n as usize * self.channels);
        Ok(pcm)
    }

    pub fn final_range(&self) -> u32 {
        unsafe { glint_sys::glint_opus_dec_final_range(self.handle) }
    }
}

impl Drop for OpusDecoder {
    fn drop(&mut self) {
        unsafe { glint_sys::glint_opus_dec_destroy(self.handle) };
    }
}

#[cfg(test)]
mod opus_tests {
    use super::*;

    #[test]
    fn opus_roundtrip_final_range() {
        let mut enc = OpusEncoder::new(2, 96000, false).unwrap();
        let mut dec = OpusDecoder::new(2, 48000).unwrap();
        for f in 0..5 {
            let mut pcm = vec![0f32; 960 * 2];
            for i in 0..960 {
                let t = (f * 960 + i) as f32 / 48000.0;
                pcm[i * 2] = 0.4 * (2.0 * std::f32::consts::PI * 440.0 * t).sin();
                pcm[i * 2 + 1] = 0.3 * (2.0 * std::f32::consts::PI * 660.0 * t).sin();
            }
            let pkt = enc.encode(&pcm).unwrap();
            let out = dec.decode(&pkt).unwrap();
            assert_eq!(out.len(), 960 * 2);
            assert_eq!(enc.final_range(), dec.final_range());
        }
        let lost = dec.decode_lost(960, None).unwrap();
        assert_eq!(lost.len(), 960 * 2);
    }
}


/// A decoded frame's stream parameters.
#[derive(Debug, Clone, Copy)]
pub struct DecFrameInfo {
    pub sample_rate: u32,
    pub channels: u32,
    pub samples: u32,
    pub frame_bytes: u32,
}

macro_rules! frame_decoder {
    ($name:ident, $create:ident, $decode:ident, $info:ident,
     $destroy:ident, $doc:literal) => {
        #[doc = $doc]
        pub struct $name {
            handle: glint_sys::glint_t,
        }
        unsafe impl Send for $name {}
        impl $name {
            pub fn new() -> Result<Self, &'static str> {
                let handle = unsafe { glint_sys::$create() };
                if handle.is_null() {
                    return Err("decoder create failed");
                }
                Ok($name { handle })
            }

            /// Parse one frame header; None if data does not start with a
            /// valid frame sync.
            pub fn frame_info(&self, data: &[u8]) -> Option<DecFrameInfo> {
                let mut fi = glint_sys::GlintDecFrameInfo::default();
                let ok = unsafe {
                    glint_sys::$info(data.as_ptr(), data.len() as i32, &mut fi)
                };
                if ok < 0 {
                    return None;
                }
                Some(DecFrameInfo {
                    sample_rate: fi.sample_rate as u32,
                    channels: fi.channels as u32,
                    samples: fi.samples as u32,
                    frame_bytes: fi.frame_bytes as u32,
                })
            }

            /// Decode ONE frame at data[0]. Returns interleaved f32 PCM
            /// (empty while an MP3 reservoir fills) and the frame info.
            pub fn decode_frame(
                &mut self,
                data: &[u8],
            ) -> Result<(Vec<f32>, DecFrameInfo), &'static str> {
                let mut pcm = vec![0f32; 2 * 1152];
                let mut fi = glint_sys::GlintDecFrameInfo::default();
                let n = unsafe {
                    glint_sys::$decode(self.handle, data.as_ptr(),
                        data.len() as i32, pcm.as_mut_ptr(), &mut fi)
                };
                if n < 0 {
                    return Err("decode failed");
                }
                let ch = fi.channels.max(1) as usize;
                pcm.truncate(n as usize * ch);
                Ok((pcm, DecFrameInfo {
                    sample_rate: fi.sample_rate as u32,
                    channels: fi.channels as u32,
                    samples: n as u32,
                    frame_bytes: fi.frame_bytes as u32,
                }))
            }

            /// Decode a whole stream (walks frames, skips ID3v2). Returns
            /// interleaved f32 PCM.
            pub fn decode(&mut self, data: &[u8]) -> Vec<f32> {
                let mut off = 0usize;
                if data.len() > 10 && &data[..3] == b"ID3" {
                    let sz = ((data[6] as usize & 0x7f) << 21)
                        | ((data[7] as usize & 0x7f) << 14)
                        | ((data[8] as usize & 0x7f) << 7)
                        | (data[9] as usize & 0x7f);
                    off = 10 + sz;
                }
                let mut out = Vec::new();
                while off + 7 <= data.len() {
                    let info = match self.frame_info(&data[off..]) {
                        Some(i) => i,
                        None => {
                            off += 1;
                            continue;
                        }
                    };
                    let fb = info.frame_bytes as usize;
                    if fb == 0 || off + fb > data.len() {
                        break;
                    }
                    if let Ok((pcm, _)) = self.decode_frame(&data[off..off + fb]) {
                        out.extend_from_slice(&pcm);
                    }
                    off += fb;
                }
                out
            }
        }
        impl Drop for $name {
            fn drop(&mut self) {
                unsafe { glint_sys::$destroy(self.handle) };
            }
        }
    };
}

frame_decoder!(Mp3Decoder, glint_mp3_dec_create, glint_mp3_decode,
    glint_mp3_frame_info, glint_mp3_dec_destroy,
    "MPEG-1/2 Layer III decoder (keeps a bit reservoir across frames).");
frame_decoder!(AacDecoder, glint_aac_dec_create, glint_aac_decode,
    glint_aac_frame_info, glint_aac_dec_destroy,
    "ADTS AAC-LC decoder.");

#[cfg(test)]
mod decoder_tests {
    use super::*;

    #[test]
    fn mp3_encode_decode_roundtrip() {
        let mut enc = Encoder::new(44100, 2, 128).unwrap();
        let spf = enc.samples_per_frame();
        let mut stream = Vec::new();
        let mut phase = 0.0f64;
        for _ in 0..40 {
            let mut pcm = vec![0i16; spf * 2];
            for i in 0..spf {
                let s = 0.4 * (2.0 * std::f64::consts::PI * 440.0 * phase
                    / 44100.0).sin();
                phase += 1.0;
                pcm[i * 2] = (s * 20000.0) as i16;
                pcm[i * 2 + 1] = (s * 15000.0) as i16;
            }
            stream.extend_from_slice(&enc.encode(&pcm));
        }
        stream.extend_from_slice(&enc.flush());
        let mut dec = Mp3Decoder::new().unwrap();
        let out = dec.decode(&stream);
        assert!(out.len() > 40000, "decoded {} samples", out.len());
    }
}


// ---------------------------------------------------------------------------
// High-level convenience: resample + whole-file decode (PLAN buckets A+B)
// ---------------------------------------------------------------------------

/// Resample interleaved f32 PCM (±1.0) from `sr_in` to `sr_out` with a
/// Kaiser-windowed sinc kernel (anti-aliased, unity passband). `pcm` is
/// `frames * channels` interleaved samples; returns the resampled buffer.
/// Pass-through (a clone) when the rates match.
pub fn resample(pcm: &[f32], channels: u32, sr_in: u32, sr_out: u32) -> Vec<f32> {
    if channels == 0 || pcm.is_empty() {
        return Vec::new();
    }
    let ch = channels as i32;
    let in_frames = pcm.len() as i32 / ch;
    let mut out_frames: core::ffi::c_int = 0;
    let ptr = unsafe {
        glint_sys::glint_resample(pcm.as_ptr(), in_frames, ch,
            sr_in as i32, sr_out as i32, &mut out_frames)
    };
    if ptr.is_null() {
        return Vec::new();
    }
    let total = out_frames as usize * channels as usize;
    let out = unsafe { std::slice::from_raw_parts(ptr, total) }.to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    out
}

/// Decoded audio: interleaved f32 PCM plus its stream parameters.
#[derive(Debug, Clone)]
pub struct DecodedAudio {
    pub pcm: Vec<f32>,
    pub sample_rate: u32,
    pub channels: u32,
}

/// Decode a whole encoded stream (MP3 / AAC-LC / Ogg-Opus / Ogg-Vorbis /
/// FLAC, format auto-detected from the header) to interleaved f32 PCM.
/// Returns `None` on unrecognized or corrupt input.
pub fn decode_audio(data: &[u8]) -> Option<DecodedAudio> {
    if data.is_empty() {
        return None;
    }
    let mut sr: core::ffi::c_int = 0;
    let mut ch: core::ffi::c_int = 0;
    let mut frames: core::ffi::c_int = 0;
    let ptr = unsafe {
        glint_sys::glint_decode_audio(data.as_ptr(), data.len() as i32,
            &mut sr, &mut ch, &mut frames)
    };
    if ptr.is_null() || ch <= 0 {
        return None;
    }
    let total = frames as usize * ch as usize;
    let pcm = unsafe { std::slice::from_raw_parts(ptr, total) }.to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some(DecodedAudio { pcm, sample_rate: sr as u32, channels: ch as u32 })
}

/// Decode a complete in-memory Ogg-Vorbis I stream (identification +
/// comment + setup headers followed by audio packets) to interleaved f32
/// PCM at its native rate — the shape the `.sf3` soundfont sample path
/// needs. Mirrors [`OpusDecoder`]; [`decode_audio`] also decodes Vorbis
/// transparently. Returns `None` on a non-Vorbis or corrupt buffer.
pub fn decode_vorbis(ogg: &[u8]) -> Option<DecodedAudio> {
    if ogg.is_empty() {
        return None;
    }
    let mut sr: core::ffi::c_int = 0;
    let mut ch: core::ffi::c_int = 0;
    let mut frames: core::ffi::c_int = 0;
    let ptr = unsafe {
        glint_sys::glint_vorbis_decode(ogg.as_ptr(), ogg.len() as i32,
            &mut sr, &mut ch, &mut frames)
    };
    if ptr.is_null() || ch <= 0 {
        return None;
    }
    let total = frames as usize * ch as usize;
    let pcm = unsafe { std::slice::from_raw_parts(ptr, total) }.to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some(DecodedAudio { pcm, sample_rate: sr as u32, channels: ch as u32 })
}

/// Decode a complete in-memory native FLAC stream to interleaved f32 PCM at
/// its native rate. [`decode_audio`] also decodes FLAC transparently.
pub fn decode_flac(flac: &[u8]) -> Option<DecodedAudio> {
    if flac.is_empty() {
        return None;
    }
    let mut sr: core::ffi::c_int = 0;
    let mut ch: core::ffi::c_int = 0;
    let mut frames: core::ffi::c_int = 0;
    let ptr = unsafe {
        glint_sys::glint_flac_decode(flac.as_ptr(), flac.len() as i32,
            &mut sr, &mut ch, &mut frames)
    };
    if ptr.is_null() || ch <= 0 {
        return None;
    }
    let total = frames as usize * ch as usize;
    let pcm = unsafe { std::slice::from_raw_parts(ptr, total) }.to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some(DecodedAudio { pcm, sample_rate: sr as u32, channels: ch as u32 })
}

/// Encode interleaved 48 kHz f32 PCM (±1.0, `frames` per channel, 1-2
/// channels) to a complete Ogg-Opus file (CELT-only, 20 ms frames). `vbr`
/// selects unconstrained VBR. Input MUST be 48 kHz — resample first with
/// [`resample`]. Returns `None` on error.
pub fn encode_opus_file(pcm: &[f32], channels: u32, bitrate_bps: u32,
    vbr: bool) -> Option<Vec<u8>> {
    if channels == 0 || channels > 2 || pcm.is_empty() {
        return None;
    }
    let frames = pcm.len() as i32 / channels as i32;
    let mut out_size: core::ffi::c_int = 0;
    let ptr = unsafe {
        glint_sys::glint_opus_encode_file(pcm.as_ptr(), frames,
            channels as i32, bitrate_bps as i32, vbr as i32, &mut out_size)
    };
    if ptr.is_null() || out_size <= 0 {
        return None;
    }
    let data = unsafe { std::slice::from_raw_parts(ptr, out_size as usize) }
        .to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some(data)
}

/// Decode a whole stream to interleaved f32 PCM, resampled to `out_rate`
/// (0 = keep native). Auto-detects MP3/AAC/Opus/Vorbis/FLAC
/// (incl. Opus surround).
pub fn decode_audio_rate(data: &[u8], out_rate: u32) -> Option<DecodedAudio> {
    if data.is_empty() {
        return None;
    }
    let (mut sr, mut ch, mut fr) = (0i32, 0i32, 0i32);
    let ptr = unsafe {
        glint_sys::glint_decode_audio_ex(data.as_ptr(), data.len() as i32,
            out_rate as i32, 0, &mut sr, &mut ch, &mut fr)
    } as *mut f32;
    if ptr.is_null() || ch <= 0 {
        return None;
    }
    let total = fr as usize * ch as usize;
    let pcm = unsafe { std::slice::from_raw_parts(ptr, total) }.to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some(DecodedAudio { pcm, sample_rate: sr as u32, channels: ch as u32 })
}

/// Decode a whole stream to interleaved i16 PCM, resampled to `out_rate`
/// (0 = keep native). Returns (pcm, sample_rate, channels).
pub fn decode_audio_i16(data: &[u8], out_rate: u32)
    -> Option<(Vec<i16>, u32, u32)> {
    if data.is_empty() {
        return None;
    }
    let (mut sr, mut ch, mut fr) = (0i32, 0i32, 0i32);
    let ptr = unsafe {
        glint_sys::glint_decode_audio_ex(data.as_ptr(), data.len() as i32,
            out_rate as i32, 1, &mut sr, &mut ch, &mut fr)
    } as *mut i16;
    if ptr.is_null() || ch <= 0 {
        return None;
    }
    let total = fr as usize * ch as usize;
    let pcm = unsafe { std::slice::from_raw_parts(ptr, total) }.to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some((pcm, sr as u32, ch as u32))
}

/// Output codec for [`encode_audio`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Codec {
    Mp3 = 0,
    Aac = 1,
    Opus = 2,
}

/// One-call encode: interleaved f32 PCM (±1.0) at any rate / 1-2 channels
/// -> a complete MP3 / AAC-LC / Ogg-Opus stream. The input is auto-
/// resampled to a codec-valid rate (Opus->48k, MP3/AAC->nearest
/// supported). `bitrate_kbps` is the CBR/target rate; `vbr_quality`
/// Some(0..9) selects VBR. Returns `None` on error.
pub fn encode_audio(pcm: &[f32], channels: u32, sample_rate: u32,
    codec: Codec, bitrate_kbps: u32, vbr_quality: Option<u32>,
    quality: u32) -> Option<Vec<u8>> {
    if channels == 0 || channels > 2 || pcm.is_empty() {
        return None;
    }
    let frames = pcm.len() as i32 / channels as i32;
    let mut out_size: core::ffi::c_int = 0;
    let ptr = unsafe {
        glint_sys::glint_encode_audio(pcm.as_ptr(), frames, channels as i32,
            sample_rate as i32, codec as i32, bitrate_kbps as i32,
            vbr_quality.map(|q| q as i32).unwrap_or(-1), quality as i32,
            &mut out_size)
    };
    if ptr.is_null() || out_size <= 0 {
        return None;
    }
    let data = unsafe { std::slice::from_raw_parts(ptr, out_size as usize) }
        .to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some(data)
}

/// Read a WAV buffer (PCM 8/16/24/32, IEEE float 32/64, A-law, mu-law,
/// EXTENSIBLE) into interleaved f32 PCM. Returns `None` on malformed or
/// unsupported input.
pub fn read_wav(data: &[u8]) -> Option<DecodedAudio> {
    if data.is_empty() {
        return None;
    }
    let mut sr: core::ffi::c_int = 0;
    let mut ch: core::ffi::c_int = 0;
    let mut frames: core::ffi::c_int = 0;
    let ptr = unsafe {
        glint_sys::glint_wav_read(data.as_ptr(), data.len() as i32,
            &mut sr, &mut ch, &mut frames)
    };
    if ptr.is_null() || ch <= 0 {
        return None;
    }
    let total = frames as usize * ch as usize;
    let pcm = unsafe { std::slice::from_raw_parts(ptr, total) }.to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some(DecodedAudio { pcm, sample_rate: sr as u32, channels: ch as u32 })
}

/// Encode interleaved f32 PCM (±1.0) to a WAV file buffer. `bits`:
/// 8/16/24/32 integer PCM, or 32/64 with `float_fmt` for IEEE float
/// (invalid combos fall back to 16-bit). Returns `None` on error.
pub fn write_wav(pcm: &[f32], channels: u32, sample_rate: u32, bits: u32,
    float_fmt: bool) -> Option<Vec<u8>> {
    if channels == 0 {
        return None;
    }
    let frames = pcm.len() as i32 / channels as i32;
    let mut out_size: core::ffi::c_int = 0;
    let ptr = unsafe {
        glint_sys::glint_wav_write(pcm.as_ptr(), frames, channels as i32,
            sample_rate as i32, bits as i32, float_fmt as i32, &mut out_size)
    };
    if ptr.is_null() || out_size <= 0 {
        return None;
    }
    let data = unsafe { std::slice::from_raw_parts(ptr, out_size as usize) }
        .to_vec();
    unsafe { glint_sys::glint_free(ptr as *mut core::ffi::c_void) };
    Some(data)
}

#[cfg(test)]
mod buckets_ab_tests {
    use super::*;

    #[test]
    fn resample_length_and_passthrough() {
        let n = 4410usize;
        let pcm: Vec<f32> = (0..n)
            .map(|i| (2.0 * std::f32::consts::PI * 200.0 * i as f32 / 44100.0).sin())
            .collect();
        let up = resample(&pcm, 1, 44100, 48000);
        let expect = (n as f64 * 48000.0 / 44100.0).round() as usize;
        assert!((up.len() as i64 - expect as i64).abs() <= 2,
            "got {} want ~{}", up.len(), expect);
        let same = resample(&pcm, 1, 44100, 44100);
        assert_eq!(same.len(), n);
    }

    #[test]
    fn resample_preserves_amplitude() {
        let n = 8820usize;
        let pcm: Vec<f32> = (0..n)
            .map(|i| 0.5 * (2.0 * std::f32::consts::PI * 300.0 * i as f32 / 44100.0).sin())
            .collect();
        let out = resample(&pcm, 1, 44100, 22050);
        let peak = out[100..out.len() - 100].iter().fold(0f32, |m, &x| m.max(x.abs()));
        assert!(peak > 0.45 && peak < 0.55, "peak {}", peak);
    }

    #[test]
    fn decode_audio_mp3_and_aac() {
        // Build a short sine, encode to MP3 and AAC, decode both back.
        let spf = 1152usize;
        let mut mp3enc = Encoder::new(44100, 2, 128).unwrap();
        let mut mp3 = Vec::new();
        let mut aacenc = AacEncoder::new(44100, 2, 128, 1).unwrap();
        let mut aac = Vec::new();
        let mut phase = 0.0f64;
        for _ in 0..50 {
            let mut pcm = vec![0i16; spf * 2];
            for i in 0..spf {
                let s = (0.4 * (2.0 * std::f64::consts::PI * 440.0 * phase / 44100.0).sin()
                    * 20000.0) as i16;
                phase += 1.0;
                pcm[i * 2] = s;
                pcm[i * 2 + 1] = s;
            }
            mp3.extend_from_slice(&mp3enc.encode(&pcm));
            // AAC frame is 1024; feed the first 1024 of this 1152 block.
            let mut af = vec![0i16; 1024 * 2];
            af.copy_from_slice(&pcm[..1024 * 2]);
            aac.extend_from_slice(&aacenc.encode(&af));
        }
        mp3.extend_from_slice(&mp3enc.flush());
        aac.extend_from_slice(&aacenc.flush());

        let d1 = decode_audio(&mp3).expect("mp3 decodes");
        assert_eq!(d1.channels, 2);
        assert!(d1.pcm.len() > 40000, "mp3 {} samples", d1.pcm.len());

        let d2 = decode_audio(&aac).expect("aac decodes");
        assert_eq!(d2.channels, 2);
        assert!(d2.pcm.len() > 40000, "aac {} samples", d2.pcm.len());
    }

    #[test]
    fn encoder_knobs() {
        // MP3 with explicit mode/quality/VBR and AAC VBR both decode back.
        let spf = 1152usize;
        let mut enc = Encoder::with_options(44100, 2, 192, 3, 2, 0, Some(3))
            .expect("mp3 opts");
        let mut mp3 = Vec::new();
        let mut aac_enc = AacEncoder::with_options(44100, 2, 128, 1, Some(2))
            .expect("aac opts");
        let mut aac = Vec::new();
        let mut phase = 0.0f64;
        for _ in 0..50 {
            let mut pcm = vec![0i16; spf * 2];
            for i in 0..spf {
                let s = (0.4 * (2.0 * std::f64::consts::PI * 440.0 * phase / 44100.0).sin()
                    * 20000.0) as i16;
                phase += 1.0;
                pcm[i * 2] = s;
                pcm[i * 2 + 1] = s;
            }
            mp3.extend_from_slice(&enc.encode(&pcm));
            let mut af = vec![0i16; 1024 * 2];
            af.copy_from_slice(&pcm[..1024 * 2]);
            aac.extend_from_slice(&aac_enc.encode(&af));
        }
        mp3.extend_from_slice(&enc.flush());
        aac.extend_from_slice(&aac_enc.flush());
        assert_eq!(decode_audio(&mp3).unwrap().channels, 2);
        assert_eq!(decode_audio(&aac).unwrap().channels, 2);
    }

    #[test]
    fn decode_dtype_and_rate() {
        let (sr, ch, n) = (44100u32, 2usize, 44100usize);
        let mut pcm = vec![0f32; n * ch];
        for i in 0..n {
            let v = 0.4 * (2.0 * std::f32::consts::PI * 440.0 * i as f32 / sr as f32).sin();
            pcm[i * 2] = v;
            pcm[i * 2 + 1] = v;
        }
        let mp3 = encode_audio(&pcm, ch as u32, sr, Codec::Mp3, 128, None, 1)
            .expect("encode");
        let f = decode_audio_rate(&mp3, 0).expect("float");
        assert_eq!((f.sample_rate, f.channels), (44100, 2));
        let (i16pcm, isr, ich) = decode_audio_i16(&mp3, 0).expect("i16");
        assert_eq!((isr, ich), (44100, 2));
        assert_eq!(i16pcm.len(), f.pcm.len());
        let r = decode_audio_rate(&mp3, 24000).expect("resampled");
        assert_eq!(r.sample_rate, 24000);
    }

    #[test]
    fn encode_audio_all_codecs_odd_rate() {
        // 37 kHz (invalid for MP3/AAC) sine -> each codec, decode back.
        let (sr, ch, n) = (37000u32, 2usize, 37000usize);
        let mut pcm = vec![0f32; n * ch];
        for i in 0..n {
            let v = 0.4 * (2.0 * std::f32::consts::PI * 440.0 * i as f32 / sr as f32).sin();
            pcm[i * 2] = v;
            pcm[i * 2 + 1] = v;
        }
        for (codec, want) in [(Codec::Mp3, 32000u32), (Codec::Aac, 32000),
            (Codec::Opus, 48000)] {
            let data = encode_audio(&pcm, ch as u32, sr, codec, 128, None, 1)
                .expect("encode");
            assert!(data.len() > 1000, "{:?} {}", codec, data.len());
            let d = decode_audio(&data).expect("decode");
            assert_eq!(d.sample_rate, want, "{:?} rate", codec);
            assert_eq!(d.channels, 2, "{:?} ch", codec);
        }
    }

    #[test]
    fn wav_bit_depths_roundtrip() {
        let n = 4096usize;
        let pcm: Vec<f32> = (0..n)
            .map(|i| 0.5 * (2.0 * std::f32::consts::PI * 300.0 * i as f32 / 44100.0).sin())
            .collect();
        for &(bits, flt, tol) in &[(8u32, false, 0.02f32), (16, false, 2e-4),
            (24, false, 1e-5), (32, false, 1e-6), (32, true, 1e-6)] {
            let wav = write_wav(&pcm, 1, 44100, bits, flt).expect("write");
            let a = read_wav(&wav).expect("read");
            assert_eq!((a.sample_rate, a.channels), (44100, 1));
            let err = pcm.iter().zip(a.pcm.iter())
                .map(|(x, y)| (x - y).abs()).fold(0f32, f32::max);
            assert!(err < tol, "{}-bit err {}", bits, err);
        }
    }

    #[test]
    fn opus_file_encode_decode() {
        // 48 kHz f32 sine -> encode_opus_file -> decode_audio.
        let n = 48000usize;
        let mut pcm = vec![0f32; n * 2];
        for i in 0..n {
            let v = 0.4 * (2.0 * std::f32::consts::PI * 440.0 * i as f32 / 48000.0).sin();
            pcm[i * 2] = v;
            pcm[i * 2 + 1] = v;
        }
        let opus = encode_opus_file(&pcm, 2, 96000, false).expect("opus encodes");
        assert_eq!(&opus[..4], b"OggS");
        let dec = decode_audio(&opus).expect("opus decodes");
        assert_eq!((dec.sample_rate, dec.channels), (48000, 2));
        assert!(dec.pcm.len() > 40000 * 2, "opus {} samples", dec.pcm.len());
    }
}