Various tweaks to get example working.

This commit is contained in:
Nolan Darilek 2022-05-18 15:39:37 -05:00
parent fd2b29a7a2
commit eab98cca4e
3 changed files with 22 additions and 18 deletions

View File

@ -10,4 +10,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
bevy = { version = "0.7", default-features = false } bevy = { version = "0.7", default-features = false }
synthizer = "0.4" synthizer = "0.4"
[dev-dependencies]
bevy = { version = "0.7", default-features = true }

View File

@ -43,7 +43,7 @@ fn load_and_create(
let buffer = asset_server.get_handle(handle); let buffer = asset_server.get_handle(handle);
commands commands
.spawn() .spawn()
.insert(Transform::from_translation(Vec3::new(15., 0., 0.))) .insert(Transform::from_translation(Vec3::new(45., 0., 0.)))
.insert(Sound { .insert(Sound {
buffer, buffer,
looping: true, looping: true,
@ -55,15 +55,17 @@ 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());
transform.rotation = Quat::from_rotation_z(f32::consts::PI * 2. * timer.percent()); let angle = f32::consts::PI * 2. * timer.percent();
transform.rotation = Quat::from_rotation_z(angle);
} }
} }
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_system(bevy::input::system::exit_on_esc_system) .insert_resource(SynthizerConfig { hrtf: true })
.add_plugin(SynthizerPlugin) .add_plugin(SynthizerPlugin)
.add_system(bevy::input::system::exit_on_esc_system)
.init_resource::<AssetHandles>() .init_resource::<AssetHandles>()
.add_startup_system(setup) .add_startup_system(setup)
.add_system(load_and_create) .add_system(load_and_create)

View File

@ -20,25 +20,18 @@ struct BufferAssetLoader;
impl AssetLoader for BufferAssetLoader { impl AssetLoader for BufferAssetLoader {
fn load<'a>( fn load<'a>(
&'a self, &'a self,
_bytes: &'a [u8], bytes: &'a [u8],
load_context: &'a mut LoadContext, load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<(), anyhow::Error>> { ) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
Box::pin(async move { Box::pin(async move {
let buffer: Option<Buffer> = let buffer: Option<Buffer> =
match load_context.path().extension().unwrap().to_str().unwrap() { match load_context.path().extension().unwrap().to_str().unwrap() {
"flac" | "mp3" | "wav" => { "flac" | "mp3" | "wav" => {
if let Ok(path) = load_context.path().canonicalize() { syz::Buffer::from_encoded_data(bytes).map(Buffer).ok()
println!("Supported asset: {:?}", path);
syz::Buffer::from_file(path).map(Buffer).ok()
} else {
println!("Couldn't");
None
}
} }
_ => None, _ => None,
}; };
if let Some(buffer) = buffer { if let Some(buffer) = buffer {
println!("Got a buffer");
load_context.set_default_asset(LoadedAsset::new(buffer)); load_context.set_default_asset(LoadedAsset::new(buffer));
} }
Ok(()) Ok(())
@ -50,7 +43,7 @@ impl AssetLoader for BufferAssetLoader {
} }
} }
#[derive(Component, Clone, Reflect)] #[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)] #[reflect(Component)]
pub struct Sound { pub struct Sound {
pub buffer: Handle<Buffer>, pub buffer: Handle<Buffer>,
@ -69,7 +62,7 @@ impl Default for Sound {
fn default() -> Self { fn default() -> Self {
Self { Self {
buffer: Default::default(), buffer: Default::default(),
gain: 0., gain: 1.,
pitch: 1., pitch: 1.,
looping: false, looping: false,
paused: false, paused: false,
@ -191,7 +184,6 @@ pub fn update_sound_properties(
global_transform, global_transform,
) in query.iter_mut() ) in query.iter_mut()
{ {
let has_transform = transform.is_some();
let Sound { let Sound {
gain, gain,
pitch, pitch,
@ -212,11 +204,18 @@ pub fn update_sound_properties(
let generator = let generator =
syz::BufferGenerator::new(&context).expect("Failed to create generator"); syz::BufferGenerator::new(&context).expect("Failed to create generator");
generator.buffer().set(&**b).expect("Unable to set buffer"); generator.buffer().set(&**b).expect("Unable to set buffer");
if has_transform { let translation = global_transform
.map(|v| v.translation)
.or_else(|| transform.map(|v| v.translation));
if let Some(translation) = translation {
let source = syz::Source3D::new( let source = syz::Source3D::new(
&context, &context,
syz::PannerStrategy::Delegate, syz::PannerStrategy::Delegate,
(0.0, 0.0, 0.0), (
translation.x as f64,
translation.y as f64,
translation.z as f64,
),
) )
.expect("Failed to create source"); .expect("Failed to create source");
source source