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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use crate::base::*;
use crate::conversion::conversion_fn_for;

pub struct Frames<'s> {
    data: &'s [u8],
    format: Format,
    channels: u32,
}

impl<'s> Frames<'s> {
    #[inline]
    pub fn wrap<S: Sample>(data: &'s [S], format: Format, channels: u32) -> Frames<'s> {
        let byte_data_len = data.len() * std::mem::size_of::<S>();
        let byte_data =
            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, byte_data_len) };

        Frames {
            data: byte_data,
            format,
            channels,
        }
    }

    /// Convert this frames samples into another format, placing the new converted
    /// frames into `dest`.
    #[inline]
    pub fn convert(&self, dest: &mut FramesMut, dither_mode: DitherMode) {
        assert!(
            self.frame_count() == dest.frame_count(),
            "frame conversion with different frame counts (input: {}, output: {})",
            self.frame_count(),
            dest.frame_count()
        );

        let convert_fn = conversion_fn_for(self.format, dest.format);
        unsafe {
            convert_fn(
                dest.as_mut_ptr() as *mut _,
                self.as_ptr() as *const _,
                self.frame_count() as u64,
                dither_mode as _,
            );
        }
    }

    pub(crate) fn as_ptr(&self) -> *const u8 {
        self.data.as_ptr()
    }

    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        self.data
    }

    #[inline]
    pub fn as_samples<S: Sample>(&self) -> &[S] {
        assert!(
            self.format() == S::format(),
            "format mismatch (frames: {:?}, requested: {:?})",
            self.format,
            S::format()
        );

        let len = self.sample_count();
        unsafe { std::slice::from_raw_parts(self.data.as_ptr().cast::<S>(), len) }
    }

    #[inline]
    pub fn frames<'t, S: 'static + Sample>(&'t self) -> impl 't + Iterator<Item = &[S]> {
        FramesIter {
            samples: self.as_samples(),
            channels: self.channels,
            offset: 0,
        }
    }

    #[inline]
    pub fn byte_count(&self) -> usize {
        self.data.len()
    }

    /// Returns the number of frames contained.
    #[inline]
    pub fn frame_count(&self) -> usize {
        self.sample_count() / self.channels as usize
    }

    /// Returns the number of samples contained.
    #[inline]
    pub fn sample_count(&self) -> usize {
        self.data.len() / self.format.size_in_bytes()
    }

    #[inline]
    pub fn format(&self) -> Format {
        self.format
    }

    #[inline]
    pub fn channels(&self) -> u32 {
        self.channels
    }
}

pub struct FramesMut<'s> {
    data: &'s mut [u8],
    format: Format,
    channels: u32,
}

