@audio/synth-poly 1.0.0 → 1.1.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/audio.d.ts +35 -0
- package/audio.js +44 -0
- package/package.json +12 -2
package/audio.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Generated from the audio.js manifest (params metadata is the source of truth).
|
|
2
|
+
// Regenerate: node tools/dts.js in @audio/compile. Do not edit by hand.
|
|
3
|
+
|
|
4
|
+
/** Automatable number — scalar, `t => value` fn, or breakpoint curve {t, v} */
|
|
5
|
+
type Auto = number | ((t: number) => number) | { t: number[], v: number[] }
|
|
6
|
+
/** Per-block param values as delivered by hosts (numbers arrive as 1-length Float32Array) */
|
|
7
|
+
type Live = Record<string, Float32Array | string | boolean>
|
|
8
|
+
type Ctx = { sampleRate: number, maxBlockSize: number, maxChannels: number, currentTime: number, duration?: number, events?: readonly any[], emit?: (name: string, ...args: any[]) => void, [k: string]: unknown }
|
|
9
|
+
type Process = (inputs: Float32Array[][], outputs: Float32Array[][], params: Live) => void
|
|
10
|
+
|
|
11
|
+
/** Chainable-host options for 'poly' */
|
|
12
|
+
export interface PolyOptions {
|
|
13
|
+
/** 1..32 (default 16) */
|
|
14
|
+
"voices"?: Auto
|
|
15
|
+
/** default "sawtooth" */
|
|
16
|
+
"type"?: "sine" | "square" | "sawtooth" | "triangle"
|
|
17
|
+
/** 0..1 (default 0.7) */
|
|
18
|
+
"amp"?: Auto
|
|
19
|
+
at?: number | string
|
|
20
|
+
duration?: number | string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export declare const poly: {
|
|
24
|
+
(ctx: Ctx): Process
|
|
25
|
+
channels: {"inputs":[],"outputs":"any"}
|
|
26
|
+
streaming: false
|
|
27
|
+
params: {
|
|
28
|
+
/** 1..32 (default 16) */
|
|
29
|
+
"voices": { type: "number", default: 16 }
|
|
30
|
+
/** default "sawtooth" */
|
|
31
|
+
"type": { type: "enum", values: ["sine","square","sawtooth","triangle"], default: "sawtooth" }
|
|
32
|
+
/** 0..1 (default 0.7) */
|
|
33
|
+
"amp": { type: "number", default: 0.7 }
|
|
34
|
+
}
|
|
35
|
+
}
|
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.1",
|
|
4
4
|
"description": "Polyphonic voice allocator — wrap any voice with N-voice polyphony",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -17,12 +17,22 @@
|
|
|
17
17
|
"main": "poly.js",
|
|
18
18
|
"exports": {
|
|
19
19
|
".": "./poly.js",
|
|
20
|
+
"./audio": {
|
|
21
|
+
"types": "./audio.d.ts",
|
|
22
|
+
"default": "./audio.js"
|
|
23
|
+
},
|
|
20
24
|
"./package.json": "./package.json"
|
|
21
25
|
},
|
|
22
26
|
"files": [
|
|
23
|
-
"poly.js"
|
|
27
|
+
"poly.js",
|
|
28
|
+
"audio.js",
|
|
29
|
+
"audio.d.ts"
|
|
24
30
|
],
|
|
25
31
|
"publishConfig": {
|
|
26
32
|
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"audio": "./audio.js",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@audio/synth-voice": "^1.1.0"
|
|
27
37
|
}
|
|
28
38
|
}
|