Skip to main content

concinnity_asset/
audio_clip.rs

1// Baked audio-clip schema.
2
3use crate::{AssetId, PayloadLocator};
4use alloc::string::String;
5
6/// A baked audio clip: the sound an [AudioEmitter](#audioemitter) plays.
7///
8/// The build reads the `source` file (any format the engine can decode:
9/// `.ogg`, `.wav`, `.flac`, `.mp3`) and packs it into the world.
10///
11/// An `AudioClip` is inert on its own: reference it from an
12/// [AudioEmitter](#audioemitter)'s `clip` field to place the sound in the world.
13///
14/// ```rust
15/// # use concinnity_asset::AudioClip;
16/// AudioClip {
17///     source: "audio/fire_crackle.ogg".into(),
18///     ..Default::default()
19/// };
20/// ```
21#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
22#[serde(default)]
23#[derive(Default)]
24pub struct AudioClip {
25    /// Asset identity; injected via `inject_name`. Not part of `args`.
26    #[serde(skip)]
27    pub asset_id: AssetId,
28    /// Path to the source audio file.
29    pub source: String,
30    /// Injected at load time from the compiled blob payload.
31    #[serde(skip)]
32    pub locator: Option<PayloadLocator>,
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn a_blank_clip_names_no_source() {
41        let c = AudioClip::default();
42        assert!(c.source.is_empty());
43        assert_eq!(c.asset_id, AssetId::default());
44        assert!(c.locator.is_none());
45    }
46
47    #[test]
48    fn the_source_path_is_the_only_authored_field() {
49        let c: AudioClip = serde_json::from_str(r#"{"source":"audio/theme.wav"}"#).unwrap();
50        assert_eq!(c.source, "audio/theme.wav");
51        // Identity and payload location are injected, so neither is authorable
52        // nor carried on the wire.
53        assert_eq!(
54            serde_json::to_string(&c).unwrap(),
55            r#"{"source":"audio/theme.wav"}"#
56        );
57
58        let bytes = postcard::to_allocvec(&c).unwrap();
59        let back: AudioClip = postcard::from_bytes(&bytes).unwrap();
60        assert_eq!(back.source, "audio/theme.wav");
61        assert_eq!(back.asset_id, AssetId::default());
62        assert!(back.locator.is_none());
63    }
64}