wavers 0.3.0

A Rust crate for reading and writing WAVE files.
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! 
//! Everything related to how wavers represents and converts Samples. **Very important note:** All maths operations on samples are done in the space of digital representation of sound. i.e. floats are between -1.0 and 1.0. 
//! 

use std::ops::{Add, Div, Mul, Neg, Sub};

///
/// The core enum for representing a single wav file sample in wavers. 
/// Each supported sample type is represented by a variant of this enum.
/// 
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Sample {
    I16(i16),
    I32(i32),
    F32(f32),
    F64(f64),
}

impl Sample {

    ///
    /// Converts a a sample to the correct memory representation in bytes of the underlying sample.
    /// 
    /// Returns a vector of bytes representing the sample in little endian format.
    /// 
    pub fn to_le_bytes(self) -> Vec<u8> {
        match self {
            Sample::I16(sample) => sample.to_le_bytes().to_vec(),
            Sample::I32(sample) => sample.to_le_bytes().to_vec(),
            Sample::F32(sample) => sample.to_le_bytes().to_vec(),
            Sample::F64(sample) => sample.to_le_bytes().to_vec(),
        }
    }

    ///
    /// Converts a a sample to the correct memory representation in bytes of the underlying sample.
    /// 
    /// Returns a vector of bytes representing the sample in native endian format.
    /// 
    pub fn to_ne_bytes(self) -> Vec<u8> {
        match self {
            Sample::I16(sample) => sample.to_ne_bytes().to_vec(),
            Sample::I32(sample) => sample.to_ne_bytes().to_vec(),
            Sample::F32(sample) => sample.to_ne_bytes().to_vec(),
            Sample::F64(sample) => sample.to_ne_bytes().to_vec(),
        }
    }

    ///
    /// Utility to report the size of the underlying sample in bytes.
    /// 
    /// Returns the size of the underlying sample in bytes.
    /// 
    #[inline(always)]
    pub fn size_of_underlying(&self) -> usize {
        match self {
            Sample::I16(_) => std::mem::size_of::<i16>(),
            Sample::I32(_) => std::mem::size_of::<i32>(),
            Sample::F32(_) => std::mem::size_of::<f32>(),
            Sample::F64(_) => std::mem::size_of::<f64>(),
        }
    }
}

impl Add for Sample {
    type Output = Sample;

    ///
    /// Enables addition of two samples. Note that the rhs is converted to the sample type of the lhs.
    /// 
    /// Returns the sum of the two samples.
    /// 
    /// ## Example
    /// ```rust
    /// use wavers::Sample;
    /// 
    /// fn main() {
    ///     let sample1 = Sample::I16(1);
    ///     let sample2 = Sample::I16(2);
    ///     println!("{:?}", sample1 + sample2);
    /// }
    /// ```
    /// 
    fn add(self, rhs: Self) -> Self::Output {
        match self {
            Sample::I16(x) => Sample::I16(x + rhs.as_i16()),
            Sample::I32(x) => Sample::I32(x + rhs.as_i32()),
            Sample::F32(x) => Sample::F32(x + rhs.as_f32()),
            Sample::F64(x) => Sample::F64(x + rhs.as_f64()),
        }
    }
}

impl Sub for Sample {
    type Output = Sample;

    ///
    /// Enables subtraction of two samples. Note that the rhs is converted to the sample type of the lhs.
    /// 
    /// Returns the difference of the two samples.
    /// 
    /// ## Example
    /// ```rust
    /// use wavers::Sample;
    /// 
    /// fn main() {
    ///     let sample1 = Sample::I16(1);
    ///     let sample2 = Sample::I16(2);
    ///     println!("{:?}", sample1 - sample2);
    /// }
    /// ```
    /// 
    fn sub(self, rhs: Self) -> Self::Output {
        match self {
            Sample::I16(x) => Sample::I16(x - rhs.as_i16()),
            Sample::I32(x) => Sample::I32(x - rhs.as_i32()),
            Sample::F32(x) => Sample::F32(x - rhs.as_f32()),
            Sample::F64(x) => Sample::F64(x - rhs.as_f64()),
        }
    }
}

impl Mul for Sample {
    type Output = Sample;

    ///
    /// Enables multiplication of two samples. Note that the rhs is converted to the sample type of the lhs.
    /// 
    /// Returns the product of the two samples.
    /// 
    /// ## Example
    /// ```rust
    /// use wavers::Sample;
    /// 
    /// fn main() {
    ///    let sample1 = Sample::I16(1);
    ///   let sample2 = Sample::I16(2);
    ///    println!("{:?}", sample1 * sample2);
    /// }
    /// ```
    /// 
    fn mul(self, rhs: Self) -> Self::Output {
        match self {
            Sample::I16(x) => Sample::I16(x * rhs.as_i16()),
            Sample::I32(x) => Sample::I32(x * rhs.as_i32()),
            Sample::F32(x) => Sample::F32(x * rhs.as_f32()),
            Sample::F64(x) => Sample::F64(x * rhs.as_f64()),
        }
    }
}

impl Div for Sample {
    type Output = Sample;

    ///
    /// Enables division of two samples. Note that the rhs is converted to the sample type of the lhs.
    /// 
    /// Returns the result of division of the two samples.
    /// 
    /// ## Example
    /// 
    /// ```rust
    /// use wavers::Sample;
    /// 
    /// fn main() {
    ///    let sample1 = Sample::I16(4);
    ///   let sample2 = Sample::I16(2);
    ///     println!("{:?}", sample1 / sample2);
    /// }
    /// ```
    /// 
    fn div(self, rhs: Self) -> Self::Output {
        match self {
            Sample::I16(x) => Sample::I16(x / rhs.as_i16()),
            Sample::I32(x) => Sample::I32(x / rhs.as_i32()),
            Sample::F32(x) => Sample::F32(x / rhs.as_f32()),
            Sample::F64(x) => Sample::F64(x / rhs.as_f64()),
        }
    }
}

impl Neg for Sample {
    type Output = Sample;

    ///
    /// Enables negation of a sample.
    /// 
    /// Returns the negation of the sample.
    /// 
    /// ## Example
    /// 
    /// ```rust
    /// use wavers::Sample;
    /// 
    /// fn main() {
    ///    let sample1 = Sample::I16(1);
    ///    println!("{:?}", -sample1);
    /// }
    /// ```
    /// 
    fn neg(self) -> Self::Output {
        match self {
            Sample::I16(x) => Sample::I16(-x),
            Sample::I32(x) => Sample::I32(-x),
            Sample::F32(x) => Sample::F32(-x),
            Sample::F64(x) => Sample::F64(-x),
        }
    }
}

///
/// Trait which enables the easy conversion of samples to other sample types.
/// 
pub trait AudioConversion {
    fn as_i16(self) -> i16;
    fn as_i32(self) -> i32;
    fn as_f32(self) -> f32;
    fn as_f64(self) -> f64;
    fn as_type(self, as_type: Sample) -> Sample
    where
        Self: Sized,
    {
        match as_type {
            Sample::I16(_) => Sample::I16(self.as_i16()),
            Sample::I32(_) => Sample::I32(self.as_i32()),
            Sample::F32(_) => Sample::F32(self.as_f32()),
            Sample::F64(_) => Sample::F64(self.as_f64()),
        }
    }
}

///
/// Trait which enables the easy conversion of iterators of samples to other sample types.
/// 
pub trait IterAudioConversion {
    fn as_i16(&mut self) -> Vec<i16>;
    fn as_i32(&mut self) -> Vec<i32>;
    fn as_f32(&mut self) -> Vec<f32>;
    fn as_f64(&mut self) -> Vec<f64>;
    fn as_i16_samples(&mut self) -> Vec<Sample> {
        self.as_i16()
            .iter()
            .map(|sample| Sample::I16(*sample))
            .collect::<Vec<Sample>>()
    }
    fn as_i32_samples(&mut self) -> Vec<Sample> {
        self.as_i32()
            .iter()
            .map(|sample| Sample::I32(*sample))
            .collect::<Vec<Sample>>()
    }

    fn as_f32_samples(&mut self) -> Vec<Sample> {
        self.as_f32()
            .iter()
            .map(|sample| Sample::F32(*sample))
            .collect::<Vec<Sample>>()
    }

    fn as_f64_samples(&mut self) -> Vec<Sample> {
        self.as_f64()
            .iter()
            .map(|sample| Sample::F64(*sample))
            .collect::<Vec<Sample>>()
    }

