diff --git a/.gitignore b/.gitignore index b3c22eedfbd838..840c8c0bb1390b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ **/*.rs.bk Cargo.lock .cargo/config -/.idea \ No newline at end of file +/.idea +/.vscode diff --git a/crates/bevy_audio/src/audio_output.rs b/crates/bevy_audio/src/audio_output.rs index 5b578fbfebde9a..3d61bb2366fe57 100644 --- a/crates/bevy_audio/src/audio_output.rs +++ b/crates/bevy_audio/src/audio_output.rs @@ -1,16 +1,22 @@ -use crate::AudioSource; +use crate::Decodable; use bevy_asset::{Assets, Handle}; use bevy_ecs::Res; -use rodio::{Decoder, Device, Sink}; -use std::{collections::VecDeque, io::Cursor, sync::RwLock}; +use rodio::{Device, Sink}; +use std::{collections::VecDeque, sync::RwLock}; /// Used to play audio on the current "audio device" -pub struct AudioOutput { +pub struct AudioOutput
+where
+ P: Decodable,
+{
device: Device,
- queue: RwLock Default for AudioOutput
+where
+ P: Decodable,
+{
fn default() -> Self {
Self {
device: rodio::default_output_device().unwrap(),
@@ -19,18 +25,23 @@ impl Default for AudioOutput {
}
}
-impl AudioOutput {
- pub fn play_source(&self, audio_source: &AudioSource) {
+impl AudioOutput
+where
+ P: Decodable,
+ ::Decoder: rodio::Source + Send + Sync,
+ < ::Decoder as Iterator>::Item: rodio::Sample + Send + Sync,
+{
+ pub fn play_source(&self, audio_source: &P) {
let sink = Sink::new(&self.device);
- sink.append(Decoder::new(Cursor::new(audio_source.clone())).unwrap());
+ sink.append(audio_source.decoder());
sink.detach();
}
- pub fn play(&self, audio_source: Handle ) {
self.queue.write().unwrap().push_front(audio_source);
}
- pub fn try_play_queued(&self, audio_sources: &Assets ) {
let mut queue = self.queue.write().unwrap();
let len = queue.len();
let mut i = 0;
@@ -48,9 +59,11 @@ impl AudioOutput {
}
/// Plays audio currently queued in the [AudioOutput] resource
-pub(crate) fn play_queued_audio_system(
- audio_sources: Res (audio_sources: Res ::Decoder: rodio::Source + Send + Sync,
+ < ::Decoder as Iterator>::Item: rodio::Sample + Send + Sync,
+{
audio_output.try_play_queued(&audio_sources);
}
diff --git a/crates/bevy_audio/src/audio_source.rs b/crates/bevy_audio/src/audio_source.rs
index 16a032e3ae31ea..3635d802dcc59f 100644
--- a/crates/bevy_audio/src/audio_source.rs
+++ b/crates/bevy_audio/src/audio_source.rs
@@ -1,6 +1,6 @@
use anyhow::Result;
use bevy_asset::AssetLoader;
-use std::{path::Path, sync::Arc};
+use std::{io::Cursor, path::Path, sync::Arc};
/// A source of audio data
#[derive(Clone)]
@@ -30,3 +30,17 @@ impl AssetLoader