@audio/synth-poly 1.1.0 → 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.
Files changed (3) hide show
  1. package/audio.d.ts +35 -0
  2. package/package.json +9 -4
  3. package/poly.js +12 -7
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/synth-poly",
3
- "version": "1.1.0",
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,
@@ -17,18 +17,23 @@
17
17
  "main": "poly.js",
18
18
  "exports": {
19
19
  ".": "./poly.js",
20
- "./audio": "./audio.js",
20
+ "./audio": {
21
+ "types": "./audio.d.ts",
22
+ "default": "./audio.js"
23
+ },
21
24
  "./package.json": "./package.json"
22
25
  },
23
26
  "files": [
24
27
  "poly.js",
25
- "audio.js"
28
+ "audio.js",
29
+ "audio.d.ts"
26
30
  ],
27
31
  "publishConfig": {
28
32
  "access": "public"
29
33
  },
30
34
  "audio": "./audio.js",
31
35
  "dependencies": {
32
- "@audio/synth-voice": "^1.1.0"
36
+ "@audio/synth-voice": "^1.1.0",
37
+ "@audio/note": "^1.0.0"
33
38
  }
34
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 a plain
3
- // synthesis function (freq, {fs, duration, velocity}) → Float32Array, so every @audio
4
- // generator (osc, pluck, drum, wavetable…) is a valid voice.
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 ?? 440 * 2 ** (((n.midi ?? 69) - 69) / 12),
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
  })