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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Items for reading and opening file formats from file.

use sample;
use std;
use Format;

#[cfg(feature="flac")]
use claxon;
#[cfg(feature="wav")]
use hound;
#[cfg(feature="ogg_vorbis")]
use lewton;
#[cfg(feature="caf")]
use caf::{self, CafError};


/// Types to which read samples may be converted via the `Reader::samples` method.
pub trait Sample: sample::Sample
    + sample::FromSample<i8>
    + sample::FromSample<i16>
    + sample::FromSample<i32>
    + sample::FromSample<f32> {}

impl<T> Sample for T
    where T: sample::Sample
           + sample::FromSample<i8>
           + sample::FromSample<i16>
           + sample::FromSample<i32>
           + sample::FromSample<f32> {}


/// Returned by the `read` function, enumerates the various supported readers.
pub enum Reader<R>
    where R: std::io::Read + std::io::Seek,
{
    #[cfg(feature="flac")]
    Flac(claxon::FlacReader<R>),
    #[cfg(feature="ogg_vorbis")]
    OggVorbis(lewton::inside_ogg::OggStreamReader<R>),
    #[cfg(feature="wav")]
    Wav(hound::WavReader<R>),
    #[cfg(feature="caf_alac")]
    CafAlac(super::caf_alac::AlacReader<R>),
}


/// An iterator that reads samples from the underlying reader, converts them to the sample type `S`
/// if not already in that format and yields them.
pub struct Samples<'a, R, S>
    where R: 'a + std::io::Read + std::io::Seek,
{
    format: FormatSamples<'a, R>,
    sample: std::marker::PhantomData<S>,
}

