speex_safe/
lib.rs

1////////////////////////////////////////////////////////////////////////////////
2// Copyright (c) 2023.                                                         /
3// This Source Code Form is subject to the terms of the Mozilla Public License,/
4// v. 2.0. If a copy of the MPL was not distributed with this file, You can    /
5// obtain one at http://mozilla.org/MPL/2.0/.                                  /
6////////////////////////////////////////////////////////////////////////////////
7
8pub(crate) mod bits;
9pub(crate) mod header;
10pub(crate) mod mode;
11pub(crate) mod stereo_state;
12
13use std::ffi::{c_char, c_void, CStr};
14use std::ptr::null;
15
16pub use bits::SpeexBits;
17pub use header::SpeexHeader;
18pub use mode::{
19    ControlError,
20    ControlFunctions,
21    DynamicDecoder,
22    DynamicEncoder,
23    ModeId,
24    NbMode,
25    NbSubmodeId,
26    SpeexDecoder,
27    SpeexEncoder,
28    UwbMode,
29    UwbSubmodeId,
30    WbMode,
31    WbSubmodeId,
32};
33use speex_sys::{
34    speex_lib_ctl,
35    SPEEX_LIB_GET_EXTRA_VERSION,
36    SPEEX_LIB_GET_MAJOR_VERSION,
37    SPEEX_LIB_GET_MICRO_VERSION,
38    SPEEX_LIB_GET_MINOR_VERSION,
39    SPEEX_LIB_GET_VERSION_STRING,
40};
41pub use stereo_state::SpeexStereoState;
42
43pub fn get_major_version() -> i32 {
44    let mut major_version = 0;
45    unsafe {
46        let ptr = &mut major_version as *mut i32;
47        let ptr = ptr as *mut c_void;
48        speex_lib_ctl(SPEEX_LIB_GET_MAJOR_VERSION, ptr);
49    }
50    major_version
51}
52
53pub fn get_minor_version() -> i32 {
54    let mut minor_version = 0;
55    unsafe {
56        let ptr = &mut minor_version as *mut i32;
57        let ptr = ptr as *mut c_void;
58        speex_lib_ctl(SPEEX_LIB_GET_MINOR_VERSION, ptr);
59    }
60    minor_version
61}
62
63pub fn get_micro_version() -> i32 {
64    let mut micro_version = 0;
65    unsafe {
66        let ptr = &mut micro_version as *mut i32;
67        let ptr = ptr as *mut c_void;
68        speex_lib_ctl(SPEEX_LIB_GET_MICRO_VERSION, ptr);
69    }
70    micro_version
71}
72
73pub fn get_extra_version() -> String {
74    let cstr = unsafe {
75        let mut str = null();
76        let str_ptr = &mut str as *mut *const c_char;
77        speex_lib_ctl(SPEEX_LIB_GET_EXTRA_VERSION, str_ptr as *mut c_void);
78        CStr::from_ptr(str)
79    };
80    cstr.to_string_lossy().into_owned()
81}
82
83pub fn get_version_string() -> String {
84    let cstr = unsafe {
85        let mut str = null();
86        let str_ptr = &mut str as *mut *const c_char;
87        speex_lib_ctl(SPEEX_LIB_GET_VERSION_STRING, str_ptr as *mut c_void);
88        CStr::from_ptr(str)
89    };
90    cstr.to_string_lossy().into_owned()
91}
92
93#[cfg(test)]
94mod test {
95    use super::*;
96
97    #[test]
98    fn correct_version_outputs() {
99        let version_string = get_version_string();
100        assert_eq!(&version_string, "speex-1.2.1")
101    }
102}