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
use iced::Color;
use maolan_engine as engine;
use std::{sync::LazyLock, time::Duration};

pub const APP_BACKGROUND_COLOR: Color = Color::from_rgb8(23, 31, 48);

pub mod gui {
    use super::Duration;

    pub const MIN_CLIP_WIDTH_PX: f32 = 12.0;
    pub const PREF_DEVICE_AUTO_ID: &str = "__auto__";
    pub const METER_DIRTY_EPSILON_DB: f32 = 0.5;
    pub const METER_QUANTIZE_STEP_DB: f32 = 1.0;
    pub const AUTOSAVE_SNAPSHOT_INTERVAL: Duration = Duration::from_secs(60);
}

pub mod audio_defaults {
    pub use crate::audio_defaults::*;
}

pub mod workspace {
    use super::Color;

    pub const MIN_TIMELINE_BARS: f32 = 256.0;
    pub const PLAYHEAD_WIDTH_PX: f32 = 1.0;
    pub const TIMELINE_LEFT_INSET_PX: f32 = 0.0;

    pub const RULER_HEIGHT: f32 = 28.0;
    pub const BEATS_PER_BAR: usize = 4;
    pub const MIN_TICK_SPACING_PX: f32 = 8.0;
    pub const MIN_LABEL_SPACING_PX: f32 = 64.0;

    pub const TEMPO_HEIGHT: f32 = 56.0;
    pub const TEMPO_HIT_HEIGHT: f32 = 14.0;
    pub const TIME_SIG_HIT_X_SPLIT: f32 = 36.0;
    pub const LEFT_HIT_WIDTH: f32 = 84.0;
    pub const CONTEXT_MENU_WIDTH: f32 = 132.0;
    pub const CONTEXT_MENU_ITEM_HEIGHT: f32 = 16.0;

    pub const AUDIO_CLIP_SELECTED_BASE: Color = Color::from_rgb8(96, 126, 186);
    pub const MIDI_CLIP_SELECTED_BASE: Color = Color::from_rgb8(84, 133, 72);
    pub const MIDI_CLIP_BORDER: Color = Color::from_rgb8(148, 215, 118);
}

pub mod ui_timing {
    use super::Duration;

    pub const DOUBLE_CLICK: Duration = Duration::from_millis(350);
    pub const PLAYHEAD_UPDATE_INTERVAL: Duration = Duration::from_millis(50);
    pub const RECORDING_PREVIEW_UPDATE_INTERVAL: Duration = Duration::from_secs(1);
    pub const RECORDING_PREVIEW_PEAKS_UPDATE_INTERVAL: Duration = Duration::from_secs(2);
}

pub mod platform_caps {
    pub const HAS_SEPARATE_AUDIO_INPUT_DEVICE: bool = cfg!(any(
        target_os = "linux",
        target_os = "freebsd",
        target_os = "openbsd"
    ));
    pub const REQUIRE_SAMPLE_RATES_FOR_HW_READY: bool = cfg!(any(
        target_os = "linux",
        target_os = "freebsd",
        target_os = "openbsd"
    ));
    pub const REQUIRE_VST3_STATE_FOR_SAVE: bool = cfg!(target_os = "macos");
    pub const SUPPORTS_PLUGIN_GRAPH: bool = true;
}

pub mod main {
    pub const ICON_BYTES: &[u8] = include_bytes!("../images/icon.png");
}

pub mod workspace_ids {
    pub const EDITOR_SCROLL_ID: &str = "workspace.editor.scroll";
    pub const EDITOR_TIMELINE_SCROLL_ID: &str = "workspace.editor.timeline.scroll";
    pub const TRACKS_SCROLL_ID: &str = "workspace.tracks.scroll";
    pub const WORKSPACE_TEMPO_SCROLL_ID: &str = "workspace.tempo.scroll";
    pub const WORKSPACE_RULER_SCROLL_ID: &str = "workspace.ruler.scroll";
    pub const PIANO_TEMPO_SCROLL_ID: &str = "workspace.piano.tempo.scroll";
    pub const PIANO_RULER_SCROLL_ID: &str = "workspace.piano.ruler.scroll";
}

