phosphor-app 0.3.19

Shared business logic for Phosphor DAW frontends
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
//! Menu state — SpaceMenu, FxMenu, InstrumentModal, FX types.

// ── FX System ──

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FxType {
    Reverb,
    Delay,
    Gate,
    Eq,
    Limiter,
    Compressor,
}

impl FxType {
    pub fn label(self) -> &'static str {
        match self {
            Self::Reverb => "reverb",
            Self::Delay => "delay",
            Self::Gate => "gate",
            Self::Eq => "eq",
            Self::Limiter => "limiter",
            Self::Compressor => "comp",
        }
    }

    pub const ALL: &[FxType] = &[
        Self::Reverb, Self::Delay, Self::Gate, Self::Eq, Self::Limiter, Self::Compressor,
    ];
}

/// An FX instance on a track.
#[derive(Debug, Clone)]
pub struct FxInstance {
    pub fx_type: FxType,
    pub enabled: bool,
    /// Placeholder parameter values (0.0..1.0).
    pub params: Vec<(String, f32)>,
}

impl FxInstance {
    pub fn new(fx_type: FxType) -> Self {
        let params = match fx_type {
            FxType::Reverb => vec![
                ("mix".into(), 0.3), ("decay".into(), 0.5), ("size".into(), 0.6),
            ],
            FxType::Delay => vec![
                ("time".into(), 0.4), ("feedback".into(), 0.3), ("mix".into(), 0.25),
            ],
            FxType::Gate => vec![
                ("thresh".into(), 0.5), ("attack".into(), 0.1), ("release".into(), 0.3),
            ],
            FxType::Eq => vec![
                ("low".into(), 0.5), ("mid".into(), 0.5), ("high".into(), 0.5),
            ],
            FxType::Limiter => vec![
                ("thresh".into(), 0.8), ("release".into(), 0.2),
            ],
            FxType::Compressor => vec![
                ("thresh".into(), 0.6), ("ratio".into(), 0.4), ("attack".into(), 0.1),
                ("release".into(), 0.3),
            ],
        };
        Self { fx_type, enabled: true, params }
    }
}

/// FX menu state (opened when pressing Enter on fx button).
#[derive(Debug)]
pub struct FxMenu {
    pub open: bool,
    pub cursor: usize,
}

impl Default for FxMenu {
    fn default() -> Self { Self::new() }
}

impl FxMenu {
    pub fn new() -> Self {
        Self { open: false, cursor: 0 }
    }

    pub fn item_count(&self) -> usize {
        FxType::ALL.len()
    }

    pub fn move_up(&mut self) {
        if self.cursor > 0 { self.cursor -= 1; }
    }

    pub fn move_down(&mut self) {
        if self.cursor + 1 < self.item_count() { self.cursor += 1; }
    }
}

// ── Instrument Selection Modal ──

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstrumentType {
    Synth,
    DrumRack,
    DX7,
    Jupiter8,
    Odyssey,
    Juno60,
    Sampler,
}

impl InstrumentType {
    pub fn label(self) -> &'static str {
        match self {
            Self::Synth => "Phosphor Synth",
            Self::DrumRack => "Drum Rack",
            Self::DX7 => "DX7",
            Self::Jupiter8 => "Jupiter-8",
            Self::Odyssey => "Odyssey",
            Self::Juno60 => "Juno-60",
            Self::Sampler => "Sampler",
        }
    }

    pub fn description(self) -> &'static str {
        match self {
            Self::Synth => "polyphonic subtractive synthesizer",
            Self::DrumRack => "drum machine with sample pads",
            Self::DX7 => "6-operator FM synthesizer",
            Self::Jupiter8 => "dual-VCO analog poly synthesizer",
            Self::Odyssey => "duophonic synth with 3 filter types",
            Self::Juno60 => "single-DCO poly with BBD chorus",
            Self::Sampler => "sample-based instrument",
        }
    }

    pub const ALL: &[InstrumentType] = &[Self::Synth, Self::DrumRack, Self::DX7, Self::Jupiter8, Self::Odyssey, Self::Juno60, Self::Sampler];
}

