mtrack 0.12.0

A multitrack audio and MIDI player for live performances.
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
// Copyright (C) 2026 Michael Wilson <mike@mdwn.dev>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
use crate::lighting::effects::*;
use crate::lighting::engine::tests::common::create_test_fixture;
use crate::lighting::engine::EffectEngine;
use std::collections::HashMap;
use std::time::Duration;

#[test]
fn test_start_effect_with_elapsed_static_effect() {
    // Test starting a static effect with pre-calculated elapsed time
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    let mut params = HashMap::new();
    params.insert("dimmer".to_string(), 1.0);

    let effect = EffectInstance::new(
        "test_effect".to_string(),
        EffectType::Static {
            parameters: params,
            duration: Duration::from_secs(5),
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );

    // Start with 500ms elapsed - effect should appear as if it's been running for 500ms
    engine
        .start_effect_with_elapsed(effect, Duration::from_millis(500))
        .unwrap();

    // Update immediately - should get full value (static effects don't change over time)
    let commands = engine.update(Duration::from_millis(16), None).unwrap();
    assert_eq!(commands.len(), 1);
    assert_eq!(commands[0].value, 255);
}

#[test]
fn test_start_effect_with_elapsed_dimmer_midpoint() {
    // Test starting a dimmer effect at its midpoint
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    let effect = EffectInstance::new(
        "dimmer".to_string(),
        EffectType::Dimmer {
            start_level: 0.0,
            end_level: 1.0,
            duration: Duration::from_secs(2), // 2 second fade
            curve: DimmerCurve::Linear,
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );

    // Start with 1 second elapsed (halfway through 2 second fade)
    engine
        .start_effect_with_elapsed(effect, Duration::from_secs(1))
        .unwrap();

    // Update immediately - should be at ~50%
    let commands = engine.update(Duration::from_millis(16), None).unwrap();
    assert_eq!(commands.len(), 1);
    // Should be approximately 50% (127-128)
    assert!(
        (120..=135).contains(&commands[0].value),
        "Expected ~50% dimmer value, got {}",
        commands[0].value
    );
}

#[test]
fn test_start_effect_with_elapsed_dimmer_complete() {
    // Test starting a dimmer effect that has already completed
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    let effect = EffectInstance::new(
        "dimmer".to_string(),
        EffectType::Dimmer {
            start_level: 0.0,
            end_level: 1.0,
            duration: Duration::from_secs(1),
            curve: DimmerCurve::Linear,
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );

    // Start with 2 seconds elapsed (beyond the 1 second duration)
    engine
        .start_effect_with_elapsed(effect, Duration::from_secs(2))
        .unwrap();

    // Dimmer has already completed (2s elapsed > 1s duration), so it's
    // removed immediately. No commands are emitted.
    let commands = engine.update(Duration::from_millis(16), None).unwrap();
    assert_eq!(commands.len(), 0, "Completed dimmer produces no commands");
}

#[test]
fn test_start_effect_with_elapsed_color_cycle() {
    // Test starting a color cycle effect partway through
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    let colors = vec![
        Color::new(255, 0, 0), // Red
        Color::new(0, 255, 0), // Green
        Color::new(0, 0, 255), // Blue
    ];

    let effect = EffectInstance::new(
        "cycle".to_string(),
        EffectType::ColorCycle {
            colors,
            speed: TempoAwareSpeed::Fixed(1.0), // 1 cycle per second
            direction: CycleDirection::Forward,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );

    // Start with 0.5 seconds elapsed (halfway through first cycle)
    // At 1 cycle/sec with 3 colors, each color gets 1/3 of the cycle
    // At 0.5s progress, we're at color index 1 (green) with Snap transition
    engine
        .start_effect_with_elapsed(effect, Duration::from_millis(500))
        .unwrap();

    // Update immediately - with Snap, should be pure green (no transition)
    let commands = engine.update(Duration::from_millis(16), None).unwrap();
    assert_eq!(commands.len(), 3); // RGB channels

    // Find RGB values (channels: dimmer=1, red=2, green=3, blue=4)
    let mut red = 0;
    let mut green = 0;
    let mut blue = 0;
    for cmd in commands {
        if cmd.channel == 2 {
            red = cmd.value;
        } else if cmd.channel == 3 {
            green = cmd.value;
        } else if cmd.channel == 4 {
            blue = cmd.value;
        }
    }

    // With Snap transition at 0.5s, should be pure green
    assert_eq!(red, 0, "Red should be 0 at this point in cycle");
    assert_eq!(green, 255, "Green should be full at this point in cycle");
    assert_eq!(blue, 0, "Blue should be 0");
}

#[test]
fn test_start_effect_with_elapsed_pulse() {
    // Test starting a pulse effect partway through its cycle
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    let effect = EffectInstance::new(
        "pulse".to_string(),
        EffectType::Pulse {
            base_level: 0.5,
            pulse_amplitude: 0.5,
            frequency: TempoAwareFrequency::Fixed(1.0), // 1 Hz
            duration: Duration::from_secs(5),
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );

    // Start with 0.25 seconds elapsed (quarter cycle at 1Hz = 90 degrees)
    // At 90 degrees, sin = 1.0, so pulse should be at max (base + amplitude = 1.0)
    engine
        .start_effect_with_elapsed(effect, Duration::from_millis(250))
        .unwrap();

    // Update immediately - should be near maximum
    let commands = engine.update(Duration::from_millis(16), None).unwrap();
    assert_eq!(commands.len(), 1);
    // Should be near 100% (allowing for small timing differences)
    assert!(
        commands[0].value > 240,
        "Pulse should be near max at 90 degrees, got {}",
        commands[0].value
    );
}

#[test]
fn test_start_effect_with_elapsed_rainbow() {
    // Test starting a rainbow effect partway through
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    let effect = EffectInstance::new(
        "rainbow".to_string(),
        EffectType::Rainbow {
            speed: TempoAwareSpeed::Fixed(1.0), // 1 cycle per second
            saturation: 1.0,
            brightness: 1.0,
            duration: Duration::from_secs(10),
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );

    // Start with 0.33 seconds elapsed (1/3 through cycle = 120 degrees hue)
    // At 120 degrees, we should see green-cyan transition
    engine
        .start_effect_with_elapsed(effect, Duration::from_millis(333))
        .unwrap();

    // Update immediately - should show appropriate color for that hue
    let commands = engine.update(Duration::from_millis(16), None).unwrap();
    assert_eq!(commands.len(), 3); // RGB channels

    // Find RGB values
    let mut red = 0;
    let mut green = 0;
    let mut blue = 0;
    for cmd in commands {
        if cmd.channel == 2 {
            red = cmd.value;
        } else if cmd.channel == 3 {
            green = cmd.value;
        } else if cmd.channel == 4 {
            blue = cmd.value;
        }
    }

    // At 120 degrees hue, green should be high, red low, blue medium
    assert!(green > red, "Green should dominate at 120 degrees");
    assert!(green > blue, "Green should be highest");
}

#[test]
fn test_start_effect_with_elapsed_timed_effect_completion() {
    // Test that timed effects complete correctly when started with elapsed time
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    let mut params = HashMap::new();
    params.insert("dimmer".to_string(), 1.0);

    let effect = EffectInstance::new(
        "timed".to_string(),
        EffectType::Static {
            parameters: params,
            duration: Duration::from_secs(1),
        },
        vec!["test_fixture".to_string()],
        None,
        Some(Duration::from_secs(1)),
        None,
    );

    // Start with 1.5 seconds elapsed (beyond the 1 second duration)
    engine
        .start_effect_with_elapsed(effect, Duration::from_millis(1500))
        .unwrap();

    // Update immediately - effect should already be expired
    let commands = engine.update(Duration::from_millis(16), None).unwrap();
    // Effect should have completed immediately
    assert_eq!(commands.len(), 0);
}

#[test]
fn test_start_effect_with_elapsed_zero_elapsed() {
    // Test that zero elapsed time works the same as start_effect
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    let mut params = HashMap::new();
    params.insert("dimmer".to_string(), 0.5);

    let effect1 = EffectInstance::new(
        "effect1".to_string(),
        EffectType::Static {
            parameters: params.clone(),
            duration: Duration::from_secs(5),
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );

    engine.start_effect(effect1).unwrap();
    let commands1 = engine
        .update(Duration::from_millis(16), None)
        .unwrap()
        .to_vec();

    // Reset and try with zero elapsed
    engine.stop_all_effects();

    let effect2 = EffectInstance::new(
        "effect2".to_string(),
        EffectType::Static {
            parameters: params,
            duration: Duration::from_secs(5),
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );

    engine
        .start_effect_with_elapsed(effect2, Duration::ZERO)
        .unwrap();
    let commands2 = engine.update(Duration::from_millis(16), None).unwrap();

    // Should produce same results
    assert_eq!(commands1.len(), commands2.len());
    if !commands1.is_empty() && !commands2.is_empty() {
        assert_eq!(commands1[0].value, commands2[0].value);
    }
}

#[test]
fn test_start_effect_with_elapsed_conflict_resolution() {
    // Test that conflict resolution works correctly with elapsed time
    let mut engine = EffectEngine::new();
    let fixture = create_test_fixture("test_fixture", 1, 1);
    engine.register_fixture(fixture);

    // Start a background effect
    let mut bg_params = HashMap::new();
    bg_params.insert("dimmer".to_string(), 0.3);

    let bg_effect = EffectInstance::new(
        "bg".to_string(),
        EffectType::Static {
            parameters: bg_params,
            duration: Duration::from_secs(5),
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );
    engine.start_effect(bg_effect).unwrap();
    engine.update(Duration::from_millis(16), None).unwrap();

    // Start a foreground effect with elapsed time - should conflict and replace
    let mut fg_params = HashMap::new();
    fg_params.insert("dimmer".to_string(), 0.8);

    let mut fg_effect = EffectInstance::new(
        "fg".to_string(),
        EffectType::Static {
            parameters: fg_params,
            duration: Duration::from_secs(5),
        },
        vec!["test_fixture".to_string()],
        None,
        None,
        None,
    );
    fg_effect.layer = EffectLayer::Foreground;
    fg_effect.priority = 10;

    engine
        .start_effect_with_elapsed(fg_effect, Duration::from_millis(500))
        .unwrap();

    // Update - should see foreground effect
    let commands = engine.update(Duration::from_millis(16), None).unwrap();
    assert_eq!(commands.len(), 1);
    assert_eq!(commands[0].value, 204); // 80% of 255
}