devalang 0.2.0

Write music with code. Devalang is a domain-specific language (DSL) for sound designers and music hackers. Compose, automate, and control sound — in plain text.
Documentation
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/// Effect chain module - sequential processing of multiple effects
use super::registry::{CloneableEffect, EffectRegistry};
use crate::engine::audio::lfo::{LfoParams, LfoRate, LfoTarget, LfoWaveform};
use crate::language::syntax::ast::Value;
use std::collections::HashMap;

/// Effect chain - processes audio through multiple effects in sequence
#[derive(Debug)]
pub struct EffectChain {
    effects: Vec<Box<dyn CloneableEffect>>,
    registry: EffectRegistry,
    synth_context: bool,
}

impl EffectChain {
    /// Create a new effect chain with context
    pub fn new(synth_context: bool) -> Self {
        Self {
            effects: Vec::new(),
            registry: EffectRegistry::new(),
            synth_context,
        }
    }

    /// Add an effect to the chain if available in current context
    pub fn add_effect(&mut self, name: &str, params: Option<Value>) -> bool {
        if !self.registry.is_effect_available(name, self.synth_context) {
            return false;
        }

        if let Some(processor) =
            build_effect_processor(&self.registry, name, params, self.synth_context)
        {
            self.effects.push(processor);
            true
        } else {
            false
        }
    }

    /// Process audio samples through all effects in the chain
    pub fn process(&mut self, samples: &mut [f32], sample_rate: u32) {
        for effect in &mut self.effects {
            effect.process(samples, sample_rate);
        }
    }

    /// Reset all effects in the chain
    pub fn reset(&mut self) {
        for effect in &mut self.effects {
            effect.reset();
        }
    }

    /// Get number of effects in the chain
    pub fn len(&self) -> usize {
        self.effects.len()
    }

    /// Check if chain is empty
    pub fn is_empty(&self) -> bool {
        self.effects.is_empty()
    }

    /// Get all available effects for the current context
    pub fn available_effects(&self) -> Vec<&'static str> {
        self.registry.list_available_effects(self.synth_context)
    }

    /// Check if an effect is available in current context
    pub fn is_effect_available(&self, name: &str) -> bool {
        self.registry.is_effect_available(name, self.synth_context)
    }
}

impl Default for EffectChain {
    fn default() -> Self {
        Self::new(true) // Default to synth context
    }
}

/// Build an effect chain from a Value::Array of effect definitions
pub fn build_effect_chain(effects_array: &[Value], synth_context: bool) -> EffectChain {
    let mut chain = EffectChain::new(synth_context);

    for effect_value in effects_array {
        match effect_value {
            Value::Map(map) => {
                // Get effect type
                if let Some(effect_type) = map.get("type").or_else(|| map.get("effect")) {
                    if let Value::String(s) | Value::Identifier(s) = effect_type {
                        chain.add_effect(s, Some(Value::Map(map.clone())));
                    }
                } else {
                    // Fallback: map may be of the form { "reverb": { ... } } (no explicit "type" key)
                    // or { "reverb": value } - in that case, expand each key as an effect entry.
                    for (k, v) in map.iter() {
                        match v {
                            Value::Map(sm) => {
                                chain.add_effect(k, Some(Value::Map(sm.clone())));
                            }
                            _ => {
                                // non-map value, wrap as { "value": v }
                                let mut sub = std::collections::HashMap::new();
                                sub.insert("value".to_string(), v.clone());
                                chain.add_effect(k, Some(Value::Map(sub.clone())));
                            }
                        }
                    }
                }
            }
            Value::String(s) | Value::Identifier(s) => {
                chain.add_effect(s, None);
            }
            _ => {}
        }
    }

    chain
}

