Add more context configuration parameters to configuration, and sync with context on change.

This commit is contained in:
Nolan Darilek 2022-05-19 10:18:41 -05:00
parent ca96a99777
commit c945c8b1ba
2 changed files with 78 additions and 13 deletions

View File

@ -64,7 +64,8 @@ fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.insert_resource(SynthizerConfig { .insert_resource(SynthizerConfig {
default_panner_strategy: bevy_synthizer::syz::PannerStrategy::Hrtf, default_panner_strategy: Some(bevy_synthizer::syz::PannerStrategy::Hrtf),
..default()
}) })
.add_plugin(SynthizerPlugin) .add_plugin(SynthizerPlugin)
.add_system(bevy::input::system::exit_on_esc_system) .add_system(bevy::input::system::exit_on_esc_system)

View File

@ -380,16 +380,74 @@ fn remove_sound(mut last_buffer: ResMut<LastBuffer>, removed: RemovedComponents<
} }
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Default, Debug)]
pub struct SynthizerConfig { pub struct SynthizerConfig {
pub default_panner_strategy: syz::PannerStrategy, pub default_panner_strategy: Option<syz::PannerStrategy>,
pub default_distance_model: Option<syz::DistanceModel>,
pub default_distance_ref: Option<f64>,
pub default_distance_max: Option<f64>,
pub default_rolloff: Option<f64>,
pub default_closeness_boost: Option<f64>,
pub default_closeness_boost_distance: Option<f64>,
} }
impl Default for SynthizerConfig { #[derive(Debug)]
fn default() -> Self { struct Defaults {
Self { panner_strategy: syz::PannerStrategy,
default_panner_strategy: syz::PannerStrategy::Stereo, distance_model: syz::DistanceModel,
} distance_ref: f64,
distance_max: f64,
rolloff: f64,
closeness_boost: f64,
closeness_boost_distance: f64,
}
fn sync_config(context: Res<syz::Context>, config: Res<SynthizerConfig>, defaults: Res<Defaults>) {
if config.is_changed() {
context
.default_panner_strategy()
.set(
config
.default_panner_strategy
.unwrap_or(defaults.panner_strategy),
)
.expect("Failed to set panner strategy");
context
.default_distance_modle()
.set(
config
.default_distance_model
.unwrap_or(defaults.distance_model),
)
.expect("Failed to set distance model");
context
.default_distance_ref()
.set(config.default_distance_ref.unwrap_or(defaults.distance_ref))
.expect("Failed to set distance_ref");
context
.default_distance_max()
.set(config.default_distance_max.unwrap_or(defaults.distance_max))
.expect("Failed to set distance_max");
context
.default_rolloff()
.set(config.default_rolloff.unwrap_or(defaults.rolloff))
.expect("Failed to set rolloff");
context
.default_closeness_boost()
.set(
config
.default_closeness_boost
.unwrap_or(defaults.closeness_boost),
)
.expect("Failed to set closeness_boost");
context
.default_closeness_boost_distance()
.set(
config
.default_closeness_boost_distance
.unwrap_or(defaults.closeness_boost_distance),
)
.expect("Failed to set closeness_boost_distance");
} }
} }
@ -402,17 +460,23 @@ impl Plugin for SynthizerPlugin {
if !app.world.contains_resource::<SynthizerConfig>() { if !app.world.contains_resource::<SynthizerConfig>() {
app.insert_resource(SynthizerConfig::default()); app.insert_resource(SynthizerConfig::default());
} }
let config = *app.world.get_resource::<SynthizerConfig>().unwrap(); let defaults = Defaults {
context panner_strategy: context.default_panner_strategy().get().unwrap(),
.default_panner_strategy() distance_model: context.default_distance_modle().get().unwrap(),
.set(config.default_panner_strategy) distance_ref: context.default_distance_ref().get().unwrap(),
.expect("Failed to set panner strategy"); distance_max: context.default_distance_max().get().unwrap(),
rolloff: context.default_rolloff().get().unwrap(),
closeness_boost: context.default_closeness_boost().get().unwrap(),
closeness_boost_distance: context.default_closeness_boost_distance().get().unwrap(),
};
app.add_asset::<Buffer>() app.add_asset::<Buffer>()
.init_asset_loader::<BufferAssetLoader>() .init_asset_loader::<BufferAssetLoader>()
.register_type::<Listener>() .register_type::<Listener>()
.insert_resource(guard) .insert_resource(guard)
.insert_resource(context) .insert_resource(context)
.init_resource::<LastBuffer>() .init_resource::<LastBuffer>()
.insert_resource(defaults)
.add_system_to_stage(CoreStage::PreUpdate, sync_config)
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
swap_buffers.before(update_sound_properties), swap_buffers.before(update_sound_properties),