Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Audio control - play, pause, volume, speed, loop #3948

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use a single struct instead of a tuple in the queue
  • Loading branch information
mockersf committed Feb 14, 2022
commit 2e87fa08932069dd9c8f2e466c0da2513667b74c
39 changes: 29 additions & 10 deletions crates/bevy_audio/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where
Source: Asset + Decodable,
{
/// Queue for playing audio from asset handles
pub(crate) queue: RwLock<VecDeque<(AudioConfig, Handle<Source>)>>,
pub(crate) queue: RwLock<VecDeque<AudioToPlay<Source>>>,
}

impl<Source: Asset> fmt::Debug for Audio<Source>
Expand Down Expand Up @@ -77,11 +77,12 @@ where
/// ```
pub fn play(&self, audio_source: Handle<Source>) -> Handle<AudioSink> {
let id = HandleId::random::<AudioSink>();
let config = AudioConfig {
let config = AudioToPlay {
repeat: false,
handle: id,
sink_handle: id,
source_handle: audio_source,
};
self.queue.write().push_back((config, audio_source));
self.queue.write().push_back(config);
Handle::<AudioSink>::weak(id)
}

Expand All @@ -90,17 +91,35 @@ where
/// See [`Self::play`] on how to control playback.
pub fn play_in_loop(&self, audio_source: Handle<Source>) -> Handle<AudioSink> {
let id = HandleId::random::<AudioSink>();
let config = AudioConfig {
let config = AudioToPlay {
repeat: true,
handle: id,
sink_handle: id,
source_handle: audio_source,
};
self.queue.write().push_back((config, audio_source));
self.queue.write().push_back(config);
Handle::<AudioSink>::weak(id)
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AudioConfig {
pub(crate) handle: HandleId,
#[derive(Clone, PartialEq, Eq)]
pub(crate) struct AudioToPlay<Source>
where
Source: Asset + Decodable,
{
pub(crate) sink_handle: HandleId,
pub(crate) source_handle: Handle<Source>,
pub(crate) repeat: bool,
}

impl<Source> fmt::Debug for AudioToPlay<Source>
where
Source: Asset + Decodable,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AudioToPlay")
.field("sink_handle", &self.sink_handle)
.field("source_handle", &self.source_handle)
.field("repeat", &self.repeat)
.finish()
}
}
8 changes: 4 additions & 4 deletions crates/bevy_audio/src/audio_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ where
let len = queue.len();
let mut i = 0;
while i < len {
let (config, audio_source_handle) = queue.pop_front().unwrap();
if let Some(audio_source) = audio_sources.get(&audio_source_handle) {
let config = queue.pop_front().unwrap();
if let Some(audio_source) = audio_sources.get(&config.source_handle) {
if let Some(sink) = self.play_source(audio_source, config.repeat) {
// don't keep the strong handle. there is no way to return it to the user here as it is async
let _ = sinks.set(config.handle, AudioSink { sink: Some(sink) });
let _ = sinks.set(config.sink_handle, AudioSink { sink: Some(sink) });
}
} else {
// audio source hasn't loaded yet. add it back to the queue
queue.push_back((config, audio_source_handle));
queue.push_back(config);
}
i += 1;
}
Expand Down