sva-core 0.14.13

The parse -> tempo -> render pipeline shared by both front ends
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// Concern: the observations both front ends ask for, each written as a call with its own arguments | Non-concern: argv (sva-cli's args/), taking the reading (sva-engine) | IO: (text) -> Asked

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

use sva_ast::SpanUnit;
use sva_ast::score::{Field, Kind, Reading as Folding, Refused, evaluate};
use sva_engine::harmony::voice_leading::Norm;
use sva_engine::{
    Arg, DEFAULT_BUFFER_SECS, Given, MAX_PINNED_FRAME, READINGS, Reading, Representation,
    pinned_frame,
};

use crate::cli_error::CliError;

pub const RETIRED: [(&str, &str); 2] = [
    ("exact-envelope", "envelope, symbolic on a closed form"),
    ("exact-derivative", "derivative, symbolic on a closed form"),
];

/// One reading written as a call, `spectrum(peaks=8, frame=50ms)=out.json`: its name, its
/// arguments as written, and where it goes.
#[derive(Clone, Debug, PartialEq)]
pub struct Call {
    pub name: String,
    pub args: Vec<(String, String)>,
    /// No destination is stdout.
    pub dest: Option<PathBuf>,
}

/// Where a reading goes instead of stdout: a path, its extension picking the format, or a
/// live device named after `alsa:`.
#[derive(Clone, Debug, PartialEq)]
pub enum Destination {
    File(PathBuf),
    Alsa {
        /// Any ALSA PCM name: `default`, `hw:1`, `plughw:1,0`.
        device: String,
        /// `samples(buffer=50ms)`: how much the device holds ahead of what it plays.
        buffer_secs: f64,
    },
}

/// The most samples a time argument may count: past 2^53 neither an f64 nor its product
/// with another count is exact.
const MOST_SAMPLES: f64 = 9_007_199_254_740_992.0;

/// A call resolved to the reading it names and the arguments only a front end reads. Its
/// times stay as written until a render says what a bar lasts.
#[derive(Clone, Debug, PartialEq)]
pub struct Asked {
    pub name: String,
    given: Given,
    times: Vec<(Arg, Time)>,
    pub dest: Option<Destination>,
    /// `bindings(node=@voice)`: the instance whose bindings are read.
    pub node: Option<String>,
    /// `ledger(brief=1)`: only the rows that clipped.
    pub brief: bool,
    /// `ledger(skim=1)`: each row's level alone.
    pub skim: bool,
}

pub fn is_wav(path: &Path) -> bool {
    path.extension()
        .is_some_and(|e| e.eq_ignore_ascii_case("wav"))
}

pub fn retired(name: &str) -> Option<&'static str> {
    RETIRED
        .iter()
        .find(|(gone, _)| *gone == name)
        .map(|(_, write)| *write)
}

/// A comma list of calls; a comma inside a call's parentheses separates its arguments, and
/// one inside an `alsa:` device name, `hw:1,0`, stays in it where no reading follows it.
pub fn calls(text: &str) -> Result<Vec<Call>, CliError> {
    let mut joined: Vec<String> = Vec::new();
    for one in split_outside(text, ',')? {
        match joined.last_mut() {
            Some(last) if names_a_device(last) && !names_a_reading(one) => {
                last.push(',');
                last.push_str(one);
            }
            _ => joined.push(one.to_string()),
        }
    }
    joined.iter().map(|one| call(one.trim())).collect()
}

fn names_a_device(call: &str) -> bool {
    let close = call.rfind(')').map_or(0, |at| at + 1);
    call[close..]
        .split_once('=')
        .is_some_and(|(_, dest)| dest.trim_start().starts_with("alsa:"))
}

fn names_a_reading(text: &str) -> bool {
    let head = text[..text.find(['(', '=']).unwrap_or(text.len())].trim();
    READINGS.iter().any(|row| row.name == head) || retired(head).is_some()
}

