use crate::Sound;
use std::{fs::File, io::BufReader};
pub fn open_file<P: AsRef<std::path::Path>>(path: P) -> Result<Box<dyn Sound>, crate::Error> {
let file = File::open(path.as_ref())?;
let reader = BufReader::new(file);
open_file_with_reader(path.as_ref(), reader)
}
pub fn open_file_with_buffer_capacity<P: AsRef<std::path::Path>>(
path: P,
buffer_capacity: usize,
) -> Result<Box<dyn Sound>, crate::Error> {
let file = File::open(path.as_ref())?;
let reader = BufReader::with_capacity(buffer_capacity, file);
open_file_with_reader(path.as_ref(), reader)
}
fn open_file_with_reader(
path: &std::path::Path,
reader: BufReader<File>,
) -> Result<Box<dyn Sound>, crate::Error> {
let extension = path
.extension()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_lowercase();
let decoder: Box<dyn Sound> = match extension.as_ref() {
#[cfg(feature = "rmp3-mp3")]
"mp3" => Box::new(super::decoders::Mp3Decoder::new(reader)),
#[cfg(feature = "qoa")]
"qoa" => Box::new(super::decoders::QoaDecoder::new(reader)?),
#[cfg(feature = "hound-wav")]
"wav" => Box::new(super::decoders::WavDecoder::new(reader)?),
"_SILENCE_NEVER_MATCH_" => {
println!(
"Included to satisfy unused warnings when all features are off: {:?}",
reader
);
Box::new(crate::sounds::Silence::new(1, 1000))
}
#[cfg(feature = "symphonia")]
_ => Box::new(super::decoders::SymphoniaDecoder::new(
Box::new(reader.into_inner()),
Some(&extension),
)?),
#[cfg(not(feature = "symphonia"))]
_ => return Err(std::io::Error::from(std::io::ErrorKind::Unsupported).into()),
};
Ok(decoder)
}