@audio/synth-poly 1.0.0 → 1.1.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/audio.js +44 -0
- package/package.json +8 -2
package/audio.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// audio.js manifest — polyphonic note renderer (N-voice allocation, oldest-note
|
|
2
|
+
// stealing) driven by contract note events, voiced by @audio/synth-voice. Generator +
|
|
3
|
+
// whole-render: the host compiles a `notes` option into §events slots; the manifest
|
|
4
|
+
// reconstructs the kernel's note list from on/off pairs and lets the kernel allocate.
|
|
5
|
+
// Custom voice functions (pluck, wavetable, …) take the kernel via direct import.
|
|
6
|
+
|
|
7
|
+
import polyFn from './poly.js'
|
|
8
|
+
import voiceFn from '@audio/synth-voice'
|
|
9
|
+
|
|
10
|
+
export const poly = (ctx) => {
|
|
11
|
+
return (inputs, outputs, params) => {
|
|
12
|
+
const out = outputs[0]
|
|
13
|
+
if (!out || !out.length) return
|
|
14
|
+
const n = out[0].length, sr = ctx.sampleRate
|
|
15
|
+
const evs = ctx.events || []
|
|
16
|
+
const on = new Map(), notes = []
|
|
17
|
+
for (let i = 0; i < evs.length; i++) {
|
|
18
|
+
const e = evs[i]
|
|
19
|
+
if (e.kind === 'on') on.set(e.id, e)
|
|
20
|
+
else if (e.kind === 'off' && on.has(e.id)) {
|
|
21
|
+
const s = on.get(e.id)
|
|
22
|
+
notes.push({ time: s.time / sr, midi: s.pitch, duration: (e.time - s.time) / sr, velocity: s.velocity ?? 1 })
|
|
23
|
+
on.delete(e.id)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
for (const s of on.values()) notes.push({ time: s.time / sr, midi: s.pitch, duration: (n - s.time) / sr, velocity: s.velocity ?? 1 })
|
|
27
|
+
const type = params.type, amp = params.amp[0]
|
|
28
|
+
const d = polyFn(notes, {
|
|
29
|
+
fs: sr, duration: n / sr,
|
|
30
|
+
voices: params.voices[0] | 0,
|
|
31
|
+
voice: (freq, o) => voiceFn(freq, { fs: o.fs, duration: o.duration, type, amp: amp * (o.velocity ?? 1) }),
|
|
32
|
+
})
|
|
33
|
+
const m = Math.min(n, d.length)
|
|
34
|
+
for (let c = 0; c < out.length; c++) { out[c].set(d.subarray(0, m)); out[c].fill(0, m) }
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
poly.channels = { inputs: [], outputs: 'any' }
|
|
38
|
+
poly.streaming = false
|
|
39
|
+
poly.events = { in: ['note'] }
|
|
40
|
+
poly.params = {
|
|
41
|
+
voices: { type: 'number', min: 1, max: 32, default: 16, step: 1 },
|
|
42
|
+
type: { type: 'enum', values: ['sine', 'square', 'sawtooth', 'triangle'], default: 'sawtooth' },
|
|
43
|
+
amp: { type: 'number', min: 0, max: 1, default: 0.7 },
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/synth-poly",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Polyphonic voice allocator — wrap any voice with N-voice polyphony",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -17,12 +17,18 @@
|
|
|
17
17
|
"main": "poly.js",
|
|
18
18
|
"exports": {
|
|
19
19
|
".": "./poly.js",
|
|
20
|
+
"./audio": "./audio.js",
|
|
20
21
|
"./package.json": "./package.json"
|
|
21
22
|
},
|
|
22
23
|
"files": [
|
|
23
|
-
"poly.js"
|
|
24
|
+
"poly.js",
|
|
25
|
+
"audio.js"
|
|
24
26
|
],
|
|
25
27
|
"publishConfig": {
|
|
26
28
|
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"audio": "./audio.js",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@audio/synth-voice": "^1.1.0"
|
|
27
33
|
}
|
|
28
34
|
}
|