add open function for audio

This commit is contained in:
Breval Ferrari 2025-03-17 00:49:32 -04:00
parent 4d80833bbd
commit af7b4f77bb
No known key found for this signature in database
GPG key ID: F71E304D6400AB8E
3 changed files with 41 additions and 4 deletions

View file

@ -12,6 +12,8 @@ mod dynamic {
path::Path,
};
use crate::snd::{self, Audio};
use super::{
img::{self, DynamicImage},
Bytes,
@ -26,7 +28,7 @@ mod dynamic {
pub enum DynamicBendable {
Image(DynamicImage),
Binary(Bytes),
Sound,
Sound(Audio),
Text,
}
@ -36,6 +38,8 @@ mod dynamic {
Io(#[from] io::Error),
#[error("{0:?}")]
Image(#[from] img::ImageError),
#[error("{0:?}")]
Audio(#[from] snd::AudioOpenError),
}
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)?;
buf
})),
Audio => todo!(),
Audio => Some(DynamicBendable::Sound(crate::snd::Audio::open(path)?)),
Book => todo!(),
Doc => todo!(),
Font => todo!(),

View file

@ -1,4 +1,5 @@
pub use symphonia::core::sample::Sample;
mod raw;
pub use raw::RawSamples;
pub use raw::*;
mod simphonia;
pub use simphonia::*;

View file

@ -1,4 +1,8 @@
use std::io::{self, Read};
use std::{
fs::File,
io::{self, Read},
path::Path,
};
use derive_new::new;
use symphonia::{
@ -14,6 +18,7 @@ use symphonia::{
},
default,
};
use thiserror::Error;
use crate::{IntoDataBytes, TryFromDataBytes};
@ -28,6 +33,33 @@ pub struct Audio {
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 {
type Error = Error;
type Format = Hint;