@audio/synth-poly 1.1.1 → 1.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/package.json +3 -2
- package/poly.js +12 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/synth-poly",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Polyphonic voice allocator — wrap any voice with N-voice polyphony",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"audio": "./audio.js",
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@audio/synth-voice": "^1.1.0"
|
|
36
|
+
"@audio/synth-voice": "^1.1.0",
|
|
37
|
+
"@audio/note": "^1.0.0"
|
|
37
38
|
}
|
|
38
39
|
}
|
package/poly.js
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
|
+
import { midiToHz } from '@audio/note'
|
|
2
|
+
|
|
1
3
|
// Polyphonic voice allocator — renders note events through any voice function with
|
|
2
|
-
// N-voice polyphony, oldest-note stealing, and per-note velocity. The voice is
|
|
3
|
-
//
|
|
4
|
-
// generator (osc, pluck,
|
|
4
|
+
// N-voice polyphony, oldest-note stealing, and per-note velocity. The voice contract is
|
|
5
|
+
// the family's generator shape: (freq, {fs, duration, velocity, ...voiceOpts}) →
|
|
6
|
+
// Float32Array — every pitched @audio/synth generator satisfies it (osc, pluck, fm,
|
|
7
|
+
// modal, drum's membrane/metal, bell, epiano, risset, wavetable…). Extra per-voice
|
|
8
|
+
// config (fm's ratio/index, modal's model, wavetable's tables…) rides in via voiceOpts.
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* @param {Array} notes — [{ time, freq | midi, duration, velocity=1 }]
|
|
8
12
|
* @param {object} opts — {
|
|
9
|
-
* voice: (freq, { fs, duration, velocity }) => Float32Array — required,
|
|
13
|
+
* voice: (freq, { fs, duration, velocity, ...voiceOpts }) => Float32Array — required,
|
|
14
|
+
* voiceOpts: {} — extra options forwarded to every voice call (e.g. wavetable's { tables }),
|
|
10
15
|
* voices: 16 — max simultaneous notes (oldest is stolen with a 5 ms fade),
|
|
11
16
|
* duration — total seconds (default: end of last note + 0.5),
|
|
12
17
|
* fs: 44100
|
|
13
18
|
* }
|
|
14
19
|
* @returns {Float32Array} mono mix
|
|
15
20
|
*/
|
|
16
|
-
export default function poly (notes, { voice, voices = 16, duration, fs = 44100 } = {}) {
|
|
21
|
+
export default function poly (notes, { voice, voices = 16, duration, fs = 44100, voiceOpts = {} } = {}) {
|
|
17
22
|
if (typeof voice !== 'function') throw new TypeError('poly: opts.voice function is required')
|
|
18
23
|
let evs = [...notes].map(n => ({
|
|
19
24
|
time: n.time ?? 0,
|
|
20
|
-
freq: n.freq ??
|
|
25
|
+
freq: n.freq ?? midiToHz(n.midi ?? 69),
|
|
21
26
|
duration: n.duration ?? 0.5,
|
|
22
27
|
velocity: n.velocity ?? 1,
|
|
23
28
|
})).sort((a, b) => a.time - b.time)
|
|
@@ -25,7 +30,7 @@ export default function poly (notes, { voice, voices = 16, duration, fs = 44100
|
|
|
25
30
|
// render every note, then simulate allocation to decide steals (cut = absolute sample
|
|
26
31
|
// where the note is silenced; stolen notes get a short fade there instead of a click)
|
|
27
32
|
let rendered = evs.map(e => {
|
|
28
|
-
let d = voice(e.freq, { fs, duration: e.duration, velocity: e.velocity })
|
|
33
|
+
let d = voice(e.freq, { ...voiceOpts, fs, duration: e.duration, velocity: e.velocity })
|
|
29
34
|
let at = Math.round(e.time * fs)
|
|
30
35
|
return { at, data: d, cut: at + d.length, velocity: e.velocity }
|
|
31
36
|
})
|