// The inner part of the `Samples` iterator, specific to the format of the `Reader` used to produce
// the `Samples`.
enum FormatSamples<'a, R>
    where R: 'a + std::io::Read + std::io::Seek,
{
    #[cfg(feature="flac")]
    Flac(claxon::FlacSamples<&'a mut claxon::input::BufferedReader<R>>),

    #[cfg(feature="ogg_vorbis")]
    OggVorbis {
        reader: &'a mut lewton::inside_ogg::OggStreamReader<R>, 
        index: usize,
        buffer: Vec<i16>,
    },

    #[cfg(feature="wav")]
    Wav(WavSamples<'a, R>),

    #[cfg(feature="caf_alac")]
    CafAlac {
        reader: &'a mut super::caf_alac::AlacReader<R>,
        index: usize,
        buffer: Vec<i32>,
    },
}

// The variants of hound's supported sample bit depths.
#[cfg(feature="wav")]
enum WavSamples<'a, R: 'a> {
    I8(hound::WavSamples<'a, R, i8>),
    I16(hound::WavSamples<'a, R, i16>),
    I32(hound::WavSamples<'a, R, i32>),
    F32(hound::WavSamples<'a, R, f32>),
}

/// An iterator that reads samples from the underlying reader, converts them to frames of type `F`
/// and yields them.
pub struct Frames<'a, R, F>
    where R: 'a + std::io::Read + std::io::Seek,
          F: sample::Frame,
{
    samples: Samples<'a, R, F::Sample>,
    frame: std::marker::PhantomData<F>,
}

/// An alias for the buffered, file `Reader` type returned from the `open` function.
pub type BufFileReader = Reader<std::io::BufReader<std::fs::File>>;

/// A description of the audio format that was read from file.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Description {
    format: Format,
    channel_count: u32,
    sample_rate: u32,
}


/// Errors that might be returned from the `Reader::new` function.
#[derive(Debug)]
pub enum ReadError {
    Io(std::io::Error),
    Reader(FormatError),
    UnsupportedFormat,
}

/// Format-specific errors that might occur when opening or reading from an audio file.
#[derive(Debug)]
pub enum FormatError {
    #[cfg(feature="flac")]
    Flac(claxon::Error),
    #[cfg(feature="ogg_vorbis")]
    OggVorbis(lewton::VorbisError),
    #[cfg(feature="wav")]
    Wav(hound::Error),
    #[cfg(feature="caf")]
    Caf(caf::CafError),
    #[cfg(feature="alac")]
    Alac(()),
}


/// Attempts to open an audio `Reader` from the file at the specified `Path`.
///
/// The format is determined from the path's file extension.
pub fn open<P>(file_path: P) -> Result<BufFileReader, ReadError>
    where P: AsRef<std::path::Path>,
{
    BufFileReader::open(file_path)
}


impl Description {

    /// The format from which the audio will be read.
    pub fn format(&self) -> Format {
        self.format
    }

    /// The number of channels of audio.
    ///
    /// E.g. For audio stored in stereo this should return `2`. Mono audio will return `1`.
    pub fn channel_count(&self) -> u32 {
        self.channel_count
    }

    /// The rate in Hertz at which each channel of the stored audio is sampled.
    ///
    /// E.g. A `sample_rate` of 44_100 indicates that the audio is sampled 44_100 times per second
    /// per channel.
    pub fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

}


impl BufFileReader {

    /// Attempts to open an audio `Reader` from the file at the specified `Path`.
    ///
    /// This function is a convenience wrapper around the `Reader::new` function.
    ///
    /// This function pays no attention to the `file_path`'s extension and instead attempts to read
    /// a supported `Format` via the file header.
    pub fn open<P>(file_path: P) -> Result<Self, ReadError>
        where P: AsRef<std::path::Path>,
    {
        let path = file_path.as_ref();
        let file = try!(std::fs::File::open(path));
        let reader = std::io::BufReader::new(file);
        Reader::new(reader)
    }

}

impl<R> Reader<R>
    where R: std::io::Read + std::io::Seek,
{

    /// Attempts to read the format of the audio read by the given `reader` and returns the associated
    /// `Reader` variant.
    ///
    /// The format is determined by attempting to construct each specific format reader until one
    /// is successful.
    pub fn new(mut reader: R) -> Result<Self, ReadError> {

        #[cfg(feature="wav")]
        {
            let is_wav = match hound::WavReader::new(&mut reader) {
                Err(hound::Error::FormatError(_)) => false,
                Err(err) => return Err(err.into()),
                Ok(_) => true,
            };
            try!(reader.seek(std::io::SeekFrom::Start(0)));
            if is_wav {
                return Ok(Reader::Wav(try!(hound::WavReader::new(reader))));
            }
        }

        #[cfg(feature="flac")]
        {
            let is_flac = match claxon::FlacReader::new(&mut reader) {
                Err(claxon::Error::FormatError(_)) => false,
                Err(err) => return Err(err.into()),
                Ok(_) => true,
            };
            try!(reader.seek(std::io::SeekFrom::Start(0)));
            if is_flac {
                return Ok(Reader::Flac(try!(claxon::FlacReader::new(reader))));
            }
        }

        #[cfg(feature="ogg_vorbis")]
        {
            let is_ogg_vorbis = match lewton::inside_ogg::OggStreamReader::new(&mut reader) {
                Err(lewton::VorbisError::OggError(_)) |
                Err(lewton::VorbisError::BadHeader(lewton::header::HeaderReadError::NotVorbisHeader)) => false,
                Err(err) => return Err(err.into()),
                Ok(_) => true,
            };
            try!(reader.seek(std::io::SeekFrom::Start(0)));
            if is_ogg_vorbis {
                return Ok(Reader::OggVorbis(try!(lewton::inside_ogg::OggStreamReader::new(reader))));
            }
        }

        #[cfg(feature="caf_alac")]
        {
            let is_caf_alac = match super::caf_alac::AlacReader::new(&mut reader) {
                Err(FormatError::Caf(CafError::NotCaf)) => false,
                Err(err) => return Err(err.into()),
                // There is a CAF container, but no ALAC inside
                Ok(None) => false,
                // Everything is fine!
                Ok(Some(_)) => true,
            };
            try!(reader.seek(std::io::SeekFrom::Start(0)));
            if is_caf_alac {
                return Ok(Reader::CafAlac(try!(super::caf_alac::AlacReader::new(reader)).unwrap()));
            }
        }

        Err(ReadError::UnsupportedFormat)
    }

    /// The format from which the audio will be read.
    pub fn format(&self) -> Format {
        match *self {
            #[cfg(feature="flac")]
            Reader::Flac(_) => Format::Flac,
            #[cfg(feature="ogg_vorbis")]
            Reader::OggVorbis(_) => Format::OggVorbis,
            #[cfg(feature="wav")]
            Reader::Wav(_) => Format::Wav,
            #[cfg(feature="caf_alac")]
            Reader::CafAlac(_) => Format::CafAlac,
        }
    }

    /// A basic description of the audio being read.
    pub fn description(&self) -> Description {
        match *self {

            #[cfg(feature="flac")]
            Reader::Flac(ref reader) => {
                let info = reader.streaminfo();
                Description {
                    format: Format::Flac,
                    channel_count: info.channels as u32,
                    sample_rate: info.sample_rate,
                }
            },

            #[cfg(feature="ogg_vorbis")]
            Reader::OggVorbis(ref reader) => {
                Description {
                    format: Format::OggVorbis,
                    channel_count: reader.ident_hdr.audio_channels as u32,
                    sample_rate: reader.ident_hdr.audio_sample_rate as u32,
                }
            },

            #[cfg(feature="wav")]
            Reader::Wav(ref reader) => {
                let spec = reader.spec();
                Description {
                    format: Format::Wav,
                    channel_count: spec.channels as u32,
                    sample_rate: spec.sample_rate,
                }
            },

            #[cfg(feature="caf_alac")]
            Reader::CafAlac(ref reader) => {
                let desc = &reader.caf_reader.audio_desc;
                Description {
                    format: Format::CafAlac,
                    channel_count: desc.channels_per_frame as u32,
                    sample_rate: (1.0 / desc.sample_rate) as u32,
                }
            },

        }
    }

    /// Produce an iterator that reads samples from the underlying reader, converts them to the
    /// sample type `S` if not already in that format and yields them.
    ///
    /// When reading from multiple channels, samples are **interleaved**.
    pub fn samples<S>(&mut self) -> Samples<R, S>
        where S: Sample,
    {
        let format = match *self {

            #[cfg(feature="flac")]
            Reader::Flac(ref mut reader) => FormatSamples::Flac(reader.samples()),

            #[cfg(feature="ogg_vorbis")]
            Reader::OggVorbis(ref mut reader) => FormatSamples::OggVorbis {
                reader: reader,
                index: 0,
                buffer: Vec::new(),
            },

            #[cfg(feature="wav")]
            Reader::Wav(ref mut reader) => {
                let spec = reader.spec();
                match spec.sample_format {
                    hound::SampleFormat::Int => match spec.bits_per_sample {
                        8 => FormatSamples::Wav(WavSamples::I8(reader.samples())),
                        16 => FormatSamples::Wav(WavSamples::I16(reader.samples())),
                        _ => FormatSamples::Wav(WavSamples::I32(reader.samples())),
                    },
                    hound::SampleFormat::Float =>
                        FormatSamples::Wav(WavSamples::F32(reader.samples())),
                }
            },

            #[cfg(feature="caf_alac")]
            Reader::CafAlac(ref mut reader) => FormatSamples::CafAlac {
                reader: reader,
                index: 0,
                buffer: Vec::new(),
            },
        };

        Samples {
            format: format,
            sample: std::marker::PhantomData,
        }
    }

    /// Produce an iterator that yields read frames from the underlying `Reader`.
    ///
    /// This method currently expects that the frame type `F` has the same number of channels as
    /// stored in the underlying audio format.
    ///
    /// TODO: Should consider changing this behaviour to check the audio file's actual number of
    /// channels and automatically convert to `F`'s number of channels while reading.
    pub fn frames<F>(&mut self) -> Frames<R, F>
        where F: sample::Frame,
              F::Sample: Sample,
    {
        Frames {
            samples: self.samples(),
            frame: std::marker::PhantomData,
        }
    }

}


impl<'a, R, S> Iterator for Samples<'a, R, S>
    where R: std::io::Read + std::io::Seek,
          S: Sample,
{
    type Item = Result<S, FormatError>;
    fn next(&mut self) -> Option<Self::Item> {
        match self.format {

            #[cfg(feature="flac")]
            FormatSamples::Flac(ref mut flac_samples) => {
                flac_samples.next().map(|sample| {
                    sample.map_err(FormatError::Flac).map(sample::Sample::to_sample)
                })
            },

            #[cfg(feature="ogg_vorbis")]
            FormatSamples::OggVorbis { ref mut reader, ref mut index, ref mut buffer } => loop {

                // Convert and return any pending samples.
                if *index < buffer.len() {
                    let sample = sample::Sample::to_sample(buffer[*index]);
                    *index += 1;
                    return Some(Ok(sample));
                }

                // If there are no samples left in the buffer, refill the buffer.
                match reader.read_dec_packet_itl() {
                    Ok(Some(packet)) => {
                        std::mem::replace(buffer, packet);
                        *index = 0;
                    },
                    Ok(None) => return None,
                    Err(err) => return Some(Err(err.into())),
                }
            },

            #[cfg(feature="wav")]
            FormatSamples::Wav(ref mut wav_samples) => {

                macro_rules! next_sample {
                    ($samples:expr) => {{
                        $samples.next().map(|sample| {
                            sample.map_err(FormatError::Wav).map(sample::Sample::to_sample)
                        })
                    }};
                }

                match *wav_samples {
                    WavSamples::I8(ref mut samples) => next_sample!(samples),
                    WavSamples::I16(ref mut samples) => next_sample!(samples),
                    WavSamples::I32(ref mut samples) => next_sample!(samples),
                    WavSamples::F32(ref mut samples) => next_sample!(samples),
                }
            },

            #[cfg(feature="caf_alac")]
            FormatSamples::CafAlac { ref mut reader, ref mut index, ref mut buffer } => loop {

                // Convert and return any pending samples.
                if *index < buffer.len() {
                    let sample = sample::Sample::to_sample(buffer[*index]);
                    *index += 1;
                    return Some(Ok(sample));
                }

                // If there are no samples left in the buffer, refill the buffer.
                match reader.read_packet() {
                    Ok(Some(packet)) => {
                        std::mem::replace(buffer, packet);
                        *index = 0;
                    },
                    Ok(None) => return None,
                    Err(err) => return Some(Err(err.into())),
                }
            },

        }
    }
}


impl<'a, R, F> Iterator for Frames<'a, R, F>
    where R: std::io::Read + std::io::Seek,
          F: sample::Frame,
          F::Sample: Sample,
{
    type Item = Result<F, FormatError>;
    fn next(&mut self) -> Option<Self::Item> {

        enum FrameConstruction {
            NotEnoughSamples,
            Ok,
            Err(FormatError),
        }

        let mut result = FrameConstruction::Ok;
        let frame = F::from_fn(|_| match self.samples.next() {
            Some(Ok(sample)) => sample,
            Some(Err(error)) => {
                result = FrameConstruction::Err(error.into());
                <F::Sample as sample::Sample>::equilibrium()
            },
            None => {
                result = FrameConstruction::NotEnoughSamples;
                <F::Sample as sample::Sample>::equilibrium()
            },
        });

        match result {
            FrameConstruction::Ok => Some(Ok(frame)),
            FrameConstruction::Err(error) => Some(Err(error)),
            FrameConstruction::NotEnoughSamples => None,
        }
    }
}


#[cfg(feature="flac")]
impl From<claxon::Error> for FormatError {
    fn from(err: claxon::Error) -> Self {
        FormatError::Flac(err)
    }
}

#[cfg(feature="ogg_vorbis")]
impl From<lewton::VorbisError> for FormatError {
    fn from(err: lewton::VorbisError) -> Self {
        FormatError::OggVorbis(err)
    }
}

#[cfg(feature="wav")]
impl From<hound::Error> for FormatError {
    fn from(err: hound::Error) -> Self {
        FormatError::Wav(err)
    }
}

#[cfg(feature="caf")]
impl From<CafError> for FormatError {
    fn from(err: CafError) -> Self {
        FormatError::Caf(err)
    }
}

impl<T> From<T> for ReadError
    where T: Into<FormatError>,
{
    fn from(err: T) -> Self {
        ReadError::Reader(err.into())
    }
}

impl From<std::io::Error> for ReadError {
    fn from(err: std::io::Error) -> Self {
        ReadError::Io(err)
    }
}


impl std::error::Error for FormatError {
    fn description(&self) -> &str {
        match *self {
            #[cfg(feature="flac")]
            FormatError::Flac(ref err) => std::error::Error::description(err),
            #[cfg(feature="ogg_vorbis")]
            FormatError::OggVorbis(ref err) => std::error::Error::description(err),
            #[cfg(feature="wav")]
            FormatError::Wav(ref err) => std::error::Error::description(err),
            #[cfg(feature="caf")]
            FormatError::Caf(ref err) => std::error::Error::description(err),
            #[cfg(feature="alac")]
            FormatError::Alac(_) => "Alac decode error",
        }
    }
    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            #[cfg(feature="flac")]
            FormatError::Flac(ref err) => Some(err),
            #[cfg(feature="ogg_vorbis")]
            FormatError::OggVorbis(ref err) => Some(err),
            #[cfg(feature="wav")]
            FormatError::Wav(ref err) => Some(err),
            #[cfg(feature="caf")]
            FormatError::Caf(ref err) => Some(err),
            #[cfg(feature="alac")]
            FormatError::Alac(_) => None,
        }
    }
}

impl std::error::Error for ReadError {
    fn description(&self) -> &str {
        match *self {
            ReadError::Io(ref err) => std::error::Error::description(err),
            ReadError::Reader(ref err) => std::error::Error::description(err),
            ReadError::UnsupportedFormat => "no supported format was detected",
        }
    }
    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            ReadError::Io(ref err) => Some(err),
            ReadError::Reader(ref err) => Some(err),
            ReadError::UnsupportedFormat => None,
        }
    }
}


impl std::fmt::Display for FormatError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match *self {
            #[cfg(feature="flac")]
            FormatError::Flac(ref err) => err.fmt(f),
            #[cfg(feature="ogg_vorbis")]
            FormatError::OggVorbis(ref err) => err.fmt(f),
            #[cfg(feature="wav")]
            FormatError::Wav(ref err) => err.fmt(f),
            #[cfg(feature="caf")]
            FormatError::Caf(ref err) => err.fmt(f),
            #[cfg(feature="alac")]
            FormatError::Alac(_) => write!(f, "{}",
                std::error::Error::description(self)),
        }
    }
}

impl std::fmt::Display for ReadError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match *self {
            ReadError::Io(ref err) => err.fmt(f),
            ReadError::Reader(ref err) => err.fmt(f),
            ReadError::UnsupportedFormat =>
                write!(f, "{}", std::error::Error::description(self)),
        }
    }
}