-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathaudio_basic.rs
133 lines (115 loc) · 3.37 KB
/
audio_basic.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use notan::egui::{self, *};
use notan::prelude::*;
#[derive(AppState)]
struct State {
music: [AudioSource; 2],
sound: [Option<Sound>; 2],
repeat: [bool; 2],
volume: [f32; 2],
}
fn play_music(index: usize, app: &mut App, state: &mut State) {
let sound = app.audio.play_sound(
&state.music[index],
state.volume[index],
state.repeat[index],
);
state.sound[index] = Some(sound);
}
fn stop_music(index: usize, app: &mut App, state: &mut State) {
if let Some(s) = state.sound[index].take() {
app.audio.stop(&s);
}
}
fn is_playing(index: usize, app: &mut App, state: &State) -> bool {
match &state.sound[index] {
Some(s) => !app.audio.is_stopped(s),
None => false,
}
}
fn is_paused(index: usize, app: &mut App, state: &State) -> bool {
match &state.sound[index] {
Some(s) => app.audio.is_paused(s),
None => false,
}
}
fn toggle_music(index: usize, app: &mut App, state: &mut State) {
match &state.sound[index] {
None => {}
Some(s) => {
if app.audio.is_paused(s) {
app.audio.resume(s);
} else {
app.audio.pause(s);
}
}
}
}
fn set_volume(index: usize, app: &mut App, state: &mut State) {
match &state.sound[index] {
None => {}
Some(s) => app.audio.set_volume(s, state.volume[index]),
}
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup)
.add_config(EguiConfig)
.add_config(WindowConfig::default().set_size(300, 300))
.draw(draw)
.build()
}
fn setup(app: &mut App) -> State {
let music = app
.audio
.create_source(include_bytes!("assets/jingles_NES00.ogg"))
.unwrap();
let music1 = app
.audio
.create_source(include_bytes!("assets/jingles_PIZZI01.ogg"))
.unwrap();
State {
music: [music, music1],
sound: [None, None],
repeat: [false, false],
volume: [1.0, 1.0],
}
}
// -- UI
fn draw_controls(index: usize, name: &str, ctx: &Context, app: &mut App, state: &mut State) {
egui::Window::new(name).show(ctx, |ui| {
ui.label("Volume");
ui.add(egui::Slider::new(&mut state.volume[index], 0.0..=1.0));
ui.add(egui::Checkbox::new(&mut state.repeat[index], "Repeat"));
let is_playing = is_playing(index, app, state);
if is_playing {
let btn = ui.button("Stop");
if btn.clicked() {
stop_music(index, app, state);
}
let pause = if is_paused(index, app, state) {
"Resume"
} else {
"Pause"
};
let btn = ui.button(pause);
if btn.clicked() {
toggle_music(index, app, state);
}
} else {
let play = ui.button("Play");
if play.clicked() {
play_music(index, app, state);
}
}
});
}
fn draw(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut State) {
let len = state.music.len();
let mut output = plugins.egui(|ctx| {
(0..len).for_each(|i| draw_controls(i, &format!("Music {i}"), ctx, app, state));
});
output.clear_color(Color::GRAY);
gfx.render(&output);
// set volume
(0..len).for_each(|i| set_volume(i, app, state));
}