/// `name`, `name(k=v, ...)`, either followed by `=path`.
pub fn call(text: &str) -> Result<Call, CliError> {
    let head_end = text.find(['(', '=']).unwrap_or(text.len());
    let name = text[..head_end].trim().to_string();
    if name.is_empty() {
        return Err(unwritten(text, "it names no reading"));
    }
    let mut rest = &text[head_end..];
    let mut args = Vec::new();
    if rest.starts_with('(') {
        let close = closing(rest).ok_or_else(|| unwritten(text, "its `(` is never closed"))?;
        let inner = rest[1..close].trim();
        if !inner.is_empty() {
            for arg in split_outside(inner, ',')? {
                let Some((key, value)) = arg.split_once('=') else {
                    return Err(unwritten(
                        text,
                        &format!("`{}` is no named argument; write `key=value`", arg.trim()),
                    ));
                };
                args.push((key.trim().to_string(), value.trim().to_string()));
            }
        }
        rest = &rest[close + 1..];
    }
    let dest = match rest.trim_start().strip_prefix('=') {
        Some("") => {
            return Err(unwritten(
                text,
                "`=` names no destination; drop it to read the reading under \
                 `data.representations`",
            ));
        }
        Some(path) => Some(PathBuf::from(path)),
        None if rest.trim().is_empty() => None,
        None => {
            return Err(unwritten(
                text,
                &format!("`{}` follows the call", rest.trim()),
            ));
        }
    };
    Ok(Call { name, args, dest })
}

/// Every call resolved. An answer keys each reading by its name, so a name asked twice
/// would print one key twice and a JSON reader would keep only the last; it refuses.
pub fn asked_all(calls: &[Call]) -> Result<Vec<Asked>, CliError> {
    let mut out: Vec<Asked> = Vec::with_capacity(calls.len());
    for call in calls {
        let one = asked(call)?;
        if out.iter().any(|held| held.name == one.name) {
            return Err(CliError::usage(
                "query.duplicate",
                format!(
                    "`{}` is asked more than once; ask each reading once, every argument in \
                 its one call",
                    one.name
                ),
            ));
        }
        out.push(one);
    }
    Ok(out)
}

/// A time argument as written: seconds, or bars a tempo counts.
#[derive(Clone, Debug, PartialEq)]
struct Time {
    amount: f64,
    unit: SpanUnit,
    written: String,
}

impl Asked {
    fn reading(&self) -> &'static Reading {
        READINGS
            .iter()
            .find(|row| row.name == self.name)
            .expect("an asked reading names a row")
    }

    /// Whether the reading answers off samples alone, as `Representation::off_samples`.
    pub fn off_samples(&self, sampled: bool) -> bool {
        self.reading().reads.off_samples(sampled)
    }

    /// The reading at `rate`, every time argument in seconds: a bar is `seconds_per_bar`,
    /// and one written where no tempo is known refuses.
    pub fn representation(
        &self,
        rate: u32,
        seconds_per_bar: Option<f64>,
    ) -> Result<Representation, CliError> {
        let mut given = self.given;
        for (arg, time) in &self.times {
            let secs = match (time.unit, seconds_per_bar) {
                (SpanUnit::Seconds, _) => time.amount,
                (SpanUnit::Bars, Some(per)) => time.amount * per,
                (SpanUnit::Bars, None) => {
                    return Err(CliError::BadTempo(format!(
                        "`{}({}={})` is written in bars, and this reading has no tempo to \
                         count them in: a render reads it off `variables/bpm` and \
                         `variables/meter`, and a file holds none; write seconds",
                        self.name,
                        arg.key(),
                        time.written
                    )));
                }
            };
            if *arg == Arg::Frame && (secs * f64::from(rate)).round() < 1.0 {
                return Err(CliError::usage(
                    "query.bad_value",
                    format!(
                        "`{}(frame={})` is {secs}s, under half a sample at {rate} Hz, and a \
                         frame steps by whole samples, rounded; ask at least {}s, half a sample",
                        self.name,
                        time.written,
                        0.5 / f64::from(rate)
                    ),
                ));
            }
            let most = MOST_SAMPLES / f64::from(rate);
            if secs > most {
                return Err(CliError::usage(
                    "query.bad_value",
                    format!(
                        "`{}({}={})` is {secs}s, past the 2^53 samples a count holds exactly at \
                         {rate} Hz; ask at most {most}s",
                        self.name,
                        arg.key(),
                        time.written
                    ),
                ));
            }
            match arg {
                Arg::Frame => given.frame = Some(secs),
                Arg::Window => given.window = Some(secs),
                Arg::Cell => given.cell_secs = secs,
                Arg::Smooth => given.smooth_secs = secs,
                _ => unreachable!("only a time argument is held as a time"),
            }
        }
        let representation = self.reading().representation(&given);
        pinned(representation, rate)?;
        Ok(representation)
    }

    pub fn path(&self) -> Option<&Path> {
        match &self.dest {
            Some(Destination::File(path)) => Some(path),
            Some(Destination::Alsa { .. }) | None => None,
        }
    }
}

