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, an all-pole fit of 2 poles and 1 per kHz \
264 of rate, at most 64: a frame or target holding no more samples than its order \
265 refuses, and a tail frame reads zero past its end",
266 build: |g| Representation::Formants {
267 max_formants: g.peaks,
268 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
269 },
270};
271
272static STEREO: Reading = Reading {
273 name: "stereo",
274 takes: &[Arg::Frame],
275 reads: Reads::Samples,
276 help: "correlation, width and balance between exactly two channels, per frame; any other \
277 count refuses",
278 build: |g| Representation::Stereo {
279 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
280 },
281};
282
283static BANDS: Reading = Reading {
284 name: "bands",
285 takes: &[],
286 reads: Reads::Samples,
287 help: "each auditory band's level over time, every channel's band energy averaged",
288 build: |_| Representation::Bands,
289};
290
291static CREST: Reading = Reading {
292 name: "crest",
293 takes: &[],
294 reads: Reads::Samples,
295 help: "peak against rms, broadband and per band: any channel's peak against the rms of \
296 every channel's samples together",
297 build: |_| Representation::Crest,
298};
299
300static LOUDNESS: Reading = Reading {
301 name: "loudness",
302 takes: &[],
303 reads: Reads::Samples,
304 help: "integrated, momentary and short-term loudness in LUFS",
305 build: |_| Representation::Loudness,
306};
307
308static ONSETS: Reading = Reading {
309 name: "onsets",
310 takes: &[],
311 reads: Reads::Samples,
312 help: "onset times, their strength and the spacing between them, each channel's spectral \
313 flux summed",
314 build: |_| Representation::Onsets,
315};
316
317static ALIAS: Reading = Reading {
318 name: "alias",
319 takes: &[Arg::Oversample],
320 reads: Reads::Graph,
321 help: "what folded, against a re-render at a multiple of the rate",
322 build: |g| Representation::Alias {
323 oversample: g.oversample,
324 },
325};
326
327static DISSONANCE: Reading = Reading {
328 name: "dissonance",
329 takes: &[Arg::Frame, Arg::Floor, Arg::Pairs],
330 reads: Reads::Either,
331 help: "Sethares' sensory roughness per frame of its partials over a floor, with its mean, \
332 max and min; measured partials off every channel's power per bin averaged",
333 build: |g| Representation::Harmony {
334 reading: Harmonic::Dissonance {
335 max_pairs: g.max_pairs,
336 },
337 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
338 floor_db: g.floor_db,
339 },
340};
341
342static VOICE_LEADING: Reading = Reading {
343 name: "voice_leading",
344 takes: &[Arg::Frame, Arg::Floor, Arg::Norm],
345 reads: Reads::Either,
346 help: "its chords over time, and the smallest voice leading between each two, l1 or l2 \
347 in semitones; measured partials off every channel's power per bin averaged",
348 build: |g| Representation::Harmony {
349 reading: Harmonic::VoiceLeading { norm: g.norm },
350 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
351 floor_db: g.floor_db,
352 },
353};
354
355static TONAL_DISTANCE: Reading = Reading {
356 name: "tonal_distance",
357 takes: &[Arg::Frame, Arg::Floor],
358 reads: Reads::Either,
359 help: "its chords over time, the Tonnetz steps between each two triads and each one's \
360 from the first triad; measured partials off every channel's power per bin averaged",
361 build: |g| Representation::Harmony {
362 reading: Harmonic::TonalDistance,
363 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
364 floor_db: g.floor_db,
365 },
366};
367
368static INHARMONICITY: Reading = Reading {
369 name: "inharmonicity",
370 takes: &[Arg::Frame, Arg::Floor],
371 reads: Reads::Either,
372 help: "its harmonic series per frame: each member's cents off n f1, the stiff-string B each \
373 fits, and the energy in no series; measured partials off every channel's power per \
374 bin averaged",
375 build: |g| Representation::Harmony {
376 reading: Harmonic::Inharmonicity,
377 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
378 floor_db: g.floor_db,
379 },
380};
381
382static PERIODICITY: Reading = Reading {
383 name: "periodicity",
384 takes: &[Arg::Frame, Arg::Window, Arg::Curve],
385 reads: Reads::Samples,
386 help: "how strongly its onset strength repeats per sliding window, broadband, under 150 Hz \
387 and over: the leading periods in seconds and bpm with their bounds, their \
388 autocorrelation heights, the drift between windows; every channel's band energy \
389 averaged",
390 build: |g| Representation::Periodicity {
391 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
392 window_secs: g.window.unwrap_or(DEFAULT_PERIODICITY_WINDOW_SECS),
393 curve: g.curve,
394 },
395};
396
397static MODULATION: Reading = Reading {
398 name: "modulation",
399 takes: &[Arg::Frame, Arg::Window, Arg::Bpm],
400 reads: Reads::Samples,
401 help: "how its band envelopes move per sliding window, under 150 Hz, to 2 kHz and over: \
402 the leading modulation rates from 0.5 to 20 Hz, in beats where `bpm` is given, \
403 their depth as a modulation index, each with its bounds, the level, and the \
404 spectral centroid's mean and its own swing in Hz; every channel's band energy \
405 averaged",
406 build: |g| Representation::Modulation {
407 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
408 window_secs: g.window.unwrap_or(DEFAULT_MODULATION_WINDOW_SECS),
409 bpm: g.bpm,
410 },
411};
412
413static SELF_SIMILARITY: Reading = Reading {
414 name: "self_similarity",
415 takes: &[
416 Arg::Frame,
417 Arg::Cell,
418 Arg::Smooth,
419 Arg::Window,
420 Arg::Floor,
421 Arg::Peaks,
422 Arg::Curve,
423 ],
424 reads: Reads::Samples,
425 help: "how its stretches resemble one another, by chroma and by band levels, over cells \
426 of `cell`: the repeated stretches as stripes, each pair of spans with its \
427 similarity and, by chroma, its transposition in semitones; Foote's novelty at every \
428 cell boundary and its peaks; `curve=1` adds the cells and the matrix; every \
429 channel's power per bin and band energy averaged",
430 build: |g| Representation::SelfSimilarity {
431 frame_secs: g.frame.unwrap_or(DEFAULT_FRAME_SECS),
432 cell_secs: g.cell_secs,
433 smooth_secs: g.smooth_secs,
434 window_secs: g.window.unwrap_or(DEFAULT_NOVELTY_WINDOW_SECS),
435 floor_db: g.floor_db,
436 listed: g.peaks,
437 curve: g.curve,
438 },
439};
440
441static BINDINGS: Reading = Reading {
442 name: "bindings",
443 takes: &[Arg::Node],
444 reads: Reads::Structure,
445 help: "the bindings one instance resolved, each with its source",
446 build: |_| Representation::Bindings,
447};
448
449static ARGUMENTS: Reading = Reading {
450 name: "arguments",
451 takes: &[],
452 reads: Reads::Structure,
453 help: "each builtin call's named arguments as the numbers it was lowered with",
454 build: |_| Representation::Arguments,
455};
456
457pub static READINGS: [&Reading; 24] = [
459 &LINES,
460 &ATOMS,
461 &SPECTRUM,
462 &ENVELOPE,
463 &DERIVATIVE,
464 &SAMPLES,
465 &LEDGER,
466 &PITCH,
467 &FORMANTS,
468 &STEREO,
469 &BANDS,
470 &CREST,
471 &LOUDNESS,
472 &ONSETS,
473 &ALIAS,
474 &DISSONANCE,
475 &VOICE_LEADING,
476 &TONAL_DISTANCE,
477 &INHARMONICITY,
478 &PERIODICITY,
479 &MODULATION,
480 &SELF_SIMILARITY,
481 &BINDINGS,
482 &ARGUMENTS,
483];
484
485#[derive(Clone, Copy, Debug, PartialEq)]
486pub enum Representation {
487 Lines,
488 Atoms,
489 Spectrum {
490 max_peaks: usize,
491 frame_secs: Option<f64>,
492 },
493 Envelope {
494 frame_secs: Option<f64>,
495 },
496 Derivative,
497 Samples,
498 Ledger {
499 depth: usize,
500 },
501 Pitch {
502 max_notes: usize,
503 frame_secs: f64,
504 },
505 Formants {
506 max_formants: usize,
507 frame_secs: f64,
508 },
509 Stereo {
510 frame_secs: f64,
511 },
512 Bands,
513 Crest,
514 Loudness,
515 Onsets,
516 Alias {
517 oversample: u32,
518 },
519 Harmony {
521 reading: Harmonic,
522 frame_secs: f64,
523 floor_db: f64,
524 },
525 Periodicity {
527 frame_secs: f64,
528 window_secs: f64,
529 curve: bool,
530 },
531 Modulation {
533 frame_secs: f64,
534 window_secs: f64,
535 bpm: Option<f64>,
536 },
537 SelfSimilarity {
540 frame_secs: f64,
541 cell_secs: f64,
542 smooth_secs: f64,
543 window_secs: f64,
544 floor_db: f64,
545 listed: usize,
546 curve: bool,
547 },
548 Bindings,
549 Arguments,
550}
551
552#[derive(Clone, Copy, Debug, PartialEq, Eq)]
554pub enum Harmonic {
555 Dissonance { max_pairs: usize },
556 VoiceLeading { norm: Norm },
557 TonalDistance,
558 Inharmonicity,
559}
560
561#[derive(Clone, Debug, PartialEq)]
563pub struct Ask {
564 pub node: String,
565 pub representation: Representation,
566}
567
568#[derive(Clone, Debug, PartialEq)]
569pub enum Output {
570 Lines(Vec<Line>),
571 Atoms(Vec<Atom>),
572 Symbolic(Box<SpectralSum>),
573 Samples(Box<Buffer>),
574 Spectrum(Box<Spectrum>),
575 Envelope(Vec<EnvelopeFrame>),
576 Pitch(Vec<PitchFrame>),
577 Formants(Vec<FormantFrame>),
578 Stereo(Box<StereoImage>),
579 Bands(Box<Bands>),
580 Crest(Box<Crest>),
581 Loudness(Box<Loudness>),
582 Onsets(Box<Onsets>),
583 Alias(Box<Alias>),
584 Ledger(Vec<LedgerEntry>),
585 Dissonance(Box<Dissonance>),
586 VoiceLeading(Box<VoiceLeading>),
587 TonalDistance(Box<TonalDistance>),
588 Inharmonicity(Box<Inharmonicity>),
589 Periodicity(Box<Periodicity>),
590 Modulation(Box<Modulation>),
591 SelfSimilarity(Box<SelfSimilarity>),
592 Bindings(Vec<Binding>),
593 Arguments(Vec<Arguments>),
594}
595
596#[derive(Clone, Debug, PartialEq)]
598pub struct Atom {
599 pub var: Var,
600 pub channel: usize,
602 pub sketch: String,
603 pub amp: C64,
604 pub turns: f64,
605 pub decay: f64,
606 pub decay_from: f64,
607 pub degree: u16,
608 pub polynomial_at: f64,
609 pub start: Option<f64>,
610 pub end: Option<f64>,
611}
612
613#[derive(Clone, Debug, PartialEq)]
616pub struct Answer {
617 pub value: Output,
618 pub source: Source,
619 pub profile: &'static str,
620 pub rate: Option<u32>,
621 pub dropped: Vec<sva_formula::Line>,
623 pub tail_db: Option<f64>,
624}
625
626impl Answer {
627 pub fn whole(
628 value: Output,
629 source: Source,
630 profile: &'static str,
631 rate: Option<u32>,
632 ) -> Answer {
633 Answer {
634 value,
635 source,
636 profile,
637 rate,
638 dropped: Vec::new(),
639 tail_db: None,
640 }
641 }
642}
643
644impl Representation {
645 pub fn reading(self) -> &'static Reading {
647 match self {
648 Representation::Lines => &LINES,
649 Representation::Atoms => &ATOMS,
650 Representation::Spectrum { .. } => &SPECTRUM,
651 Representation::Envelope { .. } => &ENVELOPE,
652 Representation::Derivative => &DERIVATIVE,
653 Representation::Samples => &SAMPLES,
654 Representation::Ledger { .. } => &LEDGER,
655 Representation::Pitch { .. } => &PITCH,
656 Representation::Formants { .. } => &FORMANTS,
657 Representation::Stereo { .. } => &STEREO,
658 Representation::Bands => &BANDS,
659 Representation::Crest => &CREST,
660 Representation::Loudness => &LOUDNESS,
661 Representation::Onsets => &ONSETS,
662 Representation::Alias { .. } => &ALIAS,
663 Representation::Harmony { reading, .. } => match reading {
664 Harmonic::Dissonance { .. } => &DISSONANCE,
665 Harmonic::VoiceLeading { .. } => &VOICE_LEADING,
666 Harmonic::TonalDistance => &TONAL_DISTANCE,
667 Harmonic::Inharmonicity => &INHARMONICITY,
668 },
669 Representation::Periodicity { .. } => &PERIODICITY,
670 Representation::Modulation { .. } => &MODULATION,
671 Representation::SelfSimilarity { .. } => &SELF_SIMILARITY,
672 Representation::Bindings => &BINDINGS,
673 Representation::Arguments => &ARGUMENTS,
674 }
675 }
676
677 pub fn name(self) -> &'static str {
678 self.reading().name
679 }
680
681 pub fn consumes(self, closed: bool) -> Consumes {
682 self.reading().reads.consumes(closed)
683 }
684
685 pub fn off_samples(self, sampled: bool) -> bool {
687 self.reading().reads.off_samples(sampled)
688 }
689
690 pub fn from_name(name: &str) -> Option<Representation> {
691 READINGS
692 .iter()
693 .find(|row| row.name == name)
694 .map(|row| row.representation(&Given::default()))
695 }
696}