#[derive(Debug)]
pub struct InstrumentModal {
    pub open: bool,
    pub cursor: usize,
}

impl Default for InstrumentModal {
    fn default() -> Self { Self::new() }
}

impl InstrumentModal {
    pub fn new() -> Self {
        Self { open: false, cursor: 0 }
    }

    pub fn move_up(&mut self) {
        if self.cursor > 0 { self.cursor -= 1; }
    }

    pub fn move_down(&mut self) {
        if self.cursor + 1 < InstrumentType::ALL.len() { self.cursor += 1; }
    }

    pub fn selected(&self) -> InstrumentType {
        InstrumentType::ALL[self.cursor]
    }
}

// ── Preset Browser Modal ──

/// The user-preset browser for the track under the cursor.
///
/// A modal rather than extra entries on the patch knob: the patch selector
/// stores a normalised fraction, so lengthening the bank it indexes would
/// remap every value already saved in a session. Browsing presets in their own
/// list moves nothing.
#[derive(Debug)]
pub struct PresetModal {
    pub open: bool,
    /// Whose bank this is. `None` until the modal is opened on a track.
    pub instrument: Option<InstrumentType>,
    /// The track the bank was opened for. Held so a load lands on that track
    /// even if something moved the cursor while the modal was up.
    pub track_idx: usize,
    pub cursor: usize,
    /// Preset names in bank order, read when the modal opened.
    pub entries: Vec<String>,
    /// Why the bank could not be read, when it could not.
    pub error: Option<String>,
    /// Name waiting on an overwrite confirmation.
    pub pending_name: String,
}

impl Default for PresetModal {
    fn default() -> Self { Self::new() }
}

impl PresetModal {
    /// Row 0 is always "save the current panel"; the presets follow it.
    pub const SAVE_ROW: usize = 0;

    pub fn new() -> Self {
        Self {
            open: false,
            instrument: None,
            track_idx: 0,
            cursor: 0,
            entries: Vec::new(),
            error: None,
            pending_name: String::new(),
        }
    }

    pub fn show(&mut self, instrument: InstrumentType, track_idx: usize, entries: Vec<String>) {
        self.open = true;
        self.instrument = Some(instrument);
        self.track_idx = track_idx;
        self.cursor = 0;
        self.entries = entries;
        self.error = None;
        self.pending_name.clear();
    }

    pub fn close(&mut self) {
        self.open = false;
        self.entries.clear();
        self.error = None;
        self.pending_name.clear();
        self.cursor = 0;
    }

    /// Rows in the list: the save row plus one per preset.
    pub fn item_count(&self) -> usize { self.entries.len() + 1 }

    pub fn move_up(&mut self) {
        if self.cursor > 0 { self.cursor -= 1; }
    }

    pub fn move_down(&mut self) {
        if self.cursor + 1 < self.item_count() { self.cursor += 1; }
    }

    /// Index into the bank for the selected row, or `None` on the save row.
    pub fn selected_preset(&self) -> Option<usize> {
        self.cursor.checked_sub(1).filter(|i| *i < self.entries.len())
    }

    /// Name of the selected preset, or `None` on the save row.
    pub fn selected_name(&self) -> Option<&str> {
        self.selected_preset().map(|i| self.entries[i].as_str())
    }

    /// Replace the list after a save or delete, keeping the cursor on
    /// something that exists.
    pub fn set_entries(&mut self, entries: Vec<String>) {
        self.entries = entries;
        let max = self.item_count() - 1;
        if self.cursor > max { self.cursor = max; }
    }
}

// ── Space Menu ──

/// Actions that can be triggered from the space menu.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpaceAction {
    PlayPause,
    ToggleRecord,
    ToggleLoop,
    ToggleMetronome,
    Panic,
    Save,
    Open,
    AddInstrument,
    Delete,
    CycleTheme,
    NewTrack,
    EditMode,
    Quantize,
    Presets,
}