/// The reading a call names, each argument read as that reading takes it.
pub fn asked(call: &Call) -> Result<Asked, CliError> {
    let name = call.name.as_str();
    if let Some(write) = retired(name) {
        return Err(CliError::usage(
            "query.retired",
            format!("`{name}` left the language; write `{write}`"),
        ));
    }
    let Some(row) = READINGS.iter().find(|row| row.name == name) else {
        let names: Vec<&str> = READINGS.iter().map(|row| row.name).collect();
        return Err(CliError::usage(
            "query.unknown_representation",
            format!(
                "unknown representation `{name}`; the representations are {}",
                names.join(", ")
            ),
        ));
    };
    let (mut given, mut node, mut brief, mut skim) = (Given::default(), None, false, false);
    let (mut buffer, mut times) = (None, Vec::new());
    for (key, raw) in &call.args {
        let Some(arg) = row.takes.iter().find(|arg| arg.key() == key) else {
            let keys: Vec<&str> = row.takes.iter().map(|arg| arg.key()).collect();
            return Err(CliError::usage(
                "query.unknown_argument",
                match keys[..] {
                    [] => format!("`{name}` takes no arguments, not `{key}`"),
                    _ => format!("`{name}` takes {}, not `{key}`", keys.join(", ")),
                },
            ));
        };
        match arg {
            Arg::Peaks => given.peaks = count(raw, key)?,
            Arg::Depth => given.depth = count(raw, key)?,
            Arg::Frame | Arg::Window | Arg::Cell | Arg::Smooth => {
                times.push((*arg, time(raw, key)?));
            }
            Arg::Oversample => given.oversample = factor(raw)?,
            Arg::Brief => brief = switch(raw, key)?,
            Arg::Skim => skim = switch(raw, key)?,
            Arg::Node => node = Some(self::node(raw)?),
            Arg::Buffer => buffer = Some(microseconds(raw, key)?),
            Arg::Floor => given.floor_db = decibels(raw, key)?,
            Arg::Norm => given.norm = norm(raw)?,
            Arg::Pairs => given.max_pairs = count(raw, key)?,
            Arg::Curve => given.curve = switch(raw, key)?,
            Arg::Bpm => given.bpm = Some(tempo(raw, key)?),
        }
    }
    if row.takes.contains(&Arg::Node) && node.is_none() {
        return Err(CliError::usage(
            "query.missing_argument",
            format!("`{name}` reads one instance's {name}; name it, as `{name}(node=@voice)`"),
        ));
    }
    Ok(Asked {
        name: call.name.clone(),
        given,
        times,
        dest: destination(name, call.dest.as_deref(), buffer)?,
        node,
        brief,
        skim,
    })
}

/// `alsa:` opens a device name, and only a device has a buffer to size.
fn destination(
    name: &str,
    dest: Option<&Path>,
    buffer: Option<f64>,
) -> Result<Option<Destination>, CliError> {
    let device = dest.and_then(|d| d.to_str()?.strip_prefix("alsa:"));
    match (dest, device, buffer) {
        (_, Some(""), _) => Err(CliError::usage(
            "destination.no_device",
            format!(
                "`{name}=alsa:` names no device; write an ALSA device after it, as \
                 `alsa:default` or `alsa:hw:1`"
            ),
        )),
        (_, Some(device), _) => Ok(Some(Destination::Alsa {
            device: device.to_string(),
            buffer_secs: buffer.unwrap_or(DEFAULT_BUFFER_SECS),
        })),
        (_, None, Some(_)) => Err(CliError::usage(
            "query.buffer_without_device",
            format!(
                "`buffer` sizes a live device's buffer, and `{name}` names none; write \
                 `{name}(buffer=...)=alsa:<device>`, or drop `buffer`"
            ),
        )),
        (dest, None, None) => Ok(dest.map(|d| Destination::File(d.to_path_buf()))),
    }
}

