summaryrefslogtreecommitdiff
path: root/src/audio.rs
blob: 5cbce90c321968a6bc7aa48d1bdb9eddb86a56dd (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::{cell::RefCell, collections::HashMap};

#[cfg(target_arch = "wasm32")]
pub struct Context {
    pub audio: web_sys::AudioContext,
}

#[cfg(target_arch = "wasm32")]
impl Context {
    pub fn new() -> Self {
        let audio = web_sys::AudioContext::new()
            .expect("failed to create audio context");
        Self {
            audio,
        }
    }
}

#[cfg(target_arch = "wasm32")]
pub struct Audio {
    pub buffer: &'static RefCell<Option<web_sys::AudioBuffer>>,
    //pub source: &'static web_sys::AudioBufferSourceNode,
}

#[cfg(target_arch = "wasm32")]
impl Audio {
    pub fn new(ctx: &Context, bytes: &[u8]) -> Self {
        let sbuffer: &_ = Box::leak(Box::new(RefCell::new(None)));
        let sclone: &'static RefCell<Option<web_sys::AudioBuffer>> =
            <&_>::clone(&sbuffer);
        let ret = Audio {
            buffer: sclone,
        };
        let jsp = ctx.audio.decode_audio_data(&js_sys::Uint8Array::from(bytes).buffer()).expect("failed to decode audio");
        let promise = wasm_bindgen_futures::JsFuture::from(jsp);
        wasm_bindgen_futures::spawn_local(async {
            if let Some(data) = promise.await.ok() {
                *sbuffer.borrow_mut() = Some(web_sys::AudioBuffer::from(data));
            }
            ()
        });
        ret
    }

    pub fn play(&self, ctx: &Context, looping: Option<(Option<f64>, Option<f64>)>) -> Option<web_sys::AudioBufferSourceNode> {
        let source = ctx.audio.create_buffer_source().ok()?;
        source.set_buffer((&*self.buffer.borrow()).as_ref());
        if let Some((ms, me)) = looping {
            source.set_loop(true);
            if let Some(s) = ms { source.set_loop_start(s) }
            if let Some(e) = me { source.set_loop_end(e) }
        }
        source.connect_with_audio_node(&ctx.audio.destination()).ok()?;
        source.start().ok()?;
        Some(source)
    }
}

#[cfg(target_arch = "wasm32")]
pub struct Assets {
    pub ctx: Context,

    pub audio: HashMap<String, Audio>,

    pub music_node: Option<web_sys::AudioBufferSourceNode>, 
}

#[cfg(target_arch = "wasm32")]
impl Assets {
    pub fn new<F>(f : F) -> Self where F: Fn(&Context) -> HashMap<String, Audio> {
        let ctx = Context::new();

        let audio = f(&ctx);

        Self {
            ctx,
            audio,
            music_node: None,
        }
    }

    pub fn play_sfx(&mut self, name: &str) {
        if let Some(a) = self.audio.get(name) {
            a.play(&self.ctx, None);
        }
    }

    pub fn is_music_playing(&self) -> bool {
        if let Some(ms) = &self.music_node {
            ms.buffer().is_some()
        } else { false }
    }

    pub fn play_music(&mut self, name: &str, start: Option<f64>, end: Option<f64>) {
        if let Some(s) = &self.music_node {
            let _ = s.stop();
        }
        if let Some(a) = self.audio.get(name) {
            self.music_node = a.play(&self.ctx, Some((start, end)));
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub struct Context {
}

#[cfg(not(target_arch = "wasm32"))]
impl Context {
    pub fn new() -> Self {
        Self {
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub struct Audio {
}

#[cfg(not(target_arch = "wasm32"))]
impl Audio {
    pub fn new(ctx: &Context, _bytes: &[u8]) -> Self {
        Self {
        }
    }

    pub fn play(&self, ctx: &Context, looping: Option<(Option<f64>, Option<f64>)>) {
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub struct Assets {
    pub ctx: Context,
    pub audio: HashMap<String, Audio>,
}

#[cfg(not(target_arch = "wasm32"))]
impl Assets {
    pub fn new<F>(f : F) -> Self where F: Fn(&Context) -> HashMap<String, Audio> {
        let ctx = Context::new();

        let audio = f(&ctx);

        Self {
            ctx,
            audio,
        }
    }

    pub fn play_sfx(&mut self, name: &str) {
        if let Some(a) = self.audio.get(name) {
            a.play(&self.ctx, None);
        }
    }

    pub fn is_music_playing(&self) -> bool {
        false
    }

    pub fn play_music(&mut self, name: &str, start: Option<f64>, end: Option<f64>) {
    }
}