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
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
// 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 std::sync::Arc;
use std::time::Duration;

use crossterm::event::{KeyCode, KeyEvent};
use tokio::sync::watch;

use crate::player::Player;
use crate::state::{FixtureSnapshot, StateSnapshot};

/// Actions the TUI main loop should take after handling an event.
pub enum Action {
    None,
    Quit,
}

/// Cached fixture color for rendering.
pub struct FixtureColor {
    pub name: String,
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

/// Application state for the TUI, updated each tick.
pub struct App {
    player: Arc<Player>,
    state_rx: watch::Receiver<Arc<StateSnapshot>>,

    // Playlist state
    pub playlist_name: String,
    pub song_names: Vec<String>,
    pub current_index: usize,

    // Now Playing state
    pub current_song_name: String,
    pub current_song_duration: Duration,
    pub current_song_tracks: Vec<String>,
    pub is_playing: bool,
    pub elapsed: Option<Duration>,

    // Lighting state
    pub fixture_colors: Vec<FixtureColor>,
    pub active_effects: Vec<String>,

    // Log buffer
    pub log_lines: Vec<String>,
}

impl App {
    pub fn new(player: Arc<Player>, state_rx: watch::Receiver<Arc<StateSnapshot>>) -> Self {
        let playlist = player.get_playlist();
        let song_names: Vec<String> = playlist.songs().to_vec();
        let current = playlist.current();

        Self {
            player,
            state_rx,
            playlist_name: playlist.name().to_string(),
            song_names,
            current_index: 0,
            current_song_name: current
                .as_ref()
                .map(|s| s.name().to_string())
                .unwrap_or_default(),
            current_song_duration: current
                .as_ref()
                .map(|s| s.duration())
                .unwrap_or(std::time::Duration::ZERO),
            current_song_tracks: current
                .as_ref()
                .map(|s| s.tracks().iter().map(|t| t.name().to_string()).collect())
                .unwrap_or_default(),
            is_playing: false,
            elapsed: None,
            fixture_colors: Vec::new(),
            active_effects: Vec::new(),
            log_lines: Vec::new(),
        }
    }

    /// Polls the player for current state. Called each tick (~15 FPS).
    pub async fn tick(&mut self) {
        // Update playlist state
        let playlist = self.player.get_playlist();
        self.playlist_name = playlist.name().to_string();
        self.song_names = playlist.songs().to_vec();
        self.current_index = playlist.position();

        if let Some(current) = playlist.current() {
            self.current_song_name = current.name().to_string();
            self.current_song_duration = current.duration();
            self.current_song_tracks = current
                .tracks()
                .iter()
                .map(|t| t.name().to_string())
                .collect();
        } else {
            self.current_song_name = String::new();
            self.current_song_duration = std::time::Duration::ZERO;
            self.current_song_tracks = vec![];
        }

        // Update playback state
        self.is_playing = self.player.is_playing().await;
        self.elapsed = self.player.elapsed().await.unwrap_or(None);

        // Update lighting state from the shared watch channel
        let snapshot = self.state_rx.borrow_and_update().clone();
        self.fixture_colors = fixture_colors_from_snapshot(&snapshot.fixtures);
        self.active_effects = snapshot.active_effects.clone();

        // Update log buffer (acquire parking_lot mutex off the async runtime).
        if let Some(buffer) = super::logging::get_log_buffer() {
            let buffer = buffer.lock();
            self.log_lines = buffer.iter().cloned().collect();
        }
    }

