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
use dasp_frame::Frame;

mod fixed_size_array;

/// For converting from a slice of `Frame`s to a slice of `Sample`s.
pub trait FromFrameSlice<'a, F>
where
    F: Frame,
{
    fn from_frame_slice(slice: &'a [F]) -> Self;
}

/// For converting from a slice of `Frame`s to a slice of `Sample`s.
pub trait FromFrameSliceMut<'a, F>
where
    F: Frame,
{
    fn from_frame_slice_mut(slice: &'a mut [F]) -> Self;
}

/// For converting from a slice of `Sample`s to a slice of `Frame`s.
pub trait ToFrameSlice<'a, F>
where
    F: Frame,
{
    fn to_frame_slice(self) -> Option<&'a [F]>;
}

/// For converting from a mutable slice of `Sample`s to a mutable slice of `Frame`s.
pub trait ToFrameSliceMut<'a, F>
where
    F: Frame,
{
    fn to_frame_slice_mut(self) -> Option<&'a mut [F]>;
}

/// For converting to and from a slice of `Frame`s.
pub trait DuplexFrameSlice<'a, F>: FromFrameSlice<'a, F> + ToFrameSlice<'a, F>
where
    F: Frame,
{
}

/// For converting to and from a mutable slice of `Frame`s.
pub trait DuplexFrameSliceMut<'a, F>: FromFrameSliceMut<'a, F> + ToFrameSliceMut<'a, F>
where
    F: Frame,
{
}

impl<'a, F> FromFrameSlice<'a, F> for &'a [F]
where
    F: Frame,
{
    #[inline]
    fn from_frame_slice(slice: &'a [F]) -> Self {
        slice
    }
}

impl<'a, F> FromFrameSliceMut<'a, F> for &'a mut [F]
where
    F: Frame,
{
    #[inline]
    fn from_frame_slice_mut(slice: &'a mut [F]) -> Self {
        slice
    }
}

impl<'a, F> ToFrameSlice<'a, F> for &'a [F]
where
    F: Frame,
{
    #[inline]
    fn to_frame_slice(self) -> Option<&'a [F]> {
        Some(self)
    }
}

impl<'a, F> ToFrameSliceMut<'a, F> for &'a mut [F]
where
    F: Frame,
{
    #[inline]
    fn to_frame_slice_mut(self) -> Option<&'a mut [F]> {
        Some(self)
    }
}

impl<'a, F, T> DuplexFrameSlice<'a, F> for T
where
    F: Frame,
    T: FromFrameSlice<'a, F> + ToFrameSlice<'a, F>,
{
}

impl<'a, F, T> DuplexFrameSliceMut<'a, F> for T
where
    F: Frame,
    T: FromFrameSliceMut<'a, F> + ToFrameSliceMut<'a, F>,
{
}

/// Converts the given slice into a slice of `Frame`s.
///
/// Returns `None` if the number of channels in a single frame `F` is not a multiple of the number
/// of samples in the given slice.
///
/// This is a convenience function that wraps the `ToFrameSlice` trait.
///
/// # Examples
///
/// ```
/// fn main() {
///     let foo = &[0.0, 0.5, 0.0, -0.5][..];
///     let bar = dasp_slice::to_frame_slice(foo);
///     assert_eq!(bar, Some(&[[0.0, 0.5], [0.0, -0.5]][..]));
///
///     let foo = &[0.0, 0.5, 0.0][..];
///     let bar = dasp_slice::to_frame_slice(foo);
///     assert_eq!(bar, None::<&[[f32; 2]]>);
/// }
/// ```
pub fn to_frame_slice<'a, T, F>(slice: T) -> Option<&'a [F]>
where
    F: Frame,
    T: ToFrameSlice<'a, F>,
{
    slice.to_frame_slice()
}

/// Converts the given mutable slice into a mutable slice of `Frame`s.
///
/// Returns `None` if the number of channels in a single frame `F` is not a multiple of the number
/// of samples in the given slice.
///
/// This is a convenience function that wraps the `ToFrameSliceMut` trait.
///
/// # Examples
///
/// ```
/// fn main() {
///     let foo = &mut [0.0, 0.5, 0.0, -0.5][..];
///     let bar = dasp_slice::to_frame_slice_mut(foo);
///     assert_eq!(bar, Some(&mut [[0.0, 0.5], [0.0, -0.5]][..]));
///
///     let foo = &mut [0.0, 0.5, 0.0][..];
///     let bar = dasp_slice::to_frame_slice_mut(foo);
///     assert_eq!(bar, None::<&mut [[f32; 2]]>);
/// }
/// ```
pub fn to_frame_slice_mut<'a, T, F>(slice: T) -> Option<&'a mut [F]>
where
    F: Frame,
    T: ToFrameSliceMut<'a, F>,
{
    slice.to_frame_slice_mut()
}

/// Converts the given slice of `Frame`s into some slice `T`.
///
/// This is a convenience function that wraps the `FromFrameSlice` trait.
///
/// # Examples
///
/// ```
/// fn main() {
///     let foo = &[[0.0, 0.5], [0.0, -0.5]][..];
///     let bar: &[f32] = dasp_slice::from_frame_slice(foo);
///     assert_eq!(bar, &[0.0, 0.5, 0.0, -0.5][..]);
/// }
/// ```
pub fn from_frame_slice<'a, T, F>(slice: &'a [F]) -> T
where
    F: Frame,
    T: FromFrameSlice<'a, F>,
{
    T::from_frame_slice(slice)
}

/// Converts the given slice of mutable `Frame`s into some mutable slice `T`.
///
/// This is a convenience function that wraps the `FromFrameSliceMut` trait.
///
/// # Examples
///
/// ```
/// fn main() {
///     let foo = &mut [[0.0, 0.5], [0.0, -0.5]][..];
///     let bar: &mut [f32] = dasp_slice::from_frame_slice_mut(foo);
///     assert_eq!(bar, &mut [0.0, 0.5, 0.0, -0.5][..]);
/// }
/// ```
pub fn from_frame_slice_mut<'a, T, F>(slice: &'a mut [F]) -> T
where
    F: Frame,
    T: FromFrameSliceMut<'a, F>,
{
    T::from_frame_slice_mut(slice)
}