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