    /// Processes a keyboard event and returns the action to take.
    pub async fn handle_key_event(&mut self, key: KeyEvent) -> Action {
        match key.code {
            KeyCode::Char('q') | KeyCode::Esc => {
                self.player.stop().await;
                Action::Quit
            }
            KeyCode::Char(' ') | KeyCode::Enter => {
                if self.is_playing {
                    self.player.stop().await;
                } else {
                    let _ = self.player.play().await;
                }
                Action::None
            }
            KeyCode::Right | KeyCode::Char('n') => {
                self.player.next().await;
                Action::None
            }
            KeyCode::Left | KeyCode::Char('p') => {
                self.player.prev().await;
                Action::None
            }
            KeyCode::Char('a') => {
                let _ = self.player.switch_to_playlist("all_songs").await;
                Action::None
            }
            KeyCode::Char('l') => {
                let name = self.player.persisted_playlist_name();
                let _ = self.player.switch_to_playlist(&name).await;
                Action::None
            }
            _ => Action::None,
        }
    }
}

/// Converts fixture snapshots into display-ready colors by extracting RGB channels.
fn fixture_colors_from_snapshot(fixtures: &[FixtureSnapshot]) -> Vec<FixtureColor> {
    fixtures
        .iter()
        .map(|f| {
            let r = f.channels.get("red").copied().unwrap_or(0);
            let g = f.channels.get("green").copied().unwrap_or(0);
            let b = f.channels.get("blue").copied().unwrap_or(0);
            FixtureColor {
                name: f.name.clone(),
                r,
                g,
                b,
            }
        })
        .collect()
}

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

    use crate::config;
    use crate::player::PlayerDevices;
    use crate::playlist;
    use crate::songs::{Song, Songs};

    fn make_fixture(name: &str, channels: &[(&str, u8)]) -> FixtureSnapshot {
        let mut map = HashMap::new();
        for (k, v) in channels {
            map.insert(k.to_string(), *v);
        }
        FixtureSnapshot {
            name: name.to_string(),
            channels: map,
        }
    }

    /// Creates a test Player with no hardware devices.
    fn test_player(song_names: &[&str]) -> Arc<Player> {
        let mut map = HashMap::new();
        for name in song_names {
            map.insert(
                name.to_string(),
                Arc::new(Song::new_for_test(name, &["track1"])),
            );
        }
        let songs = Arc::new(Songs::new(map));
        let playlist_config =
            config::Playlist::new(&song_names.iter().map(|s| s.to_string()).collect::<Vec<_>>());
        let pl = playlist::Playlist::new("test", &playlist_config, songs.clone()).unwrap();
        let devices = PlayerDevices {
            audio: None,
            mappings: None,
            midi: None,
            dmx_engine: None,
            sample_engine: None,
            trigger_engine: None,
        };
        let mut playlists = HashMap::new();
        playlists.insert(
            "all_songs".to_string(),
            playlist::from_songs(songs.clone()).unwrap(),
        );
        playlists.insert(pl.name().to_string(), pl);
        Arc::new(Player::new_with_devices(devices, playlists, "test".to_string(), None).unwrap())
    }

    fn test_app(song_names: &[&str]) -> App {
        let player = test_player(song_names);
        let (state_tx, state_rx) = watch::channel(Arc::new(StateSnapshot::default()));
        let _ = state_tx; // keep alive
        App::new(player, state_rx)
    }

    mod fixture_colors_from_snapshot_tests {
        use super::*;

        #[test]
        fn empty_fixtures() {
            let result = fixture_colors_from_snapshot(&[]);
            assert!(result.is_empty());
        }

        #[test]
        fn full_rgb() {
            let fixtures = vec![make_fixture(
                "spot1",
                &[("red", 255), ("green", 128), ("blue", 64)],
            )];
            let colors = fixture_colors_from_snapshot(&fixtures);
            assert_eq!(colors.len(), 1);
            assert_eq!(colors[0].name, "spot1");
            assert_eq!(colors[0].r, 255);
            assert_eq!(colors[0].g, 128);
            assert_eq!(colors[0].b, 64);
        }

        #[test]
        fn missing_channels_default_to_zero() {
            let fixtures = vec![make_fixture("dimmer", &[("intensity", 200)])];
            let colors = fixture_colors_from_snapshot(&fixtures);
            assert_eq!(colors[0].r, 0);
            assert_eq!(colors[0].g, 0);
            assert_eq!(colors[0].b, 0);
        }

