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
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2023.                                                         /
// This Source Code Form is subject to the terms of the Mozilla Public License,/
// v. 2.0. If a copy of the MPL was not distributed with this file, You can    /
// obtain one at http://mozilla.org/MPL/2.0/.                                  /
////////////////////////////////////////////////////////////////////////////////

pub(crate) mod decoder;
pub(crate) mod encoder;

use std::error::Error;
use std::ffi::c_void;
use std::fmt::Display;

pub use decoder::{DynamicDecoder, SpeexDecoder};
pub use encoder::{DynamicEncoder, SpeexEncoder};
use speex_sys::{SpeexMode, SPEEX_MODEID_NB, SPEEX_MODEID_UWB, SPEEX_MODEID_WB};

/// Possible modes for the encoder and decoder.
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ModeId {
    NarrowBand = SPEEX_MODEID_NB,
    WideBand = SPEEX_MODEID_WB,
    UltraWideBand = SPEEX_MODEID_UWB,
}

impl From<i32> for ModeId {
    fn from(value: i32) -> Self {
        match value {
            SPEEX_MODEID_NB => ModeId::NarrowBand,
            SPEEX_MODEID_WB => ModeId::WideBand,
            SPEEX_MODEID_UWB => ModeId::UltraWideBand,
            _ => panic!("Invalid mode id"),
        }
    }
}

/// Possible submodes for the narrowband mode.
///
/// As wideband and ultra-wideband modes both embed narrowband, this is also
/// used for those.
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum NbSubmodeId {
    /// 2150 bps "vocoder-like" mode for comfort noise
    VocoderLike = 1,
    /// 3.95 kbps very low bit-rate mode
    ExtremeLow = 8,
    /// 5.95 kbps very low bit-rate mode
    VeryLow = 2,
    /// 8 kbps low bit-rate mode
    Low = 3,
    /// 11 kbps medium bit-rate mode
    Medium = 4,
    /// 15 kbps high bit-rate mode
    High = 5,
    /// 18.2 kbps very high bit-rate mode
    VeryHigh = 6,
    /// 24.6 kbps very high bit-rate mode
    ExtremeHigh = 7,
}

impl From<i32> for NbSubmodeId {
    fn from(value: i32) -> Self {
        match value {
            1 => NbSubmodeId::VocoderLike,
            2 => NbSubmodeId::VeryLow,
            3 => NbSubmodeId::Low,
            4 => NbSubmodeId::Medium,
            5 => NbSubmodeId::High,
            6 => NbSubmodeId::VeryHigh,
            7 => NbSubmodeId::ExtremeHigh,
            8 => NbSubmodeId::ExtremeLow,
            _ => panic!("Invalid submode id"),
        }
    }
}

/// Possible submodes for the Wideband mode.
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum WbSubmodeId {
    /// disables innovation quantization entirely
    NoQuantize = 1,
    /// enables innovation quantization, but with a lower rate than the default
    QuantizedLow = 2,
    /// enables innovation quantization with the default rate
    QuantizedMedium = 3,
    /// enables innovation quantization, but with a higher rate than the default
    QuantizedHigh = 4,
}

impl From<i32> for WbSubmodeId {
    fn from(value: i32) -> Self {
        match value {
            1 => WbSubmodeId::NoQuantize,
            2 => WbSubmodeId::QuantizedLow,
            3 => WbSubmodeId::QuantizedMedium,
            4 => WbSubmodeId::QuantizedHigh,
            _ => panic!("Invalid submode id"),
        }
    }
}

/// Possible submodes for the UWB mode.
///
/// While this is an enum, UWB mode only has one submode, so it's effectively a
/// constant.
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum UwbSubmodeId {
    Only = WbSubmodeId::NoQuantize as i32,
}

impl From<i32> for UwbSubmodeId {
    fn from(value: i32) -> Self {
        match value {
            1 => UwbSubmodeId::Only,
            _ => panic!("Invalid submode id"),
        }
    }
}

impl ModeId {
    pub fn get_mode(self) -> &'static SpeexMode {
        unsafe {
            let ptr = speex_sys::speex_lib_get_mode(self as i32);
            // speexmodes are hard constants defined within the codebase itself, so the
            // backing memory *should* always be valid. Should.
            let reference: &'static SpeexMode = &*ptr;
            reference
        }
    }

    pub fn get_frame_size(self) -> i32 {
        unsafe {
            let ptr = speex_sys::speex_lib_get_mode(self as i32);
            let mut frame_size = 0;
            let frame_size_ptr = &mut frame_size as *mut i32;
            speex_sys::speex_mode_query(
                ptr,
                speex_sys::SPEEX_MODE_FRAME_SIZE,
                frame_size_ptr as *mut c_void,
            );
            frame_size
        }
    }
}

