Skip to main content

goud_engine/ecs/components/audiosource/
channel.rs

1//! Audio channel types for grouping and mixing audio sources.
2
3/// Audio channel enumeration for audio mixing and grouping.
4///
5/// Channels allow you to group audio sources together for volume control,
6/// filtering, and organization. Each audio source belongs to one channel.
7///
8/// # Built-in Channels
9///
10/// - **Music**: Background music tracks (typically looped)
11/// - **SFX**: Sound effects (footsteps, impacts, UI clicks)
12/// - **Voice**: Voice-overs, dialogue, speech
13/// - **Ambience**: Ambient environment sounds (wind, rain, room tone)
14/// - **UI**: User interface sounds (button clicks, menu navigation)
15/// - **Custom**: User-defined channels (bits 5-31)
16///
17/// # Examples
18///
19/// ```
20/// use goud_engine::ecs::components::AudioChannel;
21///
22/// let music = AudioChannel::Music;
23/// let sfx = AudioChannel::SFX;
24/// let custom = AudioChannel::Custom(8); // Custom channel ID 8
25///
26/// assert_eq!(music.id(), 0);
27/// assert_eq!(sfx.id(), 1);
28/// assert_eq!(custom.id(), 8);
29/// ```
30#[repr(u8)]
31#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
32pub enum AudioChannel {
33    /// Background music tracks (channel ID: 0)
34    Music = 0,
35    /// Sound effects (channel ID: 1)
36    SFX = 1,
37    /// Voice-overs and dialogue (channel ID: 2)
38    Voice = 2,
39    /// Ambient environment sounds (channel ID: 3)
40    Ambience = 3,
41    /// User interface sounds (channel ID: 4)
42    UI = 4,
43    /// Custom channel (ID 5-31)
44    Custom(u8),
45}
46
47impl AudioChannel {
48    /// Returns the numeric channel ID (0-31).
49    ///
50    /// # Examples
51    ///
52    /// ```
53    /// use goud_engine::ecs::components::AudioChannel;
54    ///
55    /// assert_eq!(AudioChannel::Music.id(), 0);
56    /// assert_eq!(AudioChannel::SFX.id(), 1);
57    /// assert_eq!(AudioChannel::Custom(10).id(), 10);
58    /// ```
59    pub fn id(&self) -> u8 {
60        match self {
61            AudioChannel::Music => 0,
62            AudioChannel::SFX => 1,
63            AudioChannel::Voice => 2,
64            AudioChannel::Ambience => 3,
65            AudioChannel::UI => 4,
66            AudioChannel::Custom(id) => *id,
67        }
68    }
69
70    /// Returns the channel name for debugging.
71    ///
72    /// # Examples
73    ///
74    /// ```
75    /// use goud_engine::ecs::components::AudioChannel;
76    ///
77    /// assert_eq!(AudioChannel::Music.name(), "Music");
78    /// assert_eq!(AudioChannel::SFX.name(), "SFX");
79    /// assert_eq!(AudioChannel::Custom(10).name(), "Custom(10)");
80    /// ```
81    pub fn name(&self) -> String {
82        match self {
83            AudioChannel::Music => "Music".to_string(),
84            AudioChannel::SFX => "SFX".to_string(),
85            AudioChannel::Voice => "Voice".to_string(),
86            AudioChannel::Ambience => "Ambience".to_string(),
87            AudioChannel::UI => "UI".to_string(),
88            AudioChannel::Custom(id) => format!("Custom({})", id),
89        }
90    }
91}
92
93impl Default for AudioChannel {
94    /// Returns `AudioChannel::SFX` as the default.
95    fn default() -> Self {
96        AudioChannel::SFX
97    }
98}
99
100impl std::fmt::Display for AudioChannel {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        write!(f, "{}", self.name())
103    }
104}