fixed_resample/
non_realtime.rs

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
use std::num::NonZeroUsize;

use rubato::Sample;

use crate::{ResampleQuality, ResamplerType};

/// An easy to use resampler for use in non-realtime applications.
pub struct NonRtResampler<T: Sample> {
    resampler: ResamplerType<T>,
    in_buf: Vec<Vec<T>>,
    out_buf: Vec<Vec<T>>,
    intlv_buf: Vec<T>,
    in_buf_len: usize,
    num_channels: NonZeroUsize,
    input_frames_max: usize,
}

impl<T: Sample> NonRtResampler<T> {
    /// Create a new non-realtime resampler.
    ///
    /// * `in_sample_rate` - The sample rate of the input stream.
    /// * `out_sample_rate` - The sample rate of the output stream.
    /// * `num_channels` - The number of channels.
    /// * `interleaved` - If you are planning to use [`NonRtResampler::process_interleaved`],
    /// set this to `true`. Otherwise you can set this to `false` to save a bit of
    /// memory.
    /// * `quality` - The quality of the resampling algorithm.
    ///
    /// # Panics
    ///
    /// Panics if:
    /// * `in_sample_rate == 0`
    /// * `out_sample_rate == 0`
    /// * `num_channels == 0`,
    pub fn new(
        in_sample_rate: u32,
        out_sample_rate: u32,
        num_channels: usize,
        quality: ResampleQuality,
    ) -> Self {
        let resampler =
            ResamplerType::from_quality(in_sample_rate, out_sample_rate, num_channels, quality);
        Self::from_custom(resampler)
    }

    /// Create a new non-realtime resampler using the given rubato resampler.
    ///
    /// # Panics
    /// Panics if `max_out_frames == 0`.
    pub fn from_custom(resampler: impl Into<ResamplerType<T>>) -> Self {
        let mut resampler: ResamplerType<T> = resampler.into();

        let num_channels = resampler.num_channels();
        assert!(num_channels > 0);

        let input_frames_max = resampler.input_frames_max();
        let output_frames_max = resampler.output_frames_max();

        Self {
            resampler,
            in_buf: (0..num_channels)
                .map(|_| vec![T::zero(); input_frames_max])
                .collect(),
            out_buf: (0..num_channels)
                .map(|_| vec![T::zero(); output_frames_max])
                .collect(),
            intlv_buf: Vec::new(),
            in_buf_len: 0,
            num_channels: NonZeroUsize::new(num_channels).unwrap(),
            input_frames_max,
        }
    }

    /// Get the number of channels this resampler is configured for.
    pub fn num_channels(&self) -> NonZeroUsize {
        self.num_channels
    }

    /// Reset the resampler state and clear all internal buffers.
    pub fn reset(&mut self) {
        self.resampler.reset();

        self.in_buf_len = 0;
    }

    /// Resample the given input data.
    ///
    /// * `input` - The input data in interleaved format.
    /// * `on_processed` - Called whenever there is new resampled data. This data is in
    /// interleaved format.
    /// * `is_last_packet` - Whether or not this is the last (or only) packet of data that
    /// will be resampled. This ensures that any leftover samples in the internal resampler
    /// are flushed to the output.
    ///
    /// Note, this method is *NOT* realtime-safe.
    pub fn process_interleaved(
        &mut self,
        input: &[T],
        mut on_processed: impl FnMut(&[T]),
        is_last_packet: bool,
    ) {
        let num_channels = self.num_channels.get();

        let total_in_frames = input.len() / num_channels;

        if self.intlv_buf.is_empty() {
            let alloc_frames = self.resampler.output_frames_max() * num_channels;

            self.intlv_buf.reserve_exact(alloc_frames);
            self.intlv_buf.resize(alloc_frames, T::zero());
        }

        let mut in_frames_copied = 0;
        while in_frames_copied < total_in_frames {
            if self.in_buf_len < self.input_frames_max {
                let copy_frames = (self.input_frames_max - self.in_buf_len)
                    .min(total_in_frames - in_frames_copied);

                crate::interleave::deinterleave(
                    input,
                    &mut self.in_buf,
                    in_frames_copied,
                    self.in_buf_len,
                    copy_frames,
                );

                self.in_buf_len += copy_frames;
                in_frames_copied += copy_frames;

                if self.in_buf_len < self.input_frames_max && !is_last_packet {
                    // Must wait for more input data before continuing.
                    return;
                }
            }

            if is_last_packet && in_frames_copied == total_in_frames {
                let mut is_first = true;

                loop {
                    let (_, out_frames) = self
                        .resampler
                        .process_partial_into_buffer(
                            if is_first { Some(&self.in_buf) } else { None },
                            &mut self.out_buf,
                            None,
                        )
                        .unwrap();

                    is_first = false;

                    if num_channels == 1 {
                        // Mono, no need to copy to an intermediate buffer.
                        (on_processed)(&self.out_buf[0][..out_frames]);
                    } else {
                        crate::interleave::interleave(
                            &self.out_buf,
                            &mut self.intlv_buf,
                            0,
                            0,
                            out_frames,
                        );

                        (on_processed)(&self.intlv_buf[0..out_frames * num_channels]);
                    }

                    if out_frames < self.out_buf[0].len() {
                        break;
                    }
                }
            } else {
                let (_, out_frames) = self
                    .resampler
                    .process_into_buffer(&self.in_buf, &mut self.out_buf, None)
                    .unwrap();

                if num_channels == 1 {
                    // Mono, no need to copy to an intermediate buffer.
                    (on_processed)(&self.out_buf[0][..out_frames]);
                } else {
                    crate::interleave::interleave(
                        &self.out_buf,
                        &mut self.intlv_buf,
                        0,
                        0,
                        out_frames,
                    );

                    (on_processed)(&self.intlv_buf[0..out_frames * num_channels]);
                }
            }

            self.in_buf_len = 0;
        }
    }

