maolan 0.2.2

Rust DAW application for recording, editing, routing, automation, export, and plugin hosting
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
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

impl From<LaunchQuantization> for maolan_engine::message::LaunchQuantization {
    fn from(value: LaunchQuantization) -> Self {
        match value {
            LaunchQuantization::None => Self::None,
            LaunchQuantization::Beat => Self::Beat,
            LaunchQuantization::Bar => Self::Bar,
            LaunchQuantization::TwoBars => Self::TwoBars,
            LaunchQuantization::FourBars => Self::FourBars,
            LaunchQuantization::EightBars => Self::EightBars,
        }
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LaunchMode {
    Trigger,
    Gate,
    #[default]
    Toggle,
    Repeat,
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LaunchQuantization {
    None,
    Beat,
    #[default]
    Bar,
    TwoBars,
    FourBars,
    EightBars,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlotClipRef {
    pub clip_id: String,
    #[serde(default)]
    pub launch_mode: LaunchMode,
    #[serde(default)]
    pub launch_quantization: LaunchQuantization,
    #[serde(default = "default_loop_enabled")]
    pub loop_enabled: bool,
    #[serde(default)]
    pub loop_start_samples: usize,
    #[serde(default)]
    pub loop_end_samples: usize,
}

fn default_loop_enabled() -> bool {
    true
}

fn default_play_stop_icon() -> Option<bool> {
    Some(false)
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClipSlot {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub clip: Option<SlotClipRef>,
    #[serde(default = "default_play_stop_icon")]
    pub play_stop_icon: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub clip_name: Option<String>,
}

impl ClipSlot {
    /// Returns `true` when this slot references a clip and is marked with the
    /// play icon, meaning it should take part in scene launches.
    pub fn is_play_enabled(&self) -> bool {
        self.play_stop_icon == Some(true) && self.clip.is_some()
    }
}

impl Default for ClipSlot {
    fn default() -> Self {
        Self {
            clip: None,
            play_stop_icon: default_play_stop_icon(),
            clip_name: None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Scene {
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub color: Option<[f32; 4]>,
    #[serde(default)]
    pub launch_quantization: LaunchQuantization,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tempo: Option<f32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionMatrix {
    #[serde(default = "default_scenes")]
    pub scenes: Vec<Scene>,
    #[serde(default)]
    pub slots: HashMap<String, Vec<ClipSlot>>,
}

impl Default for SessionMatrix {
    fn default() -> Self {
        Self {
            scenes: default_scenes(),
            slots: HashMap::new(),
        }
    }
}

fn default_scenes() -> Vec<Scene> {
    vec![Scene {
        name: "Scene 1".to_string(),
        color: None,
        launch_quantization: LaunchQuantization::Bar,
        tempo: None,
    }]
}

impl SessionMatrix {
    pub fn ensure_track_slots(&mut self, track_name: &str) -> &mut Vec<ClipSlot> {
        let scene_count = self.scenes.len().max(1);
        self.slots.entry(track_name.to_string()).or_insert_with(|| {
            (0..scene_count)
                .map(|_| ClipSlot {
                    clip: None,
                    play_stop_icon: default_play_stop_icon(),
                    clip_name: None,
                })
                .collect()
        })
    }

    pub fn slot_mut(&mut self, track_name: &str, scene_index: usize) -> Option<&mut ClipSlot> {
        self.slots.get_mut(track_name)?.get_mut(scene_index)
    }

    pub fn slot(&self, track_name: &str, scene_index: usize) -> Option<&ClipSlot> {
        self.slots.get(track_name)?.get(scene_index)
    }

    pub fn scene_count(&self) -> usize {
        self.scenes.len()
    }

    pub fn add_scene(&mut self) {
        let index = self.scenes.len() + 1;
        self.scenes.push(Scene {
            name: format!("Scene {}", index),
            color: None,
            launch_quantization: LaunchQuantization::Bar,
            tempo: None,
        });
        for slots in self.slots.values_mut() {
            slots.push(ClipSlot {
                clip: None,
                play_stop_icon: default_play_stop_icon(),
                clip_name: None,
            });
        }
    }

    /// Backfill missing play/stop icons so legacy slots default to stopped.
    pub fn backfill_play_stop_icons(&mut self) {
        for slots in self.slots.values_mut() {
            for slot in slots {
                if slot.play_stop_icon.is_none() {
                    slot.play_stop_icon = Some(false);
                }
            }
        }
    }

    /// Move the clip reference from one slot to another. The destination slot
    /// is overwritten if it exists. Returns the moved clip reference when a
    /// move actually happened (i.e. `from != to` and the source slot had a
    /// clip).
    pub fn move_slot(
        &mut self,
        from_track: &str,
        from_scene: usize,
        to_track: &str,
        to_scene: usize,
    ) -> Option<SlotClipRef> {
        if from_track == to_track && from_scene == to_scene {
            return None;
        }
        let (clip_ref, clip_name) = self
            .slot_mut(from_track, from_scene)
            .map(|slot| (slot.clip.take(), slot.clip_name.take()))?;
        let clip_ref = clip_ref?;
        self.ensure_track_slots(to_track);
        if let Some(slot) = self.slot_mut(to_track, to_scene) {
            slot.clip = Some(clip_ref.clone());
            slot.clip_name = clip_name;
        }
        Some(clip_ref)
    }

    /// Copy the clip reference from one slot to another. Returns `true` if the
    /// source slot had a clip and the destination slot exists (after ensuring
    /// the destination track has enough slots).
    pub fn copy_slot(
        &mut self,
        from_track: &str,
        from_scene: usize,
        to_track: &str,
        to_scene: usize,
    ) -> bool {
        let (clip_ref, clip_name) = match self.slot(from_track, from_scene) {
            Some(slot) => (slot.clip.clone(), slot.clip_name.clone()),
            None => return false,
        };
        let Some(clip_ref) = clip_ref else {
            return false;
        };
        self.ensure_track_slots(to_track);
        if let Some(slot) = self.slot_mut(to_track, to_scene) {
            slot.clip = Some(clip_ref);
            slot.clip_name = clip_name;
            true
        } else {
            false
        }
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SlotPlayState {
    #[default]
    Stopped,
    Queued,
    Playing,
    Stopping,
}

#[derive(Debug, Clone, Default)]
pub struct SlotRuntime {
    pub state: SlotPlayState,
    pub next_state: Option<SlotPlayState>,
    pub play_position_samples: usize,
    pub elapsed_samples: usize,
    pub launch_at_sample: Option<usize>,
    pub stop_at_sample: Option<usize>,
}

pub type SlotRuntimes = HashMap<(String, usize), SlotRuntime>;
pub type SelectedSlots = HashSet<(String, usize)>;

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

    #[test]
    fn session_matrix_default_has_one_scene() {
        let matrix = SessionMatrix::default();
        assert_eq!(matrix.scenes.len(), 1);
        assert!(matrix.slots.is_empty());
    }

    #[test]
    fn ensure_track_slots_creates_one_slot_per_scene() {
        let mut matrix = SessionMatrix::default();
        matrix.ensure_track_slots("track 1");
        assert_eq!(
            matrix.slots.get("track 1").unwrap().len(),
            matrix.scenes.len()
        );
    }

    #[test]
    fn slot_round_trips_clip_reference() {
        let mut matrix = SessionMatrix::default();
        matrix.ensure_track_slots("track 1");
        matrix.slot_mut("track 1", 0).unwrap().clip = Some(SlotClipRef {
            clip_id: "clip-1".to_string(),
            launch_mode: LaunchMode::Toggle,
            launch_quantization: LaunchQuantization::Bar,
            loop_enabled: true,
            loop_start_samples: 0,
            loop_end_samples: 0,
        });
        assert_eq!(
            matrix
                .slot("track 1", 0)
                .unwrap()
                .clip
                .as_ref()
                .unwrap()
                .clip_id,
            "clip-1"
        );
    }

    #[test]
    fn session_matrix_serde_round_trip() {
        let mut matrix = SessionMatrix::default();
        matrix.ensure_track_slots("track 1");
        matrix.slot_mut("track 1", 0).unwrap().clip = Some(SlotClipRef {
            clip_id: "clip-1".to_string(),
            launch_mode: LaunchMode::Repeat,
            launch_quantization: LaunchQuantization::TwoBars,
            loop_enabled: false,
            loop_start_samples: 48000,
            loop_end_samples: 96000,
        });
        let json = serde_json::to_string(&matrix).unwrap();
        let restored: SessionMatrix = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.scenes.len(), matrix.scenes.len());
        let clip_ref = restored.slot("track 1", 0).unwrap().clip.as_ref().unwrap();
        assert_eq!(clip_ref.clip_id, "clip-1");
        assert_eq!(clip_ref.launch_mode, LaunchMode::Repeat);
        assert_eq!(clip_ref.launch_quantization, LaunchQuantization::TwoBars);
        assert!(!clip_ref.loop_enabled);
        assert_eq!(clip_ref.loop_start_samples, 48000);
        assert_eq!(clip_ref.loop_end_samples, 96000);
    }

    #[test]
    fn launch_quantization_default_is_bar() {
        assert_eq!(LaunchQuantization::default(), LaunchQuantization::Bar);
    }

    #[test]
    fn launch_mode_default_is_toggle() {
        assert_eq!(LaunchMode::default(), LaunchMode::Toggle);
    }

    #[test]
    fn clip_slot_is_play_enabled_requires_play_icon_and_clip() {
        let mut slot = ClipSlot::default();
        assert!(!slot.is_play_enabled());

        slot.play_stop_icon = Some(true);
        assert!(!slot.is_play_enabled(), "a slot still needs a clip");

        slot.clip = Some(SlotClipRef {
            clip_id: "clip-1".to_string(),
            launch_mode: LaunchMode::Toggle,
            launch_quantization: LaunchQuantization::Bar,
            loop_enabled: true,
            loop_start_samples: 0,
            loop_end_samples: 0,
        });
        assert!(slot.is_play_enabled());

        slot.play_stop_icon = Some(false);
        assert!(!slot.is_play_enabled());

        slot.play_stop_icon = None;
        assert!(!slot.is_play_enabled());
    }

    #[test]
    fn backfill_play_stop_icons_defaults_missing_to_false() {
        let mut matrix = SessionMatrix::default();
        matrix.slots.insert(
            "track 1".to_string(),
            vec![
                ClipSlot {
                    clip: None,
                    play_stop_icon: None,
                    clip_name: None,
                },
                ClipSlot {
                    clip: None,
                    play_stop_icon: Some(false),
                    clip_name: None,
                },
            ],
        );
        matrix.backfill_play_stop_icons();
        let slots = matrix.slots.get("track 1").unwrap();
        assert_eq!(slots[0].play_stop_icon, Some(false));
        assert_eq!(slots[1].play_stop_icon, Some(false));
    }

    #[test]
    fn move_slot_transfers_clip_reference() {
        let mut matrix = SessionMatrix::default();
        matrix.ensure_track_slots("track 1");
        matrix.slot_mut("track 1", 0).unwrap().clip = Some(SlotClipRef {
            clip_id: "clip-1".to_string(),
            launch_mode: LaunchMode::Toggle,
            launch_quantization: LaunchQuantization::Bar,
            loop_enabled: true,
            loop_start_samples: 0,
            loop_end_samples: 0,
        });
        matrix.add_scene();
        matrix.add_scene();
        let moved = matrix.move_slot("track 1", 0, "track 1", 2);
        assert!(moved.is_some());
        assert!(matrix.slot("track 1", 0).unwrap().clip.is_none());
        assert_eq!(
            matrix
                .slot("track 1", 2)
                .unwrap()
                .clip
                .as_ref()
                .unwrap()
                .clip_id,
            "clip-1"
        );
    }

    #[test]
    fn move_slot_to_same_location_is_no_op() {
        let mut matrix = SessionMatrix::default();
        matrix.ensure_track_slots("track 1");
        matrix.slot_mut("track 1", 0).unwrap().clip = Some(SlotClipRef {
            clip_id: "clip-1".to_string(),
            launch_mode: LaunchMode::Toggle,
            launch_quantization: LaunchQuantization::Bar,
            loop_enabled: true,
            loop_start_samples: 0,
            loop_end_samples: 0,
        });
        assert!(matrix.move_slot("track 1", 0, "track 1", 0).is_none());
        assert_eq!(
            matrix
                .slot("track 1", 0)
                .unwrap()
                .clip
                .as_ref()
                .unwrap()
                .clip_id,
            "clip-1"
        );
    }

    #[test]
    fn move_slot_from_empty_source_returns_none() {
        let mut matrix = SessionMatrix::default();
        matrix.ensure_track_slots("track 1");
        assert!(matrix.move_slot("track 1", 0, "track 1", 2).is_none());
    }

    #[test]
    fn copy_slot_creates_independent_reference() {
        let mut matrix = SessionMatrix::default();
        matrix.ensure_track_slots("track 1");
        matrix.slot_mut("track 1", 0).unwrap().clip = Some(SlotClipRef {
            clip_id: "clip-1".to_string(),
            launch_mode: LaunchMode::Toggle,
            launch_quantization: LaunchQuantization::Bar,
            loop_enabled: true,
            loop_start_samples: 0,
            loop_end_samples: 0,
        });
        matrix.add_scene();
        matrix.add_scene();
        matrix.add_scene();
        assert!(matrix.copy_slot("track 1", 0, "track 1", 3));
        assert_eq!(
            matrix
                .slot("track 1", 0)
                .unwrap()
                .clip
                .as_ref()
                .unwrap()
                .clip_id,
            "clip-1"
        );
        assert_eq!(
            matrix
                .slot("track 1", 3)
                .unwrap()
                .clip
                .as_ref()
                .unwrap()
                .clip_id,
            "clip-1"
        );
    }

    #[test]
    fn copy_slot_from_empty_source_returns_false() {
        let mut matrix = SessionMatrix::default();
        matrix.ensure_track_slots("track 1");
        assert!(!matrix.copy_slot("track 1", 0, "track 1", 3));
    }
}