drawbar 0.4.0

Desktop and browser app for Clavia / Nord keyboards — view, edit, and transfer sounds over USB
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
//! The Sample Editor project document.
//!
//! A `.nsmpproj` is the text file the editor saves and generates an `nsmp`
//! from. What is settable is what `nord-format` can edit in place: the
//! instrument's name and velocity defaults, each zone's root key and key
//! range, each stroke's trim, loop, gain and velocity window, and each audio
//! file's path. Paths are the CLI's — `name`, `zone129.root_key`,
//! `stroke1.loop_start`, `velocity.attack_amount`, `file1.path` — with zones
//! and files addressed by their stored ids and a stroke by its global id.

use std::collections::HashMap;
use std::io::Cursor;

use eframe::egui;
use nord_format::formats::nsmpproj::{Project, StrokeField, VelocityDefaults, MAX_VELOCITY};
use nord_format::Entity;

use super::controls::Sets;
use super::sample::note_picker;
use crate::note;

pub fn is_project(entity: &Entity) -> bool {
    matches!(entity, Entity::SampleProject(_))
}

fn project(entity: &Entity) -> Option<&Project> {
    match entity {
        Entity::SampleProject(project) => Some(project),
        _ => None,
    }
}

fn project_mut(entity: &mut Entity) -> Option<&mut Project> {
    match entity {
        Entity::SampleProject(project) => Some(project),
        _ => None,
    }
}

/// One zone, under the id the file gives it.
#[derive(Clone, PartialEq, Eq)]
pub struct Zone {
    pub id: u32,
    pub root_key: u8,
    pub bottom_note: u8,
    pub top_note: u8,
    pub enabled: bool,
}

/// One audio file, under the id the strokes reference it by.
#[derive(Clone, PartialEq, Eq)]
pub struct AudioFile {
    pub id: u32,
    pub path: String,
}

/// One stroke, under the global id both blocks naming it use.
#[derive(Clone, PartialEq)]
pub struct Stroke {
    pub id: u32,
    pub start: f64,
    pub stop: f64,
    pub gain: f64,
    /// `velocity_min..=velocity_max`.
    pub velocity: (u8, u8),
    pub loop_enabled: bool,
    pub loop_start: f64,
    pub loop_length: f64,
}

/// Everything settable, in one read.
#[derive(Clone, PartialEq)]
pub struct Snapshot {
    pub name: String,
    pub zones: Vec<Zone>,
    pub files: Vec<AudioFile>,
    pub strokes: Vec<Stroke>,
    pub velocity: VelocityDefaults,
}

pub fn snapshot(entity: &Entity) -> Option<Result<Snapshot, String>> {
    let project = project(entity)?;
    Some(read(project))
}

fn read(project: &Project) -> Result<Snapshot, String> {
    // Gain and the velocity window sit in the `map_stroke`, the trim and loop
    // points in the `common_stroke`; the global id is what joins them.
    let played: Vec<_> = project
        .zones()
        .map_err(|e| e.to_string())?
        .into_iter()
        .flat_map(|z| z.strokes)
        .collect();
    let strokes = project
        .strokes()
        .map_err(|e| e.to_string())?
        .into_iter()
        .map(|s| {
            let map = played
                .iter()
                .find(|z| z.global_id == s.global_id)
                .ok_or_else(|| format!("stroke {} is in no zone's map", s.global_id))?;
            Ok(Stroke {
                id: s.global_id,
                start: s.start,
                stop: s.stop,
                gain: map.gain,
                velocity: map.velocity,
                loop_enabled: s.loop_enabled,
                loop_start: s.loop_start,
                loop_length: s.loop_length,
            })
        })
        .collect::<Result<_, String>>()?;

    Ok(Snapshot {
        name: project.name().map_err(|e| e.to_string())?,
        strokes,
        velocity: project.velocity_defaults().map_err(|e| e.to_string())?,
        zones: project
            .zones()
            .map_err(|e| e.to_string())?
            .into_iter()
            .map(|z| Zone {
                id: z.zone_id,
                root_key: z.root_key,
                bottom_note: z.bottom_note,
                top_note: z.top_note,
                enabled: z.enabled,
            })
            .collect(),
        files: project
            .audio_files()
            .map_err(|e| e.to_string())?
            .into_iter()
            .map(|f| AudioFile {
                id: f.id,
                path: f.path,
            })
            .collect(),
    })
}