pub mod state_ids {
    pub const HW_IN_ID: &str = "hw:in";
    pub const HW_OUT_ID: &str = "hw:out";
    pub const METRONOME_TRACK_ID: &str = "metronome";
    pub const MIDI_HW_IN_ID: &str = "midi:hw:in";
    pub const MIDI_HW_OUT_ID: &str = "midi:hw:out";
}

pub mod state_track {
    pub const TRACK_FOLDER_HEADER_HEIGHT: f32 = 24.0;
    pub const TRACK_FOLDER_BODY_HEIGHT: f32 = 28.0;
    pub const TRACK_SUBTRACK_GAP: f32 = 2.0;
    pub const TRACK_SUBTRACK_MIN_HEIGHT: f32 = 18.0;
    pub const TRACK_MIN_HEIGHT: f32 = 82.0;
}

pub mod connections_plugins {
    pub const PLUGIN_W: f32 = 170.0;
    pub const MIN_PLUGIN_H: f32 = 96.0;
    pub const AUDIO_PORT_RADIUS: f32 = 4.5;
    pub const MIDI_PORT_RADIUS: f32 = 3.5;
    pub const MIN_PORT_GAP: f32 = 2.0;
    pub const PORT_HIT_RADIUS: f32 = AUDIO_PORT_RADIUS + 2.0;
    pub const MIDI_HIT_RADIUS: f32 = MIDI_PORT_RADIUS + 2.0;
    pub const TRACK_IO_W: f32 = 86.0;
    pub const TRACK_IO_H: f32 = 200.0;
    pub const TRACK_IO_MARGIN_X: f32 = 24.0;
}

#[cfg(target_os = "freebsd")]
pub mod state_platform_freebsd {
    pub const AFMT_S16_LE: u64 = 0x00000010;
    pub const AFMT_S16_BE: u64 = 0x00000020;
    pub const AFMT_S8: u64 = 0x00000040;
    pub const AFMT_S32_LE: u64 = 0x00001000;
    pub const AFMT_S32_BE: u64 = 0x00002000;
    pub const AFMT_S24_LE: u64 = 0x00010000;
    pub const AFMT_S24_BE: u64 = 0x00020000;
}

pub mod widget_piano {
    pub use maolan_widgets::midi::{
        CTRL_SCROLL_ID, H_ZOOM_MAX, H_ZOOM_MIN, KEYBOARD_WIDTH, KEYS_SCROLL_ID, MAIN_SPLIT_SPACING,
        MAX_RPN_NRPN_POINTS, MIDI_DIN_BYTES_PER_SEC, MIDI_NOTE_COUNT, NOTES_PER_OCTAVE,
        NOTES_SCROLL_ID, OCTAVES, PITCH_MAX, RIGHT_SCROLL_GUTTER_WIDTH, SYSEX_SCROLL_ID,
        TOOLS_STRIP_WIDTH, WHITE_KEY_HEIGHT, WHITE_KEYS_PER_OCTAVE,
    };
}

pub mod workspace_mixer {
    use super::LazyLock;

    pub const FADER_MIN_DB: f32 = -90.0;
    pub const FADER_MAX_DB: f32 = 20.0;
    pub const STRIP_WIDTH: f32 = 96.0;
    pub const READOUT_WIDTH: f32 = 72.0;
    pub const FADER_WIDTH: f32 = 14.0;
    pub const SCALE_WIDTH: f32 = 22.0;
    pub const PAN_SLIDER_WIDTH: f32 = 52.0;
    pub const PAN_ROW_HEIGHT: f32 = 12.0;
    pub const STRIP_NAME_CHAR_PX: f32 = 6.3;
    pub const METER_BAR_WIDTH: f32 = 3.0;
    pub const METER_BAR_GAP: f32 = 2.0;
    pub const METER_PAD_X: f32 = 3.0;
    pub const BAY_INSET: f32 = 1.0;

    pub static LEVEL_LABELS: LazyLock<Vec<&'static str>> = LazyLock::new(|| {
        let mut labels = Vec::with_capacity(1101);
        for i in 0..=1100 {
            let level = -90.0 + (i as f32) * 0.1;
            let s: &'static str = Box::leak(format!("{:+.1} dB", level).into_boxed_str());
            labels.push(s);
        }
        labels
    });

    pub static BALANCE_LABELS: LazyLock<Vec<&'static str>> = LazyLock::new(|| {
        let mut labels = Vec::with_capacity(201);
        for i in -100..=100 {
            let s: &'static str = if i == 0 {
                "C"
            } else if i < 0 {
                Box::leak(format!("L{}", -i).into_boxed_str())
            } else {
                Box::leak(format!("R{}", i).into_boxed_str())
            };
            labels.push(s);
        }
        labels
    });
}

