Skip to main content

mireforge_game_audio/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/mireforge/mireforge
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use limnus_assets::Assets;
6use limnus_audio_mixer::{AudioMixer, StereoSample, StereoSampleRef};
7use tracing::debug;
8
9pub type SoundHandle = u16;
10
11pub trait Audio {
12    fn play(&mut self, audio: &StereoSampleRef) -> SoundHandle;
13}
14
15// We will only borrow these resources for a single function call
16pub struct GameAudio<'a> {
17    pub mixer: &'a mut AudioMixer,
18    pub stereo_samples: &'a Assets<StereoSample>,
19}
20
21impl<'a> GameAudio<'a> {
22    pub const fn new(mixer: &'a mut AudioMixer, stereo_samples: &'a Assets<StereoSample>) -> Self {
23        Self {
24            mixer,
25            stereo_samples,
26        }
27    }
28}
29
30impl Audio for GameAudio<'_> {
31    fn play(&mut self, sample_id: &StereoSampleRef) -> SoundHandle {
32        debug!(sample_id=%sample_id, "playing sample");
33        let stereo_sample = self.stereo_samples.fetch(sample_id);
34        self.mixer.play(stereo_sample);
35
36        // TODO: fix this
37        53
38    }
39}