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