@@ -12,6 +12,113 @@ pub enum Error {
12
12
InvalidArg ,
13
13
}
14
14
15
+ pub trait Sample : Copy + Clone {
16
+ fn process (
17
+ st : & mut speex:: SpeexResamplerState ,
18
+ index : usize ,
19
+ input : & [ Self ] ,
20
+ output : & mut [ Self ] ,
21
+ ) -> Result < ( usize , usize ) , Error > ;
22
+
23
+ fn process_interleaved (
24
+ st : & mut speex:: SpeexResamplerState ,
25
+ input : & [ Self ] ,
26
+ output : & mut [ Self ] ,
27
+ ) -> Result < ( usize , usize ) , Error > ;
28
+ }
29
+
30
+ impl Sample for i16 {
31
+ fn process (
32
+ st : & mut speex:: SpeexResamplerState ,
33
+ index : usize ,
34
+ input : & [ i16 ] ,
35
+ output : & mut [ i16 ] ,
36
+ ) -> Result < ( usize , usize ) , Error > {
37
+ let mut in_len = input. len ( ) as u32 ;
38
+ let mut out_len = output. len ( ) as u32 ;
39
+ let ret = st. process_int (
40
+ index as u32 ,
41
+ input,
42
+ & mut in_len,
43
+ output,
44
+ & mut out_len,
45
+ ) ;
46
+
47
+ if ret != 0 {
48
+ Err ( Error :: AllocFailed )
49
+ } else {
50
+ Ok ( ( in_len as usize , out_len as usize ) )
51
+ }
52
+ }
53
+
54
+ fn process_interleaved (
55
+ st : & mut speex:: SpeexResamplerState ,
56
+ input : & [ i16 ] ,
57
+ output : & mut [ i16 ] ,
58
+ ) -> Result < ( usize , usize ) , Error > {
59
+ let mut in_len = input. len ( ) as u32 ;
60
+ let mut out_len = output. len ( ) as u32 ;
61
+ let ret = st. process_interleaved_int (
62
+ input,
63
+ & mut in_len,
64
+ output,
65
+ & mut out_len,
66
+ ) ;
67
+
68
+ if ret != 0 {
69
+ Err ( Error :: AllocFailed )
70
+ } else {
71
+ Ok ( ( in_len as usize , out_len as usize ) )
72
+ }
73
+ }
74
+ }
75
+
76
+ impl Sample for f32 {
77
+ fn process (
78
+ st : & mut speex:: SpeexResamplerState ,
79
+ index : usize ,
80
+ input : & [ f32 ] ,
81
+ output : & mut [ f32 ] ,
82
+ ) -> Result < ( usize , usize ) , Error > {
83
+ let mut in_len = input. len ( ) as u32 ;
84
+ let mut out_len = output. len ( ) as u32 ;
85
+ let ret = st. process_float (
86
+ index as u32 ,
87
+ input,
88
+ & mut in_len,
89
+ output,
90
+ & mut out_len,
91
+ ) ;
92
+
93
+ if ret != 0 {
94
+ Err ( Error :: AllocFailed )
95
+ } else {
96
+ Ok ( ( in_len as usize , out_len as usize ) )
97
+ }
98
+ }
99
+
100
+ fn process_interleaved (
101
+ st : & mut speex:: SpeexResamplerState ,
102
+ input : & [ f32 ] ,
103
+ output : & mut [ f32 ] ,
104
+ ) -> Result < ( usize , usize ) , Error > {
105
+ let mut in_len = input. len ( ) as u32 ;
106
+ let mut out_len = output. len ( ) as u32 ;
107
+ let ret = st. process_interleaved_float (
108
+ input,
109
+ & mut in_len,
110
+ output,
111
+ & mut out_len,
112
+ ) ;
113
+
114
+ if ret != 0 {
115
+ Err ( Error :: AllocFailed )
116
+ } else {
117
+ Ok ( ( in_len as usize , out_len as usize ) )
118
+ }
119
+ }
120
+ }
121
+
15
122
impl State {
16
123
pub fn new (
17
124
channels : usize ,
@@ -46,27 +153,21 @@ impl State {
46
153
self . st . get_ratio ( )
47
154
}
48
155
49
- pub fn process_float (
156
+ pub fn process < S : Sample > (
50
157
& mut self ,
51
158
index : usize ,
52
- input : & [ f32 ] ,
53
- output : & mut [ f32 ] ,
159
+ input : & [ S ] ,
160
+ output : & mut [ S ] ,
54
161
) -> Result < ( usize , usize ) , Error > {
55
- let mut in_len = input. len ( ) as u32 ;
56
- let mut out_len = output. len ( ) as u32 ;
57
- let ret = self . st . process_float (
58
- index as u32 ,
59
- input,
60
- & mut in_len,
61
- output,
62
- & mut out_len,
63
- ) ;
162
+ S :: process ( & mut self . st , index, input, output)
163
+ }
64
164
65
- if ret != 0 {
66
- Err ( Error :: AllocFailed )
67
- } else {
68
- Ok ( ( in_len as usize , out_len as usize ) )
69
- }
165
+ pub fn process_interleaved < S : Sample > (
166
+ & mut self ,
167
+ input : & [ S ] ,
168
+ output : & mut [ S ] ,
169
+ ) -> Result < ( usize , usize ) , Error > {
170
+ S :: process_interleaved ( & mut self . st , input, output)
70
171
}
71
172
72
173
pub fn skip_zeros ( & mut self ) {
0 commit comments