1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! This example demonstrates the interaural time difference (ITD) node.
//!
//! ITD is generally used in concert with other effects, such as volume panning,
//! since it's rather subtle on its own. We demonstrate such a combination here.
//! You should notice a little more "depth" to the directionality of the sound,
//! but you'll still be unable to distinguish front from back.
//!
//! Unlike an HRTF, simulating ITD is very cheap.
use bevy::{app::ScheduleRunnerPlugin, log::LogPlugin, prelude::*};
use bevy_seedling::{nodes::itd::ItdNode, prelude::*};
use std::time::Duration;
fn main() {
App::new()
.add_plugins((
// Without a window, the event loop tends to run quite fast.
// We'll slow it down so we don't drop any audio events.
MinimalPlugins.set(ScheduleRunnerPlugin::run_loop(Duration::from_millis(16))),
LogPlugin::default(),
AssetPlugin::default(),
TransformPlugin,
SeedlingPlugin::default(),
))
.add_systems(Startup, startup)
.add_systems(Update, spinner)
.run();
}
fn startup(server: Res<AssetServer>, mut commands: Commands) {
// Here we spawn a sample player with a spatial effect,
// making sure our sample player entity has a transform.
//
// This emitter will slowly circle the listener clockwise.
commands.spawn((
Spinner(0.0),
SamplePlayer::new(server.load("divine_comedy.ogg")).looping(),
Transform::default(),
sample_effects![SpatialBasicNode::default(), ItdNode::default()],
));
// Then, we'll spawn a simple listener.
//
// `Transform` is a required component of `SpatialListener2D`, so we
// don't have to explicitly insert one.
commands.spawn(SpatialListener2D);
}
#[derive(Component)]
struct Spinner(f32);
fn spinner(mut spinners: Query<(&mut Spinner, &mut Transform), With<Spinner>>, time: Res<Time>) {
for (mut spinner, mut transform) in spinners.iter_mut() {
let spin_radius = 2.0;
let spin_seconds = 15.0;
let position =
Vec2::new(spinner.0.cos() * spin_radius, spinner.0.sin() * spin_radius).extend(0.0);
transform.translation = position;
spinner.0 += core::f32::consts::TAU * time.delta().as_secs_f32() / spin_seconds;
}
}