/// Error type for the control functions of the encoder and decoder.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ControlError {
    /// The request type passed to the control function was invalid
    /// The parameter is the request type that was passed
    UnknownRequest(i32),
    /// The parameter passed to the control function was invalid (and probably
    /// caused a segfault, making this error unreachable)
    InvalidParameter,
}

impl Display for ControlError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ControlError::UnknownRequest(id) => {
                write!(
                    f,
                    "Unknown request type passed to a control function ({id})"
                )
            }
            ControlError::InvalidParameter => write!(f, "Invalid parameter"),
        }
    }
}

impl Error for ControlError {}

mod private {
    pub trait Sealed {}
}

/// Trait for the control functions of the encoder and decoder
///
/// This trait is implemented for both the encoder and decoder, and provides a
/// common interface for the control functions of both.
///
/// `ctl` is the only function that needs to be implemented, and is used to call
/// the control functions of the underlying speex library.
///
/// This trait is sealed, and cannot be implemented outside of this crate.
pub trait ControlFunctions: private::Sealed {
    /// Internal function used to convert the error codes returned by the
    /// control function into a result type
    fn check_error(err_code: i32, param: Option<i32>) -> Result<(), ControlError> {
        match err_code {
            0 => Ok(()),
            -1 => Err(ControlError::UnknownRequest(param.unwrap())),
            -2 => Err(ControlError::InvalidParameter),
            _ => panic!("Unknown error code passed to make_error(), this is a bug"),
        }
    }

    /// Calls a control function of the underlying speex library
    ///
    /// # Safety
    ///
    /// Implementations of this function call the control functions of the
    /// underlying speex library, and as such are unsafe. The caller must
    /// ensure that the parameters passed to this function are valid.
    unsafe fn ctl(&mut self, request: i32, ptr: *mut c_void) -> Result<(), ControlError>;

