@ac-kit/media-piano-audio 0.1.0 → 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 +21 -0
- package/README.md +41 -1
- 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.map +1 -1
- package/dist/acoustics/piano-partials.js +3 -9
- package/dist/acoustics/piano-partials.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- 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/package.json +10 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @ac-kit/media-piano-audio
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 065cf8d: Synthetic signals for tests, demonstrations and loopback tones, deterministic for given arguments. `synthesizePiano(notes, { sampleRate, durationMs, referenceHz?, noise?, seed? })` sounds each `{ pitch, time, release?, level? }` note as the stretched partials of `pianoPartialPitches` under an attack and a pitch-dependent decay, a released note dying away in about 50 ms, plus seeded uniform white noise of the given RMS. `chordNotes(pitches, time, { spread?, level?, direction? })` gives the notes of a chord struck together or spread from the lowest or highest key, and refuses any pitch, level or resulting time that `synthesizePiano` would reject. `synthesizeTick({ sampleRate, band, durationMs, level?, seed? })` is a burst of noise confined to a band under a Hann envelope, its peak at `level`. Each throws `RangeError` on an option or note out of range. `@ac-kit/math-random` becomes a runtime dependency.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [065cf8d]
|
|
12
|
+
- Updated dependencies [065cf8d]
|
|
13
|
+
- Updated dependencies [065cf8d]
|
|
14
|
+
- Updated dependencies [065cf8d]
|
|
15
|
+
- Updated dependencies [065cf8d]
|
|
16
|
+
- Updated dependencies [065cf8d]
|
|
17
|
+
- @ac-kit/core@0.11.0
|
|
18
|
+
- @ac-kit/math-random@0.3.0
|
|
19
|
+
- @ac-kit/math-signal@0.3.0
|
|
20
|
+
- @ac-kit/math-stats@0.4.0
|
|
21
|
+
- @ac-kit/math-linear@0.2.1
|
|
22
|
+
- @ac-kit/model-music@0.8.1
|
|
23
|
+
|
|
3
24
|
## 0.1.0
|
|
4
25
|
|
|
5
26
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
Piano audio analysis. First content: score-informed chord verification — tell
|
|
4
4
|
it which chord is due, feed it the microphone, and for every key press it
|
|
5
5
|
reports which expected notes it heard, which it did not, and which other notes
|
|
6
|
-
sounded. It verifies what a score asks for; it does not transcribe.
|
|
6
|
+
sounded. It verifies what a score asks for; it does not transcribe. Beside
|
|
7
|
+
it, a synthetic piano and a band-limited tick, for tests, demonstrations and the
|
|
8
|
+
tones a page plays to measure its own loopback.
|
|
7
9
|
|
|
8
10
|
```ts
|
|
9
11
|
import { PianoChordVerifier } from "@ac-kit/media-piano-audio";
|
|
@@ -60,6 +62,44 @@ levelDb }`, a plain object `structuredClone` and `postMessage` carry. A
|
|
|
60
62
|
JSON data, so a tuning for another piano or microphone ships as a file.
|
|
61
63
|
- `pianoInharmonicity(pitch)`, `pianoPartialPitches(pitch, count,
|
|
62
64
|
inharmonicity?, out?)` — where a piano string's partials sound.
|
|
65
|
+
- `synthesizePiano(notes, { sampleRate, durationMs, referenceHz?, noise?,
|
|
66
|
+
seed? })` — a piano-like signal from the same partial model, with seeded
|
|
67
|
+
uniform white noise; `chordNotes(pitches, time, { spread?, level?,
|
|
68
|
+
direction? })` makes the notes of a chord, struck together or spread.
|
|
69
|
+
- `synthesizeTick({ sampleRate, band, durationMs, level?, seed? })` — a short
|
|
70
|
+
burst of noise confined to a band under a Hann envelope, a click kept out of
|
|
71
|
+
the bands the verifier listens to.
|
|
72
|
+
|
|
73
|
+
## Synthetic piano and tick
|
|
74
|
+
|
|
75
|
+
The synthetic piano is the verifier's own model of a piano: each note's
|
|
76
|
+
stretched partials (`pianoPartialPitches`, up to 20, none within 500 Hz of the
|
|
77
|
+
Nyquist frequency) at amplitude `level / h`, a 3 ms attack and an exponential
|
|
78
|
+
decay faster towards the treble and the upper partials; a note with a
|
|
79
|
+
`release` dies away in about 50 ms. It is not a realistic piano: test the
|
|
80
|
+
verifier's accuracy on rendered SoundFont clips. Note times are seconds on the
|
|
81
|
+
signal's clock, like `onsetTime`; the signal's length is `durationMs`. The same
|
|
82
|
+
arguments always give the same samples.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import {
|
|
86
|
+
chordNotes,
|
|
87
|
+
PianoChordVerifier,
|
|
88
|
+
synthesizePiano,
|
|
89
|
+
synthesizeTick,
|
|
90
|
+
} from "@ac-kit/media-piano-audio";
|
|
91
|
+
|
|
92
|
+
const signal = synthesizePiano(chordNotes([60, 64, 67], 0.5, { spread: 0.01 }), {
|
|
93
|
+
sampleRate: 48_000,
|
|
94
|
+
durationMs: 1500,
|
|
95
|
+
noise: 1e-4,
|
|
96
|
+
});
|
|
97
|
+
const verifier = new PianoChordVerifier({ sampleRate: 48_000 });
|
|
98
|
+
verifier.expect(1, [60, 64, 67]);
|
|
99
|
+
const verdicts = verifier.process(signal);
|
|
100
|
+
|
|
101
|
+
const tick = synthesizeTick({ sampleRate: 48_000, band: [12_000, 16_000], durationMs: 15 });
|
|
102
|
+
```
|
|
63
103
|
|
|
64
104
|
## The contract
|
|
65
105
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_assert-piano-pitch.d.ts","sourceRoot":"","sources":["../../src/acoustics/_assert-piano-pitch.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CASpD"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PIANO_PITCHES } from "@ac-kit/model-music";
|
|
2
|
+
/**
|
|
3
|
+
* Throws a `RangeError` unless `pitch` is a finite MIDI pitch on the piano,
|
|
4
|
+
* 21–108.
|
|
5
|
+
*/
|
|
6
|
+
export function assertPianoPitch(pitch) {
|
|
7
|
+
if (!(pitch >= PIANO_PITCHES.lowest && pitch <= PIANO_PITCHES.highest) ||
|
|
8
|
+
!Number.isFinite(pitch)) {
|
|
9
|
+
throw new RangeError(`pitch must be within the piano's ${PIANO_PITCHES.lowest}–${PIANO_PITCHES.highest}, got ${pitch}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=_assert-piano-pitch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_assert-piano-pitch.js","sourceRoot":"","sources":["../../src/acoustics/_assert-piano-pitch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC7C,IACC,CAAC,CAAC,KAAK,IAAI,aAAa,CAAC,MAAM,IAAI,KAAK,IAAI,aAAa,CAAC,OAAO,CAAC;QAClE,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EACtB,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,oCAAoC,aAAa,CAAC,MAAM,IAAI,aAAa,CAAC,OAAO,SAAS,KAAK,EAAE,CACjG,CAAC;IACH,CAAC;AACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"piano-partials.d.ts","sourceRoot":"","sources":["../../src/acoustics/piano-partials.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"piano-partials.d.ts","sourceRoot":"","sources":["../../src/acoustics/piano-partials.ts"],"names":[],"mappings":"AAaA;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAOxD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,aAAa,GAAE,MAAkC,EACjD,GAAG,CAAC,EAAE,YAAY,GAChB,YAAY,CAsBd"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assertPianoPitch } from "./_assert-piano-pitch.js";
|
|
2
2
|
// The inharmonicity curve is two straight lines in log10(B) meeting at C3
|
|
3
3
|
// (pitch 48), where B is lowest: 1e-4 there, rising by 0.028 decades per
|
|
4
4
|
// semitone towards the treble (≈ 1.6e-2 at C8) and by 0.012 towards the bass
|
|
@@ -9,12 +9,6 @@ const INHARMONICITY_MINIMUM_PITCH_ = 48;
|
|
|
9
9
|
const INHARMONICITY_MINIMUM_LOG10_ = -4;
|
|
10
10
|
const INHARMONICITY_TREBLE_SLOPE_ = 0.028;
|
|
11
11
|
const INHARMONICITY_BASS_SLOPE_ = 0.012;
|
|
12
|
-
function assertPianoPitch_(pitch) {
|
|
13
|
-
if (!(pitch >= PIANO_PITCHES.lowest && pitch <= PIANO_PITCHES.highest) ||
|
|
14
|
-
!Number.isFinite(pitch)) {
|
|
15
|
-
throw new RangeError(`pitch must be within the piano's ${PIANO_PITCHES.lowest}–${PIANO_PITCHES.highest}, got ${pitch}`);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
12
|
/**
|
|
19
13
|
* The inharmonicity coefficient `B` of a typical piano string at `pitch`: its
|
|
20
14
|
* partial `h` sounds at `h · f0 · √(1 + B·h²)`. A generic curve, lowest about
|
|
@@ -26,7 +20,7 @@ function assertPianoPitch_(pitch) {
|
|
|
26
20
|
* @throws {RangeError} If `pitch` is outside 21–108.
|
|
27
21
|
*/
|
|
28
22
|
export function pianoInharmonicity(pitch) {
|
|
29
|
-
|
|
23
|
+
assertPianoPitch(pitch);
|
|
30
24
|
const distance = pitch - INHARMONICITY_MINIMUM_PITCH_;
|
|
31
25
|
const slope = distance >= 0 ? INHARMONICITY_TREBLE_SLOPE_ : -INHARMONICITY_BASS_SLOPE_;
|
|
32
26
|
return 10 ** (INHARMONICITY_MINIMUM_LOG10_ + distance * slope);
|
|
@@ -46,7 +40,7 @@ export function pianoInharmonicity(pitch) {
|
|
|
46
40
|
* non-finite `inharmonicity` or an `out` of the wrong length.
|
|
47
41
|
*/
|
|
48
42
|
export function pianoPartialPitches(pitch, count, inharmonicity = pianoInharmonicity(pitch), out) {
|
|
49
|
-
|
|
43
|
+
assertPianoPitch(pitch);
|
|
50
44
|
if (!Number.isSafeInteger(count) || count < 1) {
|
|
51
45
|
throw new RangeError(`count must be a positive integer, got ${count}`);
|
|
52
46
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"piano-partials.js","sourceRoot":"","sources":["../../src/acoustics/piano-partials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"piano-partials.js","sourceRoot":"","sources":["../../src/acoustics/piano-partials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAE5D,0EAA0E;AAC1E,yEAAyE;AACzE,6EAA6E;AAC7E,+EAA+E;AAC/E,iFAAiF;AACjF,8CAA8C;AAC9C,MAAM,4BAA4B,GAAG,EAAE,CAAC;AACxC,MAAM,4BAA4B,GAAG,CAAC,CAAC,CAAC;AACxC,MAAM,2BAA2B,GAAG,KAAK,CAA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E,EAAE,CAAC;QACjC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC;YACd,KAAK,GAAG,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,aAAa,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,6 @@ export * from "./calibrate/estimate-input-latency.js";
|
|
|
3
3
|
export * from "./verify/piano-chord-verifier.js";
|
|
4
4
|
export * from "./verify/tuning.js";
|
|
5
5
|
export * from "./verify/types.js";
|
|
6
|
+
export * from "./synthesize/synthesize-piano.js";
|
|
7
|
+
export * from "./synthesize/synthesize-tick.js";
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uCAAuC,CAAC;AACtD,cAAc,kCAAkC,CAAC;AACjD,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uCAAuC,CAAC;AACtD,cAAc,kCAAkC,CAAC;AACjD,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kCAAkC,CAAC;AACjD,cAAc,iCAAiC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,4 +3,6 @@ export * from "./calibrate/estimate-input-latency.js";
|
|
|
3
3
|
export * from "./verify/piano-chord-verifier.js";
|
|
4
4
|
export * from "./verify/tuning.js";
|
|
5
5
|
export * from "./verify/types.js";
|
|
6
|
+
export * from "./synthesize/synthesize-piano.js";
|
|
7
|
+
export * from "./synthesize/synthesize-tick.js";
|
|
6
8
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uCAAuC,CAAC;AACtD,cAAc,kCAAkC,CAAC;AACjD,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uCAAuC,CAAC;AACtD,cAAc,kCAAkC,CAAC;AACjD,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kCAAkC,CAAC;AACjD,cAAc,iCAAiC,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** One struck key of {@link synthesizePiano}. */
|
|
2
|
+
export interface PianoSynthesisNote {
|
|
3
|
+
/** MIDI pitch, 21–108; fractional values are allowed. */
|
|
4
|
+
readonly pitch: number;
|
|
5
|
+
/**
|
|
6
|
+
* When the key goes down, seconds from the start of the signal. A note struck
|
|
7
|
+
* before the start (a negative time) sounds from its state at the start.
|
|
8
|
+
*/
|
|
9
|
+
readonly time: number;
|
|
10
|
+
/**
|
|
11
|
+
* When the key comes up, seconds from the start, not before `time`; the note
|
|
12
|
+
* then dies away in about 50 ms. Default: never released.
|
|
13
|
+
*/
|
|
14
|
+
readonly release?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Amplitude of the fundamental, finite and non-negative. Default 0.2, a
|
|
17
|
+
* medium-loud note.
|
|
18
|
+
*/
|
|
19
|
+
readonly level?: number;
|
|
20
|
+
}
|
|
21
|
+
/** Options of {@link synthesizePiano}. */
|
|
22
|
+
export interface PianoSynthesisOptions {
|
|
23
|
+
/** Samples per second, finite and positive. */
|
|
24
|
+
readonly sampleRate: number;
|
|
25
|
+
/** Length of the signal, ms, finite and non-negative. */
|
|
26
|
+
readonly durationMs: number;
|
|
27
|
+
/** Frequency of A4, Hz, finite and positive. Default 440. */
|
|
28
|
+
readonly referenceHz?: number;
|
|
29
|
+
/**
|
|
30
|
+
* RMS amplitude of uniform white noise added to the signal, finite and
|
|
31
|
+
* non-negative. Default 0.
|
|
32
|
+
*/
|
|
33
|
+
readonly noise?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Seeds the noise (`@ac-kit/math-random`'s `mulberry32`), a 32-bit unsigned
|
|
36
|
+
* integer; other numbers are converted as by `>>> 0`. Default 1.
|
|
37
|
+
*/
|
|
38
|
+
readonly seed?: number;
|
|
39
|
+
}
|
|
40
|
+
/** Options of {@link chordNotes}. */
|
|
41
|
+
export interface ChordNotesOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Seconds between one key going down and the next, finite and non-negative.
|
|
44
|
+
* Default 0, all at once.
|
|
45
|
+
*/
|
|
46
|
+
readonly spread?: number;
|
|
47
|
+
/**
|
|
48
|
+
* The {@link PianoSynthesisNote.level} of every note. Default: the note's own
|
|
49
|
+
* default.
|
|
50
|
+
*/
|
|
51
|
+
readonly level?: number;
|
|
52
|
+
/** `"up"` strikes the lowest key first, `"down"` the highest. Default `"up"`. */
|
|
53
|
+
readonly direction?: "up" | "down";
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A piano-like signal: each note is the stretched partials of
|
|
57
|
+
* {@link pianoPartialPitches} (up to 20, those within 500 Hz of the Nyquist
|
|
58
|
+
* frequency left out), partial `h` of amplitude `level / h`, under a 3 ms
|
|
59
|
+
* attack and an exponential decay that is faster towards the treble and the
|
|
60
|
+
* upper partials. A released note dies away with a 15 ms time constant. The
|
|
61
|
+
* same notes and options always give the same samples.
|
|
62
|
+
*
|
|
63
|
+
* A synthetic model for tests, demonstrations and loopback tones, not a
|
|
64
|
+
* realistic piano: for rendered piano sound, use a SoundFont.
|
|
65
|
+
*
|
|
66
|
+
* Time O(length × partials × notes) in the worst case (a partial stops once it
|
|
67
|
+
* has decayed below 10⁻⁶), memory O(length) for the result and as much again
|
|
68
|
+
* for the binary64 sum it is rounded from.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const signal = synthesizePiano(chordNotes([60, 64, 67], 0.5), {
|
|
72
|
+
* sampleRate: 48_000,
|
|
73
|
+
* durationMs: 1500,
|
|
74
|
+
* noise: 1e-4,
|
|
75
|
+
* });
|
|
76
|
+
*
|
|
77
|
+
* @returns `Math.round(durationMs · sampleRate / 1000)` samples.
|
|
78
|
+
* @throws {RangeError} On an option or a note field outside its documented
|
|
79
|
+
* range, including a pitch outside 21–108.
|
|
80
|
+
*/
|
|
81
|
+
export declare function synthesizePiano(notes: Iterable<PianoSynthesisNote>, options: PianoSynthesisOptions): Float32Array<ArrayBuffer>;
|
|
82
|
+
/**
|
|
83
|
+
* The notes of a chord struck at `time` (seconds), for {@link synthesizePiano}:
|
|
84
|
+
* with a `spread`, each key goes down that many seconds after the one before
|
|
85
|
+
* it, from the lowest (`direction: "up"`) or the highest (`"down"`). Time O(n
|
|
86
|
+
* log n), memory O(n).
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* chordNotes([67, 60, 64], 1, { spread: 0.01 });
|
|
90
|
+
* // [{ pitch: 60, time: 1 }, { pitch: 64, time: 1.01 }, { pitch: 67, time: 1.02 }]
|
|
91
|
+
*
|
|
92
|
+
* @throws {RangeError} When a pitch is outside 21–108, `time` or the last
|
|
93
|
+
* note's time is not finite, or `spread` or `level` is not finite and
|
|
94
|
+
* non-negative: it never returns a note {@link synthesizePiano} rejects.
|
|
95
|
+
*/
|
|
96
|
+
export declare function chordNotes(pitches: Iterable<number>, time: number, options?: ChordNotesOptions): PianoSynthesisNote[];
|
|
97
|
+
//# sourceMappingURL=synthesize-piano.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1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AAM,CAAC,EACzB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,iBAAsB,GAC7B,kBAAkB,EAAE,CA2BtB"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { mulberry32 } from "@ac-kit/math-random";
|
|
2
|
+
import { assertPianoPitch } from "../acoustics/_assert-piano-pitch.js";
|
|
3
|
+
import { pianoPartialPitches } from "../acoustics/piano-partials.js";
|
|
4
|
+
const DEFAULT_NOTE_LEVEL_ = 0.2;
|
|
5
|
+
// The partial count, attack, decay curve, release and Nyquist margin are the
|
|
6
|
+
// keylight microphone spike's synthetic piano, set by hand to give the verifier
|
|
7
|
+
// a signal with a piano's stretched partials and treble-fast decay.
|
|
8
|
+
const MAX_PARTIALS_ = 20;
|
|
9
|
+
const ATTACK_SECONDS_ = 0.003;
|
|
10
|
+
const RELEASE_SECONDS_ = 0.015;
|
|
11
|
+
const NYQUIST_MARGIN_HZ_ = 500;
|
|
12
|
+
// A partial whose envelope has fallen this low is stopped there.
|
|
13
|
+
const SILENT_ENVELOPE_ = 1e-6;
|
|
14
|
+
function assertFinite_(name, value, minimum) {
|
|
15
|
+
if (!Number.isFinite(value) || value < minimum) {
|
|
16
|
+
throw new RangeError(`${name} must be finite and at least ${minimum}, got ${value}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A piano-like signal: each note is the stretched partials of
|
|
21
|
+
* {@link pianoPartialPitches} (up to 20, those within 500 Hz of the Nyquist
|
|
22
|
+
* frequency left out), partial `h` of amplitude `level / h`, under a 3 ms
|
|
23
|
+
* attack and an exponential decay that is faster towards the treble and the
|
|
24
|
+
* upper partials. A released note dies away with a 15 ms time constant. The
|
|
25
|
+
* same notes and options always give the same samples.
|
|
26
|
+
*
|
|
27
|
+
* A synthetic model for tests, demonstrations and loopback tones, not a
|
|
28
|
+
* realistic piano: for rendered piano sound, use a SoundFont.
|
|
29
|
+
*
|
|
30
|
+
* Time O(length × partials × notes) in the worst case (a partial stops once it
|
|
31
|
+
* has decayed below 10⁻⁶), memory O(length) for the result and as much again
|
|
32
|
+
* for the binary64 sum it is rounded from.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const signal = synthesizePiano(chordNotes([60, 64, 67], 0.5), {
|
|
36
|
+
* sampleRate: 48_000,
|
|
37
|
+
* durationMs: 1500,
|
|
38
|
+
* noise: 1e-4,
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* @returns `Math.round(durationMs · sampleRate / 1000)` samples.
|
|
42
|
+
* @throws {RangeError} On an option or a note field outside its documented
|
|
43
|
+
* range, including a pitch outside 21–108.
|
|
44
|
+
*/
|
|
45
|
+
export function synthesizePiano(notes, options) {
|
|
46
|
+
const { sampleRate, durationMs, referenceHz = 440, noise = 0 } = options;
|
|
47
|
+
const seed = options.seed ?? 1;
|
|
48
|
+
assertFinite_("sampleRate", sampleRate, Number.MIN_VALUE);
|
|
49
|
+
assertFinite_("durationMs", durationMs, 0);
|
|
50
|
+
assertFinite_("referenceHz", referenceHz, Number.MIN_VALUE);
|
|
51
|
+
assertFinite_("noise", noise, 0);
|
|
52
|
+
const length = Math.round((durationMs * sampleRate) / 1000);
|
|
53
|
+
const signal = new Float64Array(length);
|
|
54
|
+
const partials = new Float64Array(MAX_PARTIALS_);
|
|
55
|
+
for (const note of notes) {
|
|
56
|
+
const level = note.level ?? DEFAULT_NOTE_LEVEL_;
|
|
57
|
+
assertFinite_("note time", note.time, -Infinity);
|
|
58
|
+
assertFinite_("note level", level, 0);
|
|
59
|
+
if (note.release !== undefined) {
|
|
60
|
+
assertFinite_("note release", note.release, note.time);
|
|
61
|
+
}
|
|
62
|
+
pianoPartialPitches(note.pitch, MAX_PARTIALS_, undefined, partials);
|
|
63
|
+
const start = Math.round(note.time * sampleRate);
|
|
64
|
+
const release = note.release === undefined
|
|
65
|
+
? Infinity
|
|
66
|
+
: Math.round(note.release * sampleRate);
|
|
67
|
+
const baseDecay = 0.4 + 2.5 * 2 ** (-(note.pitch - 21) / 24);
|
|
68
|
+
for (let partial = 1; partial <= MAX_PARTIALS_; partial++) {
|
|
69
|
+
const frequency = referenceHz * 2 ** ((partials[partial - 1] - 69) / 12);
|
|
70
|
+
if (frequency >= sampleRate / 2 - NYQUIST_MARGIN_HZ_) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
const amplitude = level / partial;
|
|
74
|
+
const decay = baseDecay / (1 + 0.15 * (partial - 1));
|
|
75
|
+
const omega = (2 * Math.PI * frequency) / sampleRate;
|
|
76
|
+
const phase = partial * 0.7;
|
|
77
|
+
for (let frame = Math.max(0, start); frame < length; frame++) {
|
|
78
|
+
const age = (frame - start) / sampleRate;
|
|
79
|
+
let envelope = Math.min(1, age / ATTACK_SECONDS_) * Math.exp(-age / decay);
|
|
80
|
+
if (frame >= release) {
|
|
81
|
+
envelope *= Math.exp(-(frame - release) / sampleRate / RELEASE_SECONDS_);
|
|
82
|
+
}
|
|
83
|
+
if (envelope < SILENT_ENVELOPE_ && age > 0.01) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
signal[frame] +=
|
|
87
|
+
amplitude * envelope * Math.sin(omega * (frame - start) + phase);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (noise > 0) {
|
|
92
|
+
const random = mulberry32(seed);
|
|
93
|
+
// Uniform noise of the RMS asked for: its variance is a third of its range's.
|
|
94
|
+
const scale = noise * Math.sqrt(3);
|
|
95
|
+
for (let frame = 0; frame < length; frame++) {
|
|
96
|
+
signal[frame] += (random() * 2 - 1) * scale;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return Float32Array.from(signal);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The notes of a chord struck at `time` (seconds), for {@link synthesizePiano}:
|
|
103
|
+
* with a `spread`, each key goes down that many seconds after the one before
|
|
104
|
+
* it, from the lowest (`direction: "up"`) or the highest (`"down"`). Time O(n
|
|
105
|
+
* log n), memory O(n).
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* chordNotes([67, 60, 64], 1, { spread: 0.01 });
|
|
109
|
+
* // [{ pitch: 60, time: 1 }, { pitch: 64, time: 1.01 }, { pitch: 67, time: 1.02 }]
|
|
110
|
+
*
|
|
111
|
+
* @throws {RangeError} When a pitch is outside 21–108, `time` or the last
|
|
112
|
+
* note's time is not finite, or `spread` or `level` is not finite and
|
|
113
|
+
* non-negative: it never returns a note {@link synthesizePiano} rejects.
|
|
114
|
+
*/
|
|
115
|
+
export function chordNotes(pitches, time, options = {}) {
|
|
116
|
+
const { spread = 0, level, direction = "up" } = options;
|
|
117
|
+
assertFinite_("time", time, -Infinity);
|
|
118
|
+
assertFinite_("spread", spread, 0);
|
|
119
|
+
if (level !== undefined) {
|
|
120
|
+
assertFinite_("level", level, 0);
|
|
121
|
+
}
|
|
122
|
+
const ordered = Array.from(pitches);
|
|
123
|
+
for (const pitch of ordered) {
|
|
124
|
+
assertPianoPitch(pitch);
|
|
125
|
+
}
|
|
126
|
+
ordered.sort((left, right) => direction === "down" ? right - left : left - right);
|
|
127
|
+
assertFinite_("last note time", time + (ordered.length - 1) * spread, -Infinity);
|
|
128
|
+
return ordered.map((pitch, index) => level === undefined
|
|
129
|
+
? { pitch, time: time + index * spread }
|
|
130
|
+
: { pitch, time: time + index * spread, level });
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=synthesize-piano.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"synthesize-piano.js","sourceRoot":"","sources":["../../src/synthesize/synthesize-piano.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AA2DrE,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,6EAA6E;A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CAAC,QAAQ,CACT,CAAC;IAEF,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CACnC,KAAK,KAAK,SAAS;QAClB,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,GAAG,KAAK,GAAG,MAAM,EAAE;QACxC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,GAAG,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,CAChD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Options of {@link synthesizeTick}. */
|
|
2
|
+
export interface TickSynthesisOptions {
|
|
3
|
+
/** Samples per second, finite and positive. */
|
|
4
|
+
readonly sampleRate: number;
|
|
5
|
+
/** The band the burst's energy is in, Hz: `0 ≤ low ≤ high ≤ sampleRate / 2`. */
|
|
6
|
+
readonly band: readonly [low: number, high: number];
|
|
7
|
+
/** Length of the burst, ms, finite and non-negative. */
|
|
8
|
+
readonly durationMs: number;
|
|
9
|
+
/** Peak amplitude, finite and non-negative. Default 0.5. */
|
|
10
|
+
readonly level?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Seeds the burst (`@ac-kit/math-random`'s `mulberry32`), a 32-bit unsigned
|
|
13
|
+
* integer; other numbers are converted as by `>>> 0`. Default 1.
|
|
14
|
+
*/
|
|
15
|
+
readonly seed?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A short burst of noise confined to a band, as a click to count by, such as a
|
|
19
|
+
* metronome tick kept out of the bands a verifier listens to: 80 sinusoids of
|
|
20
|
+
* random frequency within the band and random phase, summed under a midpoint
|
|
21
|
+
* Hann envelope (`@ac-kit/math-signal`'s `applyHannWindow` with `midpoint`) and
|
|
22
|
+
* scaled so its largest sample is `level`. The same options always give the
|
|
23
|
+
* same samples.
|
|
24
|
+
*
|
|
25
|
+
* Time O(length), memory O(length): the result and, while it is made, one
|
|
26
|
+
* binary64 buffer of the same length.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const tick = synthesizeTick({
|
|
30
|
+
* sampleRate: 48_000,
|
|
31
|
+
* band: [12_000, 16_000],
|
|
32
|
+
* durationMs: 15,
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* @returns `Math.round(durationMs · sampleRate / 1000)` samples.
|
|
36
|
+
* @throws {RangeError} On an option outside its documented range.
|
|
37
|
+
*/
|
|
38
|
+
export declare function synthesizeTick(options: TickSynthesisOptions): Float32Array<ArrayBuffer>;
|
|
39
|
+
//# sourceMappingURL=synthesize-tick.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"synthesize-tick.d.ts","sourceRoot":"","sources":["../../src/synthesize/synthesize-tick.ts"],"names":[],"mappings":"AAGA,yCAAyC;AACzC,MAAM,WAAW,oBAAoB;IACpC,+CAA+C;IAC/C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,gFAAgF;IAChF,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IACpD,wDAAwD;IACxD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,4DAA4D;IAC5D,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACvB;AAMD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,cAAc,CAC7B,OAAO,EAAE,oBAAoB,GAC3B,YAAY,CAAC,WAAW,CAAC,CAmD3B"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { mulberry32 } from "@ac-kit/math-random";
|
|
2
|
+
import { applyHannWindow } from "@ac-kit/math-signal";
|
|
3
|
+
// Enough random-phase sinusoids for the burst to sound like noise rather than a
|
|
4
|
+
// chord: set by hand in keylight.
|
|
5
|
+
const COMPONENT_COUNT_ = 80;
|
|
6
|
+
/**
|
|
7
|
+
* A short burst of noise confined to a band, as a click to count by, such as a
|
|
8
|
+
* metronome tick kept out of the bands a verifier listens to: 80 sinusoids of
|
|
9
|
+
* random frequency within the band and random phase, summed under a midpoint
|
|
10
|
+
* Hann envelope (`@ac-kit/math-signal`'s `applyHannWindow` with `midpoint`) and
|
|
11
|
+
* scaled so its largest sample is `level`. The same options always give the
|
|
12
|
+
* same samples.
|
|
13
|
+
*
|
|
14
|
+
* Time O(length), memory O(length): the result and, while it is made, one
|
|
15
|
+
* binary64 buffer of the same length.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const tick = synthesizeTick({
|
|
19
|
+
* sampleRate: 48_000,
|
|
20
|
+
* band: [12_000, 16_000],
|
|
21
|
+
* durationMs: 15,
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* @returns `Math.round(durationMs · sampleRate / 1000)` samples.
|
|
25
|
+
* @throws {RangeError} On an option outside its documented range.
|
|
26
|
+
*/
|
|
27
|
+
export function synthesizeTick(options) {
|
|
28
|
+
const { sampleRate, band, durationMs, level = 0.5 } = options;
|
|
29
|
+
const [low, high] = band;
|
|
30
|
+
if (!Number.isFinite(sampleRate) || !(sampleRate > 0)) {
|
|
31
|
+
throw new RangeError(`sampleRate must be finite and positive, got ${sampleRate}`);
|
|
32
|
+
}
|
|
33
|
+
if (!(low >= 0 && low <= high && high <= sampleRate / 2)) {
|
|
34
|
+
throw new RangeError(`band must satisfy 0 ≤ low ≤ high ≤ ${sampleRate / 2}, got [${low}, ${high}]`);
|
|
35
|
+
}
|
|
36
|
+
if (!Number.isFinite(durationMs) || durationMs < 0) {
|
|
37
|
+
throw new RangeError(`durationMs must be finite and non-negative, got ${durationMs}`);
|
|
38
|
+
}
|
|
39
|
+
if (!Number.isFinite(level) || level < 0) {
|
|
40
|
+
throw new RangeError(`level must be finite and non-negative, got ${level}`);
|
|
41
|
+
}
|
|
42
|
+
const length = Math.round((durationMs * sampleRate) / 1000);
|
|
43
|
+
if (length === 0) {
|
|
44
|
+
return new Float32Array(0);
|
|
45
|
+
}
|
|
46
|
+
const random = mulberry32(options.seed ?? 1);
|
|
47
|
+
const burst = new Float64Array(length);
|
|
48
|
+
for (let component = 0; component < COMPONENT_COUNT_; component++) {
|
|
49
|
+
const frequency = low + random() * (high - low);
|
|
50
|
+
const phase = random() * 2 * Math.PI;
|
|
51
|
+
const omega = (2 * Math.PI * frequency) / sampleRate;
|
|
52
|
+
for (let frame = 0; frame < length; frame++) {
|
|
53
|
+
burst[frame] += Math.sin(omega * frame + phase);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
applyHannWindow(burst, { midpoint: true });
|
|
57
|
+
let peak = 0;
|
|
58
|
+
for (let frame = 0; frame < length; frame++) {
|
|
59
|
+
peak = Math.max(peak, Math.abs(burst[frame]));
|
|
60
|
+
}
|
|
61
|
+
const scale = peak > 0 ? level / peak : 0;
|
|
62
|
+
return Float32Array.from(burst, (value) => value * scale);
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=synthesize-tick.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"synthesize-tick.js","sourceRoot":"","sources":["../../src/synthesize/synthesize-tick.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAmBtD,gFAAgF;AAChF,kCAAkC;AAClC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,cAAc,CAC7B,OAA6B;IAE7B,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IAC9D,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,IAAI,CAAC;IAEzB,IAAI,CAAC,MAAM,CAAC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G,CAAC,CAAC;QAChD,MAAM,KAAK,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC;QACrC,MAAM,KAAK,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,GAAG,SAAS,CAAC,GAAG,UAAU,CAAC;QAErD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;YAC7C,KAAK,CAAC,KAAK,CAAE,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC,CAAC;QAClD,CAAC;IACF,CAAC;IAED,eAAe,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAC3C,IAAI,IAAI,GAAG,CAAC,CAAC;IAEb,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,CAAC,CAAC;IAChD,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAE1C,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;AAC3D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/media-piano-audio",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Piano audio analysis: score-informed chord verification from a microphone, streaming and allocation-free, for an AudioWorklet.",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Piano audio analysis: score-informed chord verification from a microphone, streaming and allocation-free, for an AudioWorklet; a synthetic piano and tick for tests and loopback tones.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"audio",
|
|
7
7
|
"audioworklet",
|
|
8
8
|
"chord",
|
|
9
9
|
"microphone",
|
|
10
10
|
"onset detection",
|
|
11
|
-
"piano"
|
|
11
|
+
"piano",
|
|
12
|
+
"synthesis"
|
|
12
13
|
],
|
|
13
14
|
"homepage": "https://anthochamp.pages.grenadine.io/node-essentials/api/media-piano-audio/",
|
|
14
15
|
"bugs": {
|
|
@@ -47,16 +48,16 @@
|
|
|
47
48
|
"prepack": "yarn build"
|
|
48
49
|
},
|
|
49
50
|
"dependencies": {
|
|
50
|
-
"@ac-kit/core": "^0.
|
|
51
|
-
"@ac-kit/math-linear": "^0.2.
|
|
52
|
-
"@ac-kit/math-
|
|
53
|
-
"@ac-kit/math-
|
|
54
|
-
"@ac-kit/
|
|
51
|
+
"@ac-kit/core": "^0.11.0",
|
|
52
|
+
"@ac-kit/math-linear": "^0.2.1",
|
|
53
|
+
"@ac-kit/math-random": "^0.3.0",
|
|
54
|
+
"@ac-kit/math-signal": "^0.3.0",
|
|
55
|
+
"@ac-kit/math-stats": "^0.4.0",
|
|
56
|
+
"@ac-kit/model-music": "^0.8.1",
|
|
55
57
|
"@types/web": "^0.0.367"
|
|
56
58
|
},
|
|
57
59
|
"devDependencies": {
|
|
58
60
|
"@ac-kit/fixture-piano-audio": "0.1.0",
|
|
59
|
-
"@ac-kit/math-random": "0.2.0",
|
|
60
61
|
"@ac-kit/tsconfig": "0.1.0",
|
|
61
62
|
"@ac-kit/typedoc-config": "0.1.0",
|
|
62
63
|
"@ac-kit/vitest-config": "0.1.1",
|