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
pub extern crate dasp_frame;
pub extern crate dasp_sample;

#[cfg(feature = "alac")]
pub extern crate alac;
#[cfg(feature = "caf")]
pub extern crate caf;
#[cfg(feature = "flac")]
pub extern crate claxon; // flac
#[cfg(feature = "wav")]
pub extern crate hound; // wav
#[cfg(feature = "ogg_vorbis")]
pub extern crate lewton; // ogg vorbis

#[cfg(feature = "caf_alac")]
mod caf_alac;

pub mod read;
pub mod write;

pub use crate::read::{open, Reader};

/// Enumerates the various formats supported by the crate.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Format {
    #[cfg(feature = "flac")]
    Flac,
    #[cfg(feature = "ogg_vorbis")]
    OggVorbis,
    #[cfg(feature = "wav")]
    Wav,
    #[cfg(feature = "caf_alac")]
    CafAlac,
}

impl Format {
    /// Read a `Format` from the given `extension`.
    ///
    /// This function expects that the `extension` is lowercase ASCII, e.g "wav" or "ogg".
    pub fn from_extension(extension: &str) -> Option<Self> {
        match extension {
            #[cfg(feature = "flac")]
            "flac" => Some(Format::Flac),
            #[cfg(feature = "ogg_vorbis")]
            "ogg" | "oga" => Some(Format::OggVorbis),
            #[cfg(feature = "wav")]
            "wav" | "wave" => Some(Format::Wav),
            #[cfg(feature = "caf")]
            "caf" => Some(Format::CafAlac),
            _ => None,
        }
    }

    /// Return the most commonly used file extension associated with the `Format`.
    pub fn extension(self) -> &'static str {
        match self {
            #[cfg(feature = "flac")]
            Format::Flac => "flac",
            #[cfg(feature = "wav")]
            Format::Wav => "wav",
            #[cfg(feature = "ogg_vorbis")]
            Format::OggVorbis => "ogg",
            #[cfg(feature = "caf_alac")]
            Format::CafAlac => "caf",
        }
    }
}