    /// Gets the frame size (in samples) of the encoder/decoder
    fn get_frame_size(&mut self) -> i32 {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_FRAME_SIZE, ptr).unwrap();
        }
        state
    }

    /// Sets whether Variable BitRate is enabled or not
    fn set_vbr(&mut self, vbr: bool) {
        let state = if vbr { 1 } else { 0 };
        let ptr = &state as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_VBR, ptr).unwrap();
        }
    }

    /// Gets whether Variable BitRate is enabled or not
    fn get_vbr(&mut self) -> bool {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_VBR, ptr).unwrap();
        }
        state != 0
    }

    /// Sets the VBR quality of the encoder/decoder
    ///
    /// The value should be between 0 and 10, with 10 being the highest quality.
    fn set_vbr_quality(&mut self, quality: f32) {
        let ptr = &quality as *const f32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_VBR_QUALITY, ptr).unwrap();
        }
    }

    /// Gets the VBR quality of the encoder/decoder
    fn get_vbr_quality(&mut self) -> f32 {
        let mut state = 0.0;
        let ptr = &mut state as *mut f32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_VBR_QUALITY, ptr).unwrap();
        }
        state
    }

    /// Sets whether Voice Activity Detection is enabled or not
    fn set_vad(&mut self, vad: bool) {
        let state = if vad { 1 } else { 0 };
        let ptr = &state as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_VAD, ptr).unwrap();
        }
    }

    /// Gets whether Voice Activity Detection is enabled or not
    fn get_vad(&mut self) -> bool {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_VAD, ptr).unwrap();
        }
        state != 0
    }

    /// Sets the Average BitRate of the encoder/decoder
    fn set_abr(&mut self, abr: i32) {
        let ptr = &abr as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_ABR, ptr).unwrap();
        }
    }

    /// Gets the Average BitRate of the encoder/decoder
    fn get_abr(&mut self) -> i32 {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_ABR, ptr).unwrap();
        }
        state
    }

    /// Sets the overall quality of the encoder/decoder
    /// The value should be between 0 and 10, with 10 being the highest quality.
    /// Default is 8.
    fn set_quality(&mut self, quality: i32) {
        let ptr = &quality as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_QUALITY, ptr).unwrap();
        }
    }

    /// Sets the current bitrate of the encoder/decoder
    fn set_bitrate(&mut self, bitrate: i32) {
        let ptr = &bitrate as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_BITRATE, ptr).unwrap();
        }
    }

    /// Gets the current bitrate of the encoder/decoder
    fn get_bitrate(&mut self) -> i32 {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_BITRATE, ptr).unwrap();
        }
        state
    }

    /// Sets the sampling rate used for bitrate computation
    fn set_sampling_rate(&mut self, samplingrate: i32) {
        let ptr = &samplingrate as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_SAMPLING_RATE, ptr).unwrap();
        }
    }

    /// Gets the sampling rate used for bitrate computation
    fn get_sampling_rate(&mut self) -> i32 {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_SAMPLING_RATE, ptr).unwrap();
        }
        state
    }

    /// resets the encoder/decoder memories to zero
    fn reset_state(&mut self) {
        unsafe {
            self.ctl(speex_sys::SPEEX_RESET_STATE, std::ptr::null_mut())
                .unwrap();
        }
    }

    /// Sets whether submode encoding is done in each frame
    ///
    /// Note that false breaks the specification for the format
    fn set_submode_encoding(&mut self, submode: bool) {
        let state = if submode { 1 } else { 0 };
        let ptr = &state as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_SUBMODE_ENCODING, ptr)
                .unwrap();
        }
    }

    /// Gets whether submode encoding is enabled or not
    fn get_submode_encoding(&mut self) -> bool {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_SUBMODE_ENCODING, ptr)
                .unwrap();
        }
        state != 0
    }

    /// Gets the lookahead value currently in use by the encoder/decoder
    ///
    /// Sum the lookahead of a Speex decoder and the lookahead of a Speex
    /// encoder to get the total lookahead.
    fn get_lookahead(&mut self) -> i32 {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_LOOKAHEAD, ptr).unwrap();
        }
        state
    }

    /// Sets tuning for Packet-Loss Concealment (expected loss rate)
    fn set_plc_tuning(&mut self, tuning: i32) {
        let ptr = &tuning as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_PLC_TUNING, ptr).unwrap();
        }
    }

    /// Gets current Packet-Loss Concealment tuning value
    fn get_plc_tuning(&mut self) -> i32 {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_PLC_TUNING, ptr).unwrap();
        }
        state
    }

    /// Sets the max bit-rate allowed in VBR mode
    fn set_vbr_max_bitrate(&mut self, max_bitrate: i32) {
        let ptr = &max_bitrate as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_VBR_MAX_BITRATE, ptr).unwrap();
        }
    }

    /// Gets the max bit-rate allowed in VBR mode
    fn get_vbr_max_bitrate(&mut self) -> i32 {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_VBR_MAX_BITRATE, ptr).unwrap();
        }
        state
    }

    /// Enables or disables highpass filtering of the input/output
    fn set_highpass(&mut self, highpass: bool) {
        let state = if highpass { 1 } else { 0 };
        let ptr = &state as *const i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_SET_HIGHPASS, ptr).unwrap();
        }
    }

    /// Gets whether highpass filtering of the input/output is enabled
    fn get_highpass(&mut self) -> bool {
        let mut state = 0;
        let ptr = &mut state as *mut i32 as *mut c_void;
        unsafe {
            self.ctl(speex_sys::SPEEX_GET_HIGHPASS, ptr).unwrap();
        }
        state != 0
    }
}

#[macro_export]
macro_rules! dynamic_mapping {
    ($name:expr, $enum_name:ident, $inner:pat => $action:expr) => {
        match $name {
            $enum_name::Nb($inner) => $action,
            $enum_name::Wb($inner) => $action,
            $enum_name::Uwb($inner) => $action,
        }
    };
}

