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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
use crate::engine::audio::events::AudioEvent;
use crate::language::syntax::ast::Value;
use anyhow::Result;

use super::AudioInterpreter;

/// Apply automation overrides to a parameter value
/// Returns the final value after applying global automation, then note-mode templates
/// For note-mode templates, calculates progress based on the note's start_time relative to the automation block
fn apply_automation_param(
    interpreter: &AudioInterpreter,
    target: &str,
    param_names: &[&str], // e.g., ["pan"] or ["pitch", "detune"]
    base_value: f32,
    note_start_time: f32, // Time when the note starts (used for note-mode progress calculation)
) -> f32 {
    let mut result = base_value;

    // 1. Try global automation first
    for name in param_names {
        if let Some(v) =
            interpreter
                .automation_registry
                .get_value(target, name, interpreter.cursor_time)
        {
            result = v;
            break; // Use first matching param name
        }
    }

    // 2. Apply note-mode templates
    if let Some(ctx) = interpreter.note_automation_templates.get(target) {
        // Calculate progress based on note's position in the automation block
        let note_progress = ctx.progress_at_time(note_start_time);

        for tpl in ctx.templates.iter() {
            for name in param_names {
                if tpl.param_name == *name {
                    // Evaluate template at the note's progress position
                    result =
                        crate::engine::audio::automation::evaluate_template_at(tpl, note_progress);
                    return result; // Use first matching template
                }
            }
        }
    }

    result
}

/// Extract plugin-specific parameters from effects array
/// Plugin parameters should be applied BEFORE generation as synth options, not AFTER as audio effects.
/// Strategy: Any effect that's NOT a known audio effect is treated as a plugin parameter.
/// Returns remaining effects (plugin params removed)
fn extract_plugin_parameters(
    effects: &Option<Value>,
    synth_def: &mut crate::engine::audio::events::SynthDefinition,
) -> Option<Value> {
    if effects.is_none() {
        return None;
    }

    // Known audio effect names - everything else is treated as plugin parameter
    let audio_effect_names = [
        // Main effects (Both synth + trigger)
        "reverb",
        "delay",
        "distortion",
        "bitcrush",
        "lowpass",
        "lpf",
        "highpass",
        "hpf",
        "bandpass",
        "bpf",
        "tremolo",
        "vibrato",
        "chorus",
        "flanger",
        "phaser",
        "compressor",
        "comp",
        "drive",
        "gate",
        "lfo",
        // Trigger-only effects
        "reverse",
        "speed",
        "slice",
        "stretch",
        "roll",
        // Sample processing
        "mono",
        "monoizer",
        "stereo",
        // Special/control effects
        "freeze",
        "adsr",
        "eq",
        "equalizer",
        // Aliases for distortion
        "dist",
    ];

    if let Some(Value::Array(effects_arr)) = effects {
        let mut remaining_effects = Vec::new();
        let mut found_params = false;

        for effect in effects_arr {
            if let Value::Map(effect_map) = effect {
                // Check if "type" field is a known audio effect
                if let Some(Value::String(type_name)) = effect_map.get("type") {
                    let type_lower = type_name.to_lowercase();

                    if !audio_effect_names.contains(&type_lower.as_str()) {
                        // This is NOT a known audio effect - treat it as a plugin parameter
                        found_params = true;

                        // Extract value and add to synth options
                        // Priority: "value" key > any numeric value > type name itself
                        if let Some(Value::Number(val)) = effect_map.get("value") {
                            synth_def.options.insert(type_name.clone(), *val);
                        } else {
                            // Look for any numeric parameter
                            let mut found_numeric = false;
                            for (key, val) in effect_map.iter() {
                                if key != "type" {
                                    if let Value::Number(n) = val {
                                        synth_def.options.insert(key.clone(), *n);
                                        found_numeric = true;
                                    }
                                }
                            }
                            // If no numeric params found, skip this effect
                            if !found_numeric {
                                continue;
                            }
                        }
                        continue; // Don't add this effect to remaining_effects
                    }
                }
            }
            // Keep known audio effects for audio chain
            remaining_effects.push(effect.clone());
        }

        if found_params {
            if remaining_effects.is_empty() {
                return None; // All effects were plugin params
            } else {
                return Some(Value::Array(remaining_effects)); // Return non-plugin effects
            }
        }
    }

    effects.clone() // Return effects unchanged if no plugin params found
}

