@audio/synth-sfx 1.1.2 → 1.1.4

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 (4) hide show
  1. package/README.md +40 -3
  2. package/package.json +13 -2
  3. package/sfx.d.ts +40 -0
  4. package/sfx.js +8 -4
package/README.md CHANGED
@@ -1,5 +1,42 @@
1
- # @audio/synth-sfx
1
+ # @audio/synth-sfx [![npm](https://img.shields.io/npm/v/@audio/synth-sfx)](https://www.npmjs.com/package/@audio/synth-sfx) [![MIT](https://img.shields.io/badge/MIT-%E0%A5%90-white)](https://github.com/krishnized/license)
2
2
 
3
- > Game/UI sound effects generator — ZZFX / ChipTone class parameterized synthesis
3
+ ZZFX-class parameterized SFX, 8 deterministic presets
4
4
 
5
- Planned — not implemented yet. See the umbrella README for status and sources.
5
+ ```
6
+ npm install @audio/synth-sfx
7
+ ```
8
+
9
+ ```js
10
+ import sfx, { PRESETS } from '@audio/synth-sfx'
11
+ ```
12
+
13
+ Game/UI sound-effect generator (ZZFX/sfxr class): one parameterized pipeline — shaped oscillator → frequency slide/vibrato/arpeggio → ADSR-ish envelope → optional lowpass/bitcrush — behind 8 named presets covering the sfxr archetypes (`pickup`, `laser`, `explosion`, `powerup`, `hit`, `jump`, `blip`, `coin`). Every field is overridable; noise is seeded, so renders are reproducible.
14
+
15
+ ```js
16
+ sfx('laser') // named preset
17
+ sfx('coin', { seed: 42 }) // preset + override
18
+ sfx({ freq: 500, shape: 'square' }) // full custom parameter object
19
+ ```
20
+
21
+ | Param | Default | |
22
+ |---|---|---|
23
+ | `preset` | `'blip'` | Preset name or full parameter object (positional) |
24
+ | `freq` | preset | Base frequency, Hz |
25
+ | `shape` | preset | `'sine'` \| `'square'` \| `'saw'` \| `'triangle'` \| `'noise'` |
26
+ | `slide` | `0` | Octaves/second exponential pitch slide |
27
+ | `vibrato` / `vibratoDepth` | `0` / `0.1` | Hz / depth |
28
+ | `arp` / `arpAt` | `0` / `0.05` | Frequency multiplier stepped at `arpAt` seconds |
29
+ | `attack` / `sustain` / `release` | `0.005` / `0.08` / `0.12` | Seconds |
30
+ | `lowpass` | `0` | One-pole lowpass cutoff, Hz (0 = off) |
31
+ | `crush` | `0` | 0..1 bitcrush amount |
32
+ | `amp` | `0.8` | Peak amplitude |
33
+ | `seed` | — | Noise PRNG seed |
34
+ | `fs` | `44100` | Sample rate |
35
+
36
+ **Use when:** deterministic game/UI SFX without shipping audio assets.
37
+
38
+ ---
39
+
40
+ Part of [@audio/synth](https://github.com/audiojs/synth) — the synth family umbrella.
41
+
42
+ MIT © [audiojs](https://github.com/audiojs)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@audio/synth-sfx",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Game/UI sound effects generator — ZZFX / ChipTone class parameterized synthesis",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -27,11 +27,22 @@
27
27
  },
28
28
  "files": [
29
29
  "sfx.js",
30
+ "sfx.d.ts",
30
31
  "audio.js",
31
32
  "audio.d.ts"
32
33
  ],
34
+ "types": "sfx.d.ts",
35
+ "dependencies": {
36
+ "@audio/synth-osc": "^1.1.0"
37
+ },
33
38
  "publishConfig": {
34
39
  "access": "public"
35
40
  },
36
- "audio": "./audio.js"
41
+ "audio": "./audio.js",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/audiojs/synth.git",
45
+ "directory": "packages/synth-sfx"
46
+ },
47
+ "funding": "https://github.com/sponsors/audiojs"
37
48
  }
package/sfx.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ /** ZZFX/sfxr-class parameterized SFX generator — shaped osc → slide/vibrato/arp → envelope → lowpass/bitcrush. */
2
+ export type SfxPreset = 'pickup' | 'laser' | 'explosion' | 'powerup' | 'hit' | 'jump' | 'blip' | 'coin'
3
+
4
+ export interface SfxOptions {
5
+ /** base frequency, Hz */
6
+ freq?: number
7
+ /** waveform */
8
+ shape?: 'sine' | 'square' | 'saw' | 'triangle' | 'noise'
9
+ /** octaves/second exponential pitch slide, default 0 */
10
+ slide?: number
11
+ /** Hz, default 0 */
12
+ vibrato?: number
13
+ /** depth, default 0.1 */
14
+ vibratoDepth?: number
15
+ /** frequency multiplier stepped at arpAt, default 0 */
16
+ arp?: number
17
+ /** seconds, default 0.05 */
18
+ arpAt?: number
19
+ /** seconds, default 0.005 */
20
+ attack?: number
21
+ /** seconds, default 0.08 */
22
+ sustain?: number
23
+ /** seconds, default 0.12 */
24
+ release?: number
25
+ /** one-pole lowpass cutoff Hz, 0 = off, default 0 */
26
+ lowpass?: number
27
+ /** 0..1 bitcrush amount, default 0 */
28
+ crush?: number
29
+ /** sample rate, default 44100 */
30
+ fs?: number
31
+ /** peak amplitude, default 0.8 */
32
+ amp?: number
33
+ /** noise PRNG seed */
34
+ seed?: number
35
+ }
36
+
37
+ export const PRESETS: Record<SfxPreset, SfxOptions>
38
+
39
+ /** preset: named preset ('blip' default) or a full parameter object */
40
+ export default function sfx(preset?: SfxPreset | SfxOptions, options?: SfxOptions): Float32Array
package/sfx.js CHANGED
@@ -3,6 +3,8 @@
3
3
  // noise/bitcrush/lowpass. Named presets cover the sfxr archetypes; every field is
4
4
  // overridable, deterministic (seeded noise) so renders are reproducible.
5
5
 
6
+ import { wave } from '@audio/synth-osc'
7
+
6
8
  let { sin, PI, abs, exp, sign } = Math
7
9
 
8
10
  export const PRESETS = {
@@ -49,12 +51,14 @@ export default function sfx (preset = 'blip', opts = {}) {
49
51
  if (arp && t > arpAt) f *= arp ** Math.min(2, Math.floor(t / arpAt))
50
52
  if (vib) f *= 1 + vibDepth * sin(2 * PI * vib * t)
51
53
 
52
- phase += f / fs
54
+ // band-limited (synth-osc): the rising saw and the triangle rising from −1 are its falling ones negated
55
+ let dt = f / fs
56
+ phase += dt
53
57
  let ph = phase % 1
54
58
  let s = shape === 'sine' ? sin(2 * PI * ph)
55
- : shape === 'square' ? (ph < 0.5 ? 1 : -1) * 0.7
56
- : shape === 'saw' ? (2 * ph - 1) * 0.8
57
- : shape === 'triangle' ? (ph < 0.5 ? 4 * ph - 1 : 3 - 4 * ph)
59
+ : shape === 'square' ? wave('square', ph, dt) * 0.7
60
+ : shape === 'saw' ? -wave('sawtooth', ph, dt) * 0.8
61
+ : shape === 'triangle' ? -wave('triangle', ph, dt)
58
62
  : noise()
59
63
 
60
64
  // envelope: linear attack, flat sustain, exponential release