pub mod workspace_editor {
    pub const CHECKPOINTS: usize = 16;
}

pub mod gui_mod {
    use super::{LazyLock, engine};

    pub const MAX_RECENT_SESSIONS: usize = 12;
    pub const AUDIO_BIT_DEPTH_OPTIONS: [usize; 4] = [32, 24, 16, 8];
    pub const MAX_PEAK_BINS: usize = 32_768;
    pub const BINS_PER_UPDATE: usize = 2048;
    pub const CHUNK_FRAMES: usize = 16_384;
    pub static CLIENT: LazyLock<engine::client::Client> =
        LazyLock::new(engine::client::Client::default);
    pub const STANDARD_EXPORT_SAMPLE_RATES: [u32; 12] = [
        8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000, 384000,
    ];
}

pub mod gui_update_mod {
    pub const ATTACK_ALPHA: f32 = 0.60;
}

pub mod gui_update_dispatch_transport {
    pub const AUTOSAVE_KEEP_COUNT: usize = 10;
}

#[cfg(target_os = "linux")]
pub mod state_platform_linux {
    pub const SAMPLE_RATE_CANDIDATES: [u32; 12] = [
        8_000, 11_025, 16_000, 22_050, 32_000, 44_100, 48_000, 88_200, 96_000, 176_400, 192_000,
        384_000,
    ];
}

#[cfg(target_os = "freebsd")]
pub mod state_platform_freebsd_lists {
    pub const DIRECT_KEYS: [&str; 9] = [
        "formats",
        "iformats",
        "oformats",
        "pformats",
        "rformats",
        "playformats",
        "recformats",
        "pfmts",
        "rfmts",
    ];
    pub const RATE_KEYS: [&str; 8] = [
        "rates",
        "rate",
        "irates",
        "orates",
        "playrates",
        "recrates",
        "playback_rates",
        "capture_rates",
    ];
    pub const SAMPLE_RATE_CANDIDATES: [i32; 12] = [
        8_000, 11_025, 16_000, 22_050, 32_000, 44_100, 48_000, 88_200, 96_000, 176_400, 192_000,
        384_000,
    ];
}

pub mod message_lists {
    use crate::message::{
        ExportBitDepth, ExportMp3Mode, ExportNormalizeMode, ExportRenderMode, PianoChordKind,
        PianoNrpnKind, PianoRpnKind, PianoScaleRoot, PianoVelocityKind, SnapMode,
    };