    fn as_sample_type(&mut self, as_type: Sample) -> Vec<Sample> {
        match as_type {
            Sample::I16(_) => self.as_i16_samples(),
            Sample::I32(_) => self.as_i32_samples(),
            Sample::F32(_) => self.as_f32_samples(),
            Sample::F64(_) => self.as_f64_samples(),
        }
    }
}


impl AudioConversion for Sample {
    /// 
    /// Converts a sample to an i16.
    /// 
    fn as_i16(self) -> i16 {
        match self {
            Sample::I16(sample) => sample,
            Sample::I32(sample) => sample.as_i16(),
            Sample::F32(sample) => sample.as_i16(),
            Sample::F64(sample) => sample.as_i16(),
        }
    }

    ///
    /// Converts a sample to an i32.
    /// 
    fn as_i32(self) -> i32 {
        match self {
            Sample::I16(sample) => sample.as_i32(),
            Sample::I32(sample) => sample,
            Sample::F32(sample) => sample.as_i32(),
            Sample::F64(sample) => sample.as_i32(),
        }
    }

    ///
    /// Converts a sample to an f32.
    /// 
    fn as_f32(self) -> f32 {
        match self {
            Sample::I16(sample) => sample.as_f32(),
            Sample::I32(sample) => sample.as_f32(),
            Sample::F32(sample) => sample,
            Sample::F64(sample) => sample.as_f32(),
        }
    }

    ///
    /// Converts a sample to an f64.
    /// 
    fn as_f64(self) -> f64 {
        match self {
            Sample::I16(sample) => sample.as_f64(),
            Sample::I32(sample) => sample.as_f64(),
            Sample::F32(sample) => sample.as_f64(),
            Sample::F64(sample) => sample,
        }
    }

    ///
    /// Converts a sample to another, given sample type.
    /// 
    fn as_type(self, as_type: Sample) -> Sample {
        match as_type {
            Sample::I16(_) => Sample::I16(self.as_i16()),
            Sample::I32(_) => Sample::I32(self.as_i32()),
            Sample::F32(_) => Sample::F32(self.as_f32()),
            Sample::F64(_) => Sample::F64(self.as_f64()),
        }
    }
}

impl IterAudioConversion for Vec<Sample> {
    ///
    /// Converts a vector of samples to i16.
    /// 
    fn as_i16(&mut self) -> Vec<i16> {
        self.iter_mut()
            .map(|sample| sample.as_i16())
            .collect::<Vec<i16>>()
    }

    ///
    /// Converts a vector of samples to i32.
    /// 
    fn as_i32(&mut self) -> Vec<i32> {
        self.iter_mut()
            .map(|sample| sample.as_i32())
            .collect::<Vec<i32>>()
    }

    ///
    /// Converts a vector of samples to f32.
    /// 
    fn as_f32(&mut self) -> Vec<f32> {
        self.iter_mut()
            .map(|sample| sample.as_f32())
            .collect::<Vec<f32>>()
    }

    ///
    /// Converts a vector of samples to f64.
    /// 
    fn as_f64(&mut self) -> Vec<f64> {
        self.iter_mut()
            .map(|sample| sample.as_f64())
            .collect::<Vec<f64>>()
    }
}


impl AudioConversion for i16 {
    ///
    /// Converts an i16 to an i16.
    /// 
    fn as_i16(self) -> i16 {
        self
    }


    ///
    /// Converts an i16 to an i32.
    /// 
    fn as_i32(self) -> i32 {
        self as i32
    }

    ///
    /// Converts an i16 to an f32.
    /// 
    fn as_f32(self) -> f32 {
        (self as f32 / 32768.0).clamp(-1.0, 1.0)
    }

    ///
    /// Converts an i16 to an f64.
    /// 
    fn as_f64(self) -> f64 {
        (self as f64 / 32768.0).clamp(-1.0, 1.0)
    }
}

impl AudioConversion for i32 {
    
    ///
    /// Converts an i32 to an i16.
    /// 
    fn as_i16(self) -> i16 {
        (self >> 16) as i16
    }

    ///
    /// Converts an i32 to an i32.
    /// 
    fn as_i32(self) -> i32 {
        self
    }


    ///
    /// Converts an i32 to an f32.
    /// 
    fn as_f32(self) -> f32 {
        (self as f32 / 2147483648.0).clamp(-1.0, 1.0)
    }

