@audio/synth-voice 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.
Files changed (3) hide show
  1. package/audio.d.ts +55 -0
  2. package/audio.js +54 -0
  3. package/package.json +10 -3
package/audio.d.ts ADDED
@@ -0,0 +1,55 @@
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 'voice' */
12
+ export interface VoiceOptions {
13
+ /** default "sawtooth" */
14
+ "type"?: "sine" | "square" | "sawtooth" | "triangle"
15
+ /** 0.001..2 s (default 0.01) */
16
+ "attack"?: Auto
17
+ /** 0.001..2 s (default 0.15) */
18
+ "decay"?: Auto
19
+ /** 0..1 (default 0.6) */
20
+ "sustain"?: Auto
21
+ /** 0.001..4 s (default 0.25) */
22
+ "release"?: Auto
23
+ /** 100..16000 Hz (default 3000) */
24
+ "cutoff"?: Auto
25
+ /** 0..1 (default 0.6) */
26
+ "envAmount"?: Auto
27
+ /** 0..1 (default 0.7) */
28
+ "amp"?: Auto
29
+ at?: number | string
30
+ duration?: number | string
31
+ }
32
+
33
+ export declare const voice: {
34
+ (ctx: Ctx): Process
35
+ channels: {"inputs":[],"outputs":"any"}
36
+ streaming: false
37
+ params: {
38
+ /** default "sawtooth" */
39
+ "type": { type: "enum", values: ["sine","square","sawtooth","triangle"], default: "sawtooth" }
40
+ /** 0.001..2 s (default 0.01) */
41
+ "attack": { type: "number", default: 0.01 }
42
+ /** 0.001..2 s (default 0.15) */
43
+ "decay": { type: "number", default: 0.15 }
44
+ /** 0..1 (default 0.6) */
45
+ "sustain": { type: "number", default: 0.6 }
46
+ /** 0.001..4 s (default 0.25) */
47
+ "release": { type: "number", default: 0.25 }
48
+ /** 100..16000 Hz (default 3000) */
49
+ "cutoff": { type: "number", default: 3000 }
50
+ /** 0..1 (default 0.6) */
51
+ "envAmount": { type: "number", default: 0.6 }
52
+ /** 0..1 (default 0.7) */
53
+ "amp": { type: "number", default: 0.7 }
54
+ }
55
+ }
package/audio.js ADDED
@@ -0,0 +1,54 @@
1
+ // audio.js manifest — monophonic synth voice (osc × ADSR through env-scaled lowpass,
2
+ // Tone.js Synth/MonoSynth class) driven by contract note events. Generator +
3
+ // whole-render: the host compiles a `notes` option into §events slots and calls
4
+ // process once over the materialized timeline. Notes are rendered independently and
5
+ // overlap-added (a mono line rarely overlaps; no voice stealing in v1 — use
6
+ // @audio/synth-poly for allocation).
7
+
8
+ import voiceFn from './voice.js'
9
+
10
+ export const voice = (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 o0 = out[0]
16
+ o0.fill(0)
17
+ // reconstruct notes from on/off slot pairs (host pairs them by id)
18
+ const evs = ctx.events || []
19
+ const on = new Map()
20
+ const render = (slot, endSample) => {
21
+ const freq = 440 * 2 ** ((slot.pitch - 69) / 12)
22
+ const dur = Math.max(0.01, (endSample - slot.time) / sr)
23
+ const d = voiceFn(freq, {
24
+ fs: sr, duration: dur,
25
+ type: params.type,
26
+ attack: params.attack[0], decay: params.decay[0],
27
+ sustain: params.sustain[0], release: params.release[0],
28
+ cutoff: params.cutoff[0], envAmount: params.envAmount[0],
29
+ amp: params.amp[0] * (slot.velocity ?? 1),
30
+ })
31
+ for (let i = 0; i < d.length && slot.time + i < n; i++) o0[slot.time + i] += d[i]
32
+ }
33
+ for (let i = 0; i < evs.length; i++) {
34
+ const e = evs[i]
35
+ if (e.kind === 'on') on.set(e.id, { time: e.time, pitch: e.pitch, velocity: e.velocity })
36
+ else if (e.kind === 'off' && on.has(e.id)) { render(on.get(e.id), e.time); on.delete(e.id) }
37
+ }
38
+ for (const slot of on.values()) render(slot, n) // unterminated notes ring to the end
39
+ for (let c = 1; c < out.length; c++) out[c].set(o0)
40
+ }
41
+ }
42
+ voice.channels = { inputs: [], outputs: 'any' }
43
+ voice.streaming = false
44
+ voice.events = { in: ['note'] }
45
+ voice.params = {
46
+ type: { type: 'enum', values: ['sine', 'square', 'sawtooth', 'triangle'], default: 'sawtooth' },
47
+ attack: { type: 'number', min: 0.001, max: 2, default: 0.01, unit: 's', curve: 'log' },
48
+ decay: { type: 'number', min: 0.001, max: 2, default: 0.15, unit: 's', curve: 'log' },
49
+ sustain: { type: 'number', min: 0, max: 1, default: 0.6 },
50
+ release: { type: 'number', min: 0.001, max: 4, default: 0.25, unit: 's', curve: 'log' },
51
+ cutoff: { type: 'number', min: 100, max: 16000, default: 3000, unit: 'Hz', curve: 'log' },
52
+ envAmount: { type: 'number', min: 0, max: 1, default: 0.6 },
53
+ amp: { type: 'number', min: 0, max: 1, default: 0.7 },
54
+ }
package/package.json CHANGED
@@ -1,16 +1,22 @@
1
1
  {
2
2
  "name": "@audio/synth-voice",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Synth voice — osc × ADSR through envelope-scaled lowpass (Tone.js Synth class)",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "main": "voice.js",
8
8
  "exports": {
9
9
  ".": "./voice.js",
10
+ "./audio": {
11
+ "types": "./audio.d.ts",
12
+ "default": "./audio.js"
13
+ },
10
14
  "./package.json": "./package.json"
11
15
  },
12
16
  "files": [
13
- "voice.js"
17
+ "voice.js",
18
+ "audio.js",
19
+ "audio.d.ts"
14
20
  ],
15
21
  "dependencies": {
16
22
  "@audio/synth-osc": "^1.0.0",
@@ -29,5 +35,6 @@
29
35
  },
30
36
  "publishConfig": {
31
37
  "access": "public"
32
- }
38
+ },
39
+ "audio": "./audio.js"
33
40
  }