@audio/synth-noise 1.1.2 → 2.0.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/audio.js +2 -1
- package/colors.d.ts +9 -0
- package/colors.js +22 -11
- package/package.json +6 -5
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
29
|
-
let d = Float64Array.from(
|
|
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
|
-
|
|
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
|
-
|
|
43
|
-
let p =
|
|
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
|
-
|
|
50
|
-
let w =
|
|
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
|
-
|
|
57
|
-
export
|
|
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](
|
|
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": "
|
|
3
|
+
"version": "2.0.0",
|
|
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": "
|
|
7
|
+
"main": "colors.js",
|
|
8
8
|
"exports": {
|
|
9
|
-
".": "./
|
|
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,6 @@
|
|
|
37
38
|
"publishConfig": {
|
|
38
39
|
"access": "public"
|
|
39
40
|
},
|
|
40
|
-
"types": "
|
|
41
|
+
"types": "colors.d.ts",
|
|
41
42
|
"audio": "./audio.js"
|
|
42
43
|
}
|