pub fn extract_audio_event(
    interpreter: &mut AudioInterpreter,
    target: &str,
    context: &crate::engine::functions::FunctionContext,
) -> Result<()> {
    // Implementation simplified: reuse existing driver logic
    // Try single note first
    if let Some(Value::String(note_name)) = context.get("note") {
        // Prepare placeholder for merged effects (will be computed after collecting synth/note effects)
        let mut event_effects: Option<crate::language::syntax::ast::Value> = None;
        let midi = crate::engine::functions::note::parse_note_to_midi(note_name)?;

        // Duration: prefer context.duration if set (execute_arrow_call may set it), otherwise read "duration" value (ms)
        let duration = if context.duration > 0.0 {
            context.duration
        } else if let Some(Value::Number(d)) = context.get("duration") {
            d / 1000.0
        } else {
            0.5
        };

        // Velocity: accept either 0.0-1.0 or 0-127 (if > 2.0 treat as MIDI scale)
        let velocity = if let Some(Value::Number(v)) = context.get("velocity") {
            if *v > 2.0 {
                (*v) / 127.0
            } else if *v > 1.0 {
                (*v) / 100.0
            } else {
                *v
            }
        } else {
            0.8
        };

        // Pan: preference order: explicit context > automation > default
        let base_pan = if let Some(Value::Number(p)) = context.get("pan") {
            *p
        } else {
            0.0
        };

        // Check if this note should use per-note automation
        let use_per_note_automation = interpreter.note_automation_templates.contains_key(target);

        // If using per-note automation, don't apply templates here - apply at render time
        let pan = if use_per_note_automation {
            base_pan
        } else {
            apply_automation_param(
                interpreter,
                target,
                &["pan"],
                base_pan,
                interpreter.cursor_time,
            )
        };

        // Detune/Pitch: automation may come from either "pitch" or "detune" param name
        let base_detune = if let Some(Value::Number(d)) = context.get("detune") {
            *d
        } else {
            0.0
        };
        let detune = if use_per_note_automation {
            base_detune
        } else {
            apply_automation_param(
                interpreter,
                target,
                &["pitch", "detune"],
                base_detune,
                interpreter.cursor_time,
            )
        };

        // Gain/Volume: automation may come from either "volume" or "gain" param name
        let base_gain = if let Some(Value::Number(g)) = context.get("gain") {
            *g
        } else {
            1.0
        };
        let gain = if use_per_note_automation {
            base_gain
        } else {
            apply_automation_param(
                interpreter,
                target,
                &["volume", "gain"],
                base_gain,
                interpreter.cursor_time,
            )
        };

        // Use the provided target as synth id so the synth definition (including plugin info)
        // is correctly snapshotted at event creation time. Fall back to "default" if empty.
        let synth_id = if target.is_empty() { "default" } else { target };

        // Build a synth definition snapshot and apply automation overrides so that the

        let mut synth_def = interpreter
            .events
            .get_synth(synth_id)
            .cloned()
            .unwrap_or_default();

        // Apply automation overrides to synth options (cutoff, resonance, etc.)
        // Note: cutoff and resonance are in filters, not options
        for filter in &mut synth_def.filters {
            // Apply cutoff automation
            let current_cutoff = filter.cutoff;
            let automated_cutoff = apply_automation_param(
                interpreter,
                synth_id,
                &["cutoff"],
                current_cutoff,
                interpreter.cursor_time,
            );
            if (automated_cutoff - current_cutoff).abs() > 0.0001 {
                filter.cutoff = automated_cutoff;
            }

            // Apply resonance automation
            let current_resonance = filter.resonance;
            let automated_resonance = apply_automation_param(
                interpreter,
                synth_id,
                &["resonance"],
                current_resonance,
                interpreter.cursor_time,
            );
            if (automated_resonance - current_resonance).abs() > 0.0001 {
                filter.resonance = automated_resonance;
            }
        }

        // Apply automation overrides to synth type options (drive, tone, decay, etc.)
        let synth_params = ["filter_type", "drive", "tone", "decay"];
        for param in &synth_params {
            let current_val = synth_def.options.get(*param).copied().unwrap_or(0.0);
            let automated_val = apply_automation_param(
                interpreter,
                synth_id,
                &[param],
                current_val,
                interpreter.cursor_time,
            );
            if (automated_val - current_val).abs() > 0.0001 {
                synth_def.options.insert(param.to_string(), automated_val);
            }
        }

        let mut synth_effects_vec: Vec<crate::language::syntax::ast::Value> = Vec::new();
        if let Some(var_val) = interpreter.variables.get(synth_id) {
            match var_val {
                crate::language::syntax::ast::Value::Map(m) => {
                    if let Some(chain_v) = m.get("chain") {
                        if let crate::language::syntax::ast::Value::Array(arr) = chain_v {
                            synth_effects_vec.extend(arr.clone());
                        }
                    } else if let Some(effs) = m.get("effects") {
                        // Deprecated map-style: normalize into individual effect maps
                        eprintln!(
                            "DEPRECATION: synth-level effect param map support is deprecated — use chained params instead."
                        );
                        let normalized =
                            crate::engine::audio::effects::normalize_effects(&Some(effs.clone()));
                        for (k, v) in normalized.into_iter() {
                            let mut map = std::collections::HashMap::new();
                            map.insert(
                                "type".to_string(),
                                crate::language::syntax::ast::Value::String(k),
                            );
                            for (pk, pv) in v.into_iter() {
                                map.insert(pk, pv);
                            }
                            synth_effects_vec.push(crate::language::syntax::ast::Value::Map(map));
                        }
                    }
                }
                crate::language::syntax::ast::Value::Statement(stmt_box) => {
                    if let crate::language::syntax::ast::StatementKind::ArrowCall { .. } =
                        &stmt_box.kind
                    {
                        if let crate::language::syntax::ast::Value::Map(m) = &stmt_box.value {
                            if let Some(crate::language::syntax::ast::Value::Array(arr)) =
                                m.get("chain")
                            {
                                synth_effects_vec.extend(arr.clone());
                            }
                        }
                    }
                }
                _ => {}
            }
        }

        // Collect note-level effects from FunctionContext when invoking note/chord/sample
        let mut note_effects_vec: Vec<crate::language::syntax::ast::Value> = Vec::new();
        if let Some(crate::language::syntax::ast::Value::String(method_name)) =
            context.get("method")
        {
            let m = method_name.as_str();
            if m == "note" || m == "chord" || m == "sample" {
                if let Some(eff_val) = context.get("effects") {
                    match eff_val {
                        crate::language::syntax::ast::Value::Array(arr) => {
                            note_effects_vec.extend(arr.clone());
                        }
                        crate::language::syntax::ast::Value::Map(map_v) => {
                            // normalize map into per-effect entries
                            let normalized = crate::engine::audio::effects::normalize_effects(
                                &Some(crate::language::syntax::ast::Value::Map(map_v.clone())),
                            );
                            for (k, v) in normalized.into_iter() {
                                let mut map = std::collections::HashMap::new();
                                map.insert(
                                    "type".to_string(),
                                    crate::language::syntax::ast::Value::String(k),
                                );
                                for (pk, pv) in v.into_iter() {
                                    map.insert(pk, pv);
                                }
                                note_effects_vec
                                    .push(crate::language::syntax::ast::Value::Map(map));
                            }
                        }
                        _ => {}
                    }
                }
            }
        }

        // As a last resort, if no synth variable chain and context.effects exists (non-note invocation), treat context.effects as synth-level
        if synth_effects_vec.is_empty() {
            if let Some(eff_val) = context.get("effects") {
                if let crate::language::syntax::ast::Value::Array(arr) = eff_val {
                    synth_effects_vec.extend(arr.clone());
                } else if let crate::language::syntax::ast::Value::Map(map_v) = eff_val {
                    let normalized = crate::engine::audio::effects::normalize_effects(&Some(
                        crate::language::syntax::ast::Value::Map(map_v.clone()),
                    ));
                    for (k, v) in normalized.into_iter() {
                        let mut map = std::collections::HashMap::new();
                        map.insert(
                            "type".to_string(),
                            crate::language::syntax::ast::Value::String(k),
                        );
                        for (pk, pv) in v.into_iter() {
                            map.insert(pk, pv);
                        }
                        synth_effects_vec.push(crate::language::syntax::ast::Value::Map(map));
                    }
                }
            }
        }

        // Merge synth-level then note-level
        if !synth_effects_vec.is_empty() || !note_effects_vec.is_empty() {
            let mut merged: Vec<crate::language::syntax::ast::Value> = Vec::new();
            merged.extend(synth_effects_vec);
            merged.extend(note_effects_vec);
            event_effects = Some(crate::language::syntax::ast::Value::Array(merged));
        }

        // Extract plugin-specific parameters from effects and apply to synth options
        event_effects = extract_plugin_parameters(&event_effects, &mut synth_def);

        // CRITICAL: Save the updated synth_def back to the interpreter so subsequent notes
        // on the same synth reuse these parameters (accent, resonance, cutoff, etc.)
        // This ensures parameters persist across multiple notes in a sequence/group
        interpreter
            .events
            .synths
            .insert(synth_id.to_string(), synth_def.clone());

        interpreter.events.events.push(AudioEvent::Note {
            node_id: interpreter.resolve_event_node_id(synth_id),
            midi,
            start_time: interpreter.cursor_time,
            duration,
            velocity,
            synth_id: synth_id.to_string(),
            synth_def,
            pan,
            detune,
            gain,
            attack: None,
            release: None,
            delay_time: None,
            delay_feedback: None,
            delay_mix: None,
            reverb_amount: None,
            drive_amount: None,
            drive_color: None,
            effects: event_effects,
            use_per_note_automation,
        });
        return Ok(());
    }

    // Handle chords (notes array)
    if let Some(Value::Array(notes_arr)) = context.get("notes") {
        let mut midis: Vec<u8> = Vec::new();
        for n in notes_arr {
            if let Value::String(s) = n {
                if let Ok(m) = crate::engine::functions::note::parse_note_to_midi(s) {
                    midis.push(m);
                }
            }
        }

        if !midis.is_empty() {
            let duration = if context.duration > 0.0 {
                context.duration
            } else if let Some(Value::Number(d)) = context.get("duration") {
                d / 1000.0
            } else {
                0.5
            };

            let velocity = if let Some(Value::Number(v)) = context.get("velocity") {
                if *v > 2.0 {
                    (*v) / 127.0
                } else if *v > 1.0 {
                    (*v) / 100.0
                } else {
                    *v
                }
            } else {
                0.8
            };

            let pan = if let Some(Value::Number(p)) = context.get("pan") {
                *p
            } else {
                0.0
            };

            // Check if this chord should use per-note automation
            let use_per_note_automation =
                interpreter.note_automation_templates.contains_key(target);

            let pan = if use_per_note_automation {
                pan
            } else {
                apply_automation_param(interpreter, target, &["pan"], pan, interpreter.cursor_time)
            };

            let detune = if let Some(Value::Number(d)) = context.get("detune") {
                *d
            } else {
                0.0
            };
            let detune = if use_per_note_automation {
                detune
            } else {
                apply_automation_param(
                    interpreter,
                    target,
                    &["pitch", "detune"],
                    detune,
                    interpreter.cursor_time,
                )
            };

            let spread = if let Some(Value::Number(s)) = context.get("spread") {
                *s
            } else {
                0.0
            };

            let gain = if let Some(Value::Number(g)) = context.get("gain") {
                *g
            } else {
                1.0
            };
            let gain = if use_per_note_automation {
                gain
            } else {
                apply_automation_param(
                    interpreter,
                    target,
                    &["volume", "gain"],
                    gain,
                    interpreter.cursor_time,
                )
            };

            // optional envelope overrides
            let attack = context.get("attack").and_then(|v| {
                if let Value::Number(n) = v {
                    Some(*n)
                } else {
                    None
                }
            });
            let release = context.get("release").and_then(|v| {
                if let Value::Number(n) = v {
                    Some(*n)
                } else {
                    None
                }
            });

            // Effects
            let delay_time = context.get("delay_time").and_then(|v| {
                if let Value::Number(n) = v {
                    Some(*n)
                } else {
                    None
                }
            });
            let delay_feedback = context.get("delay_feedback").and_then(|v| {
                if let Value::Number(n) = v {
                    Some(*n)
                } else {
                    None
                }
            });
            let delay_mix = context.get("delay_mix").and_then(|v| {
                if let Value::Number(n) = v {
                    Some(*n)
                } else {
                    None
                }
            });
            let reverb_amount = context.get("reverb_amount").and_then(|v| {
                if let Value::Number(n) = v {
                    Some(*n)
                } else {
                    None
                }
            });
            let drive_amount = context.get("drive_amount").and_then(|v| {
                if let Value::Number(n) = v {
                    Some(*n)
                } else {
                    None
                }
            });
            let drive_color = context.get("drive_color").and_then(|v| {
                if let Value::Number(n) = v {
                    Some(*n)
                } else {
                    None
                }
            });

            let synth_id = if target.is_empty() { "default" } else { target };

            // Create chord event directly with per-note automation flag
            let mut synth_def = interpreter
                .events
                .get_synth(synth_id)
                .cloned()
                .unwrap_or_default();
            // Determine effects for this chord event by merging synth-level and any chord-level effects
            let mut event_effects: Option<crate::language::syntax::ast::Value> = None;

            // Collect synth-level effects if present in variable
            let mut synth_effects_vec: Vec<crate::language::syntax::ast::Value> = Vec::new();
            if let Some(var_val) = interpreter.variables.get(synth_id) {
                match var_val {
                    crate::language::syntax::ast::Value::Map(m) => {
                        if let Some(chain_v) = m.get("chain") {
                            if let crate::language::syntax::ast::Value::Array(arr) = chain_v {
                                synth_effects_vec.extend(arr.clone());
                            }
                        } else if let Some(effs) = m.get("effects") {
                            eprintln!(
                                "DEPRECATION: synth-level effect param map support is deprecated — use chained params instead."
                            );
                            let normalized = crate::engine::audio::effects::normalize_effects(
                                &Some(effs.clone()),
                            );
                            for (k, v) in normalized.into_iter() {
                                let mut map = std::collections::HashMap::new();
                                map.insert(
                                    "type".to_string(),
                                    crate::language::syntax::ast::Value::String(k),
                                );
                                for (pk, pv) in v.into_iter() {
                                    map.insert(pk, pv);
                                }
                                synth_effects_vec
                                    .push(crate::language::syntax::ast::Value::Map(map));
                            }
                        }
                    }
                    crate::language::syntax::ast::Value::Statement(stmt_box) => {
                        if let crate::language::syntax::ast::StatementKind::ArrowCall { .. } =
                            &stmt_box.kind
                        {
                            if let crate::language::syntax::ast::Value::Map(m) = &stmt_box.value {
                                if let Some(crate::language::syntax::ast::Value::Array(arr)) =
                                    m.get("chain")
                                {
                                    synth_effects_vec.extend(arr.clone());
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }

            // Collect chord-level (note-level) effects from FunctionContext when invoking chord
            let mut chord_effects_vec: Vec<crate::language::syntax::ast::Value> = Vec::new();
            if let Some(crate::language::syntax::ast::Value::String(method_name)) =
                context.get("method")
            {
                let m = method_name.as_str();
                if m == "chord" {
                    if let Some(eff_val) = context.get("effects") {
                        match eff_val {
                            crate::language::syntax::ast::Value::Array(arr) => {
                                chord_effects_vec.extend(arr.clone());
                            }
                            crate::language::syntax::ast::Value::Map(map_v) => {
                                let normalized = crate::engine::audio::effects::normalize_effects(
                                    &Some(crate::language::syntax::ast::Value::Map(map_v.clone())),
                                );
                                for (k, v) in normalized.into_iter() {
                                    let mut map = std::collections::HashMap::new();
                                    map.insert(
                                        "type".to_string(),
                                        crate::language::syntax::ast::Value::String(k),
                                    );
                                    for (pk, pv) in v.into_iter() {
                                        map.insert(pk, pv);
                                    }
                                    chord_effects_vec
                                        .push(crate::language::syntax::ast::Value::Map(map));
                                }
                            }
                            _ => {}
                        }
                    }
                }
            }

            if !synth_effects_vec.is_empty() || !chord_effects_vec.is_empty() {
                let mut merged: Vec<crate::language::syntax::ast::Value> = Vec::new();
                merged.extend(synth_effects_vec);
                merged.extend(chord_effects_vec);
                event_effects = Some(crate::language::syntax::ast::Value::Array(merged));
            }

            // Extract plugin-specific parameters from effects and apply to synth options
            event_effects = extract_plugin_parameters(&event_effects, &mut synth_def);

            // CRITICAL: Save the updated synth_def back to the interpreter so subsequent chords/notes
            // on the same synth reuse these parameters (accent, resonance, cutoff, etc.)
            interpreter
                .events
                .synths
                .insert(synth_id.to_string(), synth_def.clone());

            interpreter.events.events.push(AudioEvent::Chord {
                node_id: interpreter.resolve_event_node_id(synth_id),
                midis,
                start_time: interpreter.cursor_time,
                duration,
                velocity,
                synth_id: synth_id.to_string(),
                synth_def,
                pan,
                detune,
                spread,
                gain,
                attack,
                release,
                delay_time,
                delay_feedback,
                delay_mix,
                reverb_amount,
                drive_amount,
                drive_color,
                effects: event_effects,
                use_per_note_automation: false,
            });
            // Apply note-mode/global automation to synth-specific options (cutoff, resonance, etc.)
            // Collect automated values first to avoid borrowing conflicts
            let synth_params = [
                "cutoff",
                "resonance",
                "filter_type",
                "drive",
                "tone",
                "decay",
            ];
            let mut param_updates = Vec::new();

            // Snapshot current values and compute automations
            if let Some(synth_def) = interpreter.events.synths.get(synth_id) {
                for param in &synth_params {
                    let current_val = synth_def.options.get(*param).copied().unwrap_or(0.0);
                    let automated_val = apply_automation_param(
                        interpreter,
                        synth_id,
                        &[param],
                        current_val,
                        interpreter.cursor_time,
                    );
                    if (automated_val - current_val).abs() > 0.0001 {
                        param_updates.push((param.to_string(), automated_val));
                    }
                }
            }

            // Now apply the updates
            if let Some(synth_def) = interpreter.events.synths.get_mut(synth_id) {
                for (param, val) in param_updates {
                    synth_def.options.insert(param, val);
                }
            }
        }
    }
    Ok(())
}