Skip to main content

euv_engine/audio/
struct.rs

1use super::*;
2
3/// A wrapper around the Web Audio API `AudioContext` providing
4/// game engine audio playback and volume management.
5#[derive(Clone, Data, New)]
6pub struct GameAudioContext {
7    /// The underlying Web Audio API context.
8    #[set(pub(crate))]
9    #[get_mut(pub(crate))]
10    #[get(pub(crate))]
11    pub(crate) context: AudioContext,
12    /// The master gain node controlling overall volume.
13    ///
14    /// The gain node's own `AudioParam` is the single source of truth for the
15    /// master level. There is deliberately no `master_volume` field beside it:
16    /// a second copy of the level could drift from the node it mirrors, and
17    /// nothing in the engine ever read it. Volume changes go through
18    /// [`GameAudioContext::apply_master_volume`].
19    #[set(pub(crate))]
20    #[get(pub(crate))]
21    #[get_mut(pub(crate))]
22    pub(crate) master_gain: GainNode,
23}
24
25/// An audio clip wrapping a decoded `AudioBuffer` with playback control.
26#[derive(Clone, Data, New)]
27pub struct AudioClip {
28    /// The decoded audio data buffer.
29    #[set(pub(crate))]
30    #[get(pub(crate))]
31    #[get_mut(pub(crate))]
32    pub(crate) buffer: AudioBuffer,
33    /// The current playback state.
34    ///
35    /// `get_state` is public because tests/audio_clip/fn.rs reads this field from
36    /// outside this crate. `get_mut` / `set` stay crate-internal.
37    #[get_mut(pub(crate))]
38    #[get(type(copy))]
39    #[set(pub(crate))]
40    pub state: AudioPlayState,
41    /// Whether the clip should loop when it reaches the end.
42    ///
43    /// `get_looping` is public because tests/audio_clip/fn.rs reads this field from
44    /// outside this crate. `get_mut` / `set` stay crate-internal.
45    #[get_mut(pub(crate))]
46    #[get(type(copy))]
47    #[set(pub(crate))]
48    pub looping: bool,
49    /// The volume level in the range 0.0 to 1.0.
50    ///
51    /// `get_volume` is public because tests/audio_clip/fn.rs reads this field from
52    /// outside this crate. `get_mut` / `set` stay crate-internal.
53    #[get_mut(pub(crate))]
54    #[get(type(copy))]
55    #[set(pub(crate))]
56    pub volume: f64,
57    /// The playback rate multiplier (1.0 = normal speed).
58    ///
59    /// `get_playback_rate` is public because tests/audio_clip/fn.rs reads this field from
60    /// outside this crate. `get_mut` / `set` stay crate-internal.
61    #[get_mut(pub(crate))]
62    #[get(type(copy))]
63    #[set(pub(crate))]
64    pub playback_rate: f64,
65    /// The currently active source node, if the clip is playing.
66    #[get_mut(pub(crate))]
67    #[set(pub(crate))]
68    #[new(skip)]
69    #[get(pub(crate))]
70    pub(crate) source_node: Option<AudioBufferSourceNode>,
71}