// ── Confirmation Modal ──

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfirmKind {
    DeleteTrack,
    DeleteClip,
    DeletePreset,
    /// Saving over a preset name the bank already holds.
    OverwritePreset,
}

#[derive(Debug)]
pub struct ConfirmModal {
    pub open: bool,
    pub kind: ConfirmKind,
    pub message: String,
}

impl Default for ConfirmModal {
    fn default() -> Self { Self::new() }
}

impl ConfirmModal {
    pub fn new() -> Self {
        Self { open: false, kind: ConfirmKind::DeleteTrack, message: String::new() }
    }

    pub fn show(&mut self, kind: ConfirmKind, message: &str) {
        self.open = true;
        self.kind = kind;
        self.message = message.to_string();
    }

    pub fn close(&mut self) {
        self.open = false;
        self.message.clear();
    }
}

// ── Input Modal (for file path entry) ──

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputModalKind {
    SaveAs,
    Open,
    /// Naming a user preset from the preset browser.
    PresetName,
}

#[derive(Debug)]
pub struct InputModal {
    pub open: bool,
    pub kind: InputModalKind,
    pub buffer: String,
    pub cursor: usize,
}

impl Default for InputModal {
    fn default() -> Self { Self::new() }
}

impl InputModal {
    pub fn new() -> Self {
        Self { open: false, kind: InputModalKind::SaveAs, buffer: String::new(), cursor: 0 }
    }

    pub fn open_save(&mut self, default_name: &str) {
        self.open = true;
        self.kind = InputModalKind::SaveAs;
        self.buffer = format!("sessions/{default_name}");
        self.cursor = self.buffer.len();
    }

    pub fn open_load(&mut self) {
        self.open = true;
        self.kind = InputModalKind::Open;
        self.buffer = "sessions/".to_string();
        self.cursor = self.buffer.len();
    }

    /// Name a user preset. Starts empty rather than on a suggestion, because
    /// a suggestion the player accepts by reflex is how a bank fills up with
    /// eight sounds called "juno".
    pub fn open_preset_name(&mut self) {
        self.open = true;
        self.kind = InputModalKind::PresetName;
        self.buffer.clear();
        self.cursor = 0;
    }

    pub fn type_char(&mut self, ch: char) {
        self.buffer.insert(self.cursor, ch);
        self.cursor += 1;
    }

    pub fn backspace(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
            self.buffer.remove(self.cursor);
        }
    }

    pub fn delete(&mut self) {
        if self.cursor < self.buffer.len() {
            self.buffer.remove(self.cursor);
        }
    }

    pub fn move_left(&mut self) {
        if self.cursor > 0 { self.cursor -= 1; }
    }

    pub fn move_right(&mut self) {
        if self.cursor < self.buffer.len() { self.cursor += 1; }
    }

    pub fn move_home(&mut self) {
        self.cursor = 0;
    }

    pub fn move_end(&mut self) {
        self.cursor = self.buffer.len();
    }

    pub fn close(&mut self) {
        self.open = false;
        self.buffer.clear();
        self.cursor = 0;
    }

    pub fn value(&self) -> &str {
        &self.buffer
    }
}

/// The space menu: press Space to open, Space again to close.
/// Shows all Space+key shortcuts, actions, and help topics.
#[derive(Debug)]
pub struct SpaceMenu {
    pub open: bool,
    pub cursor: usize,
    /// Which section is active.
    pub section: SpaceMenuSection,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpaceMenuSection {
    /// Main shortcuts list.
    Actions,
    /// Help topics.
    Help,
}

impl Default for SpaceMenu {
    fn default() -> Self { Self::new() }
}

impl SpaceMenu {
    pub fn new() -> Self {
        Self { open: false, cursor: 0, section: SpaceMenuSection::Actions }
    }