    /// Resample the given input data.
    ///
    /// * `input` - The input data.
    /// * `on_processed` - Called whenever there is new resampled data. The first argument
    /// is the buffers containing the new data, and the second argument is the length of
    /// the new data in frames (NOTE, the second argument may be less than the length of
    /// the `Vec`s in the first argument).
    /// * `is_last_packet` - Whether or not this is the last (or only) packet of data that
    /// will be resampled. This ensures that any leftover samples in the internal resampler
    /// are flushed to the output.
    ///
    /// Note, this method is *NOT* realtime-safe.
    pub fn process<Vin: AsRef<[T]>>(
        &mut self,
        input: &[Vin],
        mut on_processed: impl FnMut(&[Vec<T>], usize),
        is_last_packet: bool,
    ) {
        let in_ch_0 = input[0].as_ref();
        let total_in_frames = in_ch_0.len();

        let mut in_frames_copied = 0;
        while in_frames_copied < total_in_frames {
            if self.in_buf_len < self.input_frames_max {
                let copy_frames = (self.input_frames_max - self.in_buf_len)
                    .min(total_in_frames - in_frames_copied);

                self.in_buf[0][self.in_buf_len..self.in_buf_len + copy_frames]
                    .copy_from_slice(&in_ch_0[in_frames_copied..in_frames_copied + copy_frames]);
                for (in_buf_ch, in_ch) in self.in_buf.iter_mut().zip(input.iter()).skip(1) {
                    let in_ch = in_ch.as_ref();
                    in_buf_ch[self.in_buf_len..self.in_buf_len + copy_frames]
                        .copy_from_slice(&in_ch[in_frames_copied..in_frames_copied + copy_frames]);
                }

                self.in_buf_len += copy_frames;
                in_frames_copied += copy_frames;

                if self.in_buf_len < self.input_frames_max && !is_last_packet {
                    // Must wait for more input data before continuing.
                    return;
                }
            }

            if is_last_packet && in_frames_copied == total_in_frames {
                let mut is_first = true;

                loop {
                    let (_, out_frames) = self
                        .resampler
                        .process_partial_into_buffer(
                            if is_first { Some(&self.in_buf) } else { None },
                            &mut self.out_buf,
                            None,
                        )
                        .unwrap();

                    is_first = false;

                    (on_processed)(self.out_buf.as_slice(), out_frames);

                    if out_frames < self.out_buf[0].len() {
                        break;
                    }
                }
            } else {
                let (_, out_frames) = self
                    .resampler
                    .process_into_buffer(&self.in_buf, &mut self.out_buf, None)
                    .unwrap();

                (on_processed)(self.out_buf.as_slice(), out_frames);
            }

            self.in_buf_len = 0;
        }
    }

    pub fn resampler_type(&self) -> &ResamplerType<T> {
        &self.resampler
    }

    pub fn resampler_type_mut(&mut self) -> &mut ResamplerType<T> {
        &mut self.resampler
    }
}

impl<T: Sample> Into<ResamplerType<T>> for NonRtResampler<T> {
    fn into(self) -> ResamplerType<T> {
        self.resampler
    }
}