2020-10-20 18:44:50 +00:00
|
|
|
use crate::{AudioSource, Decodable};
|
2020-12-03 21:57:36 +00:00
|
|
|
use bevy_asset::{Asset, Handle};
|
2020-10-20 18:44:50 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use std::{collections::VecDeque, fmt};
|
|
|
|
|
|
|
|
/// The external struct used to play audio
|
|
|
|
pub struct Audio<P = AudioSource>
|
|
|
|
where
|
2020-12-03 21:57:36 +00:00
|
|
|
P: Asset + Decodable,
|
2020-10-20 18:44:50 +00:00
|
|
|
{
|
|
|
|
pub queue: RwLock<VecDeque<Handle<P>>>,
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:57:36 +00:00
|
|
|
impl<P: Asset> fmt::Debug for Audio<P>
|
2020-10-20 18:44:50 +00:00
|
|
|
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
|
2020-12-03 21:57:36 +00:00
|
|
|
P: Asset + Decodable,
|
2020-10-20 18:44:50 +00:00
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
queue: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> Audio<P>
|
|
|
|
where
|
2020-12-03 21:57:36 +00:00
|
|
|
P: Asset + Decodable,
|
2020-10-20 18:44:50 +00:00
|
|
|
<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);
|
|
|
|
}
|
|
|
|
}
|