Skip to main content

rustriff_lib/domain/dto/effect/
effect_dto.rs

1use crate::domain::dto::effect::cabinet_dto::CabinetDto;
2use crate::domain::dto::effect::delay_dto::DelayDto;
3use crate::domain::dto::effect::hcdistortion_dto::HcDistortionDto;
4use crate::domain::dto::effect::scdistortion_dto::ScDistortionDto;
5use crate::domain::effect::Effect;
6use crate::services::effects::cabinet::cabinet::Cabinet;
7use crate::services::effects::delay::delay::Delay;
8use crate::services::effects::distortion::hc_distortion::HCDistortion;
9use crate::services::effects::distortion::sc_distortion::SCDistortion;
10use serde::{Deserialize, Serialize};
11
12/// A serialisable, tagged representation of any effect in the signal chain.
13///
14/// Uses serde's adjacently-tagged format so that both the Rust serialisation
15/// and the TypeScript typegen agree on the wire shape:
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17#[serde(tag = "kind", content = "data")]
18pub enum EffectDto {
19    /// Hard-clipping distortion effect.
20    HCDistortion(HcDistortionDto),
21    /// Soft-clipping distortion effect.
22    SCDistortion(ScDistortionDto),
23    /// Delay effect.
24    Delay(DelayDto),
25    /// Placeholder impulse-response cabinet effect.
26    Cabinet(CabinetDto),
27}
28
29impl EffectDto {
30    pub fn add_to_domain(self, next_effect_id: u32, dsp_sample_rate: u32) -> Box<dyn Effect> {
31        match self {
32            EffectDto::HCDistortion(dto) => Box::new(HCDistortion::new(
33                next_effect_id,
34                dto.name,
35                dto.is_active,
36                dto.threshold,
37                dto.level,
38                dto.color,
39            )),
40            EffectDto::SCDistortion(dto) => Box::new(SCDistortion::new(
41                next_effect_id,
42                dto.name,
43                dto.is_active,
44                dto.threshold,
45                dto.level,
46                dto.smoothing,
47                dto.color,
48            )),
49            EffectDto::Cabinet(dto) => Box::new(Cabinet::new(
50                next_effect_id,
51                dto.name,
52                dto.is_active,
53                dto.color,
54                dto.ir_file_path,
55                dsp_sample_rate,
56            )),
57            EffectDto::Delay(dto) => Box::new(Delay::new(
58                next_effect_id,
59                dto.name,
60                dto.is_active,
61                dto.color,
62                dsp_sample_rate,
63                dto.delay_time,
64                dto.level,
65            )),
66        }
67    }
68
69    pub fn to_domain(self, dsp_sample_rate: u32) -> Box<dyn Effect> {
70        match self {
71            EffectDto::HCDistortion(dto) => Box::new(HCDistortion::new(
72                dto.id,
73                dto.name,
74                dto.is_active,
75                dto.threshold,
76                dto.level,
77                dto.color,
78            )),
79            EffectDto::SCDistortion(dto) => Box::new(SCDistortion::new(
80                dto.id,
81                dto.name,
82                dto.is_active,
83                dto.threshold,
84                dto.level,
85                dto.smoothing,
86                dto.color,
87            )),
88            EffectDto::Cabinet(dto) => Box::new(Cabinet::new(
89                dto.id,
90                dto.name,
91                dto.is_active,
92                dto.color,
93                dto.ir_file_path,
94                dsp_sample_rate,
95            )),
96            EffectDto::Delay(dto) => Box::new(Delay::new(
97                dto.id,
98                dto.name,
99                dto.is_active,
100                dto.color,
101                dsp_sample_rate,
102                dto.delay_time,
103                dto.level,
104            )),
105        }
106    }
107}