/// `raw` split at each `sep` no parenthesis encloses.
fn split_outside(raw: &str, sep: char) -> Result<Vec<&str>, CliError> {
    let (mut depth, mut from, mut out) = (0i32, 0, Vec::new());
    for (at, c) in raw.char_indices() {
        match c {
            '(' => depth += 1,
            ')' => depth -= 1,
            c if c == sep && depth == 0 => {
                out.push(&raw[from..at]);
                from = at + c.len_utf8();
            }
            _ => {}
        }
        if depth < 0 {
            return Err(unwritten(raw, "a `)` closes nothing"));
        }
    }
    out.push(&raw[from..]);
    Ok(out)
}

/// The byte of the `)` that closes the `(` `text` opens with.
fn closing(text: &str) -> Option<usize> {
    let mut depth = 0i32;
    for (at, c) in text.char_indices() {
        match c {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                if depth == 0 {
                    return Some(at);
                }
            }
            _ => {}
        }
    }
    None
}

fn unwritten(text: &str, why: &str) -> CliError {
    CliError::usage(
        "query.malformed",
        format!(
            "`{text}` is no reading: {why}. write a call, as `spectrum(peaks=8, frame=50ms)`, \
             `=path` after it to write it to a file"
        ),
    )
}

pub fn wav_path(raw: &str) -> Result<PathBuf, CliError> {
    match is_wav(Path::new(raw)) {
        true => Ok(PathBuf::from(raw)),
        false => Err(CliError::usage(
            "analyze.not_wav",
            format!("`{raw}` is no `.wav` file; mp3 and every other format are out of scope"),
        )),
    }
}

fn count(raw: &str, key: &str) -> Result<usize, CliError> {
    raw.parse::<usize>().map_err(|_| {
        CliError::usage(
            "query.bad_value",
            format!("`{key}` needs a whole count, got `{raw}`"),
        )
    })
}

fn factor(raw: &str) -> Result<u32, CliError> {
    match raw.parse::<u32>() {
        Ok(k) if (2..=16).contains(&k) && k.is_power_of_two() => Ok(k),
        _ => Err(CliError::usage(
            "query.bad_value",
            format!("`oversample` needs a power of two from 2 to 16, got `{raw}`"),
        )),
    }
}

/// A positive time as a score folds one, `50ms`, `1b/8` or `2b + 1b/3`; a bare number is
/// seconds. Bars stay bars until a tempo is known.
fn time(raw: &str, key: &str) -> Result<Time, CliError> {
    let bad = |why: String| {
        CliError::usage(
            "query.bad_value",
            format!(
                "`{key}` needs a positive time, in seconds as `50ms` or `0.05`, or in bars as \
                 `1b/8`: {why}"
            ),
        )
    };
    let folding = Folding::<f64> {
        field: Field::Time,
        mark: &|_| None,
        degree: &|_| Err("a reading's time counts no degrees".to_string()),
        own: None,
    };
    let value = evaluate(raw, &folding).map_err(|refused| match refused {
        Refused::At(_, why) => bad(why),
        Refused::Mark(name, _) => bad(format!(
            "`mark({name})` is a score's, and a reading has none"
        )),
    })?;
    let unit = match value.kind {
        Kind::Bars => SpanUnit::Bars,
        Kind::Seconds | Kind::Number => SpanUnit::Seconds,
        other => return Err(bad(format!("`{raw}` is a {}", other.name()))),
    };
    match value.amount > 0.0 && value.amount.is_finite() {
        true => Ok(Time {
            amount: value.amount,
            unit,
            written: raw.to_string(),
        }),
        false => Err(bad(format!("`{raw}` is not over zero"))),
    }
}

