Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#![allow(dead_code)]

use crate::{file::load_file, get_context, Error};
use std::sync::Arc;
use crate::{file::load_file, Error};
use std::{cell::RefCell, sync::Arc};

#[cfg(feature = "audio")]
use quad_snd::{AudioContext as QuadSndContext, Sound as QuadSndSound};
Expand Down Expand Up @@ -88,8 +88,9 @@ struct QuadSndSoundGuarded(QuadSndSound);

impl Drop for QuadSndSoundGuarded {
fn drop(&mut self) {
let ctx = &get_context().audio_context;
self.0.delete(&ctx.native_ctx);
with_audio_context(|ctx| {
self.0.delete(ctx);
});
}
}

Expand All @@ -116,10 +117,7 @@ pub async fn load_sound(path: &str) -> Result<Sound, Error> {
///
/// Attempts to automatically detect the format of the source of data.
pub async fn load_sound_from_bytes(data: &[u8]) -> Result<Sound, Error> {
let sound = {
let ctx = &mut get_context().audio_context;
QuadSndSound::load(&mut ctx.native_ctx, data)
};
let sound = with_audio_context(|ctx| QuadSndSound::load(ctx, data));

// only on wasm the sound is not ready right away
#[cfg(target_arch = "wasm32")]
Expand All @@ -130,29 +128,52 @@ pub async fn load_sound_from_bytes(data: &[u8]) -> Result<Sound, Error> {
Ok(Sound(Arc::new(QuadSndSoundGuarded(sound))))
}

thread_local! {
static AUDIO_CONTEXT: RefCell<Option<QuadSndContext>> = RefCell::new(None);
}

pub(crate) fn init_sound() {
AUDIO_CONTEXT.with_borrow_mut(|opt| *opt = Some(QuadSndContext::new()));
}

fn with_audio_context<R, F>(f: F) -> R
where
F: FnOnce(&mut QuadSndContext) -> R,
{
AUDIO_CONTEXT.with_borrow_mut(|opt| {
let ctx = opt
.as_mut()
.expect("the macroquad audiocontext is not initialized on current thread");
f(ctx)
})
}

pub fn play_sound_once(sound: &Sound) {
let ctx = &mut get_context().audio_context;

sound.0 .0.play(
&mut ctx.native_ctx,
PlaySoundParams {
looped: false,
volume: 1.0,
},
);
with_audio_context(|ctx| {
sound.0 .0.play(
ctx,
PlaySoundParams {
looped: false,
volume: 1.0,
},
);
});
}

pub fn play_sound(sound: &Sound, params: PlaySoundParams) {
let ctx = &mut get_context().audio_context;
sound.0 .0.play(&mut ctx.native_ctx, params);
with_audio_context(|ctx| {
sound.0 .0.play(ctx, params);
});
}

pub fn stop_sound(sound: &Sound) {
let ctx = &mut get_context().audio_context;
sound.0 .0.stop(&mut ctx.native_ctx);
with_audio_context(|ctx| {
sound.0 .0.stop(ctx);
});
}

pub fn set_sound_volume(sound: &Sound, volume: f32) {
let ctx = &mut get_context().audio_context;
sound.0 .0.set_volume(&mut ctx.native_ctx, volume);
with_audio_context(|ctx| {
sound.0 .0.set_volume(ctx, volume);
});
}
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
//! }
//!```

use audio::init_sound;
use miniquad::*;

use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -175,8 +176,6 @@ pub(crate) mod thread_assert {
}
}
struct Context {
audio_context: audio::AudioContext,

screen_width: f32,
screen_height: f32,

Expand Down Expand Up @@ -354,9 +353,7 @@ impl Context {
texture_batcher: texture::Batcher::new(&mut *ctx),
camera_stack: vec![],

audio_context: audio::AudioContext::new(),
coroutines_context: experimental::coroutines::CoroutinesContext::new(),

pc_assets_folder: None,

start_time: miniquad::date::now(),
Expand Down Expand Up @@ -927,6 +924,7 @@ impl Window {
draw_call_index_capacity,
);
unsafe { CONTEXT = Some(context) };
init_sound();

Box::new(Stage {
main_future: Box::pin(async {
Expand Down