        #[test]
        fn partial_rgb() {
            let fixtures = vec![make_fixture("par", &[("red", 100), ("blue", 50)])];
            let colors = fixture_colors_from_snapshot(&fixtures);
            assert_eq!(colors[0].r, 100);
            assert_eq!(colors[0].g, 0);
            assert_eq!(colors[0].b, 50);
        }

        #[test]
        fn multiple_fixtures() {
            let fixtures = vec![
                make_fixture("wash1", &[("red", 255), ("green", 0), ("blue", 0)]),
                make_fixture("wash2", &[("red", 0), ("green", 255), ("blue", 0)]),
                make_fixture("wash3", &[("red", 0), ("green", 0), ("blue", 255)]),
            ];
            let colors = fixture_colors_from_snapshot(&fixtures);
            assert_eq!(colors.len(), 3);
            assert_eq!(colors[0].r, 255);
            assert_eq!(colors[1].g, 255);
            assert_eq!(colors[2].b, 255);
        }

        #[test]
        fn extra_channels_ignored() {
            let fixtures = vec![make_fixture(
                "moving_head",
                &[
                    ("red", 10),
                    ("green", 20),
                    ("blue", 30),
                    ("pan", 180),
                    ("tilt", 90),
                ],
            )];
            let colors = fixture_colors_from_snapshot(&fixtures);
            assert_eq!(colors[0].r, 10);
            assert_eq!(colors[0].g, 20);
            assert_eq!(colors[0].b, 30);
        }

        #[test]
        fn preserves_fixture_names() {
            let fixtures = vec![
                make_fixture("Front Wash Left", &[]),
                make_fixture("Back Spot", &[]),
            ];
            let colors = fixture_colors_from_snapshot(&fixtures);
            assert_eq!(colors[0].name, "Front Wash Left");
            assert_eq!(colors[1].name, "Back Spot");
        }
    }

    mod app_new_tests {
        use super::*;

        #[test]
        fn initializes_playlist_name() {
            let app = test_app(&["Song A", "Song B"]);
            assert_eq!(app.playlist_name, "test");
        }

        #[test]
        fn initializes_song_names() {
            let app = test_app(&["Song A", "Song B"]);
            assert_eq!(app.song_names, vec!["Song A", "Song B"]);
        }

        #[test]
        fn initializes_current_song() {
            let app = test_app(&["Song A", "Song B"]);
            assert_eq!(app.current_song_name, "Song A");
        }

        #[test]
        fn initializes_current_index_zero() {
            let app = test_app(&["Song A", "Song B"]);
            assert_eq!(app.current_index, 0);
        }

        #[test]
        fn initializes_not_playing() {
            let app = test_app(&["Song A"]);
            assert!(!app.is_playing);
        }

        #[test]
        fn initializes_elapsed_none() {
            let app = test_app(&["Song A"]);
            assert!(app.elapsed.is_none());
        }

        #[test]
        fn initializes_empty_fixtures() {
            let app = test_app(&["Song A"]);
            assert!(app.fixture_colors.is_empty());
        }

        #[test]
        fn initializes_empty_effects() {
            let app = test_app(&["Song A"]);
            assert!(app.active_effects.is_empty());
        }

        #[test]
        fn initializes_tracks_from_song() {
            let app = test_app(&["Song A"]);
            assert_eq!(app.current_song_tracks, vec!["track1"]);
        }
    }

    mod handle_key_event_tests {
        use super::*;
        use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

        fn key(code: KeyCode) -> KeyEvent {
            KeyEvent::new(code, KeyModifiers::NONE)
        }

        #[tokio::test]
        async fn quit_on_q() {
            let mut app = test_app(&["Song A"]);
            let action = app.handle_key_event(key(KeyCode::Char('q'))).await;
            assert!(matches!(action, Action::Quit));
        }