    ///
    /// Converts an i32 to an f64.
    /// 
    fn as_f64(self) -> f64 {
        (self as f64 / 2147483648.0).clamp(-1.0, 1.0)
    }
}

impl AudioConversion for f32 {

    ///
    /// Converts an f32 to an i16.
    /// 
    fn as_i16(self) -> i16 {
        (self * 32768.0).clamp(-32768.0, 32767.0) as i16
    }

    ///
    /// Converts an f32 to an i32.
    /// 
    fn as_i32(self) -> i32 {
        (self * 2147483648.0).clamp(-2147483648.0, 2147483647.0) as i32
    }

    ///
    /// Converts an f32 to an f32.
    /// 
    fn as_f32(self) -> f32 {
        self
    }

    ///
    /// Converts an f32 to an f64.
    /// 
    fn as_f64(self) -> f64 {
        self as f64
    }
}

impl AudioConversion for f64 {

    ///
    /// Converts an f64 to an i16.
    /// 
    fn as_i16(self) -> i16 {
        (self * 32768.0).clamp(-32768.0, 32767.0) as i16
    }

    ///
    /// Converts an f64 to an i32.
    /// 
    fn as_i32(self) -> i32 {
        (self * 2147483648.0).clamp(-2147483648.0, 2147483647.0) as i32
    }

    ///
    /// Converts an f64 to an f32.
    /// 
    fn as_f32(self) -> f32 {
        self as f32
    }

    ///
    /// Converts an f64 to an f64.
    /// 
    fn as_f64(self) -> f64 {
        self
    }
}

impl std::fmt::Display for Sample {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Sample::I16(sample) => write!(f, "{}", *sample),
            Sample::I32(sample) => write!(f, "{}", *sample),
            Sample::F32(sample) => write!(f, "{}", *sample),
            Sample::F64(sample) => write!(f, "{}", *sample),
        }
    }
}

impl PartialEq<i16> for Sample {
    fn eq(&self, other: &i16) -> bool {
        match self {
            Sample::I16(sample) => sample == other,
            Sample::I32(sample) => sample.as_i16() == *other,
            Sample::F32(sample) => sample.as_i16() == *other,
            Sample::F64(sample) => sample.as_i16() == *other,
        }
    }
}

impl PartialEq<i32> for Sample {
    fn eq(&self, other: &i32) -> bool {
        match self {
            Sample::I16(sample) => sample.as_i32() == *other,
            Sample::I32(sample) => sample == other,
            Sample::F32(sample) => sample.as_i32() == *other,
            Sample::F64(sample) => sample.as_i32() == *other,
        }
    }
}

impl PartialEq<f32> for Sample {
    fn eq(&self, other: &f32) -> bool {
        match self {
            Sample::I16(sample) => sample.as_f32() == *other,
            Sample::I32(sample) => sample.as_f32() == *other,
            Sample::F32(sample) => sample == other,
            Sample::F64(sample) => sample.as_f32() == *other,
        }
    }
}

impl PartialEq<f64> for Sample {
    fn eq(&self, other: &f64) -> bool {
        match self {
            Sample::I16(sample) => sample.as_f64() == *other,
            Sample::I32(sample) => sample.as_f64() == *other,
            Sample::F32(sample) => sample.as_f64() == *other,
            Sample::F64(sample) => sample == other,
        }
    }
}

#[cfg(test)]
pub mod sample_test {
    ///
    /// Tests the Sample struct.
    /// 
    use super::Sample;

    #[test]
    fn test_sample_can_add() {
        let sample1 = Sample::I16(1);
        let sample2 = Sample::I16(2);
        let sample3 = Sample::I16(3);
        assert_eq!(sample1 + sample2, sample3, "I16 Sample addition failed");

        let sample1 = Sample::I32(1);
        let sample2 = Sample::I32(2);
        let sample3 = Sample::I32(3);
        assert_eq!(sample1 + sample2, sample3, "I32 Sample addition failed");

        let sample1 = Sample::F32(1.0);
        let sample2 = Sample::F32(2.0);
        let sample3 = Sample::F32(3.0);
        assert_eq!(sample1 + sample2, sample3, "F32 Sample addition failed");

        let sample1 = Sample::F64(1.0);
        let sample2 = Sample::F64(2.0);
        let sample3 = Sample::F64(3.0);
        assert_eq!(sample1 + sample2, sample3, "F64 Sample addition failed");
    }
}