Compare commits
3 Commits
4c1471c2ab
...
d34c3d6e96
Author | SHA1 | Date | |
---|---|---|---|
d34c3d6e96 | |||
d7e9b1b70c | |||
2080019421 |
16
CHANGELOG.md
16
CHANGELOG.md
|
@ -2,6 +2,22 @@
|
|||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## Version 0.5.0 - 2024-02-09
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- Clean up `LastAudio` when `Sound` is removed, not `Source`.
|
||||
|
||||
### Features
|
||||
|
||||
- [**breaking**] Renamed `SynthizerSets::First` to `SynthizerSets::PreUpdate` and moved remaining systems into `PostUpdate`.
|
||||
- [**breaking**] Removed `Sound.restart`. Clear `Sound.generator` for equivalent functionality.
|
||||
|
||||
### Miscellaneous Tasks
|
||||
|
||||
- Clean up code.
|
||||
- Bump dependencies.
|
||||
|
||||
## Version 0.4.0 - 2023-07-16
|
||||
|
||||
### Miscellaneous Tasks
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "bevy_synthizer"
|
||||
version = "0.4.0"
|
||||
version = "0.5.0"
|
||||
authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
|
||||
description = "A Bevy plugin for Synthizer, a library for 3D audio and synthesis with a focus on games and VR applications"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
@ -10,12 +10,11 @@ repository = "https://labs.lightsout.games/projects/bevy_synthizer"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
bevy = { version = "0.11", default-features = false, features = ["bevy_asset"] }
|
||||
bevy = { version = "0.12", default-features = false, features = ["bevy_asset"] }
|
||||
synthizer = "0.5.6"
|
||||
|
||||
[dev-dependencies]
|
||||
bevy = { version = "0.11", default-features = true }
|
||||
bevy = { version = "0.12", default-features = true }
|
||||
|
||||
[package.metadata.release]
|
||||
publish = false
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::f32;
|
||||
|
||||
use bevy::{asset::LoadState, prelude::*};
|
||||
use bevy::{asset::LoadedFolder, prelude::*};
|
||||
use bevy_synthizer::*;
|
||||
|
||||
#[derive(Component, Deref, DerefMut)]
|
||||
|
@ -12,45 +12,40 @@ impl Default for RotationTimer {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
struct AssetHandles {
|
||||
sounds: Vec<HandleUntyped>,
|
||||
loaded: bool,
|
||||
}
|
||||
#[derive(Resource, Default, Deref, DerefMut)]
|
||||
struct AssetHandles(Handle<LoadedFolder>);
|
||||
|
||||
fn setup(asset_server: Res<AssetServer>, mut handles: ResMut<AssetHandles>) {
|
||||
handles.sounds = asset_server.load_folder(".").expect("Failed to load sfx");
|
||||
**handles = asset_server.load_folder(".");
|
||||
}
|
||||
|
||||
fn load_and_create(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut handles: ResMut<AssetHandles>,
|
||||
handles: Res<AssetHandles>,
|
||||
listeners: Query<&Listener>,
|
||||
) {
|
||||
if handles.loaded {
|
||||
if !asset_server.is_loaded_with_dependencies(&**handles) {
|
||||
return;
|
||||
}
|
||||
handles.loaded = asset_server
|
||||
.get_group_load_state(handles.sounds.iter().map(|handle| handle.id()))
|
||||
== LoadState::Loaded;
|
||||
if handles.loaded {
|
||||
commands.spawn((
|
||||
TransformBundle::default(),
|
||||
Listener,
|
||||
RotationTimer::default(),
|
||||
));
|
||||
let handle = handles.sounds[0].clone();
|
||||
let buffer = asset_server.get_handle(handle);
|
||||
commands.spawn((
|
||||
TransformBundle::from(Transform::from_translation(Vec3::new(10., 0., 0.))),
|
||||
Source::default(),
|
||||
Sound {
|
||||
audio: buffer.into(),
|
||||
looping: true,
|
||||
..default()
|
||||
},
|
||||
));
|
||||
if !listeners.is_empty() {
|
||||
return;
|
||||
}
|
||||
commands.spawn((
|
||||
TransformBundle::default(),
|
||||
Listener,
|
||||
RotationTimer::default(),
|
||||
));
|
||||
let handle = asset_server.load("footstep.wav");
|
||||
commands.spawn((
|
||||
TransformBundle::from(Transform::from_translation(Vec3::new(10., 0., 0.))),
|
||||
Source::default(),
|
||||
Sound {
|
||||
audio: handle.into(),
|
||||
looping: true,
|
||||
..default()
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn rotate_listener(time: Res<Time>, mut query: Query<(&mut RotationTimer, &mut Transform)>) {
|
||||
|
|
62
src/lib.rs
62
src/lib.rs
|
@ -2,39 +2,48 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use bevy::{
|
||||
asset::{AssetLoader, LoadContext, LoadedAsset},
|
||||
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext},
|
||||
prelude::*,
|
||||
reflect::{TypePath, TypeUuid},
|
||||
reflect::TypePath,
|
||||
transform::TransformSystem,
|
||||
utils::BoxedFuture,
|
||||
utils::{
|
||||
thiserror::{self, Error},
|
||||
BoxedFuture,
|
||||
},
|
||||
};
|
||||
pub use synthizer as syz;
|
||||
|
||||
#[derive(Clone, Debug, Deref, DerefMut, PartialEq, Eq, TypePath, TypeUuid)]
|
||||
#[uuid = "6b6b533a-bb1f-11ec-bda2-00155d8fdde9"]
|
||||
#[derive(Asset, Clone, Debug, Deref, DerefMut, PartialEq, Eq, TypePath)]
|
||||
pub struct Buffer(syz::Buffer);
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
struct BufferAssetLoader;
|
||||
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Error)]
|
||||
pub enum BufferAssetLoaderError {
|
||||
#[error("Could not load asset: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Synthizer error: {0}")]
|
||||
SynthizerError(#[from] synthizer::Error),
|
||||
}
|
||||
|
||||
impl AssetLoader for BufferAssetLoader {
|
||||
type Asset = Buffer;
|
||||
type Settings = ();
|
||||
type Error = BufferAssetLoaderError;
|
||||
|
||||
fn load<'a>(
|
||||
&'a self,
|
||||
bytes: &'a [u8],
|
||||
load_context: &'a mut LoadContext,
|
||||
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
|
||||
reader: &'a mut Reader,
|
||||
_settings: &'a (),
|
||||
_load_context: &'a mut LoadContext,
|
||||
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
||||
Box::pin(async move {
|
||||
let buffer: Option<Buffer> =
|
||||
match load_context.path().extension().unwrap().to_str().unwrap() {
|
||||
"flac" | "mp3" | "wav" => {
|
||||
syz::Buffer::from_encoded_data(bytes).map(Buffer).ok()
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
if let Some(buffer) = buffer {
|
||||
load_context.set_default_asset(LoadedAsset::new(buffer));
|
||||
}
|
||||
Ok(())
|
||||
let mut bytes = Vec::new();
|
||||
reader.read_to_end(&mut bytes).await?;
|
||||
let buffer = syz::Buffer::from_encoded_data(&bytes).map(Buffer)?;
|
||||
Ok(buffer)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -374,7 +383,7 @@ fn change_panner_strategy(
|
|||
check.push(entity);
|
||||
}
|
||||
}
|
||||
for entity in removed.iter() {
|
||||
for entity in removed.read() {
|
||||
check.push(entity);
|
||||
}
|
||||
for entity in check.iter() {
|
||||
|
@ -575,7 +584,7 @@ fn update_sound_playback_state(query: Query<&Sound>) {
|
|||
}
|
||||
|
||||
fn remove_sound(mut last_buffer: ResMut<LastAudio>, mut removed: RemovedComponents<Sound>) {
|
||||
for entity in removed.iter() {
|
||||
for entity in removed.read() {
|
||||
last_buffer.remove(&entity);
|
||||
}
|
||||
}
|
||||
|
@ -723,7 +732,7 @@ impl Plugin for SynthizerPlugin {
|
|||
};
|
||||
context.enable_events().expect("Failed to enable events");
|
||||
let context = Context(context);
|
||||
app.add_asset::<Buffer>()
|
||||
app.init_asset::<Buffer>()
|
||||
.init_asset_loader::<BufferAssetLoader>()
|
||||
.register_type::<DistanceRef>()
|
||||
.register_type::<DistanceMax>()
|
||||
|
@ -740,14 +749,15 @@ impl Plugin for SynthizerPlugin {
|
|||
.add_event::<SynthizerEvent>()
|
||||
.add_systems(
|
||||
PreUpdate,
|
||||
(sync_config, swap_buffers, change_panner_strategy).in_set(SynthizerSets::PreUpdate),
|
||||
(sync_config, swap_buffers, change_panner_strategy)
|
||||
.in_set(SynthizerSets::PreUpdate),
|
||||
)
|
||||
.add_systems(
|
||||
PostUpdate,
|
||||
(add_sound_without_source, add_source_handle, add_generator)
|
||||
.in_set(SynthizerSets::UpdateHandles),
|
||||
)
|
||||
.configure_set(
|
||||
.configure_sets(
|
||||
PostUpdate,
|
||||
SynthizerSets::UpdateHandles.before(SynthizerSets::UpdateProperties),
|
||||
)
|
||||
|
@ -761,7 +771,7 @@ impl Plugin for SynthizerPlugin {
|
|||
.in_set(SynthizerSets::UpdateProperties)
|
||||
.after(TransformSystem::TransformPropagate),
|
||||
)
|
||||
.configure_set(
|
||||
.configure_sets(
|
||||
PostUpdate,
|
||||
SynthizerSets::UpdateProperties.before(SynthizerSets::UpdateState),
|
||||
)
|
||||
|
@ -770,7 +780,7 @@ impl Plugin for SynthizerPlugin {
|
|||
(update_source_playback_state, update_sound_playback_state)
|
||||
.in_set(SynthizerSets::UpdateState),
|
||||
)
|
||||
.configure_set(
|
||||
.configure_sets(
|
||||
PostUpdate,
|
||||
SynthizerSets::UpdateState.before(SynthizerSets::Last),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user