@audio/synth-poly 1.1.1 → 1.2.1
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/README.md +33 -3
- package/package.json +12 -3
- package/poly.d.ts +26 -0
- package/poly.js +12 -7
package/README.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
|
-
# @audio/synth-poly
|
|
1
|
+
# @audio/synth-poly [](https://www.npmjs.com/package/@audio/synth-poly) [](https://github.com/krishnized/license)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
N-voice allocator — oldest-note steal with 5 ms fade, {freq|midi} notes, any pitched generator as voice
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```
|
|
6
|
+
npm install @audio/synth-poly
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import poly from '@audio/synth-poly'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Polyphonic voice allocator: renders a list of note events through any voice function with N-voice polyphony and oldest-note stealing (5 ms fade at the steal point, no click). The voice contract is the family's generator shape — `(freq, {fs, duration, velocity, ...voiceOpts}) => Float32Array` — every pitched `@audio/synth` generator satisfies it (`osc`, `pluck`, `fm`, `modal`, `drum`'s `membrane`/`metal`, `bell`, `epiano`, `risset`, `wavetable`…). Extra per-voice config (fm's `ratio`/`index`, modal's `model`, wavetable's `tables`) rides in via `voiceOpts`.
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import osc from '@audio/synth-osc'
|
|
17
|
+
poly([{ time: 0, freq: 440, duration: 0.5 }, { time: 0.2, midi: 64 }], { voice: osc, voices: 8 })
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
| Param | Default | |
|
|
21
|
+
|---|---|---|
|
|
22
|
+
| `notes` | — | `[{time, freq\|midi, duration=0.5, velocity=1}]` (positional) |
|
|
23
|
+
| `voice` | — | Required — `(freq, opts) => Float32Array` voice function |
|
|
24
|
+
| `voiceOpts` | `{}` | Extra options forwarded to every voice call |
|
|
25
|
+
| `voices` | `16` | Max simultaneous notes — oldest is stolen with a 5ms fade |
|
|
26
|
+
| `duration` | — | Total seconds; default: end of last note + 0.5 |
|
|
27
|
+
| `fs` | `44100` | Sample rate |
|
|
28
|
+
|
|
29
|
+
**Use when:** hosting any pitched generator polyphonically, or rendering MIDI: `poly(parse(smf).notes, { voice: pluck })` — see [`@audio/midi`](https://github.com/audiojs/midi).
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
Part of [@audio/synth](https://github.com/audiojs/synth) — the synth family umbrella.
|
|
34
|
+
|
|
35
|
+
MIT © [audiojs](https://github.com/audiojs)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/synth-poly",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Polyphonic voice allocator — wrap any voice with N-voice polyphony",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -25,14 +25,23 @@
|
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
27
|
"poly.js",
|
|
28
|
+
"poly.d.ts",
|
|
28
29
|
"audio.js",
|
|
29
30
|
"audio.d.ts"
|
|
30
31
|
],
|
|
32
|
+
"types": "poly.d.ts",
|
|
31
33
|
"publishConfig": {
|
|
32
34
|
"access": "public"
|
|
33
35
|
},
|
|
34
36
|
"audio": "./audio.js",
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@audio/synth-voice": "^1.1.0"
|
|
37
|
-
|
|
38
|
+
"@audio/synth-voice": "^1.1.0",
|
|
39
|
+
"@audio/note": "^1.0.0"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/audiojs/synth.git",
|
|
44
|
+
"directory": "packages/synth-poly"
|
|
45
|
+
},
|
|
46
|
+
"funding": "https://github.com/sponsors/audiojs"
|
|
38
47
|
}
|
package/poly.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** N-voice polyphonic allocator — oldest-note steal with 5ms fade, any pitched generator as voice. */
|
|
2
|
+
export interface PolyNote {
|
|
3
|
+
time?: number
|
|
4
|
+
freq?: number
|
|
5
|
+
midi?: number
|
|
6
|
+
duration?: number
|
|
7
|
+
velocity?: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type Voice = (freq: number, opts: { fs: number, duration: number, velocity: number, [key: string]: unknown }) => Float32Array
|
|
11
|
+
|
|
12
|
+
export interface PolyOptions {
|
|
13
|
+
/** required — voice function, family generator contract (freq, opts) => Float32Array */
|
|
14
|
+
voice: Voice
|
|
15
|
+
/** extra options forwarded to every voice call, default {} */
|
|
16
|
+
voiceOpts?: Record<string, unknown>
|
|
17
|
+
/** max simultaneous notes — oldest is stolen with a 5ms fade, default 16 */
|
|
18
|
+
voices?: number
|
|
19
|
+
/** total seconds; default: end of last note + 0.5 */
|
|
20
|
+
duration?: number
|
|
21
|
+
/** sample rate, default 44100 */
|
|
22
|
+
fs?: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Returns the mono mix. */
|
|
26
|
+
export default function poly(notes: PolyNote[], options: PolyOptions): Float32Array
|
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
|
})
|