impl<'s> FramesMut<'s> {
    #[inline]
    pub fn wrap<S: Sample>(data: &'s mut [S], format: Format, channels: u32) -> FramesMut<'s> {
        let byte_data_len = data.len() * std::mem::size_of::<S>();
        let byte_data =
            unsafe { std::slice::from_raw_parts_mut(data.as_ptr() as *mut u8, byte_data_len) };

        FramesMut {
            data: byte_data,
            format,
            channels,
        }
    }

    pub(crate) fn as_ptr(&self) -> *const u8 {
        self.data.as_ptr()
    }

    pub(crate) fn as_mut_ptr(&mut self) -> *mut u8 {
        self.data.as_ptr() as *mut u8
    }

    /// Convert this frames samples into another format, placing the new converted
    /// frames into `dest`.
    #[inline]
    pub fn convert(&self, dest: &mut FramesMut, dither_mode: DitherMode) {
        assert!(
            self.frame_count() == dest.frame_count(),
            "frame conversion with different frame counts (input: {}, output: {})",
            self.frame_count(),
            dest.frame_count()
        );

        let convert_fn = conversion_fn_for(self.format, dest.format);
        unsafe {
            convert_fn(
                dest.as_mut_ptr() as *mut _,
                self.as_ptr() as *const _,
                self.frame_count() as u64,
                dither_mode as _,
            );
        }
    }

    #[inline]
    pub fn as_samples<S: Sample>(&self) -> &[S] {
        assert!(
            self.format() == S::format(),
            "format mismatch (frames: {:?}, requested: {:?})",
            self.format,
            S::format()
        );

        let len = self.sample_count();
        unsafe { std::slice::from_raw_parts(self.data.as_ptr().cast::<S>(), len) }
    }

    #[inline]
    pub fn as_samples_mut<S: Sample>(&mut self) -> &mut [S] {
        assert!(
            self.format() == S::format(),
            "format mismatch (frames: {:?}, requested: {:?})",
            self.format,
            S::format()
        );

        let len = self.sample_count();
        unsafe { std::slice::from_raw_parts_mut(self.data.as_mut_ptr().cast::<S>(), len) }
    }

    #[inline]
    pub fn frames<'t, S: 'static + Sample>(&'t self) -> impl 't + Iterator<Item = &[S]> {
        FramesIter {
            samples: self.as_samples(),
            channels: self.channels,
            offset: 0,
        }
    }

    #[inline]
    pub fn frames_mut<'t, S: 'static + Sample>(
        &'t mut self,
    ) -> impl 't + Iterator<Item = &mut [S]> {
        let channels = self.channels;
        let samples = self.as_samples_mut();
        let samples_len = samples.len();

        FramesIterMut {
            samples_ptr: samples.as_mut_ptr(),
            samples_len,
            channels,
            offset: 0,
            phantom: std::marker::PhantomData,
        }
    }

    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        self.data
    }

    #[inline]
    pub fn as_bytes_mut(&mut self) -> &mut [u8] {
        self.data
    }

    #[inline]
    pub fn byte_count(&self) -> usize {
        self.data.len()
    }

    /// Returns the number of frames contained.
    #[inline]
    pub fn frame_count(&self) -> usize {
        self.sample_count() / self.channels as usize
    }

    /// Returns the number of samples contained.
    #[inline]
    pub fn sample_count(&self) -> usize {
        self.data.len() / self.format.size_in_bytes()
    }

    #[inline]
    pub fn format(&self) -> Format {
        self.format
    }

    #[inline]
    pub fn channels(&self) -> u32 {
        self.channels
    }
}

pub struct FramesIter<'s, S: Sample> {
    samples: &'s [S],
    channels: u32,
    offset: usize,
}

impl<'s, S: Sample> Iterator for FramesIter<'s, S> {
    type Item = &'s [S];

    fn next(&mut self) -> Option<Self::Item> {
        if self.offset < self.samples.len() {
            let ret = Some(&self.samples[self.offset..(self.offset + self.channels as usize)]);
            self.offset += self.channels as usize;
            ret
        } else {
            None
        }
    }
}

pub struct FramesIterMut<'s, S: Sample> {
    samples_ptr: *mut S,
    samples_len: usize,
    channels: u32,
    offset: usize,
    phantom: std::marker::PhantomData<&'s S>,
}

impl<'s, S: Sample> Iterator for FramesIterMut<'s, S> {
    type Item = &'s mut [S];

    fn next(&mut self) -> Option<Self::Item> {
        if self.offset < self.samples_len {
            // FIXME The compiler doesn't like me doing the same thing that I do in FramesIter in here
            // using mut and gives me some cryptic error, so I'm just using pointers for now.
            let ret = Some(unsafe {
                std::slice::from_raw_parts_mut(
                    self.samples_ptr.add(self.offset),
                    self.channels as usize,
                )
            });
            self.offset += self.channels as usize;
            ret
        } else {
            None
        }
    }
}

pub trait Sample {
    /// Returns the format of this sample.
    fn format() -> Format;
}

impl Sample for u8 {
    fn format() -> Format {
        Format::U8
    }
}

impl Sample for i16 {
    fn format() -> Format {
        Format::S16
    }
}

impl Sample for [u8; 3] {
    fn format() -> Format {
        Format::S24
    }
}

impl Sample for i32 {
    fn format() -> Format {
        Format::S32
    }
}

impl Sample for f32 {
    fn format() -> Format {
        Format::F32
    }
}