-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspatial_audio_2d.rs
95 lines (85 loc) · 2.85 KB
/
spatial_audio_2d.rs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! This example illustrates how to load and play an audio file, and control where the sounds seems to come from.
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(update_positions)
.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
audio: Res<Audio>,
audio_sinks: Res<Assets<SpatialAudioSink>>,
) {
let gap = 4.0;
let music = asset_server.load("sounds/Windless Slopes.ogg");
let handle = audio_sinks.get_handle(audio.play_spatial_with_settings(
music,
PlaybackSettings::LOOP,
Transform::IDENTITY,
gap,
Vec3::ZERO,
));
commands.insert_resource(AudioController(handle));
// Putting the visual presentation in a parent with a big scale as sound attenuation happens quite fast so distance are kept small
commands
.spawn(SpatialBundle {
transform: Transform::from_scale(Vec3::splat(100.0)),
..default()
})
.with_children(|parent| {
// left ear
parent.spawn(SpriteBundle {
sprite: Sprite {
color: Color::RED,
custom_size: Some(Vec2::splat(0.2)),
..default()
},
transform: Transform::from_xyz(-gap / 2.0, 0.0, 0.0),
..default()
});
// right ear
parent.spawn(SpriteBundle {
sprite: Sprite {
color: Color::GREEN,
custom_size: Some(Vec2::splat(0.2)),
..default()
},
transform: Transform::from_xyz(gap / 2.0, 0.0, 0.0),
..default()
});
// sound emitter
parent.spawn((
SpriteBundle {
sprite: Sprite {
color: Color::BLUE,
custom_size: Some(Vec2::new(0.3, 0.3)),
..default()
},
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
},
Emitter,
));
});
// camera
commands.spawn(Camera2dBundle::default());
}
#[derive(Component)]
struct Emitter;
#[derive(Resource)]
struct AudioController(Handle<SpatialAudioSink>);
fn update_positions(
audio_sinks: Res<Assets<SpatialAudioSink>>,
music_controller: Res<AudioController>,
time: Res<Time>,
mut emitter: Query<&mut Transform, With<Emitter>>,
) {
if let Some(sink) = audio_sinks.get(&music_controller.0) {
let mut emitter_transform = emitter.single_mut();
emitter_transform.translation.x = (time.elapsed_seconds()).sin() as f32 * 5.0;
sink.set_emitter_position(emitter_transform.translation);
}
}