Skip to main content

crayon_audio/
lib.rs

1#[cfg(not(target_arch = "wasm32"))]
2extern crate cpal;
3
4#[cfg(target_arch = "wasm32")]
5extern crate js_sys;
6#[cfg(target_arch = "wasm32")]
7extern crate wasm_bindgen;
8#[cfg(target_arch = "wasm32")]
9extern crate web_sys;
10
11#[macro_use]
12extern crate crayon;
13#[macro_use]
14extern crate failure;
15extern crate lewton;
16
17pub mod assets;
18pub mod source;
19
20mod mixer;
21mod system;
22
23pub mod prelude {
24    pub use assets::prelude::AudioClipHandle;
25    pub use source::{AudioSource, AudioSourceAttenuation, AudioSourceHandle, AudioSourceWrap};
26}
27
28pub use self::inside::{discard, setup};
29
30use crayon::errors::Result;
31use crayon::math::prelude::Vector3;
32use crayon::res::prelude::ResourceState;
33use crayon::uuid::Uuid;
34
35use self::assets::prelude::AudioClipHandle;
36use self::inside::ctx;
37use self::source::{AudioSource, AudioSourceHandle};
38
39/// Sets the position of listener.
40#[inline]
41pub fn set_listener<T>(position: T)
42where
43    T: Into<Vector3<f32>>,
44{
45    ctx().set_listener(position);
46}
47
48/// Creates a clip object from file asynchronously.
49#[inline]
50pub fn create_clip_from<T: AsRef<str>>(url: T) -> Result<AudioClipHandle> {
51    ctx().create_clip_from(url)
52}
53
54/// Creates a clip object from file asynchronously.
55#[inline]
56pub fn create_clip_from_uuid(uuid: Uuid) -> Result<AudioClipHandle> {
57    ctx().create_clip_from_uuid(uuid)
58}
59
60#[inline]
61pub fn clip_state(handle: AudioClipHandle) -> ResourceState {
62    ctx().clip_state(handle)
63}
64
65/// Deletes a `AudioClip` resource from `AudioSystem`.
66#[inline]
67pub fn delete_clip(handle: AudioClipHandle) {
68    ctx().delete_clip(handle);
69}
70
71/// Plays a audio source, returning a `AudioSourceHandle` for it.
72#[inline]
73pub fn play<T>(params: T) -> Result<AudioSourceHandle>
74where
75    T: Into<AudioSource>,
76{
77    ctx().play(params)
78}
79
80/// Stops a played audio source.
81#[inline]
82pub fn stop(handle: AudioSourceHandle) {
83    ctx().stop(handle)
84}
85
86/// Sets the emiiter position of playing sound.
87#[inline]
88pub fn set_position<T>(handle: AudioSourceHandle, position: T)
89where
90    T: Into<Vector3<f32>>,
91{
92    ctx().set_position(handle, position)
93}
94
95/// Sets the volume of a playing sound.
96#[inline]
97pub fn set_volume(handle: AudioSourceHandle, volume: f32) {
98    ctx().set_volume(handle, volume);
99}
100
101/// Sets the frequency-shift of a playing sound.
102#[inline]
103pub fn set_pitch(handle: AudioSourceHandle, pitch: f32) {
104    ctx().set_pitch(handle, pitch);
105}
106
107mod inside {
108    use super::system::AudioSystem;
109
110    static mut CTX: *const AudioSystem = std::ptr::null();
111
112    #[inline]
113    pub fn ctx() -> &'static AudioSystem {
114        unsafe {
115            debug_assert!(
116                !CTX.is_null(),
117                "audio system has not been initialized properly."
118            );
119
120            &*CTX
121        }
122    }
123
124    /// Setup the world system.
125    pub fn setup() -> Result<(), failure::Error> {
126        unsafe {
127            debug_assert!(CTX.is_null(), "duplicated setup of audio system.");
128
129            let ctx = AudioSystem::new()?;
130            CTX = Box::into_raw(Box::new(ctx));
131            Ok(())
132        }
133    }
134
135    /// Discard the world system.
136    pub fn discard() {
137        unsafe {
138            if CTX.is_null() {
139                return;
140            }
141
142            drop(Box::from_raw(CTX as *mut AudioSystem));
143            CTX = std::ptr::null();
144        }
145    }
146}