samplerate_rs/
converter_type.rs1use crate::error::{Error, ErrorCode};
2use libsamplerate::*;
3use std::ffi::CStr;
4
5#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
8pub enum ConverterType {
9 SincBestQuality = SRC_SINC_BEST_QUALITY as isize,
10 SincMediumQuality = SRC_SINC_MEDIUM_QUALITY as isize,
11 SincFastest = SRC_SINC_FASTEST as isize,
12 ZeroOrderHold = SRC_ZERO_ORDER_HOLD as isize,
13 Linear = SRC_LINEAR as isize,
14}
15
16impl ConverterType {
17 pub fn from_int(value: isize) -> Result<ConverterType, Error> {
19 match value {
20 0 => Ok(ConverterType::SincBestQuality),
21 1 => Ok(ConverterType::SincMediumQuality),
22 2 => Ok(ConverterType::SincFastest),
23 3 => Ok(ConverterType::ZeroOrderHold),
24 4 => Ok(ConverterType::Linear),
25 _ => Err(Error::from_code(ErrorCode::BadConverter)),
26 }
27 }
28
29 pub fn name(&self) -> &'static str {
31 unsafe { CStr::from_ptr(src_get_name(*self as i32)) }
32 .to_str()
33 .unwrap()
34 }
35
36 pub fn description(&self) -> &'static str {
38 unsafe { CStr::from_ptr(src_get_description(*self as i32)) }
39 .to_str()
40 .unwrap()
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn create_converter_type_from_int() {
50 assert_eq!(
51 ConverterType::from_int(0),
52 Ok(ConverterType::SincBestQuality)
53 );
54 assert_eq!(
55 ConverterType::from_int(1),
56 Ok(ConverterType::SincMediumQuality)
57 );
58 assert_eq!(ConverterType::from_int(2), Ok(ConverterType::SincFastest));
59 assert_eq!(ConverterType::from_int(3), Ok(ConverterType::ZeroOrderHold));
60 assert_eq!(ConverterType::from_int(4), Ok(ConverterType::Linear));
61 assert_eq!(
62 ConverterType::from_int(8),
63 Err(Error::from_code(ErrorCode::BadConverter))
64 );
65 }
66
67 #[test]
68 fn name() {
69 assert_eq!(
70 ConverterType::SincBestQuality.name(),
71 "Best Sinc Interpolator"
72 );
73 assert_eq!(
74 ConverterType::SincMediumQuality.name(),
75 "Medium Sinc Interpolator"
76 );
77 assert_eq!(
78 ConverterType::SincFastest.name(),
79 "Fastest Sinc Interpolator"
80 );
81 assert_eq!(ConverterType::ZeroOrderHold.name(), "ZOH Interpolator");
82 assert_eq!(ConverterType::Linear.name(), "Linear Interpolator");
83 }
84
85 #[test]
86 fn description() {
87 assert_eq!(
88 ConverterType::SincBestQuality.description(),
89 "Band limited sinc interpolation, best quality, 144dB SNR, 96% BW."
90 );
91 assert_eq!(
92 ConverterType::SincMediumQuality.description(),
93 "Band limited sinc interpolation, medium quality, 121dB SNR, 90% BW."
94 );
95 assert_eq!(
96 ConverterType::SincFastest.description(),
97 "Band limited sinc interpolation, fastest, 97dB SNR, 80% BW."
98 );
99 assert_eq!(
100 ConverterType::ZeroOrderHold.description(),
101 "Zero order hold interpolator, very fast, poor quality."
102 );
103 assert_eq!(
104 ConverterType::Linear.description(),
105 "Linear interpolator, very fast, poor quality."
106 );
107 }
108}