Compare commits

..

No commits in common. "d500c9c4454e30c9060c92bc8bb3623dbc61d91f" and "def95dcccb17d042ab0e00a587cafd46e4d69ab9" have entirely different histories.

3 changed files with 165 additions and 315 deletions

View File

@ -10,7 +10,7 @@ edition = "2021"
[dependencies]
anyhow = "1"
bevy = { version = "0.8", default-features = false, features = ["bevy_asset"] }
synthizer = { git = "https://github.com/ndarilek/synthizer-rs" }
synthizer = { git = "https://github.com/synthizer/synthizer-rs" }
[dev-dependencies]
bevy = { version = "0.8", default-features = true }

View File

@ -35,16 +35,15 @@ fn load_and_create(
== LoadState::Loaded;
if handles.loaded {
commands
.spawn_bundle(TransformBundle::default())
.spawn()
.insert(Listener)
.insert(Transform::default())
.insert(RotationTimer::default());
let handle = handles.sounds[0].clone();
let buffer = asset_server.get_handle(handle);
commands
.spawn_bundle(TransformBundle::from(Transform::from_translation(
Vec3::new(10., 0., 0.),
)))
.insert(Source::default())
.spawn()
.insert(Transform::from_translation(Vec3::new(10., 0., 0.)))
.insert(Sound {
buffer,
looping: true,

View File

@ -45,19 +45,30 @@ impl AssetLoader for BufferAssetLoader {
#[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)]
pub struct Source {
pub struct Sound {
pub buffer: Handle<Buffer>,
pub gain: f64,
pub pitch: f64,
pub looping: bool,
pub paused: bool,
pub restart: bool,
#[reflect(ignore)]
pub handle: Option<syz::Source>,
pub source: Option<syz::Source>,
#[reflect(ignore)]
pub generator: Option<syz::BufferGenerator>,
}
impl Default for Source {
impl Default for Sound {
fn default() -> Self {
Self {
buffer: Default::default(),
gain: 1.,
pitch: 1.,
looping: false,
paused: false,
handle: None,
restart: false,
source: None,
generator: None,
}
}
}
@ -120,33 +131,6 @@ impl ScalarPan {
}
}
#[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)]
pub struct Sound {
pub buffer: Handle<Buffer>,
pub gain: f64,
pub pitch: f64,
pub looping: bool,
pub paused: bool,
pub restart: bool,
#[reflect(ignore)]
pub generator: Option<syz::BufferGenerator>,
}
impl Default for Sound {
fn default() -> Self {
Self {
buffer: default(),
gain: 1.,
pitch: 1.,
looping: false,
paused: false,
restart: false,
generator: None,
}
}
}
pub enum SynthizerEvent {
Finished(Entity),
Looped(Entity),
@ -156,173 +140,44 @@ pub enum SynthizerEvent {
#[reflect(Component)]
pub struct Listener;
fn update_listener(
pub fn update_listener(
context: ResMut<syz::Context>,
listener: Query<Option<&GlobalTransform>, With<Listener>>,
listener: Query<(Option<&Transform>, Option<&GlobalTransform>), With<Listener>>,
) {
if let Ok(transform) = listener.get_single() {
let transform: Transform = transform
if let Ok((transform, global_transform)) = listener.get_single() {
let transform: Option<Transform> = global_transform
.map(|v| {
let transform: Transform = (*v).into();
transform
})
.unwrap_or_default();
.or_else(|| transform.cloned());
if let Some(transform) = transform {
let look = transform.local_x();
let up = transform.local_z();
context
.position()
.set((
if let Err(e) = context.position().set((
transform.translation.x as f64,
transform.translation.y as f64,
transform.translation.z as f64,
))
.expect("Failed to set listener position");
context
.orientation()
.set((
)) {
error!("Error setting listener position: {:?}", e);
}
if let Err(e) = context.orientation().set((
look.x as f64,
look.y as f64,
look.z as f64,
up.x as f64,
up.y as f64,
up.z as f64,
))
.expect("Failed to set listener orientation");
)) {
error!("Error setting listener orientation: {:?}", e);
}
}
fn add_source_handle(
context: Res<syz::Context>,
mut query: Query<(
&mut Source,
Option<&PannerStrategy>,
Option<&GlobalTransform>,
Option<&AngularPan>,
Option<&ScalarPan>,
)>,
) {
for (mut source, panner_strategy, transform, angular_pan, scalar_pan) in &mut query {
if source.handle.is_none() {
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);
context.position().set((0., 0., 0.)).ok();
context.orientation().set((0., 0., 1., 0., 1., 0.)).ok();
}
}
}
fn add_generator(
context: Res<syz::Context>,
buffers: Res<Assets<Buffer>>,
mut query: Query<(Entity, Option<&Parent>, &mut Sound)>,
mut sources: Query<&mut Source>,
parents: Query<&Parent>,
) {
for (entity, parent, mut sound) in &mut query {
if sound.generator.is_none() {
if let Some(b) = buffers.get(&sound.buffer) {
let mut source = if let Ok(s) = sources.get_mut(entity) {
Some(s)
} else if let Some(parent) = parent {
let mut parent: Option<&Parent> = Some(parent);
let mut target = None;
while let Some(p) = parent {
if sources.get(**p).is_ok() {
target = Some(**p);
break;
}
parent = parents.get(**p).ok();
}
target.map(|v| sources.get_mut(v).unwrap())
} else {
None
};
if let Some(source) = source.as_mut() {
if let Some(handle) = source.handle.as_mut() {
let generator = syz::BufferGenerator::new(&context)
.expect("Failed to create generator");
generator.buffer().set(&**b).expect("Unable to set buffer");
handle
.add_generator(&generator)
.expect("Unable to add generator");
sound.generator = Some(generator);
}
}
}
}
}
}
fn add_sound_without_source(
mut commands: Commands,
query: Query<
(Entity, Option<&Parent>, Option<&GlobalTransform>),
(Added<Sound>, Without<Source>),
>,
parents: Query<&Parent>,
sources: Query<&Source>,
transforms: Query<&GlobalTransform>,
) {
for (entity, parent, transform) in &query {
let should_check_for_transform = transform.is_none();
let mut has_transform = false;
let source = if let Some(parent) = parent {
if should_check_for_transform {
has_transform = transforms.get(**parent).is_ok();
}
let mut target = None;
let mut parent: Option<&Parent> = Some(parent);
while let Some(p) = parent {
if should_check_for_transform && !has_transform {
has_transform = transforms.get(**p).is_ok();
}
if sources.get(**p).is_ok() {
target = Some(**p);
break;
}
parent = parents.get(**p).ok();
}
target.map(|v| sources.get(v).unwrap())
} else {
None
};
if source.is_none() {
let id = commands.entity(entity).insert(Source::default()).id();
if has_transform {
commands
.entity(id)
.insert_bundle(TransformBundle::default());
}
}
context.position().set((0., 0., 0.)).ok();
context.orientation().set((0., 0., 1., 0., 1., 0.)).ok();
}
}
@ -333,9 +188,10 @@ fn swap_buffers(
mut last_buffer: ResMut<LastBuffer>,
mut query: Query<(Entity, &mut Sound), Changed<Sound>>,
) {
for (entity, mut sound) in &mut query {
for (entity, mut sound) in query.iter_mut() {
if let Some(l) = last_buffer.get(&entity) {
if sound.buffer != *l {
sound.source = None;
sound.generator = None;
}
}
@ -346,28 +202,30 @@ fn swap_buffers(
fn change_panner_strategy(
changed: Query<Entity, Changed<PannerStrategy>>,
removed: RemovedComponents<PannerStrategy>,
mut sources: Query<&mut Source>,
mut sounds: Query<&mut Sound>,
) {
let mut check = vec![];
for entity in &changed {
for entity in changed.iter() {
check.push(entity);
}
for entity in removed.iter() {
check.push(entity);
}
for entity in check.iter() {
if let Ok(mut source) = sources.get_mut(*entity) {
if source.handle.is_some() {
source.handle = None;
if let Ok(mut sound) = sounds.get_mut(*entity) {
if sound.source.is_some() {
sound.source = None;
}
}
}
}
fn update_source_properties(
pub fn update_sound_properties(
context: Res<syz::Context>,
buffers: Res<Assets<Buffer>>,
mut query: Query<(
&mut Source,
&mut Sound,
Option<&PannerStrategy>,
Option<&DistanceModel>,
Option<&DistanceRef>,
Option<&DistanceMax>,
@ -376,11 +234,13 @@ fn update_source_properties(
Option<&ClosenessBoostDistance>,
Option<&AngularPan>,
Option<&ScalarPan>,
Option<&Transform>,
Option<&GlobalTransform>,
)>,
) {
for (
mut source,
mut sound,
panner_strategy,
distance_model,
distance_ref,
distance_max,
@ -390,16 +250,103 @@ fn update_source_properties(
angular_pan,
scalar_pan,
transform,
) in &mut query
global_transform,
) in query.iter_mut()
{
let Source { gain, .. } = *source;
let Sound {
gain,
pitch,
looping,
..
} = *sound;
assert!(gain >= 0.);
if let Some(handle) = source.handle.as_mut() {
handle.gain().set(gain).expect("Failed to set gain");
assert!(pitch >= 0.);
if sound.restart {
if let Some(generator) = sound.generator.as_mut() {
generator
.playback_position()
.set(0.)
.expect("Failed to restart");
}
sound.restart = false;
}
if sound.generator.is_none() {
let generator =
syz::BufferGenerator::new(&context).expect("Failed to create generator");
sound.generator = Some(generator);
}
let translation = global_transform
.map(|v| v.translation())
.or_else(|| transform.map(|v| v.translation));
if sound.source.is_none() {
if let Some(b) = buffers.get(&sound.buffer) {
if let Some(generator) = sound.generator.as_mut() {
generator.buffer().set(&**b).expect("Unable to set buffer");
if let Some(translation) = translation {
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
let source = syz::Source3D::new(
&context,
*panner_strategy,
(
translation.x as f64,
translation.y as f64,
translation.z as f64,
),
)
.expect("Failed to create source");
source
.add_generator(generator)
.expect("Unable to add generator");
sound.source = Some(source.into());
} else if let Some(scalar_pan) = scalar_pan {
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
let source =
syz::ScalarPannedSource::new(&context, *panner_strategy, **scalar_pan)
.expect("Failed to create source");
source
.add_generator(generator)
.expect("Failed to add generator");
sound.source = Some(source.into());
} else if let Some(angular_pan) = angular_pan {
let panner_strategy = panner_strategy.cloned().unwrap_or_default();
let source = syz::AngularPannedSource::new(
&context,
*panner_strategy,
angular_pan.azimuth,
angular_pan.elevation,
)
.expect("Failed to create source");
source
.add_generator(generator)
.expect("Failed to add generator");
sound.source = Some(source.into());
} else {
let source =
syz::DirectSource::new(&context).expect("Failed to create source");
source
.add_generator(generator)
.expect("Failed to add generator");
sound.source = Some(source.into());
}
}
}
}
if let Some(generator) = sound.generator.as_mut() {
generator.gain().set(gain).expect("Failed to set gain");
generator
.pitch_bend()
.set(pitch)
.expect("Failed to set pitch");
generator
.looping()
.set(looping)
.expect("Failed to set looping");
}
if let Some(source) = sound.source.as_mut() {
source.gain().set(gain).expect("Failed to set gain");
let mut clear_source = false;
if let Some(transform) = transform {
if let Some(source) = handle.cast_to::<syz::Source3D>().unwrap() {
let translation = transform.translation();
if let Some(translation) = translation {
if let Some(source) = source.cast_to::<syz::Source3D>().unwrap() {
source
.position()
.set((
@ -461,7 +408,7 @@ fn update_source_properties(
clear_source = true;
}
} else if let Some(angular_pan) = angular_pan {
if let Some(source) = handle.cast_to::<syz::AngularPannedSource>().unwrap() {
if let Some(source) = source.cast_to::<syz::AngularPannedSource>().unwrap() {
assert!(angular_pan.azimuth >= 0. && angular_pan.azimuth <= 360.);
source
.azimuth()
@ -476,7 +423,7 @@ fn update_source_properties(
clear_source = true;
}
} else if let Some(scalar_pan) = scalar_pan {
if let Some(source) = handle.cast_to::<syz::ScalarPannedSource>().unwrap() {
if let Some(source) = source.cast_to::<syz::ScalarPannedSource>().unwrap() {
assert!(**scalar_pan >= -1. && **scalar_pan <= 1.);
source
.panning_scalar()
@ -487,59 +434,14 @@ fn update_source_properties(
}
}
if clear_source {
source.handle = None;
sound.source = None;
}
}
}
}
fn update_sound_properties(mut query: Query<&mut Sound>) {
for mut sound in &mut query {
let Sound {
gain,
pitch,
looping,
..
} = *sound;
assert!(gain >= 0.);
assert!(pitch >= 0.);
if sound.restart {
if let Some(generator) = sound.generator.as_mut() {
generator
.playback_position()
.set(0.)
.expect("Failed to restart");
}
sound.restart = false;
}
if let Some(generator) = sound.generator.as_mut() {
generator.gain().set(gain).expect("Failed to set gain");
generator
.pitch_bend()
.set(pitch)
.expect("Failed to set pitch");
generator
.looping()
.set(looping)
.expect("Failed to set looping");
}
}
}
fn update_source_playback_state(query: Query<&Source>) {
for source in &query {
if let Some(handle) = &source.handle {
if source.paused {
handle.pause().expect("Failed to pause");
} else {
handle.play().expect("Failed to play");
}
}
}
}
fn update_sound_playback_state(query: Query<&Sound>) {
for sound in &query {
pub fn update_playback_state(query: Query<&Sound>) {
for sound in query.iter() {
if let Some(generator) = &sound.generator {
if sound.paused {
generator.pause().expect("Failed to pause");
@ -550,13 +452,13 @@ fn update_sound_playback_state(query: Query<&Sound>) {
}
}
fn remove_sound(mut last_buffer: ResMut<LastBuffer>, removed: RemovedComponents<Source>) {
fn remove_sound(mut last_buffer: ResMut<LastBuffer>, removed: RemovedComponents<Sound>) {
for entity in removed.iter() {
last_buffer.remove(&entity);
}
}
#[derive(Clone, Default, Debug)]
#[derive(Clone, Copy, Default, Debug)]
pub struct SynthizerConfig {
pub default_panner_strategy: Option<syz::PannerStrategy>,
pub default_distance_model: Option<syz::DistanceModel>,
@ -565,8 +467,6 @@ pub struct SynthizerConfig {
pub default_rolloff: Option<f64>,
pub default_closeness_boost: Option<f64>,
pub default_closeness_boost_distance: Option<f64>,
pub log_level: syz::LogLevel,
pub log_to_stderr: bool,
}
#[derive(Debug)]
@ -630,8 +530,6 @@ fn sync_config(
.unwrap_or(defaults.closeness_boost_distance),
)
.expect("Failed to set closeness_boost_distance");
// syz::log_set_level(config.log_level);
// syz::log_to_stderr(config.log_to_stderr);
}
}
@ -642,7 +540,7 @@ fn events(
) {
context.get_events().for_each(|event| {
if let Ok(event) = event {
for (entity, sound) in &sounds {
for (entity, sound) in sounds.iter() {
if let Some(generator) = &sound.generator {
if *generator.handle() == event.source {
match event.r#type {
@ -662,29 +560,15 @@ fn events(
});
}
#[derive(SystemLabel, Clone, Hash, Debug, PartialEq, Eq)]
pub enum SynthizerSystems {
UpdateHandles,
UpdateProperties,
UpdateState,
}
pub struct SynthizerPlugin;
impl Plugin for SynthizerPlugin {
fn build(&self, app: &mut App) {
let guard = syz::initialize().expect("Failed to initialize Synthizer");
let context = syz::Context::new().expect("Failed to create Synthizer context");
if !app.world.contains_resource::<SynthizerConfig>() {
app.insert_resource(SynthizerConfig::default());
}
let config = app
.world
.get_resource::<SynthizerConfig>()
.unwrap()
.clone();
// syz::log_set_level(config.log_level);
// syz::log_to_stderr(config.log_to_stderr);
let guard = syz::initialize().expect("Failed to initialize Synthizer");
let context = syz::Context::new().expect("Failed to create Synthizer context");
let defaults = SynthizerDefaults {
panner_strategy: context.default_panner_strategy().get().unwrap(),
distance_model: context.default_distance_model().get().unwrap(),
@ -706,59 +590,26 @@ impl Plugin for SynthizerPlugin {
.add_system_to_stage(CoreStage::PreUpdate, sync_config)
.add_system_to_stage(
CoreStage::PostUpdate,
swap_buffers.before(SynthizerSystems::UpdateHandles),
swap_buffers.before(update_sound_properties),
)
.add_system_to_stage(
CoreStage::PostUpdate,
change_panner_strategy.before(SynthizerSystems::UpdateHandles),
)
.add_system_to_stage(
CoreStage::PostUpdate,
add_source_handle
.label(SynthizerSystems::UpdateHandles)
.before(SynthizerSystems::UpdateProperties),
)
.add_system_to_stage(
CoreStage::PostUpdate,
add_generator
.label(SynthizerSystems::UpdateHandles)
.before(SynthizerSystems::UpdateProperties),
)
.add_system_to_stage(
CoreStage::PostUpdate,
add_sound_without_source
.label(SynthizerSystems::UpdateHandles)
.before(SynthizerSystems::UpdateProperties),
change_panner_strategy.before(update_sound_properties),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_listener
.label(SynthizerSystems::UpdateProperties)
.after(TransformSystem::TransformPropagate)
.before(SynthizerSystems::UpdateState),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_source_properties
.label(SynthizerSystems::UpdateProperties)
.after(TransformSystem::TransformPropagate)
.before(SynthizerSystems::UpdateState),
.before(update_playback_state),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_sound_properties
.label(SynthizerSystems::UpdateProperties)
.before(SynthizerSystems::UpdateState),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_source_playback_state.label(SynthizerSystems::UpdateState),
)
.add_system_to_stage(
CoreStage::PostUpdate,
update_sound_playback_state.label(SynthizerSystems::UpdateState),
.after(TransformSystem::TransformPropagate)
.before(update_playback_state),
)
.add_system_to_stage(CoreStage::PostUpdate, update_playback_state)
.add_system_to_stage(CoreStage::PostUpdate, remove_sound)
.add_system_to_stage(CoreStage::PreUpdate, events);
.add_system(events);
}
}