Skip to content

Commit

Permalink
Merge pull request #111 from NiklasEi/bevy_main
Browse files Browse the repository at this point in the history
Update to Bevy 0.12
  • Loading branch information
NiklasEi authored Nov 4, 2023
2 parents 5677ca0 + 06e1020 commit fb6bd06
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 123 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v0.18.0 - 04.11.2023
- Update to Bevy `0.12`

## v0.17.0
- Multiply instance volume with channel volume ([#103](https://github.com/NiklasEi/bevy_kira_audio/issues/103))
- Allow playing a paused sound using `.play(...).paused()` ([#105](https://github.com/NiklasEi/bevy_kira_audio/issues/105))
Expand Down
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_kira_audio"
version = "0.17.0"
version = "0.18.0"
authors = ["Niklas Eicker <hello@nikl.me>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -22,16 +22,17 @@ wav = ["kira/wav"]
settings_loader = ["dep:ron", "dep:serde", "kira/serde"]

[dependencies]
bevy = { version = "0.11", default-features = false, features = ["bevy_asset"] }
bevy = { version = "0.12", default-features = false, features = ["bevy_asset"] }
anyhow = "1.0"
uuid = { version = "1", features = ["fast-rng"] }
kira = { version = "0.8", default-features = false, features = ["cpal"] }
ron = { version = "0.8", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
parking_lot = "0.12"
thiserror = "1.0"

[dev-dependencies.bevy]
version = "0.11"
version = "0.12"
default-features = false
features = [
"bevy_asset",
Expand All @@ -44,7 +45,11 @@ features = [
"bevy_sprite",
"bevy_gltf",
"bevy_scene",
"bevy_pbr","tonemapping_luts", "ktx2" , "zstd"
"bevy_pbr",
"tonemapping_luts",
"ktx2",
"zstd",
"multi-threaded"
]

[[example]]
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Sound can be played in channels. Each channel has controls to pause or stop play

## Usage

*Note: the Bevy feature `bevy_audio` is enabled by default and not compatible with this plugin. Make sure to not have the `bevy_audio` feature enabled if you want to use `bevy_kira_audio`. The same goes for Bevy's `vorbis` feature. See [Bevys' Cargo file](https://github.com/bevyengine/bevy/blob/v0.9.0/Cargo.toml#L27-L40) for a list of all default features of version `0.9` and list them manually in your Cargo file excluding the ones you do not want. Make sure to set `default-features` to `false` for the Bevy dependency. You can take a look at [bevy_game_template's cargo file as an example](https://github.com/NiklasEi/bevy_game_template/blob/main/Cargo.toml).*
*Note: the Bevy feature `bevy_audio` is enabled by default and not compatible with this plugin. Make sure to not have the `bevy_audio` feature enabled if you want to use `bevy_kira_audio`. The same goes for Bevy's `vorbis` feature. See [Bevys' Cargo file](https://github.com/bevyengine/bevy/blob/v0.12.0/Cargo.toml#L33-L57) for a list of all default features of version `0.12` and list them manually in your Cargo file excluding the ones you do not want. Make sure to set `default-features` to `false` for the Bevy dependency. You can take a look at [bevy_game_template's cargo file as an example](https://github.com/NiklasEi/bevy_game_template/blob/main/Cargo.toml).*


To play audio, you usually want to load audio files as assets. This requires `AssetLoaders`. `bevy_kira_audio` comes with loaders for most common audio formats. You can enable them with the features `ogg` (enabled by default), `mp3`, `wav`, or `flac`. The following example assumes that the feature `ogg` is enabled.
Expand Down Expand Up @@ -90,6 +90,7 @@ The main branch is compatible with the latest Bevy release.
Compatibility of `bevy_kira_audio` versions:
| `bevy_kira_audio` | `bevy` |
| :-- | :-- |
| `0.18` | `0.12` |
| `0.16` - `0.17` | `0.11` |
| `0.15` | `0.10` |
| `0.13` - `0.14` | `0.9` |
Expand All @@ -98,7 +99,7 @@ Compatibility of `bevy_kira_audio` versions:
| `0.8` | `0.6` |
| `0.4` - `0.7` | `0.5` |
| `0.3` | `0.4` |
| `main` | `0.10` |
| `main` | `0.12` |
| `bevy_main` | `main` |

## License
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple_channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ fn build_button_row<T: Component + Default + Clone>(
value: format!("Channel {}", 4 - channel_index),
style: TextStyle {
font_size: 20.0,
color: Color::rgb(0.2, 0.2, 0.2),
color: Color::rgb(0.9, 0.9, 0.9),
font: font.clone(),
},
}],
Expand Down
2 changes: 1 addition & 1 deletion examples/spacial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn player_look(
if let Ok(window) = primary_window.get_single() {
let delta_state = state.as_mut();
for mut transform in query.iter_mut() {
for ev in delta_state.reader_motion.iter(&motion) {
for ev in delta_state.reader_motion.read(&motion) {
match window.cursor.grab_mode {
CursorGrabMode::None => (),
_ => {
Expand Down
7 changes: 4 additions & 3 deletions examples/stress_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn check(
mut commands: Commands,
) {
if let Some(handle) = handle {
if asset_server.get_load_state(handle.0.id()) == LoadState::Loaded {
if asset_server.get_load_state(handle.0.id()) == Some(LoadState::Loaded) {
commands.insert_resource(AudioHandle(handle.0.clone()));
commands.remove_resource::<LoadingAudioHandle>();
}
Expand All @@ -52,8 +52,9 @@ fn check(

fn play(handle: Option<Res<AudioHandle>>, audio: Res<Audio>) {
if let Some(handle) = handle {
// the max number here depends on your hardware.
// If you get warnings and/or stuttered sounds try reducing the amount.
// The max number here depends on your hardware.
// If you get warnings and/or stuttered sounds try reducing the amount and/or changing the
// capacities of the `AudioSettings` in the `main` method.
for _ in 0..75 {
audio.play(handle.0.clone());
}
Expand Down
9 changes: 6 additions & 3 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crate::instance::AudioInstance;
use crate::source::AudioSource;
use crate::AudioSystemSet;
use bevy::app::{App, PreUpdate};
use bevy::asset::{Handle, HandleId};
use bevy::asset::{AssetId, Handle};
use bevy::ecs::system::Resource;
use bevy::prelude::{default, IntoSystemConfigs, PostUpdate};
use bevy::utils::Uuid;
use kira::sound::static_sound::{StaticSoundData, StaticSoundHandle};
use kira::sound::EndPosition;
use kira::tween::Value;
Expand Down Expand Up @@ -185,9 +186,11 @@ impl<'a> Drop for PlayAudioCommand<'a> {

impl<'a> PlayAudioCommand<'a> {
pub(crate) fn new(source: Handle<AudioSource>, que: &'a dyn AudioCommandQue) -> Self {
let handle_id = HandleId::random::<AudioInstance>();
let asset_id = AssetId::Uuid {
uuid: Uuid::new_v4(),
};
Self {
instance_handle: Handle::<AudioInstance>::weak(handle_id),
instance_handle: Handle::<AudioInstance>::Weak(asset_id),
source,
settings: PartialSoundSettings::default(),
que,
Expand Down
39 changes: 22 additions & 17 deletions src/audio_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<B: Backend> AudioOutput<B> {
if let Some(instances) = self.instances.get_mut(channel) {
let tween = map_tween(tween);
for instance in instances {
if let Some(instance) = audio_instances.get_mut(instance) {
if let Some(instance) = audio_instances.get_mut(instance.id()) {
match instance.handle.stop(tween) {
Err(CommandError::CommandQueueFull) => {
return AudioCommandResult::Retry;
Expand All @@ -82,7 +82,7 @@ impl<B: Backend> AudioOutput<B> {
if let Some(instance_handles) = self.instances.get_mut(channel) {
let tween = map_tween(tween);
for instance in instance_handles.iter_mut() {
if let Some(instance) = audio_instances.get_mut(instance) {
if let Some(instance) = audio_instances.get_mut(instance.id()) {
if kira::sound::PlaybackState::Playing == instance.handle.state() {
if let Err(error) = instance.handle.pause(tween) {
error!("Failed to pause instance: {:?}", error);
Expand Down Expand Up @@ -111,7 +111,7 @@ impl<B: Backend> AudioOutput<B> {
if let Some(instances) = self.instances.get_mut(channel) {
let tween = map_tween(tween);
for instance in instances.iter_mut() {
if let Some(instance) = audio_instances.get_mut(instance) {
if let Some(instance) = audio_instances.get_mut(instance.id()) {
if instance.handle.state() == kira::sound::PlaybackState::Paused
|| instance.handle.state() == kira::sound::PlaybackState::Pausing
|| instance.handle.state() == kira::sound::PlaybackState::Stopping
Expand Down Expand Up @@ -141,7 +141,7 @@ impl<B: Backend> AudioOutput<B> {
if let Some(instances) = self.instances.get_mut(channel) {
let tween = map_tween(tween);
for instance in instances.iter_mut() {
if let Some(instance) = audio_instances.get_mut(instance) {
if let Some(instance) = audio_instances.get_mut(instance.id()) {
if let Err(error) = instance.handle.set_volume(volume, tween) {
error!("Failed to set volume for instance: {:?}", error);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ impl<B: Backend> AudioOutput<B> {
if let Some(instances) = self.instances.get_mut(channel) {
let tween = map_tween(tween);
for instance in instances.iter_mut() {
if let Some(instance) = audio_instances.get_mut(instance) {
if let Some(instance) = audio_instances.get_mut(instance.id()) {
if let Err(error) = instance.handle.set_panning(panning, tween) {
error!("Failed to set panning for instance: {:?}", error);
}
Expand Down Expand Up @@ -197,7 +197,7 @@ impl<B: Backend> AudioOutput<B> {
if let Some(instances) = self.instances.get_mut(channel) {
let tween = map_tween(tween);
for instance in instances.iter_mut() {
if let Some(instance) = audio_instances.get_mut(instance) {
if let Some(instance) = audio_instances.get_mut(instance.id()) {
if let Err(error) = instance.handle.set_playback_rate(playback_rate, tween) {
error!("Failed to set playback rate for instance: {:?}", error);
}
Expand Down Expand Up @@ -271,8 +271,8 @@ impl<B: Backend> AudioOutput<B> {
error!("Failed to set playback rate for instance: {:?}", error);
}
}
let instance_handle = audio_instances.set(
instance_handle,
audio_instances.insert(
&instance_handle,
AudioInstance {
handle: sound_handle,
},
Expand Down Expand Up @@ -462,8 +462,9 @@ mod test {
use super::*;
use crate::channel::AudioControl;
use crate::{Audio, AudioPlugin};
use bevy::asset::{AssetPlugin, HandleId};
use bevy::asset::{AssetId, AssetPlugin};
use bevy::prelude::*;
use bevy::utils::Uuid;
use kira::manager::backend::mock::MockBackend;
use kira::manager::AudioManagerSettings;

Expand All @@ -483,10 +484,12 @@ mod test {
instances: HashMap::default(),
channels: HashMap::default(),
};
let audio_handle_one: Handle<AudioSource> =
Handle::<AudioSource>::weak(HandleId::random::<AudioSource>());
let audio_handle_two: Handle<AudioSource> =
Handle::<AudioSource>::weak(HandleId::random::<AudioSource>());
let audio_handle_one: Handle<AudioSource> = Handle::<AudioSource>::Weak(AssetId::Uuid {
uuid: Uuid::from_u128(1758302748397294),
});
let audio_handle_two: Handle<AudioSource> = Handle::<AudioSource>::Weak(AssetId::Uuid {
uuid: Uuid::from_u128(2537024739048739),
});

let channel = AudioChannel::<Audio>::default();
channel.play(audio_handle_one.clone());
Expand Down Expand Up @@ -526,10 +529,12 @@ mod test {
instances: HashMap::default(),
channels: HashMap::default(),
};
let audio_handle_one: Handle<AudioSource> =
Handle::<AudioSource>::weak(HandleId::random::<AudioSource>());
let audio_handle_two: Handle<AudioSource> =
Handle::<AudioSource>::weak(HandleId::random::<AudioSource>());
let audio_handle_one: Handle<AudioSource> = Handle::<AudioSource>::Weak(AssetId::Uuid {
uuid: Uuid::from_u128(13290473942075938),
});
let audio_handle_two: Handle<AudioSource> = Handle::<AudioSource>::Weak(AssetId::Uuid {
uuid: Uuid::from_u128(243290473942075938),
});

let channel = AudioChannel::<Audio>::default();
channel.play(audio_handle_one);
Expand Down
52 changes: 31 additions & 21 deletions src/channel/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::audio::{
use crate::channel::AudioCommandQue;
use crate::instance::AudioInstance;
use crate::{AudioControl, AudioSource, PlaybackState};
use bevy::asset::{Handle, HandleId};
use bevy::asset::{AssetId, Handle};
use bevy::ecs::system::Resource;
use bevy::utils::hashbrown::hash_map::Iter;
use bevy::utils::HashMap;
Expand All @@ -17,7 +17,7 @@ use std::collections::VecDeque;
#[derive(Default)]
pub struct DynamicAudioChannel {
pub(crate) commands: RwLock<VecDeque<AudioCommand>>,
pub(crate) states: HashMap<HandleId, PlaybackState>,
pub(crate) states: HashMap<AssetId<AudioInstance>, PlaybackState>,
}

impl AudioCommandQue for DynamicAudioChannel {
Expand Down Expand Up @@ -235,13 +235,15 @@ impl DynamicAudioChannels {
mod tests {
use crate::channel::dynamic::DynamicAudioChannels;
use crate::channel::*;
use bevy::asset::HandleId;
use bevy::asset::AssetId;
use bevy::utils::Uuid;

#[test]
fn state_is_queued_if_command_is_queued() {
let mut audio = DynamicAudioChannels::default();
let audio_handle: Handle<AudioSource> =
Handle::<AudioSource>::weak(HandleId::default::<AudioSource>());
let audio_handle: Handle<AudioSource> = Handle::<AudioSource>::Weak(AssetId::Uuid {
uuid: Uuid::from_u128(43290473942075938),
});
let instance_handle = audio.create_channel("test").play(audio_handle).handle();

assert_eq!(
Expand All @@ -253,7 +255,9 @@ mod tests {
#[test]
fn state_is_stopped_if_command_is_not_queued_and_id_not_in_state_map() {
let mut audio = DynamicAudioChannels::default();
let instance_handle = Handle::<AudioInstance>::weak(HandleId::default::<AudioInstance>());
let instance_handle = Handle::<AudioInstance>::Weak(AssetId::Uuid {
uuid: Uuid::from_u128(43290473942075938),
});

assert_eq!(
audio.create_channel("test").state(&instance_handle),
Expand All @@ -264,7 +268,9 @@ mod tests {
#[test]
fn state_is_fetched_from_state_map() {
let mut audio = DynamicAudioChannels::default();
let instance_handle = Handle::<AudioInstance>::weak(HandleId::default::<AudioInstance>());
let instance_handle = Handle::<AudioInstance>::Weak(AssetId::Uuid {
uuid: Uuid::from_u128(43290473942075938),
});
audio.create_channel("test");
audio.channels.get_mut("test").unwrap().states.insert(
instance_handle.id(),
Expand All @@ -281,26 +287,30 @@ mod tests {
fn finds_playing_sound() {
let mut audio = DynamicAudioChannels::default();
audio.create_channel("test");
audio
.channels
.get_mut("test")
.unwrap()
.states
.insert(HandleId::default::<AudioInstance>(), PlaybackState::Queued);
audio.channels.get_mut("test").unwrap().states.insert(
HandleId::default::<AudioInstance>(),
AssetId::Uuid {
uuid: Uuid::from_u128(143290473942075938),
},
PlaybackState::Queued,
);
audio.channels.get_mut("test").unwrap().states.insert(
AssetId::Uuid {
uuid: Uuid::from_u128(243290473942075938),
},
PlaybackState::Paused { position: 42. },
);
audio
.channels
.get_mut("test")
.unwrap()
.states
.insert(HandleId::default::<AudioInstance>(), PlaybackState::Stopped);
audio.channels.get_mut("test").unwrap().states.insert(
AssetId::Uuid {
uuid: Uuid::from_u128(343290473942075938),
},
PlaybackState::Stopped,
);
assert!(!audio.channel("test").is_playing_sound());

audio.channels.get_mut("test").unwrap().states.insert(
HandleId::default::<AudioInstance>(),
AssetId::Uuid {
uuid: Uuid::from_u128(43290473942075938),
},
PlaybackState::Playing { position: 42. },
);
assert!(audio.channel("test").is_playing_sound());
Expand Down
Loading

0 comments on commit fb6bd06

Please sign in to comment.