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
use crate::audio::buffer::BufferInfo;
use crate::audio::sealed::Sealed;

pub trait BufferLayout: Sized + Sealed {
    fn index_of(info: &BufferInfo<Self>, channel: usize, frame: usize) -> usize;
}

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Deinterleaved {}

impl Sealed for Deinterleaved {}

impl BufferLayout for Deinterleaved {
    #[inline]
    fn index_of(info: &BufferInfo<Self>, channel: usize, frame: usize) -> usize {
        (channel * info.frames()) + frame
    }
}

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Interleaved {}

impl Sealed for Interleaved {}

impl BufferLayout for Interleaved {
    #[inline]
    fn index_of(info: &BufferInfo<Self>, channel: usize, frame: usize) -> usize {
        (frame * info.channels()) + channel
    }
}

#[cfg(test)]
#[inline(always)]
pub(crate) fn deinterleaved<T>(input: &[T], output: &mut [T], channels: usize)
where
    T: Copy,
{
    deinterleaved_by(input, output, channels, |sample| *sample)
}

/// De-interleaves an interleaved slice using a memory access pattern
/// that's optimized for efficient cached (i.e. sequential) reads.
pub(crate) fn deinterleaved_by<T, U, F>(input: &[T], output: &mut [U], channels: usize, f: F)
where
    F: Fn(&T) -> U,
{
    assert_eq!(input.len(), output.len());
    assert_eq!(input.len() % channels, 0);

    let frames = input.len() / channels;
    let mut interleaved_index = 0;
    for frame in 0..frames {
        let mut deinterleaved_index = frame;
        for _channel in 0..channels {
            output[deinterleaved_index] = f(&input[interleaved_index]);
            interleaved_index += 1;
            deinterleaved_index += frames;
        }
    }
}

#[cfg(test)]
#[inline(always)]
pub(crate) fn interleaved<T>(input: &[T], output: &mut [T], channels: usize)
where
    T: Copy,
{
    interleaved_by(input, output, channels, |sample| *sample)
}

/// Interleaves an de-interleaved slice using a memory access pattern
/// that's optimized for efficient cached (i.e. sequential) reads.
pub(crate) fn interleaved_by<T, U, F>(input: &[T], output: &mut [U], channels: usize, f: F)
where
    F: Fn(&T) -> U,
{
    assert_eq!(input.len(), output.len());
    assert_eq!(input.len() % channels, 0);

    let frames = input.len() / channels;
    let mut deinterleaved_index = 0;
    for channel in 0..channels {
        let mut interleaved_index = channel;
        for _frame in 0..frames {
            output[interleaved_index] = f(&input[deinterleaved_index]);
            deinterleaved_index += 1;
            interleaved_index += channels;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn interleaved_1_channel() {
        let input: Vec<_> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        let mut output = vec![0; input.len()];
        let channels = 1;

        interleaved(&input[..], &mut output[..], channels);

        let actual = output;
        let expected = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

        assert_eq!(actual, expected);
    }

    #[test]
    fn deinterleaved_1_channel() {
        let input: Vec<_> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        let mut output = vec![0; input.len()];
        let channels = 1;

        deinterleaved(&input[..], &mut output[..], channels);

        let actual = output;
        let expected = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

        assert_eq!(actual, expected);
    }

    #[test]
    fn interleaved_2_channel() {
        let input: Vec<_> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        let mut output = vec![0; input.len()];
        let channels = 2;

        interleaved(&input[..], &mut output[..], channels);

        let actual = output;
        let expected = vec![0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15];

        assert_eq!(actual, expected);
    }

    #[test]
    fn deinterleaved_2_channel() {
        let input: Vec<_> = vec![0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15];
        let mut output = vec![0; input.len()];
        let channels = 2;

        deinterleaved(&input[..], &mut output[..], channels);

        let actual = output;
        let expected = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

        assert_eq!(actual, expected);
    }

    #[test]
    fn interleaved_3_channel() {
        let input: Vec<_> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
        let mut output = vec![0; input.len()];
        let channels = 3;

        interleaved(&input[..], &mut output[..], channels);

        let actual = output;
        let expected = vec![0, 5, 10, 1, 6, 11, 2, 7, 12, 3, 8, 13, 4, 9, 14];

        assert_eq!(actual, expected);
    }

    #[test]
    fn deinterleaved_3_channel() {
        let input: Vec<_> = vec![0, 5, 10, 1, 6, 11, 2, 7, 12, 3, 8, 13, 4, 9, 14];
        let mut output = vec![0; input.len()];
        let channels = 3;

        deinterleaved(&input[..], &mut output[..], channels);

        let actual = output;
        let expected = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

        assert_eq!(actual, expected);
    }
}