@audio/synth-voice 1.1.2 → 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.
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # @audio/synth-voice [![npm](https://img.shields.io/npm/v/@audio/synth-voice)](https://www.npmjs.com/package/@audio/synth-voice) [![MIT](https://img.shields.io/badge/MIT-%E0%A5%90-white)](https://github.com/krishnized/license)
2
+
3
+ Osc + envelope voice (Tone.js Synth class)
4
+
5
+ ```
6
+ npm install @audio/synth-voice
7
+ ```
8
+
9
+ ```js
10
+ import voice from '@audio/synth-voice'
11
+ ```
12
+
13
+ Composed voice — [`synth-osc`](https://github.com/audiojs/synth/tree/main/packages/synth-osc) through a one-pole lowpass whose cutoff is scaled by an [`synth-envelope`](https://github.com/audiojs/synth/tree/main/packages/synth-envelope) ADSR, the Tone.js Synth/MonoSynth class. Pitched — `(freq, opts)`.
14
+
15
+ ```js
16
+ voice(440, { type: 'sawtooth', fc: 3000, envAmount: 0.6 }) // → Float32Array
17
+ ```
18
+
19
+ | Param | Default | |
20
+ |---|---|---|
21
+ | `freq` | — | Hz (positional) |
22
+ | `fs` | `44100` | Sample rate |
23
+ | `type` | `'sawtooth'` | Oscillator waveform — see `synth-osc` |
24
+ | `duration` | `0.6` | Note-on length, seconds |
25
+ | `attack` / `decay` / `sustain` / `release` | `0.01` / `0.15` / `0.6` / `0.25` | ADSR, seconds/level |
26
+ | `fc` | `3000` | Lowpass cutoff at full envelope, Hz (`cutoff` still accepted) |
27
+ | `envAmount` | `0.6` | 0..1 — how much the envelope scales the cutoff |
28
+ | `amp` | `0.7` | Peak amplitude |
29
+
30
+ **Use when:** a ready-made subtractive voice for `synth-poly`, without composing osc+envelope+filter by hand.
31
+
32
+ ---
33
+
34
+ Part of [@audio/synth](https://github.com/audiojs/synth) — the synth family umbrella.
35
+
36
+ MIT © [audiojs](https://github.com/audiojs)
package/audio.d.ts CHANGED
@@ -21,6 +21,8 @@ export interface VoiceOptions {
21
21
  /** 0.001..4 s (default 0.25) */
22
22
  "release"?: Auto
23
23
  /** 100..16000 Hz (default 3000) */
24
+ "fc"?: Auto
25
+ /** @deprecated former name of "fc" */
24
26
  "cutoff"?: Auto
25
27
  /** 0..1 (default 0.6) */
26
28
  "envAmount"?: Auto
@@ -46,7 +48,7 @@ export declare const voice: {
46
48
  /** 0.001..4 s (default 0.25) */
47
49
  "release": { type: "number", default: 0.25 }
48
50
  /** 100..16000 Hz (default 3000) */
49
- "cutoff": { type: "number", default: 3000 }
51
+ "fc": { type: "number", default: 3000, alias: "cutoff" }
50
52
  /** 0..1 (default 0.6) */
51
53
  "envAmount": { type: "number", default: 0.6 }
52
54
  /** 0..1 (default 0.7) */
package/audio.js CHANGED
@@ -6,6 +6,7 @@
6
6
  // @audio/synth-poly for allocation).
7
7
 
8
8
  import voiceFn from './voice.js'
9
+ import { midiToHz } from '@audio/note'
9
10
 
10
11
  export const voice = (ctx) => {
11
12
  return (inputs, outputs, params) => {
@@ -25,7 +26,7 @@ export const voice = (ctx) => {
25
26
  type: params.type,
26
27
  attack: params.attack[0], decay: params.decay[0],
27
28
  sustain: params.sustain[0], release: params.release[0],
28
- cutoff: params.cutoff[0], envAmount: params.envAmount[0],
29
+ fc: params.fc[0], envAmount: params.envAmount[0],
29
30
  amp: params.amp[0] * (slot.velocity ?? 1),
30
31
  })
31
32
  for (let i = 0; i < d.length && slot.time + i < n; i++) o0[slot.time + i] += d[i]
@@ -48,7 +49,7 @@ voice.params = {
48
49
  decay: { type: 'number', min: 0.001, max: 2, default: 0.15, unit: 's', curve: 'log' },
49
50
  sustain: { type: 'number', min: 0, max: 1, default: 0.6 },
50
51
  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
+ fc: { type: 'number', min: 100, max: 16000, default: 3000, unit: 'Hz', curve: 'log', alias: 'cutoff' },
52
53
  envAmount: { type: 'number', min: 0, max: 1, default: 0.6 },
53
54
  amp: { type: 'number', min: 0, max: 1, default: 0.7 },
54
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/synth-voice",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "Synth voice — osc × ADSR through envelope-scaled lowpass (Tone.js Synth class)",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -15,9 +15,11 @@
15
15
  },
16
16
  "files": [
17
17
  "voice.js",
18
+ "voice.d.ts",
18
19
  "audio.js",
19
20
  "audio.d.ts"
20
21
  ],
22
+ "types": "voice.d.ts",
21
23
  "dependencies": {
22
24
  "@audio/synth-osc": "^1.0.0",
23
25
  "@audio/synth-envelope": "^1.0.0",
@@ -37,5 +39,11 @@
37
39
  "publishConfig": {
38
40
  "access": "public"
39
41
  },
40
- "audio": "./audio.js"
42
+ "audio": "./audio.js",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/audiojs/synth.git",
46
+ "directory": "packages/synth-voice"
47
+ },
48
+ "funding": "https://github.com/sponsors/audiojs"
41
49
  }
package/voice.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /** Osc + envelope voice — one-pole lowpass with envelope-scaled cutoff (Tone.js Synth/MonoSynth class). */
2
+ export interface VoiceOptions {
3
+ /** sample rate, default 44100 */
4
+ fs?: number
5
+ /** oscillator waveform, default 'sawtooth' */
6
+ type?: 'sine' | 'square' | 'sawtooth' | 'triangle'
7
+ /** note-on length, seconds, default 0.6 */
8
+ duration?: number
9
+ /** seconds, default 0.01 */
10
+ attack?: number
11
+ /** seconds, default 0.15 */
12
+ decay?: number
13
+ /** sustain level 0..1, default 0.6 */
14
+ sustain?: number
15
+ /** seconds, default 0.25 */
16
+ release?: number
17
+ /** lowpass cutoff at full envelope, Hz, default 3000 */
18
+ fc?: number
19
+ /** @deprecated former name of `fc` */
20
+ cutoff?: number
21
+ /** 0..1, how much the envelope scales the cutoff, default 0.6 */
22
+ envAmount?: number
23
+ /** peak amplitude, default 0.7 */
24
+ amp?: number
25
+ }
26
+
27
+ /** freq: Hz (positional) */
28
+ export default function voice(freq: number, options?: VoiceOptions): Float32Array
package/voice.js CHANGED
@@ -1,19 +1,20 @@
1
1
  // Synth voice — oscillator × ADSR envelope through a one-pole lowpass with envelope-scaled
2
- // cutoff (Tone.js Synth/MonoSynth class), composed from @audio/synth-osc + synth-envelope.
2
+ // cutoff `fc` (Tone.js Synth/MonoSynth class), composed from @audio/synth-osc + synth-envelope.
3
3
 
4
4
  import osc from '@audio/synth-osc'
5
5
  import adsr from '@audio/synth-envelope'
6
6
 
7
7
  export default function voice (freq, { fs = 44100, type = 'sawtooth', duration = 0.6,
8
8
  attack = 0.01, decay = 0.15, sustain = 0.6, release = 0.25,
9
- cutoff = 3000, envAmount = 0.6, amp = 0.7 } = {}) {
9
+ fc, cutoff, envAmount = 0.6, amp = 0.7 } = {}) {
10
+ fc ??= cutoff ?? 3000 // `cutoff`: former name
10
11
  let env = adsr({ attack, decay, sustain, release, duration, fs })
11
12
  let src = osc(freq, { duration: env.length / fs, fs, type, amp: 1 })
12
13
  let out = new Float32Array(env.length)
13
14
  let lp = 0
14
15
  for (let i = 0; i < env.length; i++) {
15
- let fc = cutoff * (1 - envAmount + envAmount * env[i])
16
- let a = Math.exp(-2 * Math.PI * fc / fs)
16
+ let f = fc * (1 - envAmount + envAmount * env[i])
17
+ let a = Math.exp(-2 * Math.PI * f / fs)
17
18
  lp = src[i] * env[i] * (1 - a) + lp * a
18
19
  out[i] = amp * lp
19
20
  }