2020-10-20 18:44:50 +00:00
|
|
|
use crate::{Audio, AudioSource, Decodable};
|
|
|
|
use bevy_asset::{Asset, Assets};
|
|
|
|
use bevy_ecs::{Resources, World};
|
|
|
|
use rodio::{OutputStream, OutputStreamHandle, Sink};
|
|
|
|
use std::marker::PhantomData;
|
2020-07-16 20:46:51 +00:00
|
|
|
|
2020-10-20 18:44:50 +00:00
|
|
|
/// Used internally to play audio on the current "audio device"
|
2020-09-08 20:56:45 +00:00
|
|
|
pub struct AudioOutput<P = AudioSource>
|
|
|
|
where
|
|
|
|
P: Decodable,
|
|
|
|
{
|
2020-10-20 18:44:50 +00:00
|
|
|
_stream: OutputStream,
|
|
|
|
stream_handle: OutputStreamHandle,
|
|
|
|
phantom: PhantomData<P>,
|
2020-10-08 18:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 20:56:45 +00:00
|
|
|
impl<P> Default for AudioOutput<P>
|
|
|
|
where
|
|
|
|
P: Decodable,
|
|
|
|
{
|
2020-07-16 20:46:51 +00:00
|
|
|
fn default() -> Self {
|
2020-10-20 18:44:50 +00:00
|
|
|
let (stream, stream_handle) = OutputStream::try_default().unwrap();
|
|
|
|
|
2020-07-16 20:46:51 +00:00
|
|
|
Self {
|
2020-10-20 18:44:50 +00:00
|
|
|
_stream: stream,
|
|
|
|
stream_handle,
|
|
|
|
phantom: PhantomData,
|
2020-07-16 20:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 20:56:45 +00:00
|
|
|
impl<P> AudioOutput<P>
|
|
|
|
where
|
2020-10-18 20:48:15 +00:00
|
|
|
P: Asset + Decodable,
|
2020-09-08 20:56:45 +00:00
|
|
|
<P as Decodable>::Decoder: rodio::Source + Send + Sync,
|
|
|
|
<<P as Decodable>::Decoder as Iterator>::Item: rodio::Sample + Send + Sync,
|
|
|
|
{
|
2020-10-20 18:44:50 +00:00
|
|
|
fn play_source(&self, audio_source: &P) {
|
|
|
|
let sink = Sink::try_new(&self.stream_handle).unwrap();
|
2020-09-08 20:56:45 +00:00
|
|
|
sink.append(audio_source.decoder());
|
2020-07-16 20:46:51 +00:00
|
|
|
sink.detach();
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:44:50 +00:00
|
|
|
fn try_play_queued(&self, audio_sources: &Assets<P>, audio: &mut Audio<P>) {
|
|
|
|
let mut queue = audio.queue.write();
|
2020-07-16 20:46:51 +00:00
|
|
|
let len = queue.len();
|
|
|
|
let mut i = 0;
|
|
|
|
while i < len {
|
|
|
|
let audio_source_handle = queue.pop_back().unwrap();
|
|
|
|
if let Some(audio_source) = audio_sources.get(&audio_source_handle) {
|
2020-07-16 21:23:57 +00:00
|
|
|
self.play_source(audio_source);
|
2020-07-16 20:46:51 +00:00
|
|
|
} else {
|
|
|
|
// audio source hasn't loaded yet. add it back to the queue
|
|
|
|
queue.push_front(audio_source_handle);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:44:50 +00:00
|
|
|
/// Plays audio currently queued in the [Audio] resource through the [AudioOutput] resource
|
|
|
|
pub fn play_queued_audio_system<P: Asset>(_world: &mut World, resources: &mut Resources)
|
|
|
|
where
|
2020-09-08 20:56:45 +00:00
|
|
|
P: Decodable,
|
|
|
|
<P as Decodable>::Decoder: rodio::Source + Send + Sync,
|
|
|
|
<<P as Decodable>::Decoder as Iterator>::Item: rodio::Sample + Send + Sync,
|
|
|
|
{
|
2020-10-20 18:44:50 +00:00
|
|
|
let audio_output = resources.get_thread_local::<AudioOutput<P>>().unwrap();
|
|
|
|
let mut audio = resources.get_mut::<Audio<P>>().unwrap();
|
|
|
|
|
|
|
|
if let Some(audio_sources) = resources.get::<Assets<P>>() {
|
|
|
|
audio_output.try_play_queued(&*audio_sources, &mut *audio);
|
|
|
|
}
|
2020-07-16 20:46:51 +00:00
|
|
|
}
|