use { bevy::{ prelude::{ default, shape, App, Assets, Camera3dBundle, Color, Commands, Component, Deref, Handle, Mesh, PbrBundle, PointLight, PointLightBundle, PostStartup, Query, Res, ResMut, Resource, StandardMaterial, Startup, Transform, Update, Vec3, With, }, time::Time, DefaultPlugins, }, bevy_oddio::{ builtins::sine::{self, Sine}, output::spatial::SpatialAudioSink, Audio, AudioPlugin, }, oddio::{Sample, Spatial, SpatialOptions}, }; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(AudioPlugin::new()) .add_systems(Startup, init_assets) .add_systems(PostStartup, setup) .add_systems(Update, change_velocity) .run(); } #[derive(Component)] struct Emitter; #[derive(Resource, Deref)] struct SineHandle(Handle); #[derive(Resource)] struct SineSink(Handle>); fn init_assets(mut commands: Commands, mut assets: ResMut>) { let handle = assets.add(Sine); commands.insert_resource(SineHandle(handle)); } fn setup( mut commands: Commands, mut audio: ResMut>, noise: Res, mut meshes: ResMut>, mut materials: ResMut>, ) { // Note is in A4. let handle = audio.play_spatial( noise.clone(), sine::Settings::new(0.0, 440.0), SpatialOptions { position: (Vec3::Y * 0.4).into(), velocity: Vec3::ZERO.into(), radius: 0.5, }, ); commands.insert_resource(SineSink(handle)); // Listener commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::UVSphere { radius: 0.2, ..default() })), material: materials.add(Color::GREEN.into()), transform: Transform::from_xyz(0.0, 0.0, 0.0), ..default() }); // Emitter commands .spawn(PbrBundle { mesh: meshes.add(Mesh::from(shape::UVSphere { radius: 0.2, ..default() })), material: materials.add(Color::BLUE.into()), transform: Transform::from_xyz(0.0, 0.0, 0.0), ..default() }) .insert(Emitter); // light commands.spawn(PointLightBundle { point_light: PointLight { intensity: 1500.0, shadows_enabled: true, ..default() }, transform: Transform::from_xyz(4.0, 8.0, 4.0), ..default() }); commands.spawn(Camera3dBundle { transform: Transform::from_xyz(0.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), ..default() }); } fn change_velocity( sink: Res, mut sinks: ResMut>>, time: Res