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
use crate::{file::load_file, get_context};
use std::collections::HashMap;
#[cfg(not(feature = "audio"))]
#[path = "audio/no_sound.rs"]
mod snd;
#[cfg(target_os = "android")]
#[cfg(feature = "audio")]
#[path = "audio/opensles_snd.rs"]
mod snd;
#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
#[cfg(feature = "audio")]
#[path = "audio/native_snd.rs"]
mod snd;
#[cfg(target_arch = "wasm32")]
#[cfg(feature = "audio")]
#[path = "audio/web_snd.rs"]
mod snd;
pub struct AudioContext {
native_ctx: snd::AudioContext,
sounds: HashMap<usize, snd::Sound>,
id: usize,
}
impl AudioContext {
pub fn new() -> AudioContext {
AudioContext {
native_ctx: snd::AudioContext::new(),
sounds: HashMap::new(),
id: 0,
}
}
#[cfg(target_os = "android")]
pub fn pause(&mut self) {
self.native_ctx.pause()
}
#[cfg(target_os = "android")]
pub fn resume(&mut self) {
self.native_ctx.resume()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Sound(usize);
pub async fn load_sound(path: &str) -> Result<Sound, crate::file::FileError> {
let data = load_file(path).await?;
load_sound_from_bytes(&data).await
}
pub async fn load_sound_from_bytes(data: &[u8]) -> Result<Sound, crate::file::FileError> {
let sound = load_native_snd(&data).await;
let ctx = &mut get_context().audio_context;
let id = ctx.id;
ctx.sounds.insert(id, sound);
ctx.id += 1;
Ok(Sound(id))
}
#[cfg(not(feature = "audio"))]
async fn load_native_snd(data: &[u8]) -> snd::Sound {
let ctx = &mut get_context().audio_context.native_ctx;
snd::Sound::load(ctx, &data)
}
#[cfg(feature = "audio")]
#[cfg(target_arch = "wasm32")]
async fn load_native_snd(data: &[u8]) -> snd::Sound {
snd::Sound::load(&data).await
}
#[cfg(feature = "audio")]
#[cfg(not(target_arch = "wasm32"))]
async fn load_native_snd(data: &[u8]) -> snd::Sound {
let ctx = &mut get_context().audio_context.native_ctx;
snd::Sound::load(ctx, &data)
}
pub fn play_sound_once(sound: Sound) {
let ctx = &mut get_context().audio_context;
let sound = &mut ctx.sounds.get_mut(&sound.0).unwrap();
sound.play(
&mut ctx.native_ctx,
PlaySoundParams {
looped: false,
volume: 1.0,
},
);
}
pub struct PlaySoundParams {
pub looped: bool,
pub volume: f32,
}
pub fn play_sound(sound: Sound, params: PlaySoundParams) {
let ctx = &mut get_context().audio_context;
let sound = &mut ctx.sounds.get_mut(&sound.0).unwrap();
sound.play(&mut ctx.native_ctx, params);
}
pub fn stop_sound(sound: Sound) {
let ctx = &mut get_context().audio_context;
let sound = &mut ctx.sounds.get_mut(&sound.0).unwrap();
sound.stop(&mut ctx.native_ctx);
}
pub fn set_sound_volume(sound: Sound, volume: f32) {
let ctx = &mut get_context().audio_context;
let sound = &mut ctx.sounds.get_mut(&sound.0).unwrap();
sound.set_volume(&mut ctx.native_ctx, volume)
}