#[macro_export]
macro_rules! shared_functions {
    ($enum_name:ident) => {
        /// Gets the frame size (in samples) of the encoder/decoder
        pub fn get_frame_size(&mut self) -> i32 {
            dynamic_mapping!(self, $enum_name, inner => inner.get_frame_size())
        }

        /// Sets whether Variable BitRate is enabled or not
        pub fn set_vbr(&mut self, vbr: bool) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_vbr(vbr))
        }

        /// Gets whether Variable BitRate is enabled or not
        pub fn get_vbr(&mut self) -> bool {
            dynamic_mapping!(self, $enum_name, inner => inner.get_vbr())
        }

        /// Sets the VBR quality of the encoder/decoder
        ///
        /// The value should be between 0 and 10, with 10 being the highest quality.
        pub fn set_vbr_quality(&mut self, quality: f32) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_vbr_quality(quality))
        }

        /// Gets the VBR quality of the encoder/decoder
        pub fn get_vbr_quality(&mut self) -> f32 {
            dynamic_mapping!(self, $enum_name, inner => inner.get_vbr_quality())
        }

        /// Sets whether Voice Activity Detection is enabled or not
        pub fn set_vad(&mut self, vad: bool) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_vad(vad))
        }

        /// Gets whether Voice Activity Detection is enabled or not
        pub fn get_vad(&mut self) -> bool {
            dynamic_mapping!(self, $enum_name, inner => inner.get_vad())
        }

        /// Sets the Average BitRate of the encoder/decoder
        pub fn set_abr(&mut self, abr: i32) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_abr(abr))
        }

        /// Gets the Average BitRate of the encoder/decoder
        pub fn get_abr(&mut self) -> i32 {
            dynamic_mapping!(self, $enum_name, inner => inner.get_abr())
        }

        /// Sets the overall quality of the encoder/decoder
        /// The value should be between 0 and 10, with 10 being the highest quality.
        /// Default is 8.
        pub fn set_quality(&mut self, quality: i32) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_quality(quality))
        }

        /// Sets the current bitrate of the encoder/decoder
        pub fn set_bitrate(&mut self, bitrate: i32) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_bitrate(bitrate))
        }

        /// Gets the current bitrate of the encoder/decoder
        pub fn get_bitrate(&mut self) -> i32 {
            dynamic_mapping!(self, $enum_name, inner => inner.get_bitrate())
        }

        /// Sets the sampling rate used for bitrate computation
        pub fn set_sampling_rate(&mut self, samplingrate: i32) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_sampling_rate(samplingrate))
        }

        /// Gets the sampling rate used for bitrate computation
        pub fn get_sampling_rate(&mut self) -> i32 {
            dynamic_mapping!(self, $enum_name, inner => inner.get_sampling_rate())
        }

        /// resets the encoder/decoder memories to zero
        pub fn reset_state(&mut self) {
            dynamic_mapping!(self, $enum_name, inner => inner.reset_state())
        }

        /// Sets whether submode encoding is done in each frame
        ///
        /// Note that false breaks the specification for the format
        pub fn set_submode_encoding(&mut self, submode: bool) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_submode_encoding(submode))
        }

        /// Gets whether submode encoding is enabled or not
        pub fn get_submode_encoding(&mut self) -> bool {
            dynamic_mapping!(self, $enum_name, inner => inner.get_submode_encoding())
        }

        /// Gets the lookahead value currently in use by the encoder/decoder
        ///
        /// Sum the lookahead of a Speex decoder and the lookahead of a Speex
        /// encoder to get the total lookahead.
        pub fn get_lookahead(&mut self) -> i32 {
            dynamic_mapping!(self, $enum_name, inner => inner.get_lookahead())
        }

        /// Sets tuning for Packet-Loss Concealment (expected loss rate)
        pub fn set_plc_tuning(&mut self, tuning: i32) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_plc_tuning(tuning))
        }

        /// Gets current Packet-Loss Concealment tuning value
        pub fn get_plc_tuning(&mut self) -> i32 {
            dynamic_mapping!(self, $enum_name, inner => inner.get_plc_tuning())
        }

        /// Sets the max bit-rate allowed in VBR mode
        pub fn set_vbr_max_bitrate(&mut self, max_bitrate: i32) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_vbr_max_bitrate(max_bitrate))
        }

        /// Gets the max bit-rate allowed in VBR mode
        pub fn get_vbr_max_bitrate(&mut self) -> i32 {
            dynamic_mapping!(self, $enum_name, inner => inner.get_vbr_max_bitrate())
        }

        /// Enables or disables highpass filtering of the input/output
        pub fn set_highpass(&mut self, highpass: bool) {
            dynamic_mapping!(self, $enum_name, inner => inner.set_highpass(highpass))
        }

        /// Gets whether highpass filtering of the input/output is enabled
        pub fn get_highpass(&mut self) -> bool {
            dynamic_mapping!(self, $enum_name, inner => inner.get_highpass())
        }
    };
}

/// Marker trait used to specify the mode of the de/encoder.
pub trait CoderMode {}

/// Narrowband mode (8kHz)
///
/// This is a marker type used to specify the mode of the de/encoder.
pub enum NbMode {}
impl CoderMode for NbMode {}
/// Wideband mode (16kHz)
///
/// This is a marker type used to specify the mode of the de/encoder.
pub enum WbMode {}
impl CoderMode for WbMode {}
/// Ultra-wideband mode (32kHz)
///
/// This is a marker type used to specify the mode of the de/encoder.
pub enum UwbMode {}
impl CoderMode for UwbMode {}