    pub const SNAP_MODE_ALL: [SnapMode; 8] = [
        SnapMode::NoSnap,
        SnapMode::Clips,
        SnapMode::Bar,
        SnapMode::Beat,
        SnapMode::Eighth,
        SnapMode::Sixteenth,
        SnapMode::ThirtySecond,
        SnapMode::SixtyFourth,
    ];
    pub const PIANO_VELOCITY_KIND_ALL: [PianoVelocityKind; 2] = [
        PianoVelocityKind::NoteVelocity,
        PianoVelocityKind::ReleaseVelocity,
    ];
    pub const PIANO_RPN_KIND_ALL: [PianoRpnKind; 3] = [
        PianoRpnKind::PitchBendSensitivity,
        PianoRpnKind::FineTuning,
        PianoRpnKind::CoarseTuning,
    ];
    pub const PIANO_SCALE_ROOT_ALL: [PianoScaleRoot; 12] = [
        PianoScaleRoot::C,
        PianoScaleRoot::CSharp,
        PianoScaleRoot::D,
        PianoScaleRoot::DSharp,
        PianoScaleRoot::E,
        PianoScaleRoot::F,
        PianoScaleRoot::FSharp,
        PianoScaleRoot::G,
        PianoScaleRoot::GSharp,
        PianoScaleRoot::A,
        PianoScaleRoot::ASharp,
        PianoScaleRoot::B,
    ];
    pub const PIANO_CHORD_KIND_ALL: [PianoChordKind; 5] = [
        PianoChordKind::MajorTriad,
        PianoChordKind::MinorTriad,
        PianoChordKind::Dominant7,
        PianoChordKind::Major7,
        PianoChordKind::Minor7,
    ];
    pub const PIANO_NRPN_KIND_ALL: [PianoNrpnKind; 3] = [
        PianoNrpnKind::Brightness,
        PianoNrpnKind::VibratoRate,
        PianoNrpnKind::VibratoDepth,
    ];
    pub const EXPORT_NORMALIZE_MODE_ALL: [ExportNormalizeMode; 2] =
        [ExportNormalizeMode::Peak, ExportNormalizeMode::Loudness];
    pub const EXPORT_MP3_MODE_ALL: [ExportMp3Mode; 2] = [ExportMp3Mode::Cbr, ExportMp3Mode::Vbr];
    pub const EXPORT_RENDER_MODE_ALL: [ExportRenderMode; 3] = [
        ExportRenderMode::Mixdown,
        ExportRenderMode::StemsPostFader,
        ExportRenderMode::StemsPreFader,
    ];
    pub const EXPORT_BIT_DEPTH_ALL: [ExportBitDepth; 4] = [
        ExportBitDepth::Int16,
        ExportBitDepth::Int24,
        ExportBitDepth::Int32,
        ExportBitDepth::Float32,
    ];
}

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

    #[test]
    fn app_background_color_has_expected_values() {
        assert!((APP_BACKGROUND_COLOR.r - 0.09).abs() < 0.01);
        assert!((APP_BACKGROUND_COLOR.g - 0.12).abs() < 0.01);
        assert!((APP_BACKGROUND_COLOR.b - 0.19).abs() < 0.01);
    }

    #[test]
    fn gui_constants_are_expected_values() {
        assert_eq!(gui::PREF_DEVICE_AUTO_ID, "__auto__");
        assert_eq!(gui::AUTOSAVE_SNAPSHOT_INTERVAL.as_secs(), 60);
    }

    #[test]
    fn workspace_constants_are_expected_values() {
        assert_eq!(workspace::BEATS_PER_BAR, 4);
        assert_eq!(workspace::MIN_LABEL_SPACING_PX, 64.0);
    }

    #[test]
    fn ui_timing_constants_are_expected_values() {
        assert_eq!(ui_timing::DOUBLE_CLICK.as_millis(), 350);
        assert_eq!(ui_timing::PLAYHEAD_UPDATE_INTERVAL.as_millis(), 50);
        assert_eq!(ui_timing::RECORDING_PREVIEW_UPDATE_INTERVAL.as_secs(), 1);
    }

    #[test]
    fn main_icon_bytes_is_not_empty() {
        assert!(!main::ICON_BYTES.is_empty());
    }

    #[test]
    fn workspace_ids_are_unique() {
        let ids = [
            workspace_ids::EDITOR_SCROLL_ID,
            workspace_ids::EDITOR_TIMELINE_SCROLL_ID,
            workspace_ids::TRACKS_SCROLL_ID,
            workspace_ids::WORKSPACE_TEMPO_SCROLL_ID,
            workspace_ids::WORKSPACE_RULER_SCROLL_ID,
            workspace_ids::PIANO_TEMPO_SCROLL_ID,
            workspace_ids::PIANO_RULER_SCROLL_ID,
        ];
        let unique: std::collections::HashSet<_> = ids.iter().collect();
        assert_eq!(unique.len(), ids.len());
    }

    #[test]
    fn state_ids_are_expected_values() {
        assert_eq!(state_ids::HW_IN_ID, "hw:in");
        assert_eq!(state_ids::HW_OUT_ID, "hw:out");
        assert_eq!(state_ids::METRONOME_TRACK_ID, "metronome");
        assert_eq!(state_ids::MIDI_HW_IN_ID, "midi:hw:in");
        assert_eq!(state_ids::MIDI_HW_OUT_ID, "midi:hw:out");
    }

    #[test]
    fn state_track_constants_are_expected_values() {
        assert_eq!(state_track::TRACK_FOLDER_HEADER_HEIGHT, 24.0);
        assert_eq!(state_track::TRACK_SUBTRACK_GAP, 2.0);
        assert_eq!(state_track::TRACK_SUBTRACK_MIN_HEIGHT, 18.0);
    }

    #[cfg(all(unix, not(target_os = "macos")))]
    #[test]
    fn connections_plugins_constants_are_expected_values() {
        assert_eq!(connections_plugins::PLUGIN_W, 170.0);
        assert_eq!(connections_plugins::MIN_PLUGIN_H, 96.0);
        assert_eq!(connections_plugins::AUDIO_PORT_RADIUS, 4.5);
        assert_eq!(connections_plugins::MIDI_PORT_RADIUS, 3.5);
    }

    #[test]
    fn workspace_mixer_level_labels_is_populated() {
        assert!(!workspace_mixer::LEVEL_LABELS.is_empty());
        assert!(workspace_mixer::LEVEL_LABELS.len() > 1000);
    }

    #[test]
    fn workspace_mixer_balance_labels_is_populated() {
        assert!(!workspace_mixer::BALANCE_LABELS.is_empty());
        assert_eq!(workspace_mixer::BALANCE_LABELS.len(), 201);
    }

    #[test]
    fn gui_mod_constants_are_valid() {
        assert_eq!(gui_mod::MAX_RECENT_SESSIONS, 12);
        assert_eq!(gui_mod::AUDIO_BIT_DEPTH_OPTIONS, [32, 24, 16, 8]);
        assert_eq!(gui_mod::MAX_PEAK_BINS, 32768);
        assert_eq!(gui_mod::BINS_PER_UPDATE, 2048);
        assert_eq!(gui_mod::CHUNK_FRAMES, 16384);
        assert!(!gui_mod::STANDARD_EXPORT_SAMPLE_RATES.is_empty());
    }

    #[test]
    fn message_lists_are_populated() {
        assert_eq!(message_lists::SNAP_MODE_ALL.len(), 8);
        assert_eq!(message_lists::PIANO_SCALE_ROOT_ALL.len(), 12);
        assert_eq!(message_lists::PIANO_CHORD_KIND_ALL.len(), 5);
        assert_eq!(message_lists::EXPORT_BIT_DEPTH_ALL.len(), 4);
        assert_eq!(message_lists::EXPORT_RENDER_MODE_ALL.len(), 3);
    }
}