/// A transform sized to fit is not the grid two windows asked to share.
fn pinned(representation: Representation, rate: u32) -> Result<(), CliError> {
    let Representation::Spectrum {
        frame_secs: Some(secs),
        ..
    } = representation
    else {
        return Ok(());
    };
    let sr = f64::from(rate);
    let frame = pinned_frame(secs, sr);
    match frame > MAX_PINNED_FRAME {
        true => Err(CliError::usage(
            "query.bad_value",
            format!(
                "`spectrum(frame={secs})` sizes its transform to {frame} samples at {sr} Hz, \
                 past the {MAX_PINNED_FRAME}-sample bound"
            ),
        )),
        false => Ok(()),
    }
}

/// A positive time the language spells, `50ms` or `1.5s`; a bare number is seconds.
pub fn seconds(raw: &str, key: &str) -> Result<f64, CliError> {
    let held = match sva_ast::tokenize(raw).as_deref() {
        Ok([one]) => match one.kind {
            sva_ast::TokenKind::Num(v)
            | sva_ast::TokenKind::Time(v, sva_ast::SpanUnit::Seconds) => Some(v),
            _ => None,
        },
        _ => None,
    };
    match held {
        Some(v) if v > 0.0 && v.is_finite() => Ok(v),
        _ => Err(CliError::usage(
            "query.bad_value",
            format!("`{key}` needs a positive time, as `50ms` or `0.05`, got `{raw}`"),
        )),
    }
}

/// A bare positive number of beats per minute.
fn tempo(raw: &str, key: &str) -> Result<f64, CliError> {
    let held = match sva_ast::tokenize(raw).as_deref() {
        Ok([one]) => match one.kind {
            sva_ast::TokenKind::Num(v) => Some(v),
            _ => None,
        },
        _ => None,
    };
    match held {
        Some(v) if v > 0.0 && v.is_finite() => Ok(v),
        _ => Err(CliError::usage(
            "query.bad_value",
            format!("`{key}` needs a positive tempo in beats per minute, as `140`, got `{raw}`"),
        )),
    }
}

/// A device takes its buffer in whole microseconds, as a C `int`.
fn microseconds(raw: &str, key: &str) -> Result<f64, CliError> {
    let secs = seconds(raw, key)?;
    match (1.0..=f64::from(i32::MAX)).contains(&(secs * 1e6).round()) {
        true => Ok(secs),
        false => Err(CliError::usage(
            "query.bad_value",
            format!(
                "`{key}` is a whole number of microseconds from 1us to {}s, got `{raw}`",
                i32::MAX / 1_000_000
            ),
        )),
    }
}

/// A level the language spells in decibels, `-40db`; a bare number names no unit.
fn decibels(raw: &str, key: &str) -> Result<f64, CliError> {
    use sva_ast::{LogUnit::Decibels, TokenKind};
    let kinds: Option<Vec<TokenKind>> = sva_ast::tokenize(raw)
        .ok()
        .map(|tokens| tokens.into_iter().map(|t| t.kind).collect());
    let held = match kinds.as_deref() {
        Some([TokenKind::Log(v, Decibels)]) => Some(*v),
        Some([TokenKind::Minus, TokenKind::Log(v, Decibels)]) => Some(-v),
        _ => None,
    };
    match held {
        Some(v) if v.is_finite() => Ok(v),
        _ => Err(CliError::usage(
            "query.bad_value",
            format!("`{key}` needs a level in decibels, as `-40db`, got `{raw}`"),
        )),
    }
}

fn norm(raw: &str) -> Result<Norm, CliError> {
    [Norm::L1, Norm::L2]
        .into_iter()
        .find(|n| n.key() == raw)
        .ok_or_else(|| {
            CliError::usage(
                "query.bad_value",
                format!("`norm` is `l1` or `l2`, got `{raw}`"),
            )
        })
}

fn switch(raw: &str, key: &str) -> Result<bool, CliError> {
    match raw {
        "1" => Ok(true),
        "0" => Ok(false),
        _ => Err(CliError::usage(
            "query.bad_value",
            format!("`{key}` is `1` or `0`, got `{raw}`"),
        )),
    }
}

/// A ref as the language writes one, `@voice`.
fn node(raw: &str) -> Result<String, CliError> {
    match raw.strip_prefix('@') {
        Some(path) if !path.is_empty() => Ok(path.to_string()),
        _ => Err(CliError::usage(
            "query.bad_value",
            format!("`node` names an instance as a ref, as `@voice`, not `{raw}`"),
        )),
    }
}