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
// Copyright Jeron Aldaron Lau 2020.
// Distributed under either the Apache License, Version 2.0
//    (See accompanying file LICENSE_APACHE_2_0.txt or copy at
//          https://apache.org/licenses/LICENSE-2.0),
// or the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_BOOST_1_0.txt or copy at
//          https://www.boost.org/LICENSE_1_0.txt)
// at your option. This file may not be copied, modified, or distributed except
// according to those terms.

//! Sample types

use crate::{chan::Channel, mono::Mono, stereo::Stereo, surround::Surround};
use core::{
    any::TypeId,
    fmt::Debug,
    mem::size_of,
    ops::{
        Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign,
    },
};

/// Frame - A number of interleaved sample [channel]s.
///
/// [channel]: crate::chan::Channel
pub trait Frame:
    Clone
    + Copy
    + Debug
    + Default
    + PartialEq
    + Unpin
    + Add<Output = Self>
    + Div<Output = Self>
    + Mul<Output = Self>
    + Sub<Output = Self>
    + Neg<Output = Self>
    + Iterator<Item = Self>
    + AddAssign
    + SubAssign
    + DivAssign
    + MulAssign
    + 'static
{
    /// Channel type
    type Chan: Channel;

    /// Number of channels
    const CHAN_COUNT: usize = size_of::<Self>() / size_of::<Self::Chan>();

    /// Speaker configuration (Stored as a list of locations, -1.0 refers to
    /// the back going left, 1.0 also refers to the back - but going right, and
    /// 0.0 refers to center (straight ahead).  These should be listed from
    /// left to right, does not include LFE.
    const CONFIG: &'static [f64];

    /// Get the channels.
    fn channels(&self) -> &[Self::Chan];

    /// Get the channels mutably.
    fn channels_mut(&mut self) -> &mut [Self::Chan];

    /// Make an audio frame with all channels set from a floating point value.
    fn from_f64(value: f64) -> Self {
        let mut ret = Self::default();
        for chan in ret.channels_mut() {
            *chan = Self::Chan::from_f64(value);
        }
        ret
    }

    /// Make an audio frame from a singular channel.
    fn from_channel(ch: Self::Chan) -> Self {
        let mut ret = Self::default();
        for chan in ret.channels_mut() {
            *chan = ch;
        }
        ret
    }

    /// Make an audio frame from a slice of channels.
    fn from_channels(ch: &[Self::Chan]) -> Self;

    /// Make an audio frame from a mono frame.
    fn from_mono(frame: Mono<Self::Chan>) -> Self {
        Self::from_channel(frame.channels()[0])
    }

    /// Linear interpolation.
    #[inline(always)]
    fn lerp(&self, rhs: Self, t: Self) -> Self {
        let mut out = Self::default();
        let main = out.channels_mut().iter_mut().zip(self.channels().iter());
        let other = rhs.channels().iter().zip(t.channels().iter());
        for ((out, this), (rhs, t)) in main.zip(other) {
            *out = this.lerp(*rhs, *t);
        }
        out
    }

    /// Convert a sample to another format.
    #[inline(always)]
    fn convert<D: Frame>(self) -> D {
        match (TypeId::of::<Self>(), TypeId::of::<D>()) {
            (a, b)
                if (a == TypeId::of::<Mono<Self::Chan>>()
                    && b == TypeId::of::<Mono<D::Chan>>())
                    || (a == TypeId::of::<Stereo<Self::Chan>>()
                        && b == TypeId::of::<Stereo<D::Chan>>())
                    || (a == TypeId::of::<Surround<Self::Chan>>()
                        && b == TypeId::of::<Surround<D::Chan>>()) =>
            {
                let mut out = [D::Chan::MID; 6];
                // Same type, 1:1
                for (src, dst) in self.channels().iter().zip(out.iter_mut()) {
                    *dst = D::Chan::from_f64(src.to_f64());
                }
                //
                D::from_channels(&out)
            }
            (a, b)
                if (a == TypeId::of::<Mono<Self::Chan>>()
                    && b == TypeId::of::<Stereo<D::Chan>>()) =>
            {
                let mut out = [D::Chan::MID; 2];
                // Mono -> Stereo, Duplicate The Channel
                out[0] = D::Chan::from_f64(self.channels()[0].to_f64());
                out[1] = D::Chan::from_f64(self.channels()[0].to_f64());
                //
                D::from_channels(&out)
            }
            (a, b)
                if (a == TypeId::of::<Mono<Self::Chan>>()
                    && b == TypeId::of::<Surround<D::Chan>>()) =>
            {
                let mut out = [D::Chan::MID; 6];
                // Mono -> Surround (Mono -> Stereo -> Surround)
                out[1] = D::Chan::from_f64(self.channels()[0].to_f64());
                out[3] = D::Chan::from_f64(self.channels()[0].to_f64());
                //
                D::from_channels(&out)
            }
            (a, b)
                if (a == TypeId::of::<Stereo<Self::Chan>>()
                    && b == TypeId::of::<Surround<D::Chan>>()) =>
            {
                let mut out = [D::Chan::MID; 6];
                // Stereo -> Surround
                out[1] = D::Chan::from_f64(self.channels()[0].to_f64());
                out[3] = D::Chan::from_f64(self.channels()[1].to_f64());
                //
                D::from_channels(&out)
            }
            (a, b)
                if (a == TypeId::of::<Surround<Self::Chan>>()
                    && b == TypeId::of::<Stereo<D::Chan>>()) =>
            {
                let mut out = [D::Chan::MID; 2];
                // Surround -> Stereo
                out[0] = D::Chan::from_f64(self.channels()[1].to_f64());
                out[1] = D::Chan::from_f64(self.channels()[3].to_f64());
                //
                D::from_channels(&out)
            }
            (a, b)
                if (a == TypeId::of::<Surround<Self::Chan>>()
                    && b == TypeId::of::<Mono<D::Chan>>()) =>
            {
                let mut out = [D::Chan::MID; 1];
                // Surround -> Stereo -> Mono
                out[0] = D::Chan::from_f64(
                    (self.channels()[1].to_f64() + self.channels()[3].to_f64())
                        * 0.5,
                );
                //
                D::from_channels(&out)
            }
            (a, b)
                if (a == TypeId::of::<Stereo<Self::Chan>>()
                    && b == TypeId::of::<Mono<D::Chan>>()) =>
            {
                let mut out = [D::Chan::MID; 1];
                // Stereo -> Mono
                out[0] = D::Chan::from_f64(
                    (self.channels()[0].to_f64() + self.channels()[1].to_f64())
                        * 0.5,
                );
                //
                D::from_channels(&out)
            }
            _ => panic!(
                "Cannot convert custom speaker configurations, \
                implement custom Frame::convert() method to override."
            ),
        }
    }
}

impl<T: Frame> crate::Stream<T> for T {
    fn sample_rate(&self) -> Option<f64> {
        None
    }

    fn len(&self) -> Option<usize> {
        None
    }
}