mittens_engine/engine/ecs/component/
audio_limiter.rs1use crate::engine::ecs::ComponentId;
2use crate::engine::ecs::component::Component;
3
4#[derive(Debug, Clone, Copy)]
5pub struct AudioLimiterComponent {
6 pub attack_ms: f32,
7 pub release_ms: f32,
8 pub threshold: f32,
9}
10
11impl AudioLimiterComponent {
12 pub fn new(attack_ms: f32, release_ms: f32, threshold: f32) -> Self {
13 Self {
14 attack_ms,
15 release_ms,
16 threshold,
17 }
18 }
19}
20
21impl Default for AudioLimiterComponent {
22 fn default() -> Self {
23 Self {
24 attack_ms: 5.0,
25 release_ms: 50.0,
26 threshold: 0.9,
27 }
28 }
29}
30
31impl Component for AudioLimiterComponent {
32 fn name(&self) -> &'static str {
33 "audio_limiter"
34 }
35
36 fn as_any(&self) -> &dyn std::any::Any {
37 self
38 }
39
40 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
41 self
42 }
43
44 fn init(&mut self, emit: &mut dyn crate::engine::ecs::SignalEmitter, component: ComponentId) {
45 emit.push_intent_now(
46 component,
47 crate::engine::ecs::IntentValue::AudioGraphDirtyImmediate {
48 component_ids: vec![component],
49 },
50 );
51 }
52
53 fn encode(&self) -> std::collections::HashMap<String, serde_json::Value> {
54 let mut map = std::collections::HashMap::new();
55 map.insert("attack_ms".to_string(), serde_json::json!(self.attack_ms));
56 map.insert("release_ms".to_string(), serde_json::json!(self.release_ms));
57 map.insert("threshold".to_string(), serde_json::json!(self.threshold));
58 map
59 }
60
61 fn decode(
62 &mut self,
63 data: &std::collections::HashMap<String, serde_json::Value>,
64 ) -> Result<(), String> {
65 if let Some(v) = data.get("attack_ms") {
66 self.attack_ms = serde_json::from_value(v.clone())
67 .map_err(|e| format!("Failed to decode attack_ms: {e}"))?;
68 }
69 if let Some(v) = data.get("release_ms") {
70 self.release_ms = serde_json::from_value(v.clone())
71 .map_err(|e| format!("Failed to decode release_ms: {e}"))?;
72 }
73 if let Some(v) = data.get("threshold") {
74 self.threshold = serde_json::from_value(v.clone())
75 .map_err(|e| format!("Failed to decode threshold: {e}"))?;
76 }
77 Ok(())
78 }
79}