        #[tokio::test]
        async fn quit_on_esc() {
            let mut app = test_app(&["Song A"]);
            let action = app.handle_key_event(key(KeyCode::Esc)).await;
            assert!(matches!(action, Action::Quit));
        }

        #[tokio::test]
        async fn space_toggles_play() {
            let mut app = test_app(&["Song A"]);
            let action = app.handle_key_event(key(KeyCode::Char(' '))).await;
            assert!(matches!(action, Action::None));
        }

        #[tokio::test]
        async fn enter_toggles_play() {
            let mut app = test_app(&["Song A"]);
            let action = app.handle_key_event(key(KeyCode::Enter)).await;
            assert!(matches!(action, Action::None));
        }

        #[tokio::test]
        async fn right_arrow_next() {
            let mut app = test_app(&["Song A", "Song B"]);
            let action = app.handle_key_event(key(KeyCode::Right)).await;
            assert!(matches!(action, Action::None));
        }

        #[tokio::test]
        async fn n_key_next() {
            let mut app = test_app(&["Song A", "Song B"]);
            let action = app.handle_key_event(key(KeyCode::Char('n'))).await;
            assert!(matches!(action, Action::None));
        }

        #[tokio::test]
        async fn left_arrow_prev() {
            let mut app = test_app(&["Song A", "Song B"]);
            let action = app.handle_key_event(key(KeyCode::Left)).await;
            assert!(matches!(action, Action::None));
        }

        #[tokio::test]
        async fn p_key_prev() {
            let mut app = test_app(&["Song A", "Song B"]);
            let action = app.handle_key_event(key(KeyCode::Char('p'))).await;
            assert!(matches!(action, Action::None));
        }

        #[tokio::test]
        async fn a_key_all_songs() {
            let mut app = test_app(&["Song A"]);
            let action = app.handle_key_event(key(KeyCode::Char('a'))).await;
            assert!(matches!(action, Action::None));
        }

        #[tokio::test]
        async fn l_key_playlist() {
            let mut app = test_app(&["Song A"]);
            let action = app.handle_key_event(key(KeyCode::Char('l'))).await;
            assert!(matches!(action, Action::None));
        }

        #[tokio::test]
        async fn unhandled_key_returns_none() {
            let mut app = test_app(&["Song A"]);
            let action = app.handle_key_event(key(KeyCode::Char('z'))).await;
            assert!(matches!(action, Action::None));
        }
    }

    mod tick_tests {
        use super::*;

        #[tokio::test]
        async fn tick_updates_state() {
            let player = test_player(&["Song A", "Song B"]);
            let snapshot = Arc::new(StateSnapshot {
                fixtures: vec![make_fixture("spot", &[("red", 255)])],
                active_effects: vec!["chase".to_string()],
            });
            let (_tx, state_rx) = watch::channel(snapshot);
            let mut app = App::new(player, state_rx);

            app.tick().await;

            assert_eq!(app.fixture_colors.len(), 1);
            assert_eq!(app.fixture_colors[0].r, 255);
            assert_eq!(app.active_effects, vec!["chase"]);
        }

        #[tokio::test]
        async fn tick_reflects_current_song() {
            let mut app = test_app(&["Song A", "Song B"]);
            app.tick().await;
            assert_eq!(app.current_song_name, "Song A");
            assert_eq!(app.current_index, 0);
        }

        #[tokio::test]
        async fn tick_after_next_updates_song() {
            let mut app = test_app(&["Song A", "Song B"]);
            app.handle_key_event(KeyEvent::new(
                KeyCode::Right,
                crossterm::event::KeyModifiers::NONE,
            ))
            .await;
            app.tick().await;
            assert_eq!(app.current_song_name, "Song B");
            assert_eq!(app.current_index, 1);
        }
    }
}