Skip to content

Commit d388480

Browse files
committed
add from_media_source methods for static and streaming sound data
1 parent ab76b9f commit d388480

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

crates/kira/src/sound/static_sound/data/from_file.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,36 @@ use crate::sound::{
99
use super::StaticSoundData;
1010

1111
impl StaticSoundData {
12-
fn from_media_source(
12+
/// Loads an audio file into a [`StaticSoundData`].
13+
#[cfg(not(target_arch = "wasm32"))]
14+
#[cfg_attr(docsrs, doc(cfg(all(feature = "symphonia", not(wasm32)))))]
15+
pub fn from_file(
16+
path: impl AsRef<std::path::Path>,
17+
settings: StaticSoundSettings,
18+
) -> Result<Self, FromFileError> {
19+
Self::from_media_source(std::fs::File::open(path)?, settings)
20+
}
21+
22+
/// Loads a cursor wrapping audio file data into a [`StaticSoundData`].
23+
#[cfg_attr(docsrs, doc(cfg(feature = "symphonia")))]
24+
pub fn from_cursor<T: AsRef<[u8]> + Send + Sync + 'static>(
25+
cursor: Cursor<T>,
26+
settings: StaticSoundSettings,
27+
) -> Result<StaticSoundData, FromFileError> {
28+
Self::from_media_source(cursor, settings)
29+
}
30+
31+
/// Loads an audio file from a type that implements Symphonia's [`MediaSource`]
32+
/// trait.
33+
#[cfg_attr(docsrs, doc(cfg(feature = "symphonia")))]
34+
pub fn from_media_source(
35+
media_source: impl MediaSource + 'static,
36+
settings: StaticSoundSettings,
37+
) -> Result<Self, FromFileError> {
38+
Self::from_boxed_media_source(Box::new(media_source), settings)
39+
}
40+
41+
fn from_boxed_media_source(
1342
media_source: Box<dyn MediaSource>,
1443
settings: StaticSoundSettings,
1544
) -> Result<Self, FromFileError> {
@@ -56,23 +85,4 @@ impl StaticSoundData {
5685
settings,
5786
})
5887
}
59-
60-
/// Loads an audio file into a [`StaticSoundData`].
61-
#[cfg(not(target_arch = "wasm32"))]
62-
#[cfg_attr(docsrs, doc(cfg(all(feature = "symphonia", not(wasm32)))))]
63-
pub fn from_file(
64-
path: impl AsRef<std::path::Path>,
65-
settings: StaticSoundSettings,
66-
) -> Result<Self, FromFileError> {
67-
Self::from_media_source(Box::new(std::fs::File::open(path)?), settings)
68-
}
69-
70-
/// Loads a cursor wrapping audio file data into a [`StaticSoundData`].
71-
#[cfg_attr(docsrs, doc(cfg(feature = "symphonia")))]
72-
pub fn from_cursor<T: AsRef<[u8]> + Send + Sync + 'static>(
73-
cursor: Cursor<T>,
74-
settings: StaticSoundSettings,
75-
) -> Result<StaticSoundData, FromFileError> {
76-
Self::from_media_source(Box::new(cursor), settings)
77-
}
7888
}

crates/kira/src/sound/streaming/data.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ impl StreamingSoundData<crate::sound::FromFileError> {
5151
))
5252
}
5353

54-
#[cfg(feature = "symphonia")]
5554
/// Creates a [`StreamingSoundData`] for a cursor wrapping audio file data.
5655
pub fn from_cursor<T: AsRef<[u8]> + Send + Sync + 'static>(
5756
cursor: std::io::Cursor<T>,
@@ -64,6 +63,20 @@ impl StreamingSoundData<crate::sound::FromFileError> {
6463
settings,
6564
))
6665
}
66+
67+
/// Creates a [`StreamingSoundData`] for a type that implements Symphonia's
68+
/// [`MediaSource`](symphonia::core::io::MediaSource) trait.
69+
pub fn from_media_source(
70+
media_source: impl symphonia::core::io::MediaSource + 'static,
71+
settings: StreamingSoundSettings,
72+
) -> Result<StreamingSoundData<crate::sound::FromFileError>, crate::sound::FromFileError> {
73+
use super::symphonia::SymphoniaDecoder;
74+
75+
Ok(Self::from_decoder(
76+
SymphoniaDecoder::new(Box::new(media_source))?,
77+
settings,
78+
))
79+
}
6780
}
6881

6982
impl<Error: Send + 'static> StreamingSoundData<Error> {

0 commit comments

Comments
 (0)