1use sva_formula::{C64, Line, SpectralSum, Var};
4use sva_samples::measure::harmony::dissonance::Dissonance;
5use sva_samples::measure::harmony::inharmonicity::Inharmonicity;
6use sva_samples::measure::harmony::tonnetz::TonalDistance;
7use sva_samples::measure::harmony::voice_leading::{Norm, VoiceLeading};
8use sva_samples::measure::modulation::Modulation;
9use sva_samples::measure::periodicity::Periodicity;
10use sva_samples::measure::self_similarity::SelfSimilarity;
11use sva_samples::{
12 Alias, Bands, Buffer, Consumes, Crest, EnvelopeFrame, FormantFrame, LedgerEntry, Loudness,
13 Onsets, PitchFrame, Source, Spectrum, StereoImage,
14};
15
16use crate::arguments::Arguments;
17use crate::bindings::Binding;
18
19pub const DEFAULT_FRAME_SECS: f64 = 0.05;
21pub const DEFAULT_MAX_PEAKS: usize = 16;
22pub const DEFAULT_LEDGER_DEPTH: usize = 3;
23pub const DEFAULT_OVERSAMPLE: u32 = 4;
24pub const DEFAULT_BUFFER_SECS: f64 = 0.05;
26pub const DEFAULT_FLOOR_DB: f64 = -40.0;
28pub const DEFAULT_MAX_PAIRS: usize = 1 << 17;
30pub const DEFAULT_PERIODICITY_WINDOW_SECS: f64 = 6.0;
32pub const DEFAULT_MODULATION_WINDOW_SECS: f64 = 4.0;
34pub const DEFAULT_CELL_SECS: f64 = 0.5;
36pub const DEFAULT_SMOOTH_SECS: f64 = 4.0;
38pub const DEFAULT_NOVELTY_WINDOW_SECS: f64 = 8.0;
40
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
43pub enum Arg {
44 Peaks,
45 Frame,
46 Depth,
47 Oversample,
48 Brief,
49 Skim,
50 Node,
51 Buffer,
52 Floor,
53 Norm,
54 Pairs,
55 Window,
56 Curve,
57 Bpm,
58 Cell,
59 Smooth,
60}
61
62impl Arg {
63 pub fn key(self) -> &'static str {
64 match self {
65 Arg::Peaks => "peaks",
66 Arg::Frame => "frame",
67 Arg::Depth => "depth",
68 Arg::Oversample => "oversample",
69 Arg::Brief => "brief",
70 Arg::Skim => "skim",
71 Arg::Node => "node",
72 Arg::Buffer => "buffer",
73 Arg::Floor => "floor",
74 Arg::Norm => "norm",
75 Arg::Pairs => "pairs",
76 Arg::Window => "window",
77 Arg::Curve => "curve",
78 Arg::Bpm => "bpm",
79 Arg::Cell => "cell",
80 Arg::Smooth => "smooth",
81 }
82 }
83}
84
85#[derive(Clone, Copy, Debug, PartialEq)]
87pub struct Given {
88 pub peaks: usize,
89 pub frame: Option<f64>,
92 pub depth: usize,
93 pub oversample: u32,
94 pub floor_db: f64,
95 pub norm: Norm,
96 pub max_pairs: usize,
97 pub window: Option<f64>,
99 pub curve: bool,
100 pub bpm: Option<f64>,
101 pub cell_secs: f64,
102 pub smooth_secs: f64,
103}
104
105impl Default for Given {
106 fn default() -> Given {
107 Given {
108 peaks: DEFAULT_MAX_PEAKS,
109 frame: None,
110 depth: DEFAULT_LEDGER_DEPTH,
111 oversample: DEFAULT_OVERSAMPLE,
112 floor_db: DEFAULT_FLOOR_DB,
113 norm: Norm::L1,
114 max_pairs: DEFAULT_MAX_PAIRS,
115 window: None,
116 curve: false,
117 bpm: None,
118 cell_secs: DEFAULT_CELL_SECS,
119 smooth_secs: DEFAULT_SMOOTH_SECS,
120 }
121 }
122}
123
124#[derive(Clone, Copy, Debug, PartialEq, Eq)]
126pub enum Reads {
127 ClosedForm,
129 Either,
131 Samples,
133 Graph,
135 Structure,
137}
138
139impl Reads {
140 pub fn consumes(self, closed: bool) -> Consumes {
141 match self {
142 Reads::ClosedForm | Reads::Structure => Consumes::ClosedForm,
143 Reads::Either if closed => Consumes::ClosedForm,
144 Reads::Either | Reads::Samples | Reads::Graph => Consumes::Buffer,
145 }
146 }
147
148 pub fn off_samples(self, sampled: bool) -> bool {
149 match self {
150 Reads::Samples => true,
151 Reads::Either => sampled,
152 Reads::ClosedForm | Reads::Graph | Reads::Structure => false,
153 }
154 }
155}
156
157pub struct Reading {
158 pub name: &'static str,
159 pub takes: &'static [Arg],
160 pub reads: Reads,
161 pub help: &'static str,
162 build: fn(&Given) -> Representation,
163}
164
165impl Reading {
166 pub fn representation(&self, given: &Given) -> Representation {
167 (self.build)(given)
168 }
169}
170
171static LINES: Reading = Reading {
172 name: "lines",
173 takes: &[],
174 reads: Reads::ClosedForm,
175 help: "the exact line list of a closed form, every channel's in channel order with no \
176 channel named; `atoms` names each term's",
177 build: |_| Representation::Lines,
178};
179
180static ATOMS: Reading = Reading {
181 name: "atoms",
182 takes: &[],
183 reads: Reads::ClosedForm,
184 help: "each term of a closed form: its channel, sketch, line, decay, polynomial and window",
185 build: |_| Representation::Atoms,
186};
187
188static SPECTRUM: Reading = Reading {
189 name: "spectrum",
190 takes: &[Arg::Peaks, Arg::Frame],
191 reads: Reads::Either,
192 help: "its lines, or its spectrum averaged over every frame and, power per bin, every \
193 channel; off samples a Hann spans the samples a frame holds, zero past them, each \
194 third-octave band reads as the amplitude of the one sine carrying its power, and, \
195 loudest first, a local maximum is no peak where its bin holds no more than the \
196 peaks kept, and a DC bin 0 holds as its maximum, can leak there, each bounded by \
197 Harris's (1978) Hann sidelobe bound 1/(pi m (m^2 - 1)) at m bins, its sinusoid taken \
198 anywhere within half a bin of its own: a bound where the frame is stationary \
199 sinusoids at the kept peaks",
200 build: |g| Representation::Spectrum {
201 max_peaks: g.peaks,
202 frame_secs: g.frame,
203 },
204};
205
206static ENVELOPE: Reading = Reading {
207 name: "envelope",
208 takes: &[Arg::Frame],
209 reads: Reads::Either,
210 help: "off a closed form the squared modulus of its analytic signal, x^2 + H{x}^2, as \
211 atoms per channel; off samples rms and peak per frame: the rms of every channel's \
212 samples together, the peak of any channel",
213 build: |g| Representation::Envelope {
214 frame_secs: g.frame,
215 },
216};
217
218static DERIVATIVE: Reading = Reading {
219 name: "derivative",
220 takes: &[],
221 reads: Reads::Either,
222 help: "the derivative's atoms per channel, each series listed as `atoms` lists it; off \
223 samples the first difference, (x[n] - x[n-1]) * rate at every sample but the first",
224 build: |_| Representation::Derivative,
225};
226
227static SAMPLES: Reading = Reading {
228 name: "samples",
229 takes: &[Arg::Buffer],
230 reads: Reads::Samples,
231 help: "the samples themselves; a `.wav` destination writes them as audio, an \
232 `alsa:<device>` one plays them as they render",
233 build: |_| Representation::Samples,
234};
235
236static LEDGER: Reading = Reading {
237 name: "ledger",
238 takes: &[Arg::Depth, Arg::Brief, Arg::Skim],
239 reads: Reads::Graph,
240 help: "rms, peak, share and clipping per node under the target",
241 build: |g| Representation::Ledger { depth: g.depth },
242};
243
244static PITCH: Reading = Reading {
245 name: "pitch",
246 takes: &[Arg::Peaks, Arg::Frame],
247 reads: Reads::Either,
248 help: "the `peaks` loudest notes it holds, per frame off samples, every channel's power \
249 per bin averaged; off samples its peaks are picked as `spectrum` picks them, and a series' \
250 fundamental is read within half a bin of its root's peak, the rate over the samples \
251 its Hann spans",
252 build: |g| Representation::Pitch {
253 max_notes: g.peaks,
254 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
255 },
256};
257
258static FORMANTS: Reading = Reading {
259 name: "formants",
260 takes: &[Arg::Peaks, Arg::Frame],
261 reads: Reads::Samples,
262 help: "formant frequencies, bandwidths and levels per frame, the `peaks` lowest, off \
263 every channel's autocorrelation averaged",
264 build: |g| Representation::Formants {
265 max_formants: g.peaks,
266 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
267 },
268};
269
270static STEREO: Reading = Reading {
271 name: "stereo",
272 takes: &[Arg::Frame],
273 reads: Reads::Samples,
274 help: "correlation, width and balance between two channels, per frame",
275 build: |g| Representation::Stereo {
276 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
277 },
278};
279
280static BANDS: Reading = Reading {
281 name: "bands",
282 takes: &[],
283 reads: Reads::Samples,
284 help: "each auditory band's level over time, every channel's band energy averaged",
285 build: |_| Representation::Bands,
286};
287
288static CREST: Reading = Reading {
289 name: "crest",
290 takes: &[],
291 reads: Reads::Samples,
292 help: "peak against rms, broadband and per band: any channel's peak against the rms of \
293 every channel's samples together",
294 build: |_| Representation::Crest,
295};
296
297static LOUDNESS: Reading = Reading {
298 name: "loudness",
299 takes: &[],
300 reads: Reads::Samples,
301 help: "integrated, momentary and short-term loudness in LUFS",
302 build: |_| Representation::Loudness,
303};
304
305static ONSETS: Reading = Reading {
306 name: "onsets",
307 takes: &[],
308 reads: Reads::Samples,
309 help: "onset times, their strength and the spacing between them, each channel's spectral \
310 flux summed",
311 build: |_| Representation::Onsets,
312};
313
314static ALIAS: Reading = Reading {
315 name: "alias",
316 takes: &[Arg::Oversample],
317 reads: Reads::Graph,
318 help: "what folded, against a re-render at a multiple of the rate",
319 build: |g| Representation::Alias {
320 oversample: g.oversample,
321 },
322};
323
324static DISSONANCE: Reading = Reading {
325 name: "dissonance",
326 takes: &[Arg::Frame, Arg::Floor, Arg::Pairs],
327 reads: Reads::Either,
328 help: "Sethares' sensory roughness per frame of its partials over a floor, with its mean, \
329 max and min; measured partials off every channel's power per bin averaged",
330 build: |g| Representation::Harmony {
331 reading: Harmonic::Dissonance {
332 max_pairs: g.max_pairs,
333 },
334 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
335 floor_db: g.floor_db,
336 },
337};
338
339static VOICE_LEADING: Reading = Reading {
340 name: "voice_leading",
341 takes: &[Arg::Frame, Arg::Floor, Arg::Norm],
342 reads: Reads::Either,
343 help: "its chords over time, and the smallest voice leading between each two, l1 or l2 \
344 in semitones; measured partials off every channel's power per bin averaged",
345 build: |g| Representation::Harmony {
346 reading: Harmonic::VoiceLeading { norm: g.norm },
347 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
348 floor_db: g.floor_db,
349 },
350};
351
352static TONAL_DISTANCE: Reading = Reading {
353 name: "tonal_distance",
354 takes: &[Arg::Frame, Arg::Floor],
355 reads: Reads::Either,
356 help: "its chords over time, the Tonnetz steps between each two triads and each one's \
357 from the first triad; measured partials off every channel's power per bin averaged",
358 build: |g| Representation::Harmony {
359 reading: Harmonic::TonalDistance,
360 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
361 floor_db: g.floor_db,
362 },
363};
364
365static INHARMONICITY: Reading = Reading {
366 name: "inharmonicity",
367 takes: &[Arg::Frame, Arg::Floor],
368 reads: Reads::Either,
369 help: "its harmonic series per frame: each member's cents off n f1, the stiff-string B each \
370 fits, and the energy in no series; measured partials off every channel's power per \
371 bin averaged",
372 build: |g| Representation::Harmony {
373 reading: Harmonic::Inharmonicity,
374 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
375 floor_db: g.floor_db,
376 },
377};
378
379static PERIODICITY: Reading = Reading {
380 name: "periodicity",
381 takes: &[Arg::Frame, Arg::Window, Arg::Curve],
382 reads: Reads::Samples,
383 help: "how strongly its onset strength repeats per sliding window, broadband, under 150 Hz \
384 and over: the leading periods in seconds and bpm with their bounds, their \
385 autocorrelation heights, the drift between windows; every channel's band energy \
386 averaged",
387 build: |g| Representation::Periodicity {
388 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
389 window_secs: g.window.unwrap_or(DEFAULT_PERIODICITY_WINDOW_SECS),
390 curve: g.curve,
391 },
392};
393
394static MODULATION: Reading = Reading {
395 name: "modulation",
396 takes: &[Arg::Frame, Arg::Window, Arg::Bpm],
397 reads: Reads::Samples,
398 help: "how its band envelopes move per sliding window, under 150 Hz, to 2 kHz and over: \
399 the leading modulation rates from 0.5 to 20 Hz, in beats where `bpm` is given, \
400 their depth as a modulation index, each with its bounds, the level, and the \
401 spectral centroid's mean and its own swing in Hz; every channel's band energy \
402 averaged",
403 build: |g| Representation::Modulation {
404 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
405 window_secs: g.window.unwrap_or(DEFAULT_MODULATION_WINDOW_SECS),
406 bpm: g.bpm,
407 },
408};
409
410static SELF_SIMILARITY: Reading = Reading {
411 name: "self_similarity",
412 takes: &[
413 Arg::Frame,
414 Arg::Cell,
415 Arg::Smooth,
416 Arg::Window,
417 Arg::Floor,
418 Arg::Peaks,
419 Arg::Curve,
420 ],
421 reads: Reads::Samples,
422 help: "how its stretches resemble one another, by chroma and by band levels, over cells \
423 of `cell`: the repeated stretches as stripes, each pair of spans with its \
424 similarity and, by chroma, its transposition in semitones; Foote's novelty at every \
425 cell boundary and its peaks; `curve=1` adds the cells and the matrix; every \
426 channel's power per bin and band energy averaged",
427 build: |g| Representation::SelfSimilarity {
428 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
429 cell_secs: g.cell_secs,
430 smooth_secs: g.smooth_secs,
431 window_secs: g.window.unwrap_or(DEFAULT_NOVELTY_WINDOW_SECS),
432 floor_db: g.floor_db,
433 listed: g.peaks,
434 curve: g.curve,
435 },
436};
437
438static BINDINGS: Reading = Reading {
439 name: "bindings",
440 takes: &[Arg::Node],
441 reads: Reads::Structure,
442 help: "the bindings one instance resolved, each with its source",
443 build: |_| Representation::Bindings,
444};
445
446static ARGUMENTS: Reading = Reading {
447 name: "arguments",
448 takes: &[],
449 reads: Reads::Structure,
450 help: "each builtin call's named arguments as the numbers it was lowered with",
451 build: |_| Representation::Arguments,
452};
453
454pub static READINGS: [&Reading; 24] = [
456 &LINES,
457 &ATOMS,
458 &SPECTRUM,
459 &ENVELOPE,
460 &DERIVATIVE,
461 &SAMPLES,
462 &LEDGER,
463 &PITCH,
464 &FORMANTS,
465 &STEREO,
466 &BANDS,
467 &CREST,
468 &LOUDNESS,
469 &ONSETS,
470 &ALIAS,
471 &DISSONANCE,
472 &VOICE_LEADING,
473 &TONAL_DISTANCE,
474 &INHARMONICITY,
475 &PERIODICITY,
476 &MODULATION,
477 &SELF_SIMILARITY,
478 &BINDINGS,
479 &ARGUMENTS,
480];
481
482#[derive(Clone, Copy, Debug, PartialEq)]
483pub enum Representation {
484 Lines,
485 Atoms,
486 Spectrum {
487 max_peaks: usize,
488 frame_secs: Option<f64>,
489 },
490 Envelope {
491 frame_secs: Option<f64>,
492 },
493 Derivative,
494 Samples,
495 Ledger {
496 depth: usize,
497 },
498 Pitch {
499 max_notes: usize,
500 frame_secs: f64,
501 },
502 Formants {
503 max_formants: usize,
504 frame_secs: f64,
505 },
506 Stereo {
507 frame_secs: f64,
508 },
509 Bands,
510 Crest,
511 Loudness,
512 Onsets,
513 Alias {
514 oversample: u32,
515 },
516 Harmony {
518 reading: Harmonic,
519 frame_secs: f64,
520 floor_db: f64,
521 },
522 Periodicity {
524 frame_secs: f64,
525 window_secs: f64,
526 curve: bool,
527 },
528 Modulation {
530 frame_secs: f64,
531 window_secs: f64,
532 bpm: Option<f64>,
533 },
534 SelfSimilarity {
537 frame_secs: f64,
538 cell_secs: f64,
539 smooth_secs: f64,
540 window_secs: f64,
541 floor_db: f64,
542 listed: usize,
543 curve: bool,
544 },
545 Bindings,
546 Arguments,
547}
548
549#[derive(Clone, Copy, Debug, PartialEq, Eq)]
551pub enum Harmonic {
552 Dissonance { max_pairs: usize },
553 VoiceLeading { norm: Norm },
554 TonalDistance,
555 Inharmonicity,
556}
557
558#[derive(Clone, Debug, PartialEq)]
560pub struct Ask {
561 pub node: String,
562 pub representation: Representation,
563}
564
565#[derive(Clone, Debug, PartialEq)]
566pub enum Output {
567 Lines(Vec<Line>),
568 Atoms(Vec<Atom>),
569 Symbolic(Box<SpectralSum>),
570 Samples(Box<Buffer>),
571 Spectrum(Box<Spectrum>),
572 Envelope(Vec<EnvelopeFrame>),
573 Pitch(Vec<PitchFrame>),
574 Formants(Vec<FormantFrame>),
575 Stereo(Box<StereoImage>),
576 Bands(Box<Bands>),
577 Crest(Box<Crest>),
578 Loudness(Box<Loudness>),
579 Onsets(Box<Onsets>),
580 Alias(Box<Alias>),
581 Ledger(Vec<LedgerEntry>),
582 Dissonance(Box<Dissonance>),
583 VoiceLeading(Box<VoiceLeading>),
584 TonalDistance(Box<TonalDistance>),
585 Inharmonicity(Box<Inharmonicity>),
586 Periodicity(Box<Periodicity>),
587 Modulation(Box<Modulation>),
588 SelfSimilarity(Box<SelfSimilarity>),
589 Bindings(Vec<Binding>),
590 Arguments(Vec<Arguments>),
591}
592
593#[derive(Clone, Debug, PartialEq)]
595pub struct Atom {
596 pub var: Var,
597 pub channel: usize,
599 pub sketch: String,
600 pub amp: C64,
601 pub turns: f64,
602 pub decay: f64,
603 pub decay_from: f64,
604 pub degree: u16,
605 pub polynomial_at: f64,
606 pub start: Option<f64>,
607 pub end: Option<f64>,
608}
609
610#[derive(Clone, Debug, PartialEq)]
613pub struct Answer {
614 pub value: Output,
615 pub source: Source,
616 pub profile: &'static str,
617 pub rate: Option<u32>,
618 pub dropped: Vec<sva_formula::Line>,
620 pub tail_db: Option<f64>,
621}
622
623impl Answer {
624 pub fn whole(
625 value: Output,
626 source: Source,
627 profile: &'static str,
628 rate: Option<u32>,
629 ) -> Answer {
630 Answer {
631 value,
632 source,
633 profile,
634 rate,
635 dropped: Vec::new(),
636 tail_db: None,
637 }
638 }
639}
640
641impl Representation {
642 pub fn reading(self) -> &'static Reading {
644 match self {
645 Representation::Lines => &LINES,
646 Representation::Atoms => &ATOMS,
647 Representation::Spectrum { .. } => &SPECTRUM,
648 Representation::Envelope { .. } => &ENVELOPE,
649 Representation::Derivative => &DERIVATIVE,
650 Representation::Samples => &SAMPLES,
651 Representation::Ledger { .. } => &LEDGER,
652 Representation::Pitch { .. } => &PITCH,
653 Representation::Formants { .. } => &FORMANTS,
654 Representation::Stereo { .. } => &STEREO,
655 Representation::Bands => &BANDS,
656 Representation::Crest => &CREST,
657 Representation::Loudness => &LOUDNESS,
658 Representation::Onsets => &ONSETS,
659 Representation::Alias { .. } => &ALIAS,
660 Representation::Harmony { reading, .. } => match reading {
661 Harmonic::Dissonance { .. } => &DISSONANCE,
662 Harmonic::VoiceLeading { .. } => &VOICE_LEADING,
663 Harmonic::TonalDistance => &TONAL_DISTANCE,
664 Harmonic::Inharmonicity => &INHARMONICITY,
665 },
666 Representation::Periodicity { .. } => &PERIODICITY,
667 Representation::Modulation { .. } => &MODULATION,
668 Representation::SelfSimilarity { .. } => &SELF_SIMILARITY,
669 Representation::Bindings => &BINDINGS,
670 Representation::Arguments => &ARGUMENTS,
671 }
672 }
673
674 pub fn name(self) -> &'static str {
675 self.reading().name
676 }
677
678 pub fn consumes(self, closed: bool) -> Consumes {
679 self.reading().reads.consumes(closed)
680 }
681
682 pub fn off_samples(self, sampled: bool) -> bool {
684 self.reading().reads.off_samples(sampled)
685 }
686
687 pub fn from_name(name: &str) -> Option<Representation> {
688 READINGS
689 .iter()
690 .find(|row| row.name == name)
691 .map(|row| row.representation(&Given::default()))
692 }
693}