/// Build a single effect processor from name and optional parameters
fn build_effect_processor(
    registry: &EffectRegistry,
    name: &str,
    params: Option<Value>,
    synth_context: bool,
) -> Option<Box<dyn CloneableEffect>> {
    let base_processor = registry.get_effect(name, synth_context)?;

    if let Some(Value::Map(params_map)) = params {
        match name {
            "chorus" => {
                let depth = get_f32_param(&params_map, "depth", 0.7);
                let rate = get_f32_param(&params_map, "rate", 0.5);
                let mix = get_f32_param(&params_map, "mix", 0.5);
                Some(Box::new(super::processors::ChorusProcessor::new(
                    depth, rate, mix,
                )))
            }
            "flanger" => {
                let depth = get_f32_param(&params_map, "depth", 0.7);
                let rate = get_f32_param(&params_map, "rate", 0.5);
                let feedback = get_f32_param(&params_map, "feedback", 0.5);
                let mix = get_f32_param(&params_map, "mix", 0.5);
                Some(Box::new(super::processors::FlangerProcessor::new(
                    depth, rate, feedback, mix,
                )))
            }
            "phaser" => {
                let stages = get_f32_param(&params_map, "stages", 4.0) as usize;
                let rate = get_f32_param(&params_map, "rate", 0.5);
                let depth = get_f32_param(&params_map, "depth", 0.7);
                let feedback = get_f32_param(&params_map, "feedback", 0.5);
                let mix = get_f32_param(&params_map, "mix", 0.5);
                Some(Box::new(super::processors::PhaserProcessor::new(
                    stages, rate, depth, feedback, mix,
                )))
            }
            "compressor" => {
                let threshold = get_f32_param(&params_map, "threshold", -20.0);
                let ratio = get_f32_param(&params_map, "ratio", 4.0);
                let attack = get_f32_param(&params_map, "attack", 0.005);
                let release = get_f32_param(&params_map, "release", 0.1);
                Some(Box::new(super::processors::CompressorProcessor::new(
                    threshold, ratio, attack, release,
                )))
            }
            "drive" => {
                let amount = get_f32_param(&params_map, "amount", 0.7);
                let mix = get_f32_param(&params_map, "mix", 0.5);
                let tone = get_f32_param(&params_map, "tone", 0.5);
                let color = get_f32_param(&params_map, "color", 0.5);

                Some(Box::new(super::processors::DriveProcessor::new(
                    amount, tone, color, mix,
                )))
            }
            "reverb" => {
                let size = get_f32_param(&params_map, "size", 0.5);
                let damping = get_f32_param(&params_map, "damping", 0.5);
                let decay = get_f32_param(&params_map, "decay", 0.5);
                let mix = get_f32_param(&params_map, "mix", 0.3);

                Some(Box::new(super::processors::ReverbProcessor::new(
                    size, damping, decay, mix,
                )))
            }
            "delay" => {
                let time = get_f32_param(&params_map, "time", 250.0);
                let feedback = get_f32_param(&params_map, "feedback", 0.4);
                let mix = get_f32_param(&params_map, "mix", 0.3);
                Some(Box::new(super::processors::DelayProcessor::new(
                    time, feedback, mix,
                )))
            }
            "eq" | "equalizer" => {
                let low = get_f32_param(&params_map, "low", 1.0);
                let mid = get_f32_param(&params_map, "mid", 1.0);
                let high = get_f32_param(&params_map, "high", 1.0);
                Some(Box::new(super::processors::EqProcessor::new(
                    low, mid, high,
                )))
            }
            "lowpass" | "lpf" => {
                let cutoff = get_f32_param(&params_map, "cutoff", 5000.0);
                let resonance = get_f32_param(&params_map, "resonance", 0.1);
                Some(Box::new(super::processors::LowpassProcessor::new(
                    cutoff, resonance,
                )))
            }
            "highpass" | "hpf" => {
                let cutoff = get_f32_param(&params_map, "cutoff", 200.0);
                let resonance = get_f32_param(&params_map, "resonance", 0.1);
                Some(Box::new(super::processors::HighpassProcessor::new(
                    cutoff, resonance,
                )))
            }
            "bandpass" | "bpf" => {
                let cutoff = get_f32_param(&params_map, "cutoff", 1000.0);
                let resonance = get_f32_param(&params_map, "resonance", 0.2);
                Some(Box::new(super::processors::BandpassProcessor::new(
                    cutoff, resonance,
                )))
            }
            "distortion" | "dist" => {
                let amount = get_f32_param(&params_map, "amount", 0.5);
                let mix = get_f32_param(&params_map, "mix", 0.5);
                Some(Box::new(super::processors::DistortionProcessor::new(
                    amount, mix,
                )))
            }
            "speed" => {
                // support both {'speed': 2.0} and {'value': 2.0} normalized forms
                let speed = get_f32_param(
                    &params_map,
                    "speed",
                    get_f32_param(&params_map, "value", 1.0),
                );

                Some(Box::new(super::processors::SpeedProcessor::new(speed)))
            }
            "lfo" => {
                // parse LFO params: rate, depth, waveform, target, phase
                // rate may be a number or string like "1/8"
                let bpm = get_f32_param(&params_map, "bpm", 120.0);
                let depth = get_f32_param(&params_map, "depth", 0.5).clamp(0.0, 1.0);
                let phase = get_f32_param(&params_map, "phase", 0.0).fract();

                // waveform
                let waveform = params_map
                    .get("waveform")
                    .and_then(|v| match v {
                        Value::String(s) | Value::Identifier(s) => Some(LfoWaveform::from_str(s)),
                        _ => None,
                    })
                    .unwrap_or(LfoWaveform::Sine);

                // rate
                let rate_value_opt = params_map.get("rate");
                let rate = match rate_value_opt {
                    Some(Value::String(s)) | Some(Value::Identifier(s)) => LfoRate::from_value(s),
                    Some(Value::Number(n)) => LfoRate::Hz(*n),
                    _ => LfoRate::Hz(1.0),
                };

                // target
                let target = params_map
                    .get("target")
                    .and_then(|v| match v {
                        Value::String(s) | Value::Identifier(s) => LfoTarget::from_str(s),
                        _ => None,
                    })
                    .unwrap_or(LfoTarget::Volume);

                let params = LfoParams {
                    rate,
                    depth,
                    waveform,
                    target,
                    phase,
                };
                // parse optional ranges
                let semitones = get_f32_param(&params_map, "semitones", 2.0);
                let base_cutoff = get_f32_param(&params_map, "cutoff", 1000.0);
                let cutoff_range = get_f32_param(&params_map, "cutoff_range", 1000.0);
                Some(Box::new(super::processors::LfoProcessor::new(
                    params,
                    bpm,
                    semitones,
                    base_cutoff,
                    cutoff_range,
                )))
            }
            "reverse" => {
                let reverse = get_bool_param(
                    &params_map,
                    "reverse",
                    get_bool_param(&params_map, "value", true),
                );
                Some(Box::new(super::processors::ReverseProcessor::new(reverse)))
            }
            _ => Some(base_processor),
        }
    } else {
        Some(base_processor)
    }
}