    pub fn toggle(&mut self) {
        self.open = !self.open;
        if self.open { self.cursor = 0; self.section = SpaceMenuSection::Actions; }
    }

    pub fn move_up(&mut self) {
        if self.cursor > 0 { self.cursor -= 1; }
    }

    pub fn move_down(&mut self) {
        let max = self.item_count();
        if self.cursor + 1 < max { self.cursor += 1; }
    }

    pub fn switch_section(&mut self) {
        self.section = match self.section {
            SpaceMenuSection::Actions => SpaceMenuSection::Help,
            SpaceMenuSection::Help => SpaceMenuSection::Actions,
        };
        self.cursor = 0;
    }

    fn item_count(&self) -> usize {
        match self.section {
            SpaceMenuSection::Actions => SPACE_ACTIONS.len(),
            SpaceMenuSection::Help => HELP_TOPICS.len(),
        }
    }
}

/// Space menu action entries: (key, label, description).
pub const SPACE_ACTIONS: &[(&str, &str, &str)] = &[
    ("spc+1", "transport", "focus transport controls"),
    ("spc+2", "tracks",    "focus the tracks panel"),
    ("spc+3", "clip view", "focus clip / piano roll panel"),
    ("spc+p", "play/pause","toggle transport playback"),
    ("spc+r", "record",    "toggle global recording"),
    ("spc+l", "loop",      "edit loop region"),
    ("spc+m", "metronome", "toggle click track"),
    ("spc+!", "panic",     "kill all sound immediately"),
    ("spc+a", "add instr", "add instrument track"),
    ("spc+s", "save",      "save project"),
    ("spc+o", "open",      "open project"),
    ("spc+d", "delete",    "delete selected track/clip"),
    ("spc+e", "edit mode", "note-level piano roll editing"),
    ("spc+q", "quantize",  "snap notes to grid"),
    ("spc+w", "presets",   "save / load instrument presets"),
    ("spc+v", "vibe",      "cycle color theme"),
    ("spc+h", "help",      "open help topics"),
];

// ── Quantize Modal ──

use super::clip_view::GridResolution;

#[derive(Debug)]
pub struct QuantizeModal {
    pub open: bool,
    pub grid: GridResolution,
    pub strength: u8,
    pub cursor: usize,
}

impl Default for QuantizeModal {
    fn default() -> Self { Self::new() }
}

impl QuantizeModal {
    pub fn new() -> Self {
        Self { open: false, grid: GridResolution::Eighth, strength: 100, cursor: 0 }
    }
    pub fn open_with(&mut self, grid: GridResolution) {
        self.open = true;
        self.grid = grid;
        self.strength = 100;
        self.cursor = 0;
    }
    pub fn close(&mut self) { self.open = false; }
    pub fn move_up(&mut self) { if self.cursor > 0 { self.cursor -= 1; } }
    pub fn move_down(&mut self) { if self.cursor < 2 { self.cursor += 1; } }
    pub fn adjust(&mut self, direction: i32) {
        match self.cursor {
            0 => { if direction > 0 { self.grid = self.grid.next(); } else { self.grid = self.grid.prev(); } }
            1 => { self.strength = (self.strength as i32 + direction * 25).clamp(25, 100) as u8; }
            _ => {}
        }
    }
}

/// Help topic entries: (title, short description).
pub const HELP_TOPICS: &[(&str, &str)] = &[
    ("navigation",  "moving between tracks, clips, and panes"),
    ("transport",   "play, pause, stop, record, loop, BPM"),
    ("tracks",      "mute, solo, arm, fx, volume, routing"),
    ("clips",       "selecting, jumping, clip-level fx"),
    ("piano roll",  "editing MIDI notes, velocity, quantize"),
    ("fx & mixing", "adding effects, sends, master bus"),
    ("shortcuts",   "full keyboard shortcut reference"),
    ("plugins",     "loading and managing plugins"),
];