-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
audio: initial (very minimal) audio plugin
- Loading branch information
Showing
10 changed files
with
139 additions
and
0 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
Binary file not shown.
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,14 @@ | ||
[package] | ||
authors = ["Carter Anderson <mcanders1@gmail.com>"] | ||
edition = "2018" | ||
name = "bevy_audio" | ||
version = "0.1.0" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
bevy_app = {path = "../bevy_app"} | ||
bevy_asset = {path = "../bevy_asset"} | ||
bevy_ecs = {path = "../bevy_ecs"} | ||
anyhow = "1.0" | ||
rodio = {version = "0.11", default-features = false, features = ["mp3"]} |
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,54 @@ | ||
use crate::AudioSource; | ||
use bevy_asset::{Assets, Handle}; | ||
use bevy_ecs::Res; | ||
use rodio::{Decoder, Device, Sink}; | ||
use std::{collections::VecDeque, io::Cursor, sync::RwLock}; | ||
|
||
pub struct AudioOutput { | ||
device: Device, | ||
queue: RwLock<VecDeque<Handle<AudioSource>>>, | ||
} | ||
|
||
impl Default for AudioOutput { | ||
fn default() -> Self { | ||
Self { | ||
device: rodio::default_output_device().unwrap(), | ||
queue: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl AudioOutput { | ||
pub fn play(&self, audio_source: &AudioSource) { | ||
let sink = Sink::new(&self.device); | ||
sink.append(Decoder::new(Cursor::new(audio_source.clone())).unwrap()); | ||
sink.detach(); | ||
} | ||
|
||
pub fn queue_play(&self, audio_source: Handle<AudioSource>) { | ||
self.queue.write().unwrap().push_front(audio_source); | ||
} | ||
|
||
pub fn try_play_queued(&self, audio_sources: &Assets<AudioSource>) { | ||
let mut queue = self.queue.write().unwrap(); | ||
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) { | ||
self.play(audio_source); | ||
} else { | ||
// audio source hasn't loaded yet. add it back to the queue | ||
queue.push_front(audio_source_handle); | ||
} | ||
i += 1; | ||
} | ||
} | ||
} | ||
|
||
pub fn play_queued_audio_system( | ||
audio_sources: Res<Assets<AudioSource>>, | ||
audio_output: Res<AudioOutput>, | ||
) { | ||
audio_output.try_play_queued(&audio_sources); | ||
} |
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,27 @@ | ||
use anyhow::Result; | ||
use bevy_asset::AssetLoader; | ||
use std::{sync::Arc, path::Path}; | ||
|
||
#[derive(Clone)] | ||
pub struct AudioSource { | ||
pub bytes: Arc<Vec<u8>>, | ||
} | ||
|
||
impl AsRef<[u8]> for AudioSource { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.bytes | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct Mp3Loader; | ||
|
||
impl AssetLoader<AudioSource> for Mp3Loader { | ||
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<AudioSource> { | ||
Ok(AudioSource { bytes: Arc::new(bytes) }) | ||
} | ||
fn extensions(&self) -> &[&str] { | ||
static EXTENSIONS: &[&str] = &["mp3"]; | ||
EXTENSIONS | ||
} | ||
} |
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,21 @@ | ||
mod audio_output; | ||
mod audio_source; | ||
|
||
pub use audio_output::*; | ||
pub use audio_source::*; | ||
|
||
use bevy_app::{stage, AppBuilder, AppPlugin}; | ||
use bevy_asset::AddAsset; | ||
use bevy_ecs::IntoQuerySystem; | ||
|
||
#[derive(Default)] | ||
pub struct AudioPlugin; | ||
|
||
impl AppPlugin for AudioPlugin { | ||
fn build(&self, app: &mut AppBuilder) { | ||
app.init_resource::<AudioOutput>() | ||
.add_asset::<AudioSource>() | ||
.add_asset_loader::<AudioSource, Mp3Loader>() | ||
.add_system_to_stage(stage::POST_UPDATE, play_queued_audio_system.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::build() | ||
.add_default_plugins() | ||
.add_startup_system(setup.system()) | ||
.run(); | ||
} | ||
|
||
fn setup(asset_server: Res<AssetServer>, audio_output: Res<AudioOutput>) { | ||
let music = asset_server | ||
.load("assets/sounds/Windless Slopes.mp3") | ||
.unwrap(); | ||
audio_output.queue_play(music); | ||
} |
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