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