Repository navigation
Expand file tree
/
Copy pathreverb_channel.rs
More file actions
94 lines (83 loc) · 2.37 KB
/
Copy pathreverb_channel.rs
File metadata and controls
94 lines (83 loc) · 2.37 KB
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
use bevy::prelude::*;
use bevy_kira_audio::prelude::*;
use std::time::Duration;
fn main() {
let mut track = AudioTrack::new();
let reverb = track.add_effect(
ReverbBuilder::new()
.feedback(0.85)
.damping(0.2)
.mix(0.5_f32),
);
App::new()
.add_plugins((DefaultPlugins, AudioPlugin))
.add_audio_channel_with_track::<ReverbChannel>(track)
.insert_resource(Reverb {
handle: reverb,
on: true,
})
.add_systems(Startup, (start_playback, setup_text))
.add_systems(Update, play_next_sound)
.run();
}
fn start_playback(
channel: Res<AudioChannel<ReverbChannel>>,
asset_server: Res<AssetServer>,
mut commands: Commands,
) {
// Start with a looped sound immediately
channel.play(asset_server.load("sounds/loop.ogg")).looped();
// Queue the one-shot sounds to play in sequence
commands.insert_resource(SoundQueue {
sounds: vec![
asset_server.load("sounds/cooking.ogg"),
asset_server.load("sounds/plop.ogg"),
asset_server.load("sounds/sound.ogg"),
],
index: 0,
timer: Timer::new(Duration::from_secs(3), TimerMode::Repeating),
});
}
fn setup_text(mut commands: Commands) {
commands.spawn(Camera2d);
commands.spawn(Text::new(
"Every three seconds a new sound plays and the channel's reverb is switched on or off",
));
}
fn play_next_sound(
time: Res<Time>,
channel: Res<AudioChannel<ReverbChannel>>,
mut queue: ResMut<SoundQueue>,
mut reverb: ResMut<Reverb>,
) {
queue.timer.tick(time.delta());
if !queue.timer.just_finished() {
return;
}
reverb.on = !reverb.on;
let mix = if reverb.on { 0.5_f32 } else { 0.0 };
reverb
.handle
.set_mix(mix, AudioTween::linear(Duration::from_millis(200)));
let sound = &queue.sounds[queue.index % queue.sounds.len()];
channel.play(sound.clone());
info!(
"Playing sound {} with reverb {}",
queue.index % queue.sounds.len(),
if reverb.on { "on" } else { "off" }
);
queue.index += 1;
}
#[derive(Resource)]
struct Reverb {
handle: ReverbHandle,
on: bool,
}
#[derive(Resource)]
struct ReverbChannel;
#[derive(Resource)]
struct SoundQueue {
sounds: Vec<bevy::asset::Handle<AudioSource>>,
index: usize,
timer: Timer,
}