/// Apply one `path = value`, in the CLI's vocabulary.
fn set(project: &mut Project, path: &str, value: &str) -> Result<(), String> {
    if path == "name" {
        return project.set_name(value).map_err(|e| e.to_string());
    }
    let unknown = || format!("unknown field {path:?}");
    let (block, field) = path.split_once('.').ok_or_else(unknown)?;
    if let Some(id) = indexed(block, "file") {
        if field != "path" {
            return Err(unknown());
        }
        return project.set_audio_path(id, value).map_err(|e| e.to_string());
    }
    if let Some(id) = indexed(block, "stroke") {
        let field = StrokeField::parse(field, value).map_err(|e| e.to_string())?;
        return project
            .set_stroke_field(id, field)
            .map_err(|e| e.to_string());
    }
    if block == "velocity" {
        let mut defaults = project.velocity_defaults().map_err(|e| e.to_string())?;
        let stored = || {
            value
                .parse::<u8>()
                .map_err(|_| format!("{path}: {value:?} is not a whole number, 0-255"))
        };
        match field {
            "attack_amount" => defaults.attack_amount = stored()?,
            "amplitude" => defaults.amplitude = stored()?,
            "timbre" => defaults.timbre = stored()?,
            _ => return Err(unknown()),
        }
        return project
            .set_velocity_defaults(defaults)
            .map_err(|e| e.to_string());
    }
    let id = indexed(block, "zone").ok_or_else(unknown)?;
    let zones = project.zones().map_err(|e| e.to_string())?;
    let zone = zones
        .iter()
        .find(|z| z.zone_id == id)
        .ok_or_else(|| format!("this project has no zone {id}"))?;
    let note = note::parse(value)?;
    match field {
        "root_key" => project.set_root_key(id, note),
        "bottom_note" => project.set_key_range(id, note, zone.top_note),
        "top_note" => project.set_key_range(id, zone.bottom_note, note),
        _ => return Err(unknown()),
    }
    .map_err(|e| e.to_string())
}

fn indexed(part: &str, label: &str) -> Option<u32> {
    part.strip_prefix(label).and_then(|n| n.parse().ok())
}

/// A drag over one number, spelling the new value only once it has moved.
fn drag<H: std::hash::Hash>(
    ui: &mut egui::Ui,
    id: (&str, H),
    value: f64,
    range: std::ops::RangeInclusive<f64>,
    speed: f64,
) -> Option<String> {
    let mut moved = value;
    let response = ui.push_id(id, |ui| {
        ui.add(egui::DragValue::new(&mut moved).range(range).speed(speed))
    });
    (response.inner.changed() && moved != value).then(|| moved.to_string())
}

/// A frame position. Nothing in the format caps one: the editor repairs a
/// position past the file's end on load.
fn frames(ui: &mut egui::Ui, id: (&str, u32), value: f64) -> Option<String> {
    drag(ui, id, value, 0.0..=f64::MAX, 1.0)
}

/// Apply every set to a fresh decode and re-encode, the same all-or-nothing rule
/// the registry bodies follow.
pub fn apply(bytes: &[u8], sets: &[(String, String)]) -> Result<Vec<u8>, String> {
    let mut entity =
        nord_format::from_stream(&mut Cursor::new(bytes)).map_err(|e| e.to_string())?;
    let project = project_mut(&mut entity).ok_or("not a Sample Editor project")?;
    for (path, value) in sets {
        set(project, path, value)?;
    }
    nord_format::to_bytes(&entity).map_err(|e| e.to_string())
}

/// The name, the zone map, and the audio files behind it.
pub fn ui(
    ui: &mut egui::Ui,
    snapshot: &Snapshot,
    name: &mut String,
    paths: &mut HashMap<u32, String>,
    sets: &mut Sets,
) {
    ui.horizontal(|ui| {
        ui.add_sized(
            [120.0, ui.spacing().interact_size.y],
            egui::Label::new("Name").halign(egui::Align::LEFT),
        );
        let response = ui.add(egui::TextEdit::singleline(name).desired_width(200.0));
        // Not committed per keystroke: half a name is a name the format would take.
        let done = response.lost_focus() || response.ctx.input(|i| i.key_pressed(egui::Key::Enter));
        if done && *name != snapshot.name {
            sets.push(("name".to_string(), name.clone()));
        }
    });

    for zone in &snapshot.zones {
        let id = zone.id;
        ui.horizontal(|ui| {
            ui.add_sized(
                [120.0, ui.spacing().interact_size.y],
                egui::Label::new(format!("Zone {id}")).halign(egui::Align::LEFT),
            );
            ui.label(
                egui::RichText::new(format!(
                    "{}..{}{}",
                    note::name(zone.bottom_note),
                    note::name(zone.top_note),
                    if zone.enabled { "" } else { "  (disabled)" },
                ))
                .small()
                .weak(),
            );
            ui.label("root key");
            if let Some(note) = note_picker(ui, ("proj_root", id as usize), zone.root_key) {
                sets.push((format!("zone{id}.root_key"), note::name(note)));
            }
            ui.label("bottom");
            if let Some(note) = note_picker(ui, ("proj_btm", id as usize), zone.bottom_note) {
                sets.push((format!("zone{id}.bottom_note"), note::name(note)));
            }
            ui.label("top");
            if let Some(note) = note_picker(ui, ("proj_top", id as usize), zone.top_note) {
                sets.push((format!("zone{id}.top_note"), note::name(note)));
            }
        });
    }

    ui.add_space(4.0);
    ui.label(
        egui::RichText::new("Strokes, by the global id the file gives them.")
            .small()
            .weak(),
    );
    for stroke in &snapshot.strokes {
        let id = stroke.id;
        ui.horizontal_wrapped(|ui| {
            ui.add_sized(
                [120.0, ui.spacing().interact_size.y],
                egui::Label::new(format!("Stroke {id}")).halign(egui::Align::LEFT),
            );
            ui.label("trim");
            if let Some(v) = frames(ui, ("proj_start", id), stroke.start) {
                sets.push((format!("stroke{id}.start"), v));
            }
            if let Some(v) = frames(ui, ("proj_stop", id), stroke.stop) {
                sets.push((format!("stroke{id}.stop"), v));
            }
            ui.label("gain");
            if let Some(v) = drag(ui, ("proj_gain", id), stroke.gain, 0.0..=16.0, 0.01) {
                sets.push((format!("stroke{id}.gain"), v));
            }
            ui.label("vel");
            let top = MAX_VELOCITY as f64;
            if let Some(v) = drag(
                ui,
                ("proj_vmin", id),
                stroke.velocity.0 as f64,
                0.0..=top,
                0.5,
            ) {
                sets.push((format!("stroke{id}.velocity_min"), v));
            }
            if let Some(v) = drag(
                ui,
                ("proj_vmax", id),
                stroke.velocity.1 as f64,
                0.0..=top,
                0.5,
            ) {
                sets.push((format!("stroke{id}.velocity_max"), v));
            }
            let mut on = stroke.loop_enabled;
            let toggled = ui
                .push_id(("proj_loop", id), |ui| ui.checkbox(&mut on, "loop"))
                .inner;
            if toggled.changed() {
                sets.push((format!("stroke{id}.loop_enabled"), on.to_string()));
            }
            if let Some(v) = frames(ui, ("proj_loop_start", id), stroke.loop_start) {
                sets.push((format!("stroke{id}.loop_start"), v));
            }
            if let Some(v) = frames(ui, ("proj_loop_len", id), stroke.loop_length) {
                sets.push((format!("stroke{id}.loop_length"), v));
            }
        });
    }

    ui.horizontal(|ui| {
        ui.add_sized(
            [120.0, ui.spacing().interact_size.y],
            egui::Label::new("Velocity").halign(egui::Align::LEFT),
        );
        for (label, field, value) in [
            ("attack", "attack_amount", snapshot.velocity.attack_amount),
            ("amplitude", "amplitude", snapshot.velocity.amplitude),
            ("timbre", "timbre", snapshot.velocity.timbre),
        ] {
            ui.label(label);
            if let Some(v) = drag(ui, ("proj_vel", field), value as f64, 0.0..=255.0, 0.5) {
                sets.push((format!("velocity.{field}"), v));
            }
        }
    });

    ui.add_space(4.0);
    ui.label(
        egui::RichText::new("Audio files, as the editor will look for them.")
            .small()
            .weak(),
    );
    for file in &snapshot.files {
        let id = file.id;
        let path = paths.entry(id).or_insert_with(|| file.path.clone());
        ui.horizontal(|ui| {
            ui.add_sized(
                [120.0, ui.spacing().interact_size.y],
                egui::Label::new(format!("File {id}")).halign(egui::Align::LEFT),
            );
            let response = ui.add(egui::TextEdit::singleline(path).desired_width(320.0));
            let done =
                response.lost_focus() || response.ctx.input(|i| i.key_pressed(egui::Key::Enter));
            if done && *path != file.path {
                sets.push((format!("file{id}.path"), path.clone()));
            }
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use nord_format::formats::nsmpproj::NewZone;

    fn project_bytes() -> Vec<u8> {
        let project = Project::new(
            "Marimba",
            &[
                NewZone {
                    path: "low.wav".into(),
                    sample_rate: 44100,
                    frames: 44100,
                    root_key: 48,
                },
                NewZone {
                    path: "high.wav".into(),
                    sample_rate: 44100,
                    frames: 44100,
                    root_key: 72,
                },
            ],
            0,
        )
        .unwrap();
        nord_format::to_bytes(&Entity::SampleProject(project)).unwrap()
    }

    /// Every edit lands under the id the snapshot shows, and the result still
    /// decodes and round-trips.
    #[test]
    fn edits_land_by_id_and_round_trip() {
        let bytes = project_bytes();
        let out = apply(
            &bytes,
            &[
                ("name".into(), "Vibes".into()),
                ("zone129.root_key".into(), "C2".into()),
                ("file1.path".into(), "verylow.wav".into()),
            ],
        )
        .unwrap();

        let entity = nord_format::from_stream(&mut Cursor::new(&out)).unwrap();
        let snapshot = snapshot(&entity).unwrap().unwrap();
        assert_eq!(snapshot.name, "Vibes");
        let zone = snapshot.zones.iter().find(|z| z.id == 129).unwrap();
        assert_eq!(zone.root_key, 36);
        assert_eq!(snapshot.files[0].path, "verylow.wav");
        assert_eq!(nord_format::to_bytes(&entity).unwrap(), out);
    }

    #[test]
    fn stroke_and_velocity_edits_land_and_read_back() {
        let bytes = project_bytes();
        let out = apply(
            &bytes,
            &[
                ("stroke1.loop_enabled".into(), "on".into()),
                ("stroke1.loop_start".into(), "1000".into()),
                ("stroke1.loop_length".into(), "500".into()),
                ("stroke1.gain".into(), "0.25".into()),
                ("stroke1.velocity_max".into(), "90".into()),
                ("velocity.attack_amount".into(), "64".into()),
            ],
        )
        .unwrap();

        let entity = nord_format::from_stream(&mut Cursor::new(&out)).unwrap();
        let snapshot = snapshot(&entity).unwrap().unwrap();
        let stroke = snapshot.strokes.iter().find(|s| s.id == 1).unwrap();
        assert!(stroke.loop_enabled);
        assert_eq!((stroke.loop_start, stroke.loop_length), (1000.0, 500.0));
        assert_eq!(stroke.gain, 0.25);
        assert_eq!(stroke.velocity, (0, 90));
        assert_eq!(snapshot.velocity.attack_amount, 64);
        assert_eq!(nord_format::to_bytes(&entity).unwrap(), out);
    }

    /// A bad path or an unknown id is refused before anything is encoded.
    #[test]
    fn unknown_paths_are_refused() {
        let bytes = project_bytes();
        for (path, value) in [
            ("zone999.root_key", "C4"),
            ("zone129.detune", "1"),
            ("file9.path", "x.wav"),
            ("file1.rate", "48000"),
            ("stroke1.nope", "1"),
            ("stroke9.gain", "1"),
            ("stroke1.velocity_max", "200"),
            ("stroke1.loop_start", "-1"),
            ("velocity.nope", "1"),
        ] {
            assert!(
                apply(&bytes, &[(path.into(), value.into())]).is_err(),
                "{path}"
            );
        }
    }

    /// An inverted key range cannot leave half an edit behind.
    #[test]
    fn an_inverted_range_is_refused_whole() {
        let bytes = project_bytes();
        let err = apply(&bytes, &[("zone129.bottom_note".into(), "C8".into())]);
        assert!(err.is_err());
    }
}