Compare commits

...

19 Commits
v0.5.0 ... main

Author SHA1 Message Date
6f18f7ffbc Release
Some checks failed
Release / release (push) Failing after 2m30s
Test / test (ubuntu-latest) (push) Successful in 2m4s
2025-01-06 19:39:37 -05:00
888a9e78fc Update CHANGELOG.
Some checks failed
Test / test (ubuntu-latest) (push) Has been cancelled
2025-01-06 19:39:20 -05:00
7738cdb27c chore: Don't set position if values are NaN. 2025-01-06 19:38:57 -05:00
0c8f557886 Release
All checks were successful
Release / release (push) Successful in 2m52s
Test / test (ubuntu-latest) (push) Successful in 2m8s
2024-12-06 09:52:15 -06:00
78e5bf2c45 Update CHANGELOG. 2024-12-06 09:52:02 -06:00
3c557fa5ab chore: Upgrade to Bevy 0.15.
All checks were successful
Test / test (ubuntu-latest) (push) Successful in 1m57s
2024-12-06 09:33:24 -06:00
fe5816b722 Release
All checks were successful
Release / release (push) Successful in 2m49s
Test / test (ubuntu-latest) (push) Successful in 59s
2024-12-02 12:47:55 -06:00
0cb5bd59f6 Update CHANGELOG.
Some checks failed
Test / test (ubuntu-latest) (push) Has been cancelled
2024-12-02 12:47:24 -06:00
041165cf61 feat: Add Sound.playback_position to support initializing new buffers at non-zero playback position. 2024-12-02 12:46:06 -06:00
45746803c9 chore: Clean up code. 2024-12-02 11:36:10 -06:00
4c02a98eb4 fix: Clear generator when source is cleared, and improve handling for changing source types. 2024-12-02 11:34:28 -06:00
2364f638db Release
All checks were successful
Release / release (push) Successful in 2m57s
Test / test (ubuntu-latest) (push) Successful in 2m7s
2024-07-07 10:59:01 -05:00
3bcf232f1e Update changelog. 2024-07-07 10:58:49 -05:00
7cef2fedd5 chore: Upgrade to Bevy 0.14.
All checks were successful
Test / test (ubuntu-latest) (push) Successful in 2m8s
2024-07-07 10:44:14 -05:00
6cf5d92cb8 chore: Switch to Gitea Actions. 2024-07-07 09:52:51 -05:00
3d422dc9e4 chore: Add pre-commit. 2024-07-07 09:50:30 -05:00
96f3457d55 Release 2024-03-14 12:45:20 -05:00
6b19103d5a Update changelog. 2024-03-14 12:45:11 -05:00
3cd68ddcbe chore: Upgrade Bevy to v0.13. 2024-03-14 12:44:32 -05:00
9 changed files with 413 additions and 302 deletions

View File

@ -1,47 +0,0 @@
kind: pipeline
type: docker
name: default
environment:
DEPENDENCIES: cmake pkg-config libx11-dev libasound2-dev libudev-dev libxcb-xfixes0-dev libwayland-dev libxkbcommon-dev libvulkan-dev libpulse-dev
steps:
- name: test
image: rust:bullseye
pull: always
commands:
- apt-get update -qq
- apt-get install -qqy $DEPENDENCIES
- rustup component add clippy rustfmt
- cargo fmt --check
- cargo test
- cargo clippy
- name: release
image: rust:bullseye
pull: always
commands:
- apt-get update -qq
- apt-get install -qqy $DEPENDENCIES
- cargo publish
when:
ref:
- refs/tags/v*
environment:
CARGO_REGISTRY_TOKEN:
from_secret: cargo_registry_token
- name: discord notification
image: appleboy/drone-discord
when:
status: [success, failure]
settings:
webhook_id:
from_secret: discord_webhook_id
webhook_token:
from_secret: discord_webhook_token
tts: true
message: >
{{#success build.status}}
{{repo.name}} build {{build.number}} succeeded: <{{build.link}}>
{{else}}
{{repo.name}} build {{build.number}} failed: <{{build.link}}>
{{/success}}

View File

@ -0,0 +1,39 @@
name: Release
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+*"
workflow_dispatch:
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustfmt, clippy
- name: install Linux build dependencies
run: sudo apt-get update; sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev libwayland-dev libclang-dev cmake
if: runner.os == 'linux'
- uses: actions/setup-python@v3
- uses: pre-commit/action@v3.0.1
- name: Publish
run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

37
.gitea/workflows/test.yml Normal file
View File

@ -0,0 +1,37 @@
name: Test
on:
pull_request:
push:
jobs:
test:
strategy:
matrix:
# os: [windows-latest, ubuntu-latest, macos-latest]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustfmt, clippy
- name: install Linux build dependencies
run: sudo apt-get update; sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev libwayland-dev libclang-dev cmake
if: runner.os == 'linux'
- uses: actions/setup-python@v3
- uses: pre-commit/action@v3.0.1

10
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,10 @@
fail_fast: true
repos:
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
- id: fmt
args: [--, --check]
- id: cargo-check
args: [--bins, --examples]
- id: clippy

View File

@ -2,6 +2,46 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Version 0.9.1 - 2025-01-07
### Miscellaneous Tasks
- Don't set position if values are NaN.
## Version 0.9.0 - 2024-12-06
### Miscellaneous Tasks
- Upgrade to Bevy 0.15.
## Version 0.8.0 - 2024-12-02
### Bug Fixes
- Clear generator when source is cleared, and improve handling for changing source types.
### Features
- Add `Sound.playback_position` to support initializing new buffers at non-zero playback position.
### Miscellaneous Tasks
- Clean up code.
## Version 0.7.0 - 2024-07-07
### Miscellaneous Tasks
- Add pre-commit.
- Switch to Gitea Actions.
- Upgrade to Bevy 0.14.
## Version 0.6.0 - 2024-03-14
### Miscellaneous Tasks
- Upgrade Bevy to v0.13.
## Version 0.5.0 - 2024-02-09 ## Version 0.5.0 - 2024-02-09
### Bug Fixes ### Bug Fixes

View File

@ -1,6 +1,6 @@
[package] [package]
name = "bevy_synthizer" name = "bevy_synthizer"
version = "0.5.0" version = "0.9.1"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"] 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" 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" license = "MIT OR Apache-2.0"
@ -10,11 +10,12 @@ repository = "https://labs.lightsout.games/projects/bevy_synthizer"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bevy = { version = "0.12", default-features = false, features = ["bevy_asset"] } bevy = { version = "0.15", default-features = false, features = ["bevy_asset"] }
synthizer = "0.5.6" synthizer = "0.5.6"
thiserror = "1"
[dev-dependencies] [dev-dependencies]
bevy = { version = "0.12", default-features = true } bevy = { version = "0.15", default-features = true }
[package.metadata.release] [package.metadata.release]
publish = false publish = false

View File

@ -31,14 +31,10 @@ fn load_and_create(
if !listeners.is_empty() { if !listeners.is_empty() {
return; return;
} }
commands.spawn(( commands.spawn((Transform::default(), Listener, RotationTimer::default()));
TransformBundle::default(),
Listener,
RotationTimer::default(),
));
let handle = asset_server.load("footstep.wav"); let handle = asset_server.load("footstep.wav");
commands.spawn(( commands.spawn((
TransformBundle::from(Transform::from_translation(Vec3::new(10., 0., 0.))), Transform::from_translation(Vec3::new(10., 0., 0.)),
Source::default(), Source::default(),
Sound { Sound {
audio: handle.into(), audio: handle.into(),
@ -51,7 +47,7 @@ fn load_and_create(
fn rotate_listener(time: Res<Time>, mut query: Query<(&mut RotationTimer, &mut Transform)>) { fn rotate_listener(time: Res<Time>, mut query: Query<(&mut RotationTimer, &mut Transform)>) {
for (mut timer, mut transform) in query.iter_mut() { for (mut timer, mut transform) in query.iter_mut() {
timer.tick(time.delta()); timer.tick(time.delta());
let angle = f32::consts::PI * 2. * timer.percent(); let angle = f32::consts::PI * 2. * timer.fraction();
transform.rotation = Quat::from_rotation_z(angle); transform.rotation = Quat::from_rotation_z(angle);
} }
} }
@ -68,9 +64,6 @@ fn main() {
)) ))
.init_resource::<AssetHandles>() .init_resource::<AssetHandles>()
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems( .add_systems(Update, (load_and_create, rotate_listener))
Update,
(bevy::window::close_on_esc, load_and_create, rotate_listener),
)
.run(); .run();
} }

View File

@ -13,16 +13,12 @@ impl Default for RotationTimer {
} }
fn setup(mut commands: Commands, context: Res<Context>) { fn setup(mut commands: Commands, context: Res<Context>) {
commands.spawn(( commands.spawn((Transform::default(), Listener, RotationTimer::default()));
TransformBundle::default(),
Listener,
RotationTimer::default(),
));
let generator: syz::Generator = syz::FastSineBankGenerator::new_sine(&context, 440.) let generator: syz::Generator = syz::FastSineBankGenerator::new_sine(&context, 440.)
.expect("Failed to create generator") .expect("Failed to create generator")
.into(); .into();
commands.spawn(( commands.spawn((
TransformBundle::from(Transform::from_translation(Vec3::new(10., 0., 0.))), Transform::from_translation(Vec3::new(10., 0., 0.)),
Source::default(), Source::default(),
Sound { Sound {
audio: generator.into(), audio: generator.into(),
@ -35,7 +31,7 @@ fn setup(mut commands: Commands, context: Res<Context>) {
fn rotate_listener(time: Res<Time>, mut query: Query<(&mut RotationTimer, &mut Transform)>) { fn rotate_listener(time: Res<Time>, mut query: Query<(&mut RotationTimer, &mut Transform)>) {
for (mut timer, mut transform) in query.iter_mut() { for (mut timer, mut transform) in query.iter_mut() {
timer.tick(time.delta()); timer.tick(time.delta());
let angle = f32::consts::PI * 2. * timer.percent(); let angle = f32::consts::PI * 2. * timer.fraction();
transform.rotation = Quat::from_rotation_z(angle); transform.rotation = Quat::from_rotation_z(angle);
} }
} }
@ -51,6 +47,6 @@ fn main() {
}, },
)) ))
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(Update, (bevy::window::close_on_esc, rotate_listener)) .add_systems(Update, rotate_listener)
.run(); .run();
} }

View File

@ -2,16 +2,12 @@
use std::collections::HashMap; use std::collections::HashMap;
use bevy::{ use bevy::{
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext}, asset::{io::Reader, AssetLoader, LoadContext},
prelude::*, prelude::*,
reflect::TypePath,
transform::TransformSystem, transform::TransformSystem,
utils::{
thiserror::{self, Error},
BoxedFuture,
},
}; };
pub use synthizer as syz; pub use synthizer as syz;
use thiserror::Error;
#[derive(Asset, Clone, Debug, Deref, DerefMut, PartialEq, Eq, TypePath)] #[derive(Asset, Clone, Debug, Deref, DerefMut, PartialEq, Eq, TypePath)]
pub struct Buffer(syz::Buffer); pub struct Buffer(syz::Buffer);
@ -33,18 +29,16 @@ impl AssetLoader for BufferAssetLoader {
type Settings = (); type Settings = ();
type Error = BufferAssetLoaderError; type Error = BufferAssetLoaderError;
fn load<'a>( async fn load(
&'a self, &self,
reader: &'a mut Reader, reader: &mut dyn Reader,
_settings: &'a (), _settings: &(),
_load_context: &'a mut LoadContext, _load_context: &mut LoadContext<'_>,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> { ) -> Result<Self::Asset, Self::Error> {
Box::pin(async move { let mut bytes = Vec::new();
let mut bytes = Vec::new(); reader.read_to_end(&mut bytes).await?;
reader.read_to_end(&mut bytes).await?; let buffer = syz::Buffer::from_encoded_data(&bytes).map(Buffer)?;
let buffer = syz::Buffer::from_encoded_data(&bytes).map(Buffer)?; Ok(buffer)
Ok(buffer)
})
} }
fn extensions(&self) -> &[&str] { fn extensions(&self) -> &[&str] {
@ -161,6 +155,7 @@ pub struct Sound {
pub gain: f64, pub gain: f64,
pub pitch: f64, pub pitch: f64,
pub looping: bool, pub looping: bool,
pub playback_position: f64,
pub paused: bool, pub paused: bool,
pub generator: Option<syz::Generator>, pub generator: Option<syz::Generator>,
} }
@ -172,6 +167,7 @@ impl Default for Sound {
gain: 1., gain: 1.,
pitch: 1., pitch: 1.,
looping: false, looping: false,
playback_position: default(),
paused: false, paused: false,
generator: None, generator: None,
} }
@ -234,41 +230,42 @@ fn add_source_handle(
)>, )>,
) { ) {
for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query { for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query {
if source.handle.is_none() { if source.handle.is_some() {
let panner_strategy = panner_strategy.cloned().unwrap_or_default(); continue;
let handle: syz::Source = if let Some(transform) = transform {
let translation = transform.translation();
syz::Source3D::new(
&context,
*panner_strategy,
(
translation.x as f64,
translation.y as f64,
translation.z as f64,
),
)
.expect("Failed to create source")
.into()
} else if let Some(scalar_pan) = scalar_pan {
syz::ScalarPannedSource::new(&context, *panner_strategy, **scalar_pan)
.expect("Failed to create source")
.into()
} else if let Some(angular_pan) = angular_pan {
syz::AngularPannedSource::new(
&context,
*panner_strategy,
angular_pan.azimuth,
angular_pan.elevation,
)
.expect("Failed to create source")
.into()
} else {
syz::DirectSource::new(&context)
.expect("Failed to create source")
.into()
};
source.handle = Some(handle);
} }
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
let handle: syz::Source = if let Some(transform) = transform {
let translation = transform.translation();
syz::Source3D::new(
&context,
*panner_strategy,
(
translation.x as f64,
translation.y as f64,
translation.z as f64,
),
)
.expect("Failed to create source")
.into()
} else if let Some(scalar_pan) = scalar_pan {
syz::ScalarPannedSource::new(&context, *panner_strategy, **scalar_pan)
.expect("Failed to create source")
.into()
} else if let Some(angular_pan) = angular_pan {
syz::AngularPannedSource::new(
&context,
*panner_strategy,
angular_pan.azimuth,
angular_pan.elevation,
)
.expect("Failed to create source")
.into()
} else {
syz::DirectSource::new(&context)
.expect("Failed to create source")
.into()
};
source.handle = Some(handle);
} }
} }
@ -280,55 +277,62 @@ fn add_generator(
parents: Query<&Parent>, parents: Query<&Parent>,
) { ) {
for (entity, parent, mut sound) in &mut query { for (entity, parent, mut sound) in &mut query {
if sound.generator.is_none() { if sound.generator.is_some() {
let mut source = if let Ok(s) = sources.get_mut(entity) { continue;
Some(s) }
} else if parent.is_some() { let mut source = if let Ok(s) = sources.get_mut(entity) {
let mut parent = parent; Some(s)
let mut target = None; } else if parent.is_some() {
while let Some(p) = parent { let mut parent = parent;
if sources.get(**p).is_ok() { let mut target = None;
target = Some(**p); while let Some(p) = parent {
break; if sources.get(**p).is_ok() {
} target = Some(**p);
parent = parents.get(**p).ok(); break;
} }
target.map(|v| sources.get_mut(v).unwrap()) parent = parents.get(**p).ok();
} else { }
None target.map(|v| sources.get_mut(v).unwrap())
}; } else {
if let Some(source) = source.as_mut() { None
if let Some(handle) = source.handle.as_mut() { };
let generator: Option<syz::Generator> = match &sound.audio { if let Some(source) = source.as_mut() {
Audio::Buffer(buffer) => { if let Some(handle) = source.handle.as_mut() {
if let Some(b) = buffers.get(buffer) { let generator: Option<syz::Generator> = match &sound.audio {
let generator = syz::BufferGenerator::new(&context) Audio::Buffer(buffer) => {
.expect("Failed to create generator"); if let Some(b) = buffers.get(buffer) {
generator.buffer().set(&**b).expect("Unable to set buffer"); let generator = syz::BufferGenerator::new(&context)
Some(generator.into()) .expect("Failed to create generator");
} else { generator.buffer().set(&**b).expect("Unable to set buffer");
None assert!(sound.playback_position >= 0.);
} generator
.playback_position()
.set(sound.playback_position)
.expect("Failed to set playback position");
Some(generator.into())
} else {
None
} }
Audio::Generator(generator) => Some(generator.clone()),
};
if let Some(generator) = generator {
assert!(sound.gain >= 0.);
generator
.gain()
.set(sound.gain)
.expect("Failed to set gain");
assert!(sound.pitch > 0. && sound.pitch <= 2.);
generator
.pitch_bend()
.set(sound.pitch)
.expect("Failed to set pitch");
handle
.add_generator(generator.handle())
.expect("Unable to add generator");
sound.generator = Some(generator);
} }
} Audio::Generator(generator) => Some(generator.clone()),
};
let Some(generator) = generator else {
continue;
};
assert!(sound.gain >= 0.);
generator
.gain()
.set(sound.gain)
.expect("Failed to set gain");
assert!(sound.pitch > 0. && sound.pitch <= 2.);
generator
.pitch_bend()
.set(sound.pitch)
.expect("Failed to set pitch");
handle
.add_generator(generator.handle())
.expect("Unable to add generator");
sound.generator = Some(generator);
} }
} }
} }
@ -366,6 +370,7 @@ fn swap_buffers(
if let Some(l) = last_audio.get(&entity) { if let Some(l) = last_audio.get(&entity) {
if sound.generator.is_some() && sound.audio != *l { if sound.generator.is_some() && sound.audio != *l {
sound.generator = None; sound.generator = None;
sound.playback_position = 0.;
} }
} }
last_audio.insert(entity, sound.audio.clone()); last_audio.insert(entity, sound.audio.clone());
@ -387,10 +392,11 @@ fn change_panner_strategy(
check.push(entity); check.push(entity);
} }
for entity in check.iter() { for entity in check.iter() {
if let Ok(mut source) = sources.get_mut(*entity) { let Ok(mut source) = sources.get_mut(*entity) else {
if source.handle.is_some() { continue;
source.handle = None; };
} if source.handle.is_some() {
source.handle = None;
} }
} }
} }
@ -408,6 +414,7 @@ fn update_source_properties(
Option<&AngularPan>, Option<&AngularPan>,
Option<&ScalarPan>, Option<&ScalarPan>,
Option<&GlobalTransform>, Option<&GlobalTransform>,
Option<&mut Sound>,
)>, )>,
) { ) {
for ( for (
@ -421,16 +428,20 @@ fn update_source_properties(
angular_pan, angular_pan,
scalar_pan, scalar_pan,
transform, transform,
sound,
) in &mut query ) in &mut query
{ {
let Source { gain, .. } = *source; let Source { gain, .. } = *source;
assert!(gain >= 0.); assert!(gain >= 0.);
if let Some(handle) = source.handle.as_mut() { let Some(handle) = source.handle.as_mut() else {
handle.gain().set(gain).expect("Failed to set gain"); continue;
let mut clear_source = false; };
handle.gain().set(gain).expect("Failed to set gain");
let mut clear_source = false;
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") {
if let Some(transform) = transform { if let Some(transform) = transform {
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") { let translation = transform.translation();
let translation = transform.translation(); if !translation.x.is_nan() && !translation.y.is_nan() && !translation.z.is_nan() {
source source
.position() .position()
.set(( .set((
@ -439,92 +450,106 @@ fn update_source_properties(
translation.z as f64, translation.z as f64,
)) ))
.expect("Failed to set position"); .expect("Failed to set position");
let distance_model = distance_model
.cloned()
.map(|v| *v)
.unwrap_or_else(|| context.default_distance_model().get().unwrap());
source
.distance_model()
.set(distance_model)
.expect("Failed to set distance_model");
let distance_ref = distance_ref
.map(|v| **v)
.unwrap_or_else(|| context.default_distance_ref().get().unwrap());
assert!(distance_ref >= 0.);
source
.distance_ref()
.set(distance_ref)
.expect("Failed to set distance_ref");
let distance_max = distance_max
.map(|v| **v)
.unwrap_or_else(|| context.default_distance_max().get().unwrap());
assert!(distance_max >= 0.);
source
.distance_max()
.set(distance_max)
.expect("Failed to set distance_max");
let rolloff = rolloff
.map(|v| **v)
.unwrap_or_else(|| context.default_rolloff().get().unwrap());
assert!(rolloff >= 0.);
source
.rolloff()
.set(rolloff)
.expect("Failed to set rolloff");
let closeness_boost = closeness_boost
.map(|v| **v)
.unwrap_or_else(|| context.default_closeness_boost().get().unwrap());
assert!(closeness_boost >= 0.);
source
.closeness_boost()
.set(closeness_boost)
.expect("Failed to set closeness_boost");
let closeness_boost_distance =
closeness_boost_distance.map(|v| **v).unwrap_or_else(|| {
context.default_closeness_boost_distance().get().unwrap()
});
assert!(closeness_boost_distance >= 0.);
source
.closeness_boost_distance()
.set(closeness_boost_distance)
.expect("Failed to set closeness_boost_distance");
} else {
clear_source = true;
}
} else if let Some(angular_pan) = angular_pan {
if let Some(source) = handle
.cast_to::<syz::AngularPannedSource>()
.expect("Failed to cast")
{
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
source
.azimuth()
.set(angular_pan.azimuth)
.expect("Failed to set azimuth");
assert!(angular_pan.elevation >= -90. && angular_pan.elevation <= 90.);
source
.elevation()
.set(angular_pan.elevation)
.expect("Failed to set elevation");
} else {
clear_source = true;
}
} else if let Some(scalar_pan) = scalar_pan {
if let Some(source) = handle
.cast_to::<syz::ScalarPannedSource>()
.expect("Failed to cast")
{
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
source
.panning_scalar()
.set(**scalar_pan)
.expect("Failed to set scalar panning");
} else {
clear_source = true;
} }
let distance_model = distance_model
.cloned()
.map(|v| *v)
.unwrap_or_else(|| context.default_distance_model().get().unwrap());
source
.distance_model()
.set(distance_model)
.expect("Failed to set distance_model");
let distance_ref = distance_ref
.map(|v| **v)
.unwrap_or_else(|| context.default_distance_ref().get().unwrap());
assert!(distance_ref >= 0.);
source
.distance_ref()
.set(distance_ref)
.expect("Failed to set distance_ref");
let distance_max = distance_max
.map(|v| **v)
.unwrap_or_else(|| context.default_distance_max().get().unwrap());
assert!(distance_max >= 0.);
source
.distance_max()
.set(distance_max)
.expect("Failed to set distance_max");
let rolloff = rolloff
.map(|v| **v)
.unwrap_or_else(|| context.default_rolloff().get().unwrap());
assert!(rolloff >= 0.);
assert!(rolloff >= 0.);
source
.rolloff()
.set(rolloff)
.expect("Failed to set rolloff");
let closeness_boost = closeness_boost
.map(|v| **v)
.unwrap_or_else(|| context.default_closeness_boost().get().unwrap());
assert!(closeness_boost >= 0.);
source
.closeness_boost()
.set(closeness_boost)
.expect("Failed to set closeness_boost");
let closeness_boost_distance = closeness_boost_distance
.map(|v| **v)
.unwrap_or_else(|| context.default_closeness_boost_distance().get().unwrap());
assert!(closeness_boost_distance >= 0.);
source
.closeness_boost_distance()
.set(closeness_boost_distance)
.expect("Failed to set closeness_boost_distance");
} else {
clear_source = true;
} }
if clear_source { } else if let Some(source) = handle
source.handle = None; .cast_to::<syz::AngularPannedSource>()
.expect("Failed to cast")
{
if let Some(angular_pan) = angular_pan {
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
source
.azimuth()
.set(angular_pan.azimuth)
.expect("Failed to set azimuth");
assert!(angular_pan.elevation >= -90. && angular_pan.elevation <= 90.);
source
.elevation()
.set(angular_pan.elevation)
.expect("Failed to set elevation");
} else {
clear_source = true;
}
} else if let Some(source) = handle
.cast_to::<syz::ScalarPannedSource>()
.expect("Failed to cast")
{
if let Some(scalar_pan) = scalar_pan {
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
source
.panning_scalar()
.set(**scalar_pan)
.expect("Failed to set scalar panning");
} else {
clear_source = true;
}
} else if handle
.cast_to::<syz::DirectSource>()
.expect("Failed to cast")
.is_some()
{
if transform.is_some() || angular_pan.is_some() || scalar_pan.is_some() {
clear_source = true;
}
} else if source.handle.is_some() {
clear_source = true;
}
if clear_source {
source.handle = None;
if let Some(mut sound) = sound {
sound.generator = None;
sound.playback_position = 0.;
} }
} }
} }
@ -540,45 +565,60 @@ fn update_sound_properties(mut query: Query<&mut Sound>) {
} = *sound; } = *sound;
assert!(gain >= 0.); assert!(gain >= 0.);
assert!(pitch > 0. && pitch <= 2.); assert!(pitch > 0. && pitch <= 2.);
if let Some(generator) = sound.generator.as_mut() { let Some(generator) = &sound.generator else {
generator.gain().set(gain).expect("Failed to set gain"); continue;
};
if let Some(generator) = generator
.cast_to::<syz::BufferGenerator>()
.expect("Failed to cast")
{
sound.playback_position = generator
.playback_position()
.get()
.expect("Failed to getplayback position");
}
let Some(generator) = sound.generator.as_mut() else {
continue;
};
generator.gain().set(gain).expect("Failed to set gain");
generator
.pitch_bend()
.set(pitch)
.expect("Failed to set pitch");
if let Some(generator) = generator
.cast_to::<syz::BufferGenerator>()
.expect("Failed to cast")
{
generator generator
.pitch_bend() .looping()
.set(pitch) .set(looping)
.expect("Failed to set pitch"); .expect("Failed to set looping");
if let Some(generator) = generator
.cast_to::<syz::BufferGenerator>()
.expect("Failed to cast")
{
generator
.looping()
.set(looping)
.expect("Failed to set looping");
}
} }
} }
} }
fn update_source_playback_state(query: Query<&Source>) { fn update_source_playback_state(query: Query<&Source>) {
for source in &query { for source in &query {
if let Some(handle) = &source.handle { let Some(handle) = &source.handle else {
if source.paused { continue;
handle.pause().expect("Failed to pause"); };
} else { if source.paused {
handle.play().expect("Failed to play"); handle.pause().expect("Failed to pause");
} } else {
handle.play().expect("Failed to play");
} }
} }
} }
fn update_sound_playback_state(query: Query<&Sound>) { fn update_sound_playback_state(query: Query<&Sound>) {
for sound in &query { for sound in &query {
if let Some(generator) = &sound.generator { let Some(generator) = &sound.generator else {
if sound.paused { continue;
generator.pause().expect("Failed to pause"); };
} else { if sound.paused {
generator.play().expect("Failed to play"); generator.pause().expect("Failed to pause");
} } else {
generator.play().expect("Failed to play");
} }
} }
} }
@ -659,22 +699,24 @@ fn events(
mut output: EventWriter<SynthizerEvent>, mut output: EventWriter<SynthizerEvent>,
) { ) {
context.get_events().for_each(|event| { context.get_events().for_each(|event| {
if let Ok(event) = event { let Ok(event) = event else {
for (entity, sound) in &sounds { return;
if let Some(generator) = &sound.generator { };
if *generator.handle() == event.source { for (entity, sound) in &sounds {
match event.r#type { let Some(generator) = &sound.generator else {
syz::EventType::Finished => { continue;
output.send(SynthizerEvent::Finished(entity)); };
} if *generator.handle() == event.source {
syz::EventType::Looped => { match event.r#type {
output.send(SynthizerEvent::Looped(entity)); syz::EventType::Finished => {
} output.send(SynthizerEvent::Finished(entity));
_ => {}
}
break;
} }
syz::EventType::Looped => {
output.send(SynthizerEvent::Looped(entity));
}
_ => {}
} }
break;
} }
} }
}); });
@ -707,10 +749,10 @@ pub struct SynthizerPlugin {
impl Plugin for SynthizerPlugin { impl Plugin for SynthizerPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
if !app.world.contains_resource::<SynthizerPlugin>() { if !app.world().contains_resource::<SynthizerPlugin>() {
app.insert_resource(*self); app.insert_resource(*self);
} }
let config = *app.world.get_resource::<SynthizerPlugin>().unwrap(); let config = *app.world().get_resource::<SynthizerPlugin>().unwrap();
let mut syz_config = syz::LibraryConfig::new(); let mut syz_config = syz::LibraryConfig::new();
syz_config.log_level(config.log_level); syz_config.log_level(config.log_level);
if config.log_to_stderr { if config.log_to_stderr {