@audio/synth-noise 1.1.2 → 2.0.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/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # @audio/synth-noise [![npm](https://img.shields.io/npm/v/@audio/synth-noise)](https://www.npmjs.com/package/@audio/synth-noise) [![MIT](https://img.shields.io/badge/MIT-%E0%A5%90-white)](https://github.com/krishnized/license)
2
+
3
+ Colors of noise — white/pink/brown/blue/violet, seeded, slope-verified; (duration, opts) seconds; pinkNoise filter kernel as named export
4
+
5
+ ```
6
+ npm install @audio/synth-noise
7
+ ```
8
+
9
+ ```js
10
+ import noise, { white, pink, brown, blue, violet, pinkNoise } from '@audio/synth-noise'
11
+ ```
12
+
13
+ Seeded, deterministic noise-color generators with defined spectral slopes: white 0 dB/oct, pink -3 (Kellet filter), brown -6 (leaky integration), blue +3 (differentiated pink), violet +6 (differentiated white). Unpitched — `(duration, opts)` in seconds, the family convention every generator follows.
14
+
15
+ ```js
16
+ noise(1, { color: 'pink', seed: 1 }) // → Float32Array
17
+ pink(1, { seed: 1 }) // same, direct
18
+ ```
19
+
20
+ | Param | Default | Applies to | |
21
+ |---|---|---|---|
22
+ | `duration` | `1` | all | Seconds (positional) |
23
+ | `color` | `'white'` | `noise()` only | `'white'` \| `'pink'` \| `'brown'` \| `'blue'` \| `'violet'` |
24
+ | `seed` | `1` | all | PRNG seed |
25
+ | `leak` | `0.999` | `brown` only | Integrator leak coefficient |
26
+ | `fs` | `44100` | all | Sample rate |
27
+
28
+ `pinkNoise` (from `./pink-noise.js`) is the underlying Kellet-filter kernel, exported directly for use outside the color generators.
29
+
30
+ **Use when:** dither, masking noise, testing filters/denoisers against known spectral slopes.
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.js CHANGED
@@ -12,7 +12,8 @@ export const noise = (ctx) => {
12
12
  const n = out[0].length
13
13
  const fn = colors[params.color]
14
14
  for (let c = 0; c < out.length; c++) {
15
- const d = fn(n, { seed: (params.seed[0] | 0) + c * 7919 }) // independent per channel
15
+ // colors API is seconds (family convention); n/fs·fs rounds back to exactly n samples
16
+ const d = fn(n / ctx.sampleRate, { fs: ctx.sampleRate, seed: (params.seed[0] | 0) + c * 7919 }) // independent per channel
16
17
  const g = params.gain[0]
17
18
  for (let i = 0; i < n; i++) out[c][i] = d[i] * g
18
19
  }
package/colors.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ type NoiseOpts = { seed?: number, fs?: number }
2
+ export function white(duration?: number, opts?: NoiseOpts): Float32Array
3
+ export function pink(duration?: number, opts?: NoiseOpts): Float32Array
4
+ export function brown(duration?: number, opts?: NoiseOpts & { leak?: number }): Float32Array
5
+ export function blue(duration?: number, opts?: NoiseOpts): Float32Array
6
+ export function violet(duration?: number, opts?: NoiseOpts): Float32Array
7
+ export { default as pinkNoise } from './pink-noise.js'
8
+ /** Seeded noise generator — duration in seconds (family convention: (duration, opts)) */
9
+ export default function noise(duration?: number, opts?: NoiseOpts & { color?: 'white' | 'pink' | 'brown' | 'blue' | 'violet' }): Float32Array
package/colors.js CHANGED
@@ -1,8 +1,13 @@
1
1
  // Colors of noise — seeded, deterministic generators with defined spectral slopes:
2
2
  // white 0 dB/oct · pink −3 (Kellet filter) · brown −6 (leaky integration) ·
3
3
  // blue +3 (differentiated pink) · violet +6 (differentiated white).
4
+ //
5
+ // Public API is (duration, opts) in SECONDS — the family convention every other
6
+ // generator follows (osc, chirp, fm…). Sample-count cores stay private: the
7
+ // differentiators (blue, violet) need exact n+1-sample sources.
4
8
 
5
9
  import pinkNoise from './pink-noise.js'
10
+ export { default as pinkNoise } from './pink-noise.js'
6
11
 
7
12
  function lcg (seed) {
8
13
  let s = seed >>> 0 || 1
@@ -18,20 +23,20 @@ function normalize (d, peak = 0.9) {
18
23
  return d
19
24
  }
20
25
 
21
- export function white (n, { seed = 1 } = {}) {
26
+ function whiteN (n, seed) {
22
27
  let rand = lcg(seed)
23
28
  let d = new Float32Array(n)
24
29
  for (let i = 0; i < n; i++) d[i] = rand() * 0.9
25
30
  return d
26
31
  }
27
32
 
28
- export function pink (n, opts = {}) {
29
- let d = Float64Array.from(white(n, opts))
33
+ function pinkN (n, seed) {
34
+ let d = Float64Array.from(whiteN(n, seed))
30
35
  pinkNoise(d, {})
31
36
  return normalize(Float32Array.from(d))
32
37
  }
33
38
 
34
- export function brown (n, { seed = 1, leak = 0.999 } = {}) {
39
+ function brownN (n, seed, leak) {
35
40
  let rand = lcg(seed)
36
41
  let d = new Float32Array(n)
37
42
  let y = 0
@@ -39,23 +44,29 @@ export function brown (n, { seed = 1, leak = 0.999 } = {}) {
39
44
  return normalize(d)
40
45
  }
41
46
 
42
- export function blue (n, opts = {}) {
43
- let p = pink(n + 1, opts)
47
+ function blueN (n, seed) {
48
+ let p = pinkN(n + 1, seed)
44
49
  let d = new Float32Array(n)
45
50
  for (let i = 0; i < n; i++) d[i] = p[i + 1] - p[i]
46
51
  return normalize(d)
47
52
  }
48
53
 
49
- export function violet (n, opts = {}) {
50
- let w = white(n + 1, opts)
54
+ function violetN (n, seed) {
55
+ let w = whiteN(n + 1, seed)
51
56
  let d = new Float32Array(n)
52
57
  for (let i = 0; i < n; i++) d[i] = w[i + 1] - w[i]
53
58
  return normalize(d)
54
59
  }
55
60
 
56
- /** noise(n, { color: 'white'|'pink'|'brown'|'blue'|'violet', seed }) */
57
- export default function noise (n, { color = 'white', ...opts } = {}) {
61
+ export function white (duration = 1, { seed = 1, fs = 44100 } = {}) { return whiteN(Math.round(duration * fs), seed) }
62
+ export function pink (duration = 1, { seed = 1, fs = 44100 } = {}) { return pinkN(Math.round(duration * fs), seed) }
63
+ export function brown (duration = 1, { seed = 1, leak = 0.999, fs = 44100 } = {}) { return brownN(Math.round(duration * fs), seed, leak) }
64
+ export function blue (duration = 1, { seed = 1, fs = 44100 } = {}) { return blueN(Math.round(duration * fs), seed) }
65
+ export function violet (duration = 1, { seed = 1, fs = 44100 } = {}) { return violetN(Math.round(duration * fs), seed) }
66
+
67
+ /** noise(duration, { color: 'white'|'pink'|'brown'|'blue'|'violet', seed, fs }) — seconds */
68
+ export default function noise (duration = 1, { color = 'white', ...opts } = {}) {
58
69
  const GEN = { white, pink, brown, blue, violet }
59
70
  if (!GEN[color]) throw new RangeError(`noise: unknown color "${color}"`)
60
- return GEN[color](n, opts)
71
+ return GEN[color](duration, opts)
61
72
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@audio/synth-noise",
3
- "version": "1.1.2",
3
+ "version": "2.0.1",
4
4
  "description": "Colors of noise — white, pink (Kellet), brown, blue, violet; seeded, slope-verified",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
- "main": "pink-noise.js",
7
+ "main": "colors.js",
8
8
  "exports": {
9
- ".": "./pink-noise.js",
9
+ ".": "./colors.js",
10
10
  "./colors": "./colors.js",
11
11
  "./pink": "./pink-noise.js",
12
12
  "./audio": {
@@ -20,7 +20,8 @@
20
20
  "pink-noise.d.ts",
21
21
  "colors.js",
22
22
  "audio.js",
23
- "audio.d.ts"
23
+ "audio.d.ts",
24
+ "colors.d.ts"
24
25
  ],
25
26
  "keywords": [
26
27
  "audio",
@@ -37,6 +38,12 @@
37
38
  "publishConfig": {
38
39
  "access": "public"
39
40
  },
40
- "types": "pink-noise.d.ts",
41
- "audio": "./audio.js"
41
+ "types": "colors.d.ts",
42
+ "audio": "./audio.js",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/audiojs/synth.git",
46
+ "directory": "packages/synth-noise"
47
+ },
48
+ "funding": "https://github.com/sponsors/audiojs"
42
49
  }