/// Helper to extract f32 parameter from map
fn get_f32_param(map: &HashMap<String, Value>, key: &str, default: f32) -> f32 {
    map.get(key)
        .and_then(|v| match v {
            Value::Number(n) => Some(*n),
            Value::String(s) => s.parse::<f32>().ok(),
            _ => None,
        })
        .unwrap_or(default)
}

/// Helper to extract bool parameter from map
fn get_bool_param(map: &HashMap<String, Value>, key: &str, default: bool) -> bool {
    map.get(key)
        .and_then(|v| match v {
            Value::Boolean(b) => Some(*b),
            Value::Number(n) => Some(*n != 0.0),
            Value::String(s) => s.parse::<bool>().ok(),
            _ => None,
        })
        .unwrap_or(default)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_effect_chain_creation() {
        let chain = EffectChain::new(true); // Synth context
        assert_eq!(chain.len(), 0);
        assert!(chain.is_empty());

        // Test available effects in synth context
        let synth_effects = chain.available_effects();
        assert!(synth_effects.contains(&"reverb"));
        assert!(!synth_effects.contains(&"speed")); // Trigger-only effect
    }

    #[test]
    fn test_effect_chain_trigger_context() {
        let mut chain = EffectChain::new(false); // Trigger context

        // Test trigger-specific effects
        assert!(chain.is_effect_available("speed"));
        assert!(chain.is_effect_available("reverse"));

        // Test adding trigger-specific effect
        assert!(chain.add_effect("speed", None));
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_effect_parameter_parsing() {
        let mut chain = EffectChain::new(true);

        // Test adding effect with parameters
        let mut params = HashMap::new();
        params.insert("depth".to_string(), Value::Number(0.8));
        params.insert("rate".to_string(), Value::Number(1.0));
        params.insert("mix".to_string(), Value::Number(0.6));

        assert!(chain.add_effect("chorus", Some(Value::Map(params))));
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_filter_effects_accept_parameter_maps() {
        let mut params = HashMap::new();
        params.insert("cutoff".to_string(), Value::Number(900.0));
        params.insert("resonance".to_string(), Value::Number(0.2));

        let effects = vec![Value::Map({
            let mut effect = HashMap::new();
            effect.insert("type".to_string(), Value::String("lowpass".to_string()));
            effect.extend(params);
            effect
        })];

        let chain = build_effect_chain(&effects, true);
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_eq_effect_accepts_three_band_map() {
        let effects = vec![Value::Map({
            let mut effect = HashMap::new();
            effect.insert("type".to_string(), Value::String("eq".to_string()));
            effect.insert("low".to_string(), Value::Number(1.2));
            effect.insert("mid".to_string(), Value::Number(0.8));
            effect.insert("high".to_string(), Value::Number(1.1));
            effect
        })];

        let chain = build_effect_chain(&effects, true);
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_effect_context_restrictions() {
        let mut synth_chain = EffectChain::new(true);
        let mut trigger_chain = EffectChain::new(false);

        // Speed effect should only work in trigger context
        assert!(!synth_chain.add_effect("speed", None));
        assert!(trigger_chain.add_effect("speed", None));

        // Reverb effect should work in both contexts
        assert!(synth_chain.add_effect("reverb", None));
        assert!(trigger_chain.add_effect("reverb", None));
    }
}