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
use std::marker::PhantomData;

use crate::audio::buffer::layout::{Deinterleaved, Interleaved};

#[derive(Eq, PartialEq, Debug)]
pub struct BufferInfo<L> {
    channels: usize,
    frames: usize,
    _phantom: PhantomData<L>,
}

impl<L> BufferInfo<L> {
    pub fn new(channels: usize, frames: usize) -> Self {
        Self {
            channels,
            frames,
            _phantom: PhantomData,
        }
    }

    /// Get a reference to the buffer info's channels.
    pub fn channels(&self) -> usize {
        self.channels
    }

    /// Set the buffer info's channels.
    pub fn set_channels(&mut self, channels: usize) {
        self.channels = channels;
    }

    /// Get a reference to the buffer info's frames.
    pub fn frames(&self) -> usize {
        self.frames
    }

    /// Set the buffer info's frames.
    pub fn set_frames(&mut self, frames: usize) {
        self.frames = frames;
    }

    pub fn samples(&self) -> usize {
        self.channels * self.frames
    }
}

impl<L> Copy for BufferInfo<L> {}

impl<L> Clone for BufferInfo<L> {
    fn clone(&self) -> Self {
        *self
    }
}

macro_rules! impl_from_buffer_info {
    ($in_layout:ty => $out_layout:ty) => {
        impl From<BufferInfo<$in_layout>> for BufferInfo<$out_layout> {
            fn from(info: BufferInfo<$in_layout>) -> Self {
                Self {
                    channels: info.channels,
                    frames: info.frames,
                    _phantom: PhantomData,
                }
            }
        }
    };
}

impl_from_buffer_info!(Interleaved => Deinterleaved);
impl_from_buffer_info!(Deinterleaved => Interleaved);

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

    #[test]
    fn new() {
        let channels = 3;
        let frames = 100;

        let interleaved = BufferInfo::<Interleaved>::new(channels, frames);

        assert_eq!(interleaved.channels, channels);
        assert_eq!(interleaved.frames, frames);

        let deinterleaved = BufferInfo::<Deinterleaved>::new(channels, frames);

        assert_eq!(deinterleaved.channels, channels);
        assert_eq!(deinterleaved.frames, frames);
    }

    #[test]
    fn clone() {
        let channels = 3;
        let frames = 100;

        let interleaved = BufferInfo::<Interleaved>::new(channels, frames);

        assert_eq!(interleaved.clone(), interleaved);

        let deinterleaved = BufferInfo::<Deinterleaved>::new(channels, frames);

        assert_eq!(deinterleaved.clone(), deinterleaved);
    }

    #[test]
    fn samples() {
        let channels = 3;
        let frames = 100;

        let interleaved = BufferInfo::<Interleaved>::new(channels, frames);

        assert_eq!(interleaved.samples(), channels * frames);

        let deinterleaved = BufferInfo::<Deinterleaved>::new(channels, frames);

        assert_eq!(deinterleaved.samples(), channels * frames);
    }
}