-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to rodio 0.12 using thread local resources (#692)
Migrate to rodio 0.12 using thread local resources
- Loading branch information
Showing
6 changed files
with
83 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::{AudioSource, Decodable}; | ||
use bevy_asset::Handle; | ||
use parking_lot::RwLock; | ||
use std::{collections::VecDeque, fmt}; | ||
|
||
/// The external struct used to play audio | ||
pub struct Audio<P = AudioSource> | ||
where | ||
P: Decodable, | ||
{ | ||
pub queue: RwLock<VecDeque<Handle<P>>>, | ||
} | ||
|
||
impl<P> fmt::Debug for Audio<P> | ||
where | ||
P: Decodable, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.debug_struct("Audio").field("queue", &self.queue).finish() | ||
} | ||
} | ||
|
||
impl<P> Default for Audio<P> | ||
where | ||
P: Decodable, | ||
{ | ||
fn default() -> Self { | ||
Self { | ||
queue: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<P> Audio<P> | ||
where | ||
P: Decodable, | ||
<P as Decodable>::Decoder: rodio::Source + Send + Sync, | ||
<<P as Decodable>::Decoder as Iterator>::Item: rodio::Sample + Send + Sync, | ||
{ | ||
pub fn play(&self, audio_source: Handle<P>) { | ||
self.queue.write().push_front(audio_source); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
mod audio; | ||
mod audio_output; | ||
mod audio_source; | ||
|
||
pub use audio::*; | ||
pub use audio_output::*; | ||
pub use audio_source::*; | ||
|
||
pub mod prelude { | ||
pub use crate::{AudioOutput, AudioSource, Decodable}; | ||
pub use crate::{Audio, AudioOutput, AudioSource, Decodable}; | ||
} | ||
|
||
use bevy_app::prelude::*; | ||
use bevy_asset::AddAsset; | ||
use bevy_ecs::IntoQuerySystem; | ||
use bevy_ecs::IntoThreadLocalSystem; | ||
|
||
/// Adds support for audio playback to an App | ||
#[derive(Default)] | ||
pub struct AudioPlugin; | ||
|
||
impl Plugin for AudioPlugin { | ||
fn build(&self, app: &mut AppBuilder) { | ||
app.init_resource::<AudioOutput<AudioSource>>() | ||
app.init_thread_local_resource::<AudioOutput<AudioSource>>() | ||
.add_asset::<AudioSource>() | ||
.init_asset_loader::<Mp3Loader>() | ||
.init_resource::<Audio<AudioSource>>() | ||
.add_system_to_stage( | ||
stage::POST_UPDATE, | ||
play_queued_audio_system::<AudioSource>.system(), | ||
play_queued_audio_system::<AudioSource>.thread_local_system(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters