duet-cli 0.3.0

duet: compose with an LLM in text both can read, and convert it to and from MIDI.
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
//! `duet capture compare`: a reading plotted and played against the performance it was read
//! from, so a listener can hear and see where it departs from the singing.
//!
//! It reads what `duet capture melody` wrote at a stem, and the take's audio, which neither
//! file names:
//!
//! - **`STEM.performance.duet.jsonl`:** the performance, every pitch frame;
//! - **`STEM.capture.duet`:** the analysis, whose `reading` (capture-analysis 1.1) holds the
//!   tuning and the notes, with each note's residual and the take's loss since 1.2, and whose events hold the onsets. `--reading` names another
//!   analysis of the same take, whose reading is compared instead; the stem's own is drawn
//!   beside it.
//!
//! It writes, each refused rather than overwritten, named after the analysis whose reading it
//! shows (`STEM.compare.svg` and `STEM.compare.wav`, or the other analysis's name with
//! `.compare.svg` and `.compare.wav` in place of `.capture.duet`), and a window's after its
//! window too (`STEM.compare.33.0-34.3.svg`), so it never collides with the whole take's:
//!
//! - **the plot** ([`svg`]): the performance's pitch in cents, the reading's notes as bars at
//!   `tuning + 100·key`, the residual shaded between them, between-keys notes marked, and the
//!   onsets as ticks;
//! - **the audio** ([`wav`]): the take on the left, as the analysis heard it (one channel, at
//!   the analysis rate, so it lines up with the frames sample for sample), and the reading on
//!   the right as a sine at the take's own tuning, gliding only through each note's slide in:
//!   its scoop where the residual measured one, else up to where its held pitch begins. It holds
//!   through a gap between notes shorter than [`wav::BRIDGE_SECS`] and is silent through a
//!   longer one, so a dropped note is heard as silence; `--legato` holds it through every gap
//!   and `--gapped` silences it in every gap;
//!   `--render` chooses what it plays inside a note ([`wav::Render`]): `melody`, the keys only (the
//!   default); `steps`, the keys with each excursion held at its key plus its offset; `bends`,
//!   the keys plus the residual, the performance's own pitch. A `steps` or `bends` run's outputs
//!   carry its name after the window's (`STEM.compare.33.0-34.3.steps.wav`), so the three never
//!   collide, and the plot draws the sine's pitch as it plays;
//! - **on standard output:** one line per note (its time, key, centre and whether it is between
//!   keys), with its residual's parts (held centre, drift, vibrato, scoop, fall, rest and
//!   excursions) when the reading has them, then the take's loss as its terms. A 1.1 reading has
//!   neither, and the output says so.
//!
//! `--from` and `--to` window all three to a stretch of the take, in seconds.
//!
//! **What it cannot check:** nothing in either file names the audio it was read from, so the
//! take is checked only by its length against the performance's.

mod svg;
mod wav;

use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use clap::Args;
use duet_capture::{
    Analysis, Event, FileSource, Origin, PerformanceFrames, PerformanceHead, Prepare, ReadNote,
    Reading, Source, Stage,
};
use duet_schemas::{Document, Stream};

use super::write_new;

/// The performance's frames may end up to this long after the take's last sample, in seconds:
/// a frame sits at the middle of its window, and the last window is padded.
const LENGTH_SLACK_SECS: f64 = 0.1;

/// What `duet capture compare` is given.
#[derive(Args)]
pub struct CompareArgs {
    /// The take's recording, as `duet capture melody` read it: the plot's and the audio's
    /// timeline, and the audio's left channel.
    input: PathBuf,
    /// The stem the take was captured to (`duet capture melody -o`): STEM.performance.duet.jsonl
    /// and STEM.capture.duet are read from it. Without it, the input's name without its
    /// extension, beside it.
    #[arg(short, long, value_name = "STEM")]
    output: Option<PathBuf>,
    /// Another analysis of the same take (a .capture.duet) whose reading is compared instead of
    /// the stem's, which is drawn beside it. The outputs are named after it.
    #[arg(long, value_name = "ANALYSIS")]
    reading: Option<PathBuf>,
    /// Where the window starts, in seconds from the start of the take. A window's outputs are
    /// named after it: STEM.compare.FROM-TO.svg and .wav.
    #[arg(long, value_name = "SECS")]
    from: Option<f64>,
    /// Where the window ends, in seconds from the start of the take.
    #[arg(long, value_name = "SECS")]
    to: Option<f64>,
    /// Hold the sine through every gap between notes, however long: one tone from the first
    /// note to the last. By default it holds only through a gap shorter than 0.2 s.
    #[arg(long, conflicts_with = "gapped")]
    legato: bool,
    /// Silence the sine through every gap between notes, however short, so it sounds only
    /// inside a note. By default it is silent only through a gap of 0.2 s or more.
    #[arg(long)]
    gapped: bool,
    /// What the sine plays inside a note: `melody`, the keys only; `steps`, the keys with each
    /// excursion held at its key plus its offset over its frames; `bends`, the keys plus the
    /// residual, following the performance's pitch. The outputs of `steps` and `bends` are
    /// named after it: STEM.compare.steps.svg and .wav.
    #[arg(long, value_enum, default_value_t, value_name = "RENDERING")]
    render: wav::Render,
}

pub fn run(args: &CompareArgs) -> Result<()> {
    let from_secs = args.from.unwrap_or(0.0);
    if !(from_secs.is_finite() && from_secs >= 0.0) {
        bail!("--from {from_secs}: the window starts at a number of seconds, 0 or more");
    }
    if let Some(to) = args.to
        && !(to.is_finite() && to > from_secs)
    {
        bail!("--to {to}: the window ends after it starts, at {from_secs} s");
    }

    let stem = args
        .output
        .clone()
        .unwrap_or_else(|| args.input.with_extension(""));
    let performance_path = with_suffix(&stem, ".performance.duet.jsonl");
    let analysis_path = with_suffix(&stem, ".capture.duet");
    let compared_path = args
        .reading
        .clone()
        .unwrap_or_else(|| analysis_path.clone());
    let gaps = if args.legato {
        wav::Gaps::Legato
    } else if args.gapped {
        wav::Gaps::Gapped
    } else {
        wav::Gaps::Bridged
    };
    let out_stem = with_suffix(
        &stem_of(&compared_path),
        &format!(
            "{}{}",
            window_name(args.from, args.to),
            args.render.suffix()
        ),
    );
    let (svg_path, wav_path) = (
        with_suffix(&out_stem, ".svg"),
        with_suffix(&out_stem, ".wav"),
    );
    // Refused before anything is read, as capture refuses: nothing is overwritten.
    for path in [&svg_path, &wav_path] {
        if path.exists() {
            bail!(
                "{} exists; compare writes new files and never overwrites one, so move it",
                path.display()
            );
        }
    }

    let frames = performance(&performance_path)?;
    let compared = analysis(&compared_path)?;
    // With --reading, the stem's own analysis is read too, for its reading to draw beside.
    let own = match &args.reading {
        Some(_) => Some(analysis(&analysis_path)?),
        None => None,
    };
    for (analysis, path) in [
        (Some(&compared), &compared_path),
        (own.as_ref(), &analysis_path),
    ] {
        if let Some(analysis) = analysis
            && analysis.rate != frames.rate
        {
            bail!(
                "{} counts frames at {} Hz and {} at {} Hz: they are not of one performance",
                path.display(),
                analysis.rate,
                performance_path.display(),
                frames.rate
            );
        }
    }
    let reading = reading_of(&compared, &compared_path)?;
    let beside = own
        .as_ref()
        .map(|own| reading_of(own, &analysis_path))
        .transpose()?;
    let onsets: Vec<u64> = compared
        .events
        .iter()
        .filter_map(|e| match *e {
            Event::Onset { frame, .. } => Some(frame),
            _ => None,
        })
        .collect();

    let take = take(&args.input, frames.rate)?;
    let rate = f64::from(frames.rate);
    let take_secs = take.len() as f64 / rate;
    if let Some(last) = frames.len().checked_sub(1).map(|k| frames.frame(k))
        && last as f64 / rate > take_secs + LENGTH_SLACK_SECS
    {
        bail!(
            "{} is {take_secs:.2} s long, and {}'s frames run to {:.2} s: it is not the take \
             that performance was read from",
            args.input.display(),
            performance_path.display(),
            last as f64 / rate
        );
    }
    let from = (from_secs * rate).round() as u64;
    let to = args.to.map_or(take.len() as u64, |t| {
        ((t * rate).round() as u64).min(take.len() as u64)
    });
    if from >= to {
        bail!(
            "--from {from_secs}: {} is {take_secs:.2} s long, so the window holds nothing",
            args.input.display()
        );
    }

    let title = format!(
        "{}: {}{} on A4 = {:.1} Hz ({:+.0} cents{}), {} to {}",
        args.input.display(),
        compared_path.display(),
        match args.render {
            wav::Render::Melody => "",
            wav::Render::Steps => ", its excursions as steps,",
            wav::Render::Bends => ", its residual as bends,",
        },
        reading.tuning.a4_hz(),
        reading.tuning.offset_cents(),
        if matches!(reading.tuning.a4, Origin::Given(_)) {
            ", given"
        } else {
            ", estimated"
        },
        seconds(from as f64 / rate),
        seconds(to as f64 / rate)
    );
    let voice = wav::Voice::new(reading, &frames, gaps, args.render);
    let plot = svg::render(&svg::Plot {
        title: &title,
        frames: &frames,
        from,
        to,
        reading,
        beside,
        onsets: &onsets,
        voice: &voice,
    });
    let right = wav::sine(&voice, from, to);
    let audio = wav::stereo(frames.rate, &take[from as usize..to as usize], &right);

    write_new(&svg_path, &plot)?;
    if let Err(e) = write_bytes_new(&wav_path, &audio) {
        let _ = fs::remove_file(&svg_path);
        return Err(e.context(format!(
            "compare wrote nothing: removed {} again",
            svg_path.display()
        )));
    }
    print!("{}", notes_table(&title, reading, from, to, rate));
    Ok(())
}

/// The lines on standard output: the title, then one line per note shown, with its residual's
/// parts when the reading measured them (capture-analysis 1.2), then the take's loss as its
/// terms.
fn notes_table(title: &str, reading: &Reading, from: u64, to: u64, rate: f64) -> String {
    let parts = reading.residual.is_some();
    let mut out = format!("# {title}\n#     time  key    centre");
    if parts {
        out.push_str(&format!(
            "  {:>7}  {:>8}   {:<13}  {:<13}  {:<13}  {:>6}  excursions",
            "held", "drift/s", "vibrato", "scoop", "fall", "rest"
        ));
    }
    out.push('\n');
    for note in reading.notes.iter().filter(|n| svg::shown(n, from, to)) {
        let mut line = format!(
            "{:>10}  {:<5} {:>+6.1}c",
            format!("{:.3} s", note.start as f64 / rate),
            svg::key_name(note.key),
            note.centre,
        );
        if let Some(r) = &note.residual {
            let transition = |t: Option<(f32, f32)>| {
                t.map_or_else(|| "-".to_owned(), |(c, ms)| format!("{c:+.0}c {ms:.0} ms"))
            };
            line.push_str(&format!(
                "  {:>+6.1}c  {:>+8.1}   {:<13}  {:<13}  {:<13}  {:>5.1}c",
                r.centre,
                r.drift.cents_per_sec,
                r.modulation.map_or_else(
                    || "-".to_owned(),
                    |m| format!("{:.1} Hz ±{:.0}c", m.rate_hz, m.depth_cents)
                ),
                transition(r.scoop.map(|t| (t.cents, t.ms))),
                transition(r.fall.map(|t| (t.cents, t.ms))),
                r.rest_cents,
            ));
            line.push_str("  ");
            line.push_str(&excursions(note, rate));
        }
        if note.between_keys {
            line.push_str("  between keys");
        }
        out.push_str(line.trim_end());
        out.push('\n');
    }
    match &reading.residual {
        Some(take) => out.push_str(&format!(
            "# loss: pitch {:.1}, voicing {:.1}, unread {:.1}, notes {:.1} ({} frames read, {} \
             unread)\n",
            take.loss.pitch,
            take.loss.voicing,
            take.loss.unread,
            take.loss.notes,
            take.read_frames,
            take.unread_frames
        )),
        None => out.push_str(
            "# no residual or loss: this reading holds neither (it was written before \
             capture-analysis 1.2)\n",
        ),
    }
    out
}

/// A note's excursions (capture-analysis 1.6) as the table gives them, in order: each its offset from the key, how
/// long it holds and where it starts on the take, `+200c 60 ms @ 0.290 s`, joined by `; `, or
/// `-` when there is none.
fn excursions(note: &ReadNote, rate: f64) -> String {
    let all: Vec<_> = note.residual.iter().flat_map(|r| &r.excursions).collect();
    if all.is_empty() {
        return "-".to_owned();
    }
    all.iter()
        .map(|e| {
            format!(
                "{:+.0}c {:.0} ms @ {:.3} s",
                e.cents,
                (e.end - e.start) as f64 / rate * 1000.0,
                e.start as f64 / rate
            )
        })
        .collect::<Vec<_>>()
        .join("; ")
}

/// The performance stream's frames.
fn performance(path: &Path) -> Result<PerformanceFrames> {
    let text = fs::read_to_string(path).with_context(|| {
        format!(
            "{}: compare reads the performance duet capture melody wrote",
            path.display()
        )
    })?;
    let stream =
        Stream::<PerformanceHead>::read(&text).with_context(|| path.display().to_string())?;
    let mut frames = PerformanceFrames::new(&stream.head);
    for block in &stream.records {
        frames
            .push(block)
            .with_context(|| path.display().to_string())?;
    }
    Ok(frames)
}

/// A capture-analysis document's body.
fn analysis(path: &Path) -> Result<Analysis> {
    let text = fs::read_to_string(path).with_context(|| path.display().to_string())?;
    Ok(Document::<Analysis>::read(&text)
        .with_context(|| path.display().to_string())?
        .body)
}

/// An analysis's reading, which a rhythm take's and a 1.0 document's lack.
fn reading_of<'a>(analysis: &'a Analysis, path: &Path) -> Result<&'a Reading> {
    analysis.reading.as_ref().with_context(|| {
        format!(
            "{} holds no reading: it is a rhythm take's, or was written before capture-analysis \
             1.1; capture the melody again with duet capture melody",
            path.display()
        )
    })
}

/// The take's audio as the analysis heard it: one channel at `rate`, from its first frame.
fn take(path: &Path, rate: u32) -> Result<Vec<f32>> {
    let named = || path.display().to_string();
    let mut source = FileSource::open(path)?;
    let mut prepare = Prepare::new(rate);
    let mut mono = Vec::new();
    while let Some(chunk) = source.next_chunk().with_context(named)? {
        prepare.push(chunk, &mut mono).with_context(named)?;
    }
    prepare.finish(&mut mono).with_context(named)?;
    let mut samples = Vec::new();
    for m in mono {
        let at = usize::try_from(m.start_frame).context("a frame past the address space")?;
        if samples.len() < at {
            samples.resize(at, 0.0);
        }
        samples.truncate(at);
        samples.extend_from_slice(&m.samples);
    }
    Ok(samples)
}

/// The outputs' name after their stem: `.compare` for the whole take, and for a window
/// `.compare.FROM-TO`, each in seconds as given (`.compare.33.0-34.3`), with `end` for a window
/// with no `--to` and `0.0` for one with no `--from`. So a window never takes the whole take's
/// names, nor another window's.
fn window_name(from: Option<f64>, to: Option<f64>) -> String {
    if from.is_none() && to.is_none() {
        return ".compare".to_owned();
    }
    // Debug is the shortest form that reads back as the same number, with a point always.
    let from = format!("{:?}", from.unwrap_or(0.0));
    let to = to.map_or_else(|| "end".to_owned(), |t| format!("{t:?}"));
    format!(".compare.{from}-{to}")
}

/// `stem` with `suffix` added to its name.
fn with_suffix(stem: &Path, suffix: &str) -> PathBuf {
    let mut path = stem.as_os_str().to_owned();
    path.push(suffix);
    PathBuf::from(path)
}

/// An analysis's stem: its name without `.capture.duet`, or else without `.duet`.
fn stem_of(analysis: &Path) -> PathBuf {
    let name = analysis.as_os_str().to_string_lossy();
    for suffix in [".capture.duet", ".duet"] {
        if let Some(stem) = name.strip_suffix(suffix) {
            return PathBuf::from(stem);
        }
    }
    analysis.to_path_buf()
}

/// Seconds as the title gives them.
fn seconds(t: f64) -> String {
    format!("{t:.2} s")
}

/// Writes bytes to a file that must not exist yet, removing it again if it cannot be filled.
fn write_bytes_new(path: &Path, bytes: &[u8]) -> Result<()> {
    use std::io::Write as _;
    let mut file = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(path)
        .with_context(|| path.display().to_string())?;
    if let Err(e) = file.write_all(bytes) {
        let _ = fs::remove_file(path);
        return Err(e).with_context(|| path.display().to_string());
    }
    Ok(())
}