|
1 |
| -use crate::smallft::*; |
2 |
| - |
3 | 1 | /* Copyright (C) 2005-2006 Jean-Marc Valin
|
4 | 2 | File: fftwrap.c
|
5 | 3 |
|
@@ -33,46 +31,94 @@ use crate::smallft::*;
|
33 | 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34 | 32 |
|
35 | 33 | */
|
36 |
| -pub fn spx_fft_init(size: usize) -> DrftLookup { |
37 |
| - DrftLookup::new(size) |
| 34 | + |
| 35 | +use crate::smallft::*; |
| 36 | + |
| 37 | +#[derive(Clone)] |
| 38 | +pub struct DrftLookup { |
| 39 | + pub n: usize, |
| 40 | + pub trigcache: Vec<f32>, |
| 41 | + pub splitcache: Vec<i32>, |
38 | 42 | }
|
39 | 43 |
|
40 |
| -pub fn spx_fft(table: &mut DrftLookup, in_0: &mut [f32], out: &mut [f32]) { |
41 |
| - let scale = (1.0f64 / table.n as f64) as f32; |
42 |
| - if in_0 == out { |
43 |
| - eprintln!("FFT should not be done in-place"); |
| 44 | +impl DrftLookup { |
| 45 | + pub fn new(n: usize) -> Self { |
| 46 | + let mut drft = Self { |
| 47 | + n: n, |
| 48 | + trigcache: vec![0.0; 3 * n], |
| 49 | + splitcache: vec![0; 32], |
| 50 | + }; |
| 51 | + |
| 52 | + fdrffti(n, &mut drft.trigcache, &mut drft.splitcache); |
| 53 | + |
| 54 | + drft |
44 | 55 | }
|
45 | 56 |
|
46 |
| - out.iter_mut() |
47 |
| - .zip(in_0.iter()) |
48 |
| - .take(table.n as usize) |
49 |
| - .for_each(|(o, i)| *o = scale * *i); |
| 57 | + pub fn spx_fft(&mut self, in_0: &[f32], out: &mut [f32]) { |
| 58 | + let scale = (1.0f64 / self.n as f64) as f32; |
| 59 | + if in_0 == out { |
| 60 | + eprintln!("FFT should not be done in-place"); |
| 61 | + } |
50 | 62 |
|
51 |
| - spx_drft_forward(table, out); |
52 |
| -} |
| 63 | + out.iter_mut() |
| 64 | + .zip(in_0.iter()) |
| 65 | + .take(self.n as usize) |
| 66 | + .for_each(|(o, i)| *o = scale * *i); |
53 | 67 |
|
54 |
| -pub fn spx_ifft(table: &mut DrftLookup, in_0: &mut [f32], out: &mut [f32]) { |
55 |
| - if in_0 == out { |
56 |
| - eprintln!("FFT should not be done in-place"); |
57 |
| - } else { |
58 |
| - out.copy_from_slice(&in_0[..table.n as usize]); |
| 68 | + self.spx_drft_forward(out); |
59 | 69 | }
|
60 | 70 |
|
61 |
| - spx_drft_backward(table, out); |
62 |
| -} |
| 71 | + pub fn spx_ifft(&mut self, in_0: &[f32], out: &mut [f32]) { |
| 72 | + if in_0 == out { |
| 73 | + eprintln!("FFT should not be done in-place"); |
| 74 | + } else { |
| 75 | + out.copy_from_slice(&in_0[..self.n as usize]); |
| 76 | + } |
63 | 77 |
|
64 |
| -pub fn spx_fft_float( |
65 |
| - table: &mut DrftLookup, |
66 |
| - in_0: &mut [f32], |
67 |
| - out: &mut [f32], |
68 |
| -) { |
69 |
| - spx_fft(table, in_0, out); |
70 |
| -} |
| 78 | + self.spx_drft_backward(out); |
| 79 | + } |
| 80 | + |
| 81 | + pub fn spx_fft_float(&mut self, in_0: &[f32], out: &mut [f32]) { |
| 82 | + self.spx_fft(in_0, out); |
| 83 | + } |
| 84 | + |
| 85 | + pub fn spx_ifft_float(&mut self, in_0: &[f32], out: &mut [f32]) { |
| 86 | + self.spx_ifft(in_0, out); |
| 87 | + } |
| 88 | + |
| 89 | + pub fn spx_drft_forward(&mut self, data: &mut [f32]) { |
| 90 | + if self.n == 1 { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + let mut trigcache_temp = self.trigcache[self.n as usize..].to_vec(); |
| 95 | + |
| 96 | + drftf1( |
| 97 | + self.n as i32, |
| 98 | + data, |
| 99 | + &mut self.trigcache, |
| 100 | + &mut trigcache_temp, |
| 101 | + &mut self.splitcache, |
| 102 | + ); |
71 | 103 |
|
72 |
| -pub fn spx_ifft_float( |
73 |
| - table: &mut DrftLookup, |
74 |
| - in_0: &mut [f32], |
75 |
| - out: &mut [f32], |
76 |
| -) { |
77 |
| - spx_ifft(table, in_0, out); |
| 104 | + self.trigcache[self.n as usize..].copy_from_slice(&trigcache_temp); |
| 105 | + } |
| 106 | + |
| 107 | + pub fn spx_drft_backward(&mut self, data: &mut [f32]) { |
| 108 | + if self.n == 1 { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + let mut trigcache_temp = self.trigcache[self.n as usize..].to_vec(); |
| 113 | + |
| 114 | + drftb1( |
| 115 | + self.n as i32, |
| 116 | + data, |
| 117 | + &mut self.trigcache, |
| 118 | + &mut trigcache_temp, |
| 119 | + &mut self.splitcache, |
| 120 | + ); |
| 121 | + |
| 122 | + self.trigcache[self.n as usize..].copy_from_slice(&trigcache_temp); |
| 123 | + } |
78 | 124 | }
|
0 commit comments