pub mod gm_drum_map {
    pub const GM_DRUM_MAP: &[(u8, &str)] = &[
        (35, "Acoustic Bass Drum"),
        (36, "Bass Drum 1"),
        (37, "Side Stick"),
        (38, "Acoustic Snare"),
        (39, "Hand Clap"),
        (40, "Electric Snare"),
        (41, "Low Floor Tom"),
        (42, "Closed Hi-Hat"),
        (43, "High Floor Tom"),
        (44, "Pedal Hi-Hat"),
        (45, "Low Tom"),
        (46, "Open Hi-Hat"),
        (47, "Low-Mid Tom"),
        (48, "Hi-Mid Tom"),
        (49, "Crash Cymbal 1"),
        (50, "High Tom"),
        (51, "Ride Cymbal 1"),
        (52, "Chinese Cymbal"),
        (53, "Ride Bell"),
        (54, "Tambourine"),
        (55, "Splash Cymbal"),
        (56, "Cowbell"),
        (57, "Crash Cymbal 2"),
        (58, "Vibraslap"),
        (59, "Ride Cymbal 2"),
        (60, "Hi Bongo"),
        (61, "Low Bongo"),
        (62, "Mute Hi Conga"),
        (63, "Open Hi Conga"),
        (64, "Low Conga"),
        (65, "High Timbale"),
        (66, "Low Timbale"),
        (67, "High Agogo"),
        (68, "Low Agogo"),
        (69, "Cabasa"),
        (70, "Maracas"),
        (71, "Short Whistle"),
        (72, "Long Whistle"),
        (73, "Short Guiro"),
        (74, "Long Guiro"),
        (75, "Claves"),
        (76, "Hi Wood Block"),
        (77, "Low Wood Block"),
        (78, "Mute Cuica"),
        (79, "Open Cuica"),
        (80, "Mute Triangle"),
        (81, "Open Triangle"),
    ];
}