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
//! # Stereo Data
//! 
//! Module containing type for handling stereo audio data.

use super::*;
use crate::utils::*;

/// Struct representing a stereophonic audio sample.
#[derive(Copy,Clone,Default)]
pub struct Stereo{
    /// Left sample value.
    pub left:SampleT,
    /// Right sample value.
    pub right:SampleT,
}

impl Stereo {
    /// Returns a new Stereo object created from individual left and right
    /// audio samples.
    /// 
    /// # Parameters
    /// 
    /// * `l` - the left audio sample.
    /// * `r` - the right audio sapmle.
    pub fn from_stereo(l:SampleT, r:SampleT) -> Stereo {
        Stereo{
            left:l,
            right:r
        }
    }

    /// Returns a new Stereo object where both left and right channels are
    /// copied from the passed sample value.
    pub fn single_stereo(x:SampleT) -> Stereo {
        Stereo{
            left:x,
            right:x
        }
    }

    /// Creates a new Stereo object from a single monophonic sample. This
    /// function reduces the power of the given sample by half to reflect human
    /// hearing.
    /// 
    /// # Parameters
    /// 
    /// * `x` - the input sample.
    pub fn from_mono(x:SampleT) -> Stereo {
        Stereo{
            left: SampleT::sqrt(0.5)*x,
            right: SampleT::sqrt(0.5)*x
        }
    }

    /// Converts the given stereophonic sample to a monophonic sample by summing
    /// the left and right samples and dividing by half power to get the full
    /// power monophonic sample.
    pub fn as_mono(self) -> SampleT {
        (self.left + self.right)/SampleT::sqrt(0.5)
    }
}

impl Panner<Stereo, f32> for Stereo {
    fn to_sample_format(s: SampleT, g: f32) -> Stereo {
        let l_lerp = utils::lerp(g, -1.0, 1.0, 0.0, -120.0);
        let r_lerp = utils::lerp(g, -1.0, 1.0, -120.0, 0.0);

        Stereo {
            left: db_to_linear(l_lerp as MathT) as SampleT * s,
            right: db_to_linear(r_lerp as MathT) as SampleT * s
        }
    }
}

impl std::ops::Neg for Stereo {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Stereo {
            left: -self.left,
            right: -self.right,
        }
    }
}

impl std::ops::Add<Stereo> for Stereo {
    type Output = Self;

    fn add(self, rhs: Stereo) -> Self::Output {
        Stereo {
            left: self.left + rhs.left,
            right: self.right + rhs.right,
        }
    }
}

impl std::ops::AddAssign<Stereo> for Stereo {
    fn add_assign(&mut self, rhs: Stereo) {
        self.left += rhs.left;
        self.right += rhs.right;
    }
}

impl std::ops::Sub<Stereo> for Stereo {
    type Output = Self;

    fn sub(self, rhs: Stereo) -> Self {
        Stereo {
            left: self.left - rhs.left,
            right: self.right - rhs.right,
        }
    }
}

impl std::ops::SubAssign<Stereo> for Stereo {
    fn sub_assign(&mut self, rhs: Stereo) {
        self.left -= rhs.left;
        self.right -= rhs.right;
    }
}

impl std::ops::Mul<Stereo> for Stereo {
    type Output = Stereo;

    fn mul(self, rhs: Stereo) -> Self::Output {
        Stereo {
            left: self.left * rhs.left,
            right: self.right * rhs.right
        }
    }
}

impl std::ops::MulAssign<Stereo> for Stereo {
    fn mul_assign(&mut self, rhs: Stereo) {
        self.left *= rhs.left;
        self.right *= rhs.right;
    }
}

impl std::ops::Mul<SampleT> for Stereo {
    type Output = Stereo;

    fn mul(self, rhs: SampleT) -> Self::Output {
        Stereo {
            left: self.left * rhs,
            right: self.right * rhs,
        }
    }
}
impl std::ops::Mul<Stereo> for SampleT {
    type Output = Stereo;

    fn mul(self, rhs: Stereo) -> Self::Output {
        Stereo {
            left: self * rhs.left,
            right: self * rhs.right,
        }
    }
}

impl std::ops::MulAssign<SampleT> for Stereo {
    fn mul_assign(&mut self, rhs: SampleT) {
        self.left *= rhs;
        self.right *= rhs;
    }
}

impl std::ops::Mul<MathT> for Stereo {
    /// Output type of the multiplication
    type Output = Stereo;

    /// Multiplies a sample by a value. E.g. scaling the sample by a gain amount.
    fn mul(self, rhs: MathT) -> Self::Output {
        Stereo {
            left:(self.left as MathT * rhs) as SampleT,
            right:(self.right as MathT * rhs) as SampleT,
        }
    }
}
impl std::ops::Mul<Stereo> for MathT {
    type Output = Stereo;

    fn mul(self, rhs: Stereo) -> Self::Output {
        Stereo {
            left: self as SampleT * rhs.left,
            right: self as SampleT * rhs.right
        }
    }
}

impl std::ops::MulAssign<MathT> for Stereo {
    fn mul_assign(&mut self, rhs: MathT) {
        self.left *= rhs as SampleT;
        self.right *= rhs as SampleT;
    }
}

impl Into<Vec<u8>> for Stereo {
    /// Converts the Stereo into a vector of bytes.
    fn into(self) -> Vec<u8> {
        let mut v = Vec::new();

            // Converts the left sample from SampleT (f32) to i16, then to bytes
        let n = ((self.left * 32768_f32) as i16).to_le_bytes();
        v.push(n[0]);
        v.push(n[1]);

            // Converts the right sample from SampleT (f32) to i16, then to bytes
        let n = ((self.right * 32768_f32) as i16).to_le_bytes();
        v.push(n[0]);
        v.push(n[1]);

        v
    }
}

impl From<SampleT> for Stereo {
    /// Copies the given sample to the left and right channels. If you want to
    /// use the half-power conversion, use [`Stereo::from_mono`].
    /// 
    /// [`Stereo::from_mono`]: struct.Stereo.html#method.from_mono
    fn from(s: SampleT) -> Self {
        Stereo{
            left: s,
            right: s,
        }
    }
}

impl From<[u8;2]> for Stereo {
    /// Converts the array of 2 bytes into a Stereo object.
    /// It is assumed that the bytes are 8-bit unsigned audio samples.
    /// 
    /// # Parameters
    /// 
    /// * `v` - The raw bytes to convert from.
    fn from(v:[u8;2]) -> Self {
        Stereo {
            left:  sample_from_u8_bytes([v[0]]),
            right: sample_from_u8_bytes([v[1]])
        }
    }
}

impl From<[u8;4]> for Stereo {
    /// Converts the array of 4 bytes into a Stereo object.
    /// It is assumed that the bytes are 16-bit signed audio samples.
    /// 
    /// # Parameters
    /// 
    /// * `v` - The raw bytes to convert from.
    fn from(v:[u8;4]) -> Self {
        Stereo {
            left:  sample_from_i16_bytes([v[0],v[1]]),
            right: sample_from_i16_bytes([v[2],v[3]])
        }
    }
}

impl From<[u8;6]> for Stereo {
    /// Converts the array of 6 bytes into a Stereo object.
    /// It is assumed that the bytes are 24-bit signed audio samples.
    /// 
    /// # Parameters
    /// 
    /// * `v` - The raw bytes to convert from.
    fn from(v:[u8;6]) -> Self {
        Stereo {
            left:  sample_from_i24_bytes([v[0],v[1],v[2]]),
            right: sample_from_i24_bytes([v[3],v[4],v[5]])
        }
    }
}