copper_rs/afx/
audio_source.rs

1
2use wasm_bindgen::prelude::*;
3use crate::afx::Audio;
4
5#[wasm_bindgen]
6extern {
7    fn copperCreateAudioSource() -> usize;
8
9    fn copperAudioSourcePlay(pointer: usize, src_pointer: usize);
10    fn copperAudioSourceStop(pointer: usize);
11    fn copperAudioSourceRepeat(pointer: usize, repeat: bool);
12    fn copperAudioSourcePlaying(pointer: usize) -> bool;
13    fn copperAudioSourceSetPosition(pointer: usize, x: f32, y: f32, z: f32);
14    fn copperAudioSourceSetVolume(pointer: usize, volume: f32);
15    fn copperAudioSourceSetPitch(pointer: usize, pitch: f32);
16
17    fn copperDropAudioSource(pointer: usize);
18}
19
20pub struct AudioSource {
21    pointer: usize
22}
23
24impl AudioSource {
25    pub fn new() -> AudioSource { AudioSource { pointer: copperCreateAudioSource() } }
26
27    pub fn play(&self, audio: &Audio) { copperAudioSourcePlay(self.pointer, audio.pointer); }
28    pub fn stop(&self) { copperAudioSourceStop(self.pointer); }
29    pub fn repeat(&self, repeat: bool) { copperAudioSourceRepeat(self.pointer, repeat); }
30    pub fn playing(&self) -> bool { copperAudioSourcePlaying(self.pointer) }
31    pub fn set_position(&self, x: f32, y: f32, z: f32) { copperAudioSourceSetPosition(self.pointer, x, y, z); }
32    pub fn set_volume(&self, volume: f32) { copperAudioSourceSetVolume(self.pointer, volume); }
33    pub fn set_pitch(&self, pitch: f32) { copperAudioSourceSetPitch(self.pointer, pitch); }
34}
35
36impl Drop for AudioSource {
37    fn drop(&mut self) {
38        copperDropAudioSource(self.pointer);
39    }
40}