@ac-kit/media-piano-audio 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +63 -0
- package/LICENSE +28 -0
- package/README.md +246 -2
- package/dist/acoustics/_assert-piano-pitch.d.ts +6 -0
- package/dist/acoustics/_assert-piano-pitch.d.ts.map +1 -0
- package/dist/acoustics/_assert-piano-pitch.js +12 -0
- package/dist/acoustics/_assert-piano-pitch.js.map +1 -0
- package/dist/acoustics/piano-partials.d.ts +27 -0
- package/dist/acoustics/piano-partials.d.ts.map +1 -0
- package/dist/acoustics/piano-partials.js +60 -0
- package/dist/acoustics/piano-partials.js.map +1 -0
- package/dist/calibrate/estimate-input-latency.d.ts +41 -0
- package/dist/calibrate/estimate-input-latency.d.ts.map +1 -0
- package/dist/calibrate/estimate-input-latency.js +58 -0
- package/dist/calibrate/estimate-input-latency.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/synthesize/synthesize-piano.d.ts +97 -0
- package/dist/synthesize/synthesize-piano.d.ts.map +1 -0
- package/dist/synthesize/synthesize-piano.js +132 -0
- package/dist/synthesize/synthesize-piano.js.map +1 -0
- package/dist/synthesize/synthesize-tick.d.ts +39 -0
- package/dist/synthesize/synthesize-tick.d.ts.map +1 -0
- package/dist/synthesize/synthesize-tick.js +64 -0
- package/dist/synthesize/synthesize-tick.js.map +1 -0
- package/dist/verify/_onset-detector.d.ts +58 -0
- package/dist/verify/_onset-detector.d.ts.map +1 -0
- package/dist/verify/_onset-detector.js +206 -0
- package/dist/verify/_onset-detector.js.map +1 -0
- package/dist/verify/_templates.d.ts +37 -0
- package/dist/verify/_templates.d.ts.map +1 -0
- package/dist/verify/_templates.js +113 -0
- package/dist/verify/_templates.js.map +1 -0
- package/dist/verify/piano-chord-verifier.d.ts +204 -0
- package/dist/verify/piano-chord-verifier.d.ts.map +1 -0
- package/dist/verify/piano-chord-verifier.js +938 -0
- package/dist/verify/piano-chord-verifier.js.map +1 -0
- package/dist/verify/tuning.d.ts +126 -0
- package/dist/verify/tuning.d.ts.map +1 -0
- package/dist/verify/tuning.js +133 -0
- package/dist/verify/tuning.js.map +1 -0
- package/dist/verify/types.d.ts +74 -0
- package/dist/verify/types.d.ts.map +1 -0
- package/dist/verify/types.js +2 -0
- package/dist/verify/types.js.map +1 -0
- package/package.json +71 -3
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { SparseColumns } from "@ac-kit/math-linear";
|
|
2
|
+
/** Number of piano keys, the first {@link KEY_COUNT} template columns. */
|
|
3
|
+
export declare const KEY_COUNT: number;
|
|
4
|
+
/** What shapes the templates of one verifier, the window length aside. */
|
|
5
|
+
export interface TemplateGeometry {
|
|
6
|
+
/** Number of analysis bins. */
|
|
7
|
+
readonly bins: number;
|
|
8
|
+
readonly minPitch: number;
|
|
9
|
+
/** Highest analysis pitch. */
|
|
10
|
+
readonly maxPitch: number;
|
|
11
|
+
readonly binsPerSemitone: number;
|
|
12
|
+
readonly maxPartials: number;
|
|
13
|
+
readonly lobeWidth: number;
|
|
14
|
+
readonly bandStep: number;
|
|
15
|
+
readonly bandWidth: number;
|
|
16
|
+
readonly referenceHz: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The partials of every key, as offsets from the key in semitones (unshifted by
|
|
20
|
+
* the reference pitch), `maxPartials` per key, and how many of them fall within
|
|
21
|
+
* the analysis range.
|
|
22
|
+
*/
|
|
23
|
+
export interface KeyPartials {
|
|
24
|
+
readonly offsets: Float64Array;
|
|
25
|
+
readonly counts: Int32Array;
|
|
26
|
+
}
|
|
27
|
+
export declare function keyPartials(geometry: TemplateGeometry): KeyPartials;
|
|
28
|
+
/** Number of smooth band templates: one every `bandStep` from `minPitch`. */
|
|
29
|
+
export declare function bandTemplateCount(geometry: TemplateGeometry): number;
|
|
30
|
+
/**
|
|
31
|
+
* The dictionary for one analysis window length: 88 key templates (inharmonic
|
|
32
|
+
* partials of amplitude 1/h, each as wide as the window's main lobe), then
|
|
33
|
+
* smooth band templates every `bandStep` semitones that soak up broadband
|
|
34
|
+
* energy (hammer thump, noise). Every column sums to 1.
|
|
35
|
+
*/
|
|
36
|
+
export declare function buildTemplates(geometry: TemplateGeometry, partials: KeyPartials, windowMs: number): SparseColumns;
|
|
37
|
+
//# sourceMappingURL=_templates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_templates.d.ts","sourceRoot":"","sources":["../../src/verify/_templates.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAKzD,0EAA0E;AAC1E,eAAO,MAAM,SAAS,QAAmD,CAAC;AAmB1E,0EAA0E;AAC1E,MAAM,WAAW,gBAAgB;IAChC,+BAA+B;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,8BAA8B;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC7B;AAED;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;CAC5B;AAED,wBAAgB,WAAW,CAAC,QAAQ,EAAE,gBAAgB,GAAG,WAAW,CAoBnE;AAED,6EAA6E;AAC7E,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,gBAAgB,GAAG,MAAM,CAIpE;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAC7B,QAAQ,EAAE,gBAAgB,EAC1B,QAAQ,EAAE,WAAW,EACrB,QAAQ,EAAE,MAAM,GACd,aAAa,CAgFf"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { pitchFrequency, PIANO_PITCHES } from "@ac-kit/model-music";
|
|
2
|
+
import { pianoPartialPitches } from "../acoustics/piano-partials.js";
|
|
3
|
+
/** Number of piano keys, the first {@link KEY_COUNT} template columns. */
|
|
4
|
+
export const KEY_COUNT = PIANO_PITCHES.highest - PIANO_PITCHES.lowest + 1;
|
|
5
|
+
// Below this pitch the first two partials of a piano note are weak against the
|
|
6
|
+
// rest (the soundboard radiates little there): their template weights are
|
|
7
|
+
// scaled down. Set in the keylight microphone spike on Salamander renderings.
|
|
8
|
+
const WEAK_FUNDAMENTAL_BELOW_ = 40;
|
|
9
|
+
const WEAK_FUNDAMENTAL_GAIN_ = 0.3;
|
|
10
|
+
const WEAK_SECOND_PARTIAL_GAIN_ = 0.7;
|
|
11
|
+
// A partial is a Gaussian on the log-frequency axis whose deviation combines
|
|
12
|
+
// the window's main lobe with a tolerance growing with the partial number,
|
|
13
|
+
// for tuning and inharmonicity errors (spike: 0.2 + 0.02·h semitone); it is
|
|
14
|
+
// cut at 2.5 deviations.
|
|
15
|
+
const PARTIAL_TOLERANCE_SEMITONES_ = 0.2;
|
|
16
|
+
const PARTIAL_TOLERANCE_PER_PARTIAL_ = 0.02;
|
|
17
|
+
const GAUSSIAN_EXTENT_ = 2.5;
|
|
18
|
+
// A band template is cut at four deviations, which drops 0.006 % of its
|
|
19
|
+
// weight and two thirds of its entries (the spike kept it dense).
|
|
20
|
+
const BAND_EXTENT_ = 4;
|
|
21
|
+
export function keyPartials(geometry) {
|
|
22
|
+
const { maxPartials } = geometry;
|
|
23
|
+
const shift = 12 * Math.log2(geometry.referenceHz / 440);
|
|
24
|
+
const offsets = new Float64Array(KEY_COUNT * maxPartials);
|
|
25
|
+
const counts = new Int32Array(KEY_COUNT);
|
|
26
|
+
const scratch = new Float64Array(maxPartials);
|
|
27
|
+
for (let key = 0; key < KEY_COUNT; key++) {
|
|
28
|
+
const pitch = PIANO_PITCHES.lowest + key;
|
|
29
|
+
pianoPartialPitches(pitch, maxPartials, undefined, scratch);
|
|
30
|
+
let count = 0;
|
|
31
|
+
while (count < maxPartials &&
|
|
32
|
+
scratch[count] + shift <= geometry.maxPitch - 0.5) {
|
|
33
|
+
offsets[key * maxPartials + count] = scratch[count] - pitch;
|
|
34
|
+
count++;
|
|
35
|
+
}
|
|
36
|
+
counts[key] = count;
|
|
37
|
+
}
|
|
38
|
+
return { offsets, counts };
|
|
39
|
+
}
|
|
40
|
+
/** Number of smooth band templates: one every `bandStep` from `minPitch`. */
|
|
41
|
+
export function bandTemplateCount(geometry) {
|
|
42
|
+
return (Math.floor((geometry.maxPitch - geometry.minPitch) / geometry.bandStep) + 1);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The dictionary for one analysis window length: 88 key templates (inharmonic
|
|
46
|
+
* partials of amplitude 1/h, each as wide as the window's main lobe), then
|
|
47
|
+
* smooth band templates every `bandStep` semitones that soak up broadband
|
|
48
|
+
* energy (hammer thump, noise). Every column sums to 1.
|
|
49
|
+
*/
|
|
50
|
+
export function buildTemplates(geometry, partials, windowMs) {
|
|
51
|
+
const { bins, minPitch, binsPerSemitone, maxPartials, lobeWidth, bandStep, bandWidth, referenceHz, } = geometry;
|
|
52
|
+
const shift = 12 * Math.log2(referenceHz / 440);
|
|
53
|
+
const offsets = [0];
|
|
54
|
+
const rows = [];
|
|
55
|
+
const values = [];
|
|
56
|
+
const column = new Float64Array(bins);
|
|
57
|
+
const pushColumn = () => {
|
|
58
|
+
let sum = 0;
|
|
59
|
+
for (let bin = 0; bin < bins; bin++)
|
|
60
|
+
sum += column[bin];
|
|
61
|
+
for (let bin = 0; bin < bins; bin++) {
|
|
62
|
+
if (column[bin] > 0) {
|
|
63
|
+
rows.push(bin);
|
|
64
|
+
values.push(column[bin] / sum);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
offsets.push(rows.length);
|
|
68
|
+
};
|
|
69
|
+
for (let key = 0; key < KEY_COUNT; key++) {
|
|
70
|
+
column.fill(0);
|
|
71
|
+
const pitch = PIANO_PITCHES.lowest + key;
|
|
72
|
+
for (let index = 0; index < partials.counts[key]; index++) {
|
|
73
|
+
const h = index + 1;
|
|
74
|
+
const partialPitch = pitch + shift + partials.offsets[key * maxPartials + index];
|
|
75
|
+
let amplitude = 1 / h;
|
|
76
|
+
if (pitch < WEAK_FUNDAMENTAL_BELOW_ && h === 1) {
|
|
77
|
+
amplitude *= WEAK_FUNDAMENTAL_GAIN_;
|
|
78
|
+
}
|
|
79
|
+
if (pitch < WEAK_FUNDAMENTAL_BELOW_ && h === 2) {
|
|
80
|
+
amplitude *= WEAK_SECOND_PARTIAL_GAIN_;
|
|
81
|
+
}
|
|
82
|
+
const frequency = pitchFrequency(partialPitch, 440);
|
|
83
|
+
const lobe = 12 * Math.log2(1 + (lobeWidth * 1000) / windowMs / frequency);
|
|
84
|
+
const tolerance = PARTIAL_TOLERANCE_SEMITONES_ + PARTIAL_TOLERANCE_PER_PARTIAL_ * h;
|
|
85
|
+
const sigma = Math.sqrt(tolerance * tolerance + lobe * lobe) * binsPerSemitone;
|
|
86
|
+
const centre = (partialPitch - minPitch) * binsPerSemitone;
|
|
87
|
+
const from = Math.max(0, Math.floor(centre - GAUSSIAN_EXTENT_ * sigma));
|
|
88
|
+
const to = Math.min(bins - 1, Math.ceil(centre + GAUSSIAN_EXTENT_ * sigma));
|
|
89
|
+
for (let bin = from; bin <= to; bin++) {
|
|
90
|
+
const z = (bin - centre) / sigma;
|
|
91
|
+
const weight = amplitude * Math.exp(-0.5 * z * z);
|
|
92
|
+
if (weight > column[bin])
|
|
93
|
+
column[bin] = weight;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
pushColumn();
|
|
97
|
+
}
|
|
98
|
+
const bandCount = bandTemplateCount(geometry);
|
|
99
|
+
for (let index = 0; index < bandCount; index++) {
|
|
100
|
+
const centre = minPitch + index * bandStep;
|
|
101
|
+
for (let bin = 0; bin < bins; bin++) {
|
|
102
|
+
const z = (minPitch + bin / binsPerSemitone - centre) / bandWidth;
|
|
103
|
+
column[bin] = Math.abs(z) <= BAND_EXTENT_ ? Math.exp(-0.5 * z * z) : 0;
|
|
104
|
+
}
|
|
105
|
+
pushColumn();
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
columnOffsets: Int32Array.from(offsets),
|
|
109
|
+
rowIndices: Int32Array.from(rows),
|
|
110
|
+
values: Float64Array.from(values),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=_templates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_templates.js","sourceRoot":"","sources":["../../src/verify/_templates.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAErE,0EAA0E;AAC1E,MAAM,CAAC,MAAM,SAAS,GAAG,aAAa,CAAC,OAAO,GAAG,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC;AAE1E,+EAA+E;AAC/E,0EAA0E;AAC1E,8EAA8E;AAC9E,MAAM,uBAAuB,GAAG,EAAE,CAAC;AACnC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AACnC,MAAM,yBAAyB,GAAG,GAAG,CAAC;AACtC,6EAA6E;AAC7E,2EAA2E;AAC3E,4EAA4E;AAC5E,yBAAyB;AACzB,MAAM,4BAA4B,GAAG,GAAG,CAAC;AACzC,MAAM,8BAA8B,GAAG,IAAI,CAAC;AAC5C,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,wEAAwE;AACxE,kEAA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AAO;QACN,aAAa,EAAE,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC;QACvC,UAAU,EAAE,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC;QACjC,MAAM,EAAE,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC;KACjC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { type PianoVerifierTuning } from "./tuning.js";
|
|
2
|
+
import type { ChordVerdict, NoiseProfile } from "./types.js";
|
|
3
|
+
/** Options of {@link PianoChordVerifier}. */
|
|
4
|
+
export interface PianoChordVerifierOptions {
|
|
5
|
+
/** Sampling rate of the input, Hz: 22 050 to 96 000. */
|
|
6
|
+
readonly sampleRate: number;
|
|
7
|
+
/**
|
|
8
|
+
* Length of the spectra compared across an onset, ms: 30 to 120, default 90.
|
|
9
|
+
* Longer catches more wrong and missing notes and decides later (about the
|
|
10
|
+
* window plus 40 ms after the onset).
|
|
11
|
+
*/
|
|
12
|
+
readonly windowMs?: number;
|
|
13
|
+
/** Non-negative input latency taken off every `onsetTime`, ms. Default 0. */
|
|
14
|
+
readonly latencyMs?: number;
|
|
15
|
+
/** Frequency of A4, Hz: 392 to 494 (a whole tone around 440). Default 440. */
|
|
16
|
+
readonly referenceHz?: number;
|
|
17
|
+
/** Default {@link DEFAULT_PIANO_VERIFIER_TUNING}. */
|
|
18
|
+
readonly tuning?: PianoVerifierTuning;
|
|
19
|
+
/** A profile from an earlier {@link PianoChordVerifier.noiseProfile}. */
|
|
20
|
+
readonly noiseProfile?: NoiseProfile;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Score-informed chord verification from a microphone: tell it which chord is
|
|
24
|
+
* due, feed it the audio, and it reports, for each key press it detects, which
|
|
25
|
+
* expected notes it heard, which it did not, and which other notes sounded. It
|
|
26
|
+
* verifies; it does not transcribe.
|
|
27
|
+
*
|
|
28
|
+
* Streaming and synchronous, for an `AudioWorkletProcessor`: {@link process}
|
|
29
|
+
* takes blocks of any length and works in 128-sample hops. In steady state it
|
|
30
|
+
* creates objects only for a verdict, and the analysis of a press is spread
|
|
31
|
+
* over several hops so no block carries all of it.
|
|
32
|
+
*
|
|
33
|
+
* Per press: an onset found by spectral flux (over all bands and over the
|
|
34
|
+
* expected notes' bands), then the new energy — the spectrum of `windowMs`
|
|
35
|
+
* after the onset minus that of the same length before it, minus the noise —
|
|
36
|
+
* decomposed onto harmonic templates of the 88 keys by KL-NMF activations, in
|
|
37
|
+
* which expected notes and notes heard in the last 1.5 s are free and other
|
|
38
|
+
* notes pay a penalty. A press whose new energy is too weak against what was
|
|
39
|
+
* already sounding gives no verdict.
|
|
40
|
+
*
|
|
41
|
+
* Time 0 is the first sample processed, or the last {@link reset}.
|
|
42
|
+
*/
|
|
43
|
+
export declare class PianoChordVerifier {
|
|
44
|
+
/** Sampling rate of the input, Hz. */
|
|
45
|
+
readonly sampleRate: number;
|
|
46
|
+
/** Analysis window, ms. */
|
|
47
|
+
readonly windowMs: number;
|
|
48
|
+
/** Frequency of A4 the templates are tuned to, Hz. */
|
|
49
|
+
readonly referenceHz: number;
|
|
50
|
+
/** The tuning in use, frozen. */
|
|
51
|
+
readonly tuning: PianoVerifierTuning;
|
|
52
|
+
private latencyMs_;
|
|
53
|
+
private readonly onset_;
|
|
54
|
+
private readonly ring_;
|
|
55
|
+
private readonly mask_;
|
|
56
|
+
private position_;
|
|
57
|
+
private hopFill_;
|
|
58
|
+
private readonly fft_;
|
|
59
|
+
private readonly fftSize_;
|
|
60
|
+
private readonly fftInput_;
|
|
61
|
+
private readonly spectrum_;
|
|
62
|
+
private readonly magnitudes_;
|
|
63
|
+
private readonly map_;
|
|
64
|
+
private readonly bins_;
|
|
65
|
+
private readonly after_;
|
|
66
|
+
private readonly before_;
|
|
67
|
+
private readonly newEnergy_;
|
|
68
|
+
private readonly penalties_;
|
|
69
|
+
private readonly uniform_;
|
|
70
|
+
private readonly window_;
|
|
71
|
+
private readonly cutWindow_;
|
|
72
|
+
private readonly cutWindows_;
|
|
73
|
+
private cutWindowLength_;
|
|
74
|
+
private readonly grid_;
|
|
75
|
+
private readonly historyFrom_;
|
|
76
|
+
private readonly historyId_;
|
|
77
|
+
private readonly historyExpected_;
|
|
78
|
+
private readonly historyBands_;
|
|
79
|
+
private bandsFrom_;
|
|
80
|
+
private bandsUntil_;
|
|
81
|
+
private historyCount_;
|
|
82
|
+
private readonly windowSamples_;
|
|
83
|
+
private readonly minAnalysis_;
|
|
84
|
+
private readonly cutMargin_;
|
|
85
|
+
private readonly guardAfter_;
|
|
86
|
+
private readonly guardBefore_;
|
|
87
|
+
private readonly mergeSamples_;
|
|
88
|
+
private readonly recentSamples_;
|
|
89
|
+
private readonly partials_;
|
|
90
|
+
private readonly templateSets_;
|
|
91
|
+
private readonly templateMaxMs_;
|
|
92
|
+
private bandNoise_;
|
|
93
|
+
private newEnergyNoise_;
|
|
94
|
+
private profile_;
|
|
95
|
+
private pendingActive_;
|
|
96
|
+
private readonly pending_;
|
|
97
|
+
private pendingDue_;
|
|
98
|
+
private readonly queue_;
|
|
99
|
+
private queueHead_;
|
|
100
|
+
private queueLength_;
|
|
101
|
+
private phase_;
|
|
102
|
+
private readonly job_;
|
|
103
|
+
private jobLength_;
|
|
104
|
+
private jobTotal_;
|
|
105
|
+
private jobNoiseScale_;
|
|
106
|
+
private jobIterations_;
|
|
107
|
+
private jobSet_;
|
|
108
|
+
private readonly allowed_;
|
|
109
|
+
private readonly recentUntil_;
|
|
110
|
+
private calibrating_;
|
|
111
|
+
private calibrationStart_;
|
|
112
|
+
private calibrationEnd_;
|
|
113
|
+
private calibrationNext_;
|
|
114
|
+
private calibrationFrames_;
|
|
115
|
+
private calibrationMeasures_;
|
|
116
|
+
private calibrationSquares_;
|
|
117
|
+
private readonly calibrationBands_;
|
|
118
|
+
private readonly calibrationNewEnergy_;
|
|
119
|
+
/**
|
|
120
|
+
* @throws {RangeError} On an option out of range, an invalid tuning (naming
|
|
121
|
+
* the field) or a noise profile measured with another sampling rate, window
|
|
122
|
+
* or tuning.
|
|
123
|
+
*/
|
|
124
|
+
constructor(options: PianoChordVerifierOptions);
|
|
125
|
+
/** Input latency taken off every `onsetTime`, ms. */
|
|
126
|
+
get latencyMs(): number;
|
|
127
|
+
/** @throws {RangeError} If negative or not finite. */
|
|
128
|
+
set latencyMs(value: number);
|
|
129
|
+
/** Whether a {@link calibrateNoise} is in progress; no verdict comes meanwhile. */
|
|
130
|
+
get calibrating(): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Schedules a chord on the {@link ChordVerdict.onsetTime} clock. It applies at
|
|
133
|
+
* `fromTime` (inclusive) until the next scheduled change. The default is the
|
|
134
|
+
* latency-corrected time of the next input frame. Updates at the same time
|
|
135
|
+
* replace one another; already confirmed onsets are unchanged. Pitches are
|
|
136
|
+
* MIDI keys 21–108 in any order, or `[]` for a rest.
|
|
137
|
+
*
|
|
138
|
+
* @throws {RangeError} On invalid pitches, a non-finite id or time, a time
|
|
139
|
+
* older than the confirmation window, or a full schedule.
|
|
140
|
+
*/
|
|
141
|
+
expect(id: number, pitches: readonly number[], options?: {
|
|
142
|
+
fromTime?: number;
|
|
143
|
+
}): void;
|
|
144
|
+
/**
|
|
145
|
+
* Measures the background noise over the next `durationMs` of input, during
|
|
146
|
+
* which the player must keep silent, and uses it from then on. Pending
|
|
147
|
+
* verdicts are dropped; none comes until it ends. Calling again restarts the
|
|
148
|
+
* measurement. Check {@link calibrating} after {@link process} to detect
|
|
149
|
+
* completion.
|
|
150
|
+
*
|
|
151
|
+
* @param durationMs - At least twice the window plus 10 ms; default 1000.
|
|
152
|
+
* @throws {RangeError} If `durationMs` is too short or not finite.
|
|
153
|
+
*/
|
|
154
|
+
calibrateNoise(durationMs?: number): void;
|
|
155
|
+
/**
|
|
156
|
+
* The noise profile in use — from the last completed calibration or the
|
|
157
|
+
* constructor — as a new object, or `null` when there is none.
|
|
158
|
+
*/
|
|
159
|
+
noiseProfile(): NoiseProfile | null;
|
|
160
|
+
private profileOf_;
|
|
161
|
+
/** Keeps the current expectation, discarding all scheduled changes. */
|
|
162
|
+
private restartHistory_;
|
|
163
|
+
private recordExpectation_;
|
|
164
|
+
/** Last change at or before the timestamp, or −1 if none is retained. */
|
|
165
|
+
private expectationIndexAt_;
|
|
166
|
+
/** Copies the expectation and latency in force when the onset is confirmed. */
|
|
167
|
+
private expectationAt_;
|
|
168
|
+
/**
|
|
169
|
+
* Restarts time at 0 and forgets the audio, pending onsets, analyses,
|
|
170
|
+
* recently heard notes and any calibration in progress. Keeps the noise
|
|
171
|
+
* profile, the latency and the current expectation, not scheduled changes.
|
|
172
|
+
*/
|
|
173
|
+
reset(): void;
|
|
174
|
+
/**
|
|
175
|
+
* Feeds the next samples of the input, mono, in [−1, 1] — an
|
|
176
|
+
* `AudioWorkletProcessor`'s input channel as it comes, of any length.
|
|
177
|
+
*
|
|
178
|
+
* @returns The verdicts decided during this block, in order; a shared frozen
|
|
179
|
+
* empty array when there are none.
|
|
180
|
+
*/
|
|
181
|
+
process(block: Float32Array): readonly ChordVerdict[];
|
|
182
|
+
private checkProfile_;
|
|
183
|
+
private dropAnalyses_;
|
|
184
|
+
/** One 128-sample hop: onset detection, then one step of analysis. */
|
|
185
|
+
private hop_;
|
|
186
|
+
private dueOf_;
|
|
187
|
+
private enqueue_;
|
|
188
|
+
private calibrationHop_;
|
|
189
|
+
private finishCalibration_;
|
|
190
|
+
/** Runs one phase of the analysis in progress, or starts the next. */
|
|
191
|
+
private step_;
|
|
192
|
+
/** One hop's share of the activation updates; the verdict after the last. */
|
|
193
|
+
private iterate_;
|
|
194
|
+
/**
|
|
195
|
+
* Windowed log-frequency magnitude spectrum of `jobLength_` samples from
|
|
196
|
+
* `start`.
|
|
197
|
+
*/
|
|
198
|
+
private logSpectrum_;
|
|
199
|
+
/** New energy, allowed notes and penalties; loads the activations. */
|
|
200
|
+
private prepare_;
|
|
201
|
+
/** The gate and the verdict, from the activations. */
|
|
202
|
+
private finish_;
|
|
203
|
+
}
|
|
204
|
+
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