add open function for audio
This commit is contained in:
parent
4d80833bbd
commit
af7b4f77bb
3 changed files with 41 additions and 4 deletions
|
@ -12,6 +12,8 @@ mod dynamic {
|
||||||
path::Path,
|
path::Path,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::snd::{self, Audio};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
img::{self, DynamicImage},
|
img::{self, DynamicImage},
|
||||||
Bytes,
|
Bytes,
|
||||||
|
@ -26,7 +28,7 @@ mod dynamic {
|
||||||
pub enum DynamicBendable {
|
pub enum DynamicBendable {
|
||||||
Image(DynamicImage),
|
Image(DynamicImage),
|
||||||
Binary(Bytes),
|
Binary(Bytes),
|
||||||
Sound,
|
Sound(Audio),
|
||||||
Text,
|
Text,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +38,8 @@ mod dynamic {
|
||||||
Io(#[from] io::Error),
|
Io(#[from] io::Error),
|
||||||
#[error("{0:?}")]
|
#[error("{0:?}")]
|
||||||
Image(#[from] img::ImageError),
|
Image(#[from] img::ImageError),
|
||||||
|
#[error("{0:?}")]
|
||||||
|
Audio(#[from] snd::AudioOpenError),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open<P: AsRef<Path>>(path: P) -> Result<Option<DynamicBendable>, OpenError> {
|
pub fn open<P: AsRef<Path>>(path: P) -> Result<Option<DynamicBendable>, OpenError> {
|
||||||
|
@ -50,7 +54,7 @@ mod dynamic {
|
||||||
File::open(path)?.read_to_end(&mut buf)?;
|
File::open(path)?.read_to_end(&mut buf)?;
|
||||||
buf
|
buf
|
||||||
})),
|
})),
|
||||||
Audio => todo!(),
|
Audio => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)),
|
||||||
Book => todo!(),
|
Book => todo!(),
|
||||||
Doc => todo!(),
|
Doc => todo!(),
|
||||||
Font => todo!(),
|
Font => todo!(),
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
pub use symphonia::core::sample::Sample;
|
pub use symphonia::core::sample::Sample;
|
||||||
mod raw;
|
mod raw;
|
||||||
pub use raw::RawSamples;
|
pub use raw::*;
|
||||||
mod simphonia;
|
mod simphonia;
|
||||||
|
pub use simphonia::*;
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
use std::io::{self, Read};
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{self, Read},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
use symphonia::{
|
use symphonia::{
|
||||||
|
@ -14,6 +18,7 @@ use symphonia::{
|
||||||
},
|
},
|
||||||
default,
|
default,
|
||||||
};
|
};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::{IntoDataBytes, TryFromDataBytes};
|
use crate::{IntoDataBytes, TryFromDataBytes};
|
||||||
|
|
||||||
|
@ -28,6 +33,33 @@ pub struct Audio {
|
||||||
decoder: Box<dyn Decoder>,
|
decoder: Box<dyn Decoder>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum AudioOpenError {
|
||||||
|
#[error("IO error: {0}")]
|
||||||
|
Io(#[from] io::Error),
|
||||||
|
#[error("symphonia can't open this file: {0}")]
|
||||||
|
Symphonia(#[from] symphonia::core::errors::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Audio {
|
||||||
|
pub fn open<P: AsRef<Path>>(path: P) -> Result<Audio, AudioOpenError> {
|
||||||
|
let registry = default::get_codecs();
|
||||||
|
let probe = default::get_probe();
|
||||||
|
let mediasource = File::open(path.as_ref())?;
|
||||||
|
let mss = MediaSourceStream::new(Box::new(mediasource), Default::default());
|
||||||
|
let reader = probe
|
||||||
|
.format(
|
||||||
|
&Default::default(),
|
||||||
|
mss,
|
||||||
|
&Default::default(),
|
||||||
|
&Default::default(),
|
||||||
|
)?
|
||||||
|
.format;
|
||||||
|
let decoder = registry.make(&Default::default(), &Default::default())?;
|
||||||
|
Ok(Audio::new(reader, decoder))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFromDataBytes for Audio {
|
impl TryFromDataBytes for Audio {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Format = Hint;
|
type Format = Hint;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue