Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
6f18f7ffbc | |||
888a9e78fc | |||
7738cdb27c | |||
0c8f557886 | |||
78e5bf2c45 | |||
3c557fa5ab | |||
fe5816b722 | |||
0cb5bd59f6 | |||
041165cf61 | |||
45746803c9 | |||
4c02a98eb4 | |||
2364f638db | |||
3bcf232f1e | |||
7cef2fedd5 | |||
6cf5d92cb8 | |||
3d422dc9e4 | |||
96f3457d55 | |||
6b19103d5a | |||
3cd68ddcbe | |||
d34c3d6e96 | |||
d7e9b1b70c | |||
2080019421 | |||
4c1471c2ab | |||
04372328c8 | |||
1fb0aa763a | |||
53891eeaa8 |
47
.drone.yml
47
.drone.yml
|
@ -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}}
|
|
39
.gitea/workflows/release.yml
Normal file
39
.gitea/workflows/release.yml
Normal 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
37
.gitea/workflows/test.yml
Normal 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
10
.pre-commit-config.yaml
Normal 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
|
56
CHANGELOG.md
56
CHANGELOG.md
|
@ -2,6 +2,62 @@
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
### 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
|
## Version 0.4.0 - 2023-07-16
|
||||||
|
|
||||||
### Miscellaneous Tasks
|
### Miscellaneous Tasks
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "bevy_synthizer"
|
name = "bevy_synthizer"
|
||||||
version = "0.4.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,12 +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]
|
||||||
anyhow = "1"
|
bevy = { version = "0.15", default-features = false, features = ["bevy_asset"] }
|
||||||
bevy = { version = "0.11", default-features = false, features = ["bevy_asset"] }
|
|
||||||
synthizer = "0.5.6"
|
synthizer = "0.5.6"
|
||||||
|
thiserror = "1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bevy = { version = "0.11", default-features = true }
|
bevy = { version = "0.15", default-features = true }
|
||||||
|
|
||||||
[package.metadata.release]
|
[package.metadata.release]
|
||||||
publish = false
|
publish = false
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::f32;
|
use std::f32;
|
||||||
|
|
||||||
use bevy::{asset::LoadState, prelude::*};
|
use bevy::{asset::LoadedFolder, prelude::*};
|
||||||
use bevy_synthizer::*;
|
use bevy_synthizer::*;
|
||||||
|
|
||||||
#[derive(Component, Deref, DerefMut)]
|
#[derive(Component, Deref, DerefMut)]
|
||||||
|
@ -12,51 +12,42 @@ impl Default for RotationTimer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default, Deref, DerefMut)]
|
||||||
struct AssetHandles {
|
struct AssetHandles(Handle<LoadedFolder>);
|
||||||
sounds: Vec<HandleUntyped>,
|
|
||||||
loaded: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup(asset_server: Res<AssetServer>, mut handles: ResMut<AssetHandles>) {
|
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(
|
fn load_and_create(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
asset_server: Res<AssetServer>,
|
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;
|
return;
|
||||||
}
|
}
|
||||||
handles.loaded = asset_server
|
if !listeners.is_empty() {
|
||||||
.get_group_load_state(handles.sounds.iter().map(|handle| handle.id()))
|
return;
|
||||||
== LoadState::Loaded;
|
}
|
||||||
if handles.loaded {
|
commands.spawn((Transform::default(), Listener, RotationTimer::default()));
|
||||||
|
let handle = asset_server.load("footstep.wav");
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
TransformBundle::default(),
|
Transform::from_translation(Vec3::new(10., 0., 0.)),
|
||||||
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(),
|
Source::default(),
|
||||||
Sound {
|
Sound {
|
||||||
audio: buffer.into(),
|
audio: handle.into(),
|
||||||
looping: true,
|
looping: true,
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
204
src/lib.rs
204
src/lib.rs
|
@ -2,40 +2,43 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
asset::{AssetLoader, LoadContext, LoadedAsset},
|
asset::{io::Reader, AssetLoader, LoadContext},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
reflect::{TypePath, TypeUuid},
|
|
||||||
transform::TransformSystem,
|
transform::TransformSystem,
|
||||||
utils::BoxedFuture,
|
|
||||||
};
|
};
|
||||||
pub use synthizer as syz;
|
pub use synthizer as syz;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deref, DerefMut, PartialEq, Eq, TypePath, TypeUuid)]
|
#[derive(Asset, Clone, Debug, Deref, DerefMut, PartialEq, Eq, TypePath)]
|
||||||
#[uuid = "6b6b533a-bb1f-11ec-bda2-00155d8fdde9"]
|
|
||||||
pub struct Buffer(syz::Buffer);
|
pub struct Buffer(syz::Buffer);
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default)]
|
#[derive(Clone, Copy, Debug, Default)]
|
||||||
struct BufferAssetLoader;
|
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 {
|
impl AssetLoader for BufferAssetLoader {
|
||||||
fn load<'a>(
|
type Asset = Buffer;
|
||||||
&'a self,
|
type Settings = ();
|
||||||
bytes: &'a [u8],
|
type Error = BufferAssetLoaderError;
|
||||||
load_context: &'a mut LoadContext,
|
|
||||||
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
|
async fn load(
|
||||||
Box::pin(async move {
|
&self,
|
||||||
let buffer: Option<Buffer> =
|
reader: &mut dyn Reader,
|
||||||
match load_context.path().extension().unwrap().to_str().unwrap() {
|
_settings: &(),
|
||||||
"flac" | "mp3" | "wav" => {
|
_load_context: &mut LoadContext<'_>,
|
||||||
syz::Buffer::from_encoded_data(bytes).map(Buffer).ok()
|
) -> Result<Self::Asset, Self::Error> {
|
||||||
}
|
let mut bytes = Vec::new();
|
||||||
_ => None,
|
reader.read_to_end(&mut bytes).await?;
|
||||||
};
|
let buffer = syz::Buffer::from_encoded_data(&bytes).map(Buffer)?;
|
||||||
if let Some(buffer) = buffer {
|
Ok(buffer)
|
||||||
load_context.set_default_asset(LoadedAsset::new(buffer));
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extensions(&self) -> &[&str] {
|
fn extensions(&self) -> &[&str] {
|
||||||
|
@ -152,8 +155,8 @@ 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 restart: bool,
|
|
||||||
pub generator: Option<syz::Generator>,
|
pub generator: Option<syz::Generator>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,8 +167,8 @@ impl Default for Sound {
|
||||||
gain: 1.,
|
gain: 1.,
|
||||||
pitch: 1.,
|
pitch: 1.,
|
||||||
looping: false,
|
looping: false,
|
||||||
|
playback_position: default(),
|
||||||
paused: false,
|
paused: false,
|
||||||
restart: false,
|
|
||||||
generator: None,
|
generator: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,7 +230,9 @@ 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() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
|
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
|
||||||
let handle: syz::Source = if let Some(transform) = transform {
|
let handle: syz::Source = if let Some(transform) = transform {
|
||||||
let translation = transform.translation();
|
let translation = transform.translation();
|
||||||
|
@ -263,7 +268,6 @@ fn add_source_handle(
|
||||||
source.handle = Some(handle);
|
source.handle = Some(handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn add_generator(
|
fn add_generator(
|
||||||
context: Res<Context>,
|
context: Res<Context>,
|
||||||
|
@ -273,11 +277,13 @@ 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() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let mut source = if let Ok(s) = sources.get_mut(entity) {
|
let mut source = if let Ok(s) = sources.get_mut(entity) {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else if let Some(parent) = parent {
|
} else if parent.is_some() {
|
||||||
let mut parent: Option<&Parent> = Some(parent);
|
let mut parent = parent;
|
||||||
let mut target = None;
|
let mut target = None;
|
||||||
while let Some(p) = parent {
|
while let Some(p) = parent {
|
||||||
if sources.get(**p).is_ok() {
|
if sources.get(**p).is_ok() {
|
||||||
|
@ -298,6 +304,11 @@ fn add_generator(
|
||||||
let generator = syz::BufferGenerator::new(&context)
|
let generator = syz::BufferGenerator::new(&context)
|
||||||
.expect("Failed to create generator");
|
.expect("Failed to create generator");
|
||||||
generator.buffer().set(&**b).expect("Unable to set buffer");
|
generator.buffer().set(&**b).expect("Unable to set buffer");
|
||||||
|
assert!(sound.playback_position >= 0.);
|
||||||
|
generator
|
||||||
|
.playback_position()
|
||||||
|
.set(sound.playback_position)
|
||||||
|
.expect("Failed to set playback position");
|
||||||
Some(generator.into())
|
Some(generator.into())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -305,7 +316,9 @@ fn add_generator(
|
||||||
}
|
}
|
||||||
Audio::Generator(generator) => Some(generator.clone()),
|
Audio::Generator(generator) => Some(generator.clone()),
|
||||||
};
|
};
|
||||||
if let Some(generator) = generator {
|
let Some(generator) = generator else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
assert!(sound.gain >= 0.);
|
assert!(sound.gain >= 0.);
|
||||||
generator
|
generator
|
||||||
.gain()
|
.gain()
|
||||||
|
@ -324,8 +337,6 @@ fn add_generator(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_sound_without_source(
|
fn add_sound_without_source(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
@ -357,8 +368,9 @@ fn swap_buffers(
|
||||||
) {
|
) {
|
||||||
for (entity, mut sound) in &mut query {
|
for (entity, mut sound) in &mut query {
|
||||||
if let Some(l) = last_audio.get(&entity) {
|
if let Some(l) = last_audio.get(&entity) {
|
||||||
if 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());
|
||||||
|
@ -376,17 +388,18 @@ fn change_panner_strategy(
|
||||||
check.push(entity);
|
check.push(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for entity in removed.iter() {
|
for entity in removed.read() {
|
||||||
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 {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
if source.handle.is_some() {
|
if source.handle.is_some() {
|
||||||
source.handle = None;
|
source.handle = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn update_source_properties(
|
fn update_source_properties(
|
||||||
context: Res<Context>,
|
context: Res<Context>,
|
||||||
|
@ -401,6 +414,7 @@ fn update_source_properties(
|
||||||
Option<&AngularPan>,
|
Option<&AngularPan>,
|
||||||
Option<&ScalarPan>,
|
Option<&ScalarPan>,
|
||||||
Option<&GlobalTransform>,
|
Option<&GlobalTransform>,
|
||||||
|
Option<&mut Sound>,
|
||||||
)>,
|
)>,
|
||||||
) {
|
) {
|
||||||
for (
|
for (
|
||||||
|
@ -414,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 {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
handle.gain().set(gain).expect("Failed to set gain");
|
handle.gain().set(gain).expect("Failed to set gain");
|
||||||
let mut clear_source = false;
|
let mut clear_source = false;
|
||||||
if let Some(transform) = transform {
|
|
||||||
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") {
|
if let Some(source) = handle.cast_to::<syz::Source3D>().expect("Failed to cast") {
|
||||||
|
if let Some(transform) = transform {
|
||||||
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((
|
||||||
|
@ -432,6 +450,7 @@ 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
|
let distance_model = distance_model
|
||||||
.cloned()
|
.cloned()
|
||||||
.map(|v| *v)
|
.map(|v| *v)
|
||||||
|
@ -460,6 +479,7 @@ fn update_source_properties(
|
||||||
.map(|v| **v)
|
.map(|v| **v)
|
||||||
.unwrap_or_else(|| context.default_rolloff().get().unwrap());
|
.unwrap_or_else(|| context.default_rolloff().get().unwrap());
|
||||||
assert!(rolloff >= 0.);
|
assert!(rolloff >= 0.);
|
||||||
|
assert!(rolloff >= 0.);
|
||||||
source
|
source
|
||||||
.rolloff()
|
.rolloff()
|
||||||
.set(rolloff)
|
.set(rolloff)
|
||||||
|
@ -472,10 +492,9 @@ fn update_source_properties(
|
||||||
.closeness_boost()
|
.closeness_boost()
|
||||||
.set(closeness_boost)
|
.set(closeness_boost)
|
||||||
.expect("Failed to set closeness_boost");
|
.expect("Failed to set closeness_boost");
|
||||||
let closeness_boost_distance =
|
let closeness_boost_distance = closeness_boost_distance
|
||||||
closeness_boost_distance.map(|v| **v).unwrap_or_else(|| {
|
.map(|v| **v)
|
||||||
context.default_closeness_boost_distance().get().unwrap()
|
.unwrap_or_else(|| context.default_closeness_boost_distance().get().unwrap());
|
||||||
});
|
|
||||||
assert!(closeness_boost_distance >= 0.);
|
assert!(closeness_boost_distance >= 0.);
|
||||||
source
|
source
|
||||||
.closeness_boost_distance()
|
.closeness_boost_distance()
|
||||||
|
@ -484,11 +503,11 @@ fn update_source_properties(
|
||||||
} else {
|
} else {
|
||||||
clear_source = true;
|
clear_source = true;
|
||||||
}
|
}
|
||||||
} else if let Some(angular_pan) = angular_pan {
|
} else if let Some(source) = handle
|
||||||
if let Some(source) = handle
|
|
||||||
.cast_to::<syz::AngularPannedSource>()
|
.cast_to::<syz::AngularPannedSource>()
|
||||||
.expect("Failed to cast")
|
.expect("Failed to cast")
|
||||||
{
|
{
|
||||||
|
if let Some(angular_pan) = angular_pan {
|
||||||
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
|
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
|
||||||
source
|
source
|
||||||
.azimuth()
|
.azimuth()
|
||||||
|
@ -502,11 +521,11 @@ fn update_source_properties(
|
||||||
} else {
|
} else {
|
||||||
clear_source = true;
|
clear_source = true;
|
||||||
}
|
}
|
||||||
} else if let Some(scalar_pan) = scalar_pan {
|
} else if let Some(source) = handle
|
||||||
if let Some(source) = handle
|
|
||||||
.cast_to::<syz::ScalarPannedSource>()
|
.cast_to::<syz::ScalarPannedSource>()
|
||||||
.expect("Failed to cast")
|
.expect("Failed to cast")
|
||||||
{
|
{
|
||||||
|
if let Some(scalar_pan) = scalar_pan {
|
||||||
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
|
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
|
||||||
source
|
source
|
||||||
.panning_scalar()
|
.panning_scalar()
|
||||||
|
@ -515,9 +534,22 @@ fn update_source_properties(
|
||||||
} else {
|
} else {
|
||||||
clear_source = true;
|
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 {
|
if clear_source {
|
||||||
source.handle = None;
|
source.handle = None;
|
||||||
|
if let Some(mut sound) = sound {
|
||||||
|
sound.generator = None;
|
||||||
|
sound.playback_position = 0.;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -533,21 +565,21 @@ 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 sound.restart {
|
let Some(generator) = &sound.generator else {
|
||||||
if let Some(generator) = sound.generator.as_mut() {
|
continue;
|
||||||
|
};
|
||||||
if let Some(generator) = generator
|
if let Some(generator) = generator
|
||||||
.cast_to::<syz::BufferGenerator>()
|
.cast_to::<syz::BufferGenerator>()
|
||||||
.expect("Failed to cast")
|
.expect("Failed to cast")
|
||||||
{
|
{
|
||||||
generator
|
sound.playback_position = generator
|
||||||
.playback_position()
|
.playback_position()
|
||||||
.set(0.)
|
.get()
|
||||||
.expect("Failed to restart");
|
.expect("Failed to getplayback position");
|
||||||
}
|
}
|
||||||
}
|
let Some(generator) = sound.generator.as_mut() else {
|
||||||
sound.restart = false;
|
continue;
|
||||||
}
|
};
|
||||||
if let Some(generator) = sound.generator.as_mut() {
|
|
||||||
generator.gain().set(gain).expect("Failed to set gain");
|
generator.gain().set(gain).expect("Failed to set gain");
|
||||||
generator
|
generator
|
||||||
.pitch_bend()
|
.pitch_bend()
|
||||||
|
@ -564,11 +596,12 @@ fn update_sound_properties(mut query: Query<&mut Sound>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
if source.paused {
|
if source.paused {
|
||||||
handle.pause().expect("Failed to pause");
|
handle.pause().expect("Failed to pause");
|
||||||
} else {
|
} else {
|
||||||
|
@ -576,11 +609,12 @@ fn update_source_playback_state(query: Query<&Source>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
if sound.paused {
|
if sound.paused {
|
||||||
generator.pause().expect("Failed to pause");
|
generator.pause().expect("Failed to pause");
|
||||||
} else {
|
} else {
|
||||||
|
@ -588,10 +622,9 @@ fn update_sound_playback_state(query: Query<&Sound>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_sound(mut last_buffer: ResMut<LastAudio>, mut removed: RemovedComponents<Source>) {
|
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);
|
last_buffer.remove(&entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -666,12 +699,14 @@ 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 {
|
||||||
let mut matched = false;
|
return;
|
||||||
|
};
|
||||||
for (entity, sound) in &sounds {
|
for (entity, sound) in &sounds {
|
||||||
if let Some(generator) = &sound.generator {
|
let Some(generator) = &sound.generator else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
if *generator.handle() == event.source {
|
if *generator.handle() == event.source {
|
||||||
matched = true;
|
|
||||||
match event.r#type {
|
match event.r#type {
|
||||||
syz::EventType::Finished => {
|
syz::EventType::Finished => {
|
||||||
output.send(SynthizerEvent::Finished(entity));
|
output.send(SynthizerEvent::Finished(entity));
|
||||||
|
@ -684,17 +719,12 @@ fn events(
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if !matched {
|
|
||||||
println!("No match");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)]
|
#[derive(SystemSet, Clone, Hash, Debug, PartialEq, Eq)]
|
||||||
pub enum SynthizerSets {
|
pub enum SynthizerSets {
|
||||||
First,
|
PreUpdate,
|
||||||
UpdateHandles,
|
UpdateHandles,
|
||||||
UpdateProperties,
|
UpdateProperties,
|
||||||
UpdateState,
|
UpdateState,
|
||||||
|
@ -719,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 {
|
||||||
|
@ -744,7 +774,7 @@ impl Plugin for SynthizerPlugin {
|
||||||
};
|
};
|
||||||
context.enable_events().expect("Failed to enable events");
|
context.enable_events().expect("Failed to enable events");
|
||||||
let context = Context(context);
|
let context = Context(context);
|
||||||
app.add_asset::<Buffer>()
|
app.init_asset::<Buffer>()
|
||||||
.init_asset_loader::<BufferAssetLoader>()
|
.init_asset_loader::<BufferAssetLoader>()
|
||||||
.register_type::<DistanceRef>()
|
.register_type::<DistanceRef>()
|
||||||
.register_type::<DistanceMax>()
|
.register_type::<DistanceMax>()
|
||||||
|
@ -761,23 +791,15 @@ impl Plugin for SynthizerPlugin {
|
||||||
.add_event::<SynthizerEvent>()
|
.add_event::<SynthizerEvent>()
|
||||||
.add_systems(
|
.add_systems(
|
||||||
PreUpdate,
|
PreUpdate,
|
||||||
(
|
(sync_config, swap_buffers, change_panner_strategy)
|
||||||
sync_config,
|
.in_set(SynthizerSets::PreUpdate),
|
||||||
swap_buffers,
|
|
||||||
change_panner_strategy,
|
|
||||||
add_sound_without_source,
|
|
||||||
)
|
|
||||||
.in_set(SynthizerSets::First),
|
|
||||||
)
|
|
||||||
.configure_set(
|
|
||||||
PreUpdate,
|
|
||||||
SynthizerSets::First.before(SynthizerSets::UpdateHandles),
|
|
||||||
)
|
)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
PostUpdate,
|
PostUpdate,
|
||||||
(add_source_handle, add_generator).in_set(SynthizerSets::UpdateHandles),
|
(add_sound_without_source, add_source_handle, add_generator)
|
||||||
|
.in_set(SynthizerSets::UpdateHandles),
|
||||||
)
|
)
|
||||||
.configure_set(
|
.configure_sets(
|
||||||
PostUpdate,
|
PostUpdate,
|
||||||
SynthizerSets::UpdateHandles.before(SynthizerSets::UpdateProperties),
|
SynthizerSets::UpdateHandles.before(SynthizerSets::UpdateProperties),
|
||||||
)
|
)
|
||||||
|
@ -791,7 +813,7 @@ impl Plugin for SynthizerPlugin {
|
||||||
.in_set(SynthizerSets::UpdateProperties)
|
.in_set(SynthizerSets::UpdateProperties)
|
||||||
.after(TransformSystem::TransformPropagate),
|
.after(TransformSystem::TransformPropagate),
|
||||||
)
|
)
|
||||||
.configure_set(
|
.configure_sets(
|
||||||
PostUpdate,
|
PostUpdate,
|
||||||
SynthizerSets::UpdateProperties.before(SynthizerSets::UpdateState),
|
SynthizerSets::UpdateProperties.before(SynthizerSets::UpdateState),
|
||||||
)
|
)
|
||||||
|
@ -800,7 +822,7 @@ impl Plugin for SynthizerPlugin {
|
||||||
(update_source_playback_state, update_sound_playback_state)
|
(update_source_playback_state, update_sound_playback_state)
|
||||||
.in_set(SynthizerSets::UpdateState),
|
.in_set(SynthizerSets::UpdateState),
|
||||||
)
|
)
|
||||||
.configure_set(
|
.configure_sets(
|
||||||
PostUpdate,
|
PostUpdate,
|
||||||
SynthizerSets::UpdateState.before(SynthizerSets::Last),
|
SynthizerSets::UpdateState.before(SynthizerSets::Last),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user