@audio/synth-drum 1.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.
Files changed (2) hide show
  1. package/drum.js +33 -0
  2. package/package.json +29 -0
package/drum.js ADDED
@@ -0,0 +1,33 @@
1
+ // Drum synthesis — membrane (pitch-dropping sine kick), metal (inharmonic square-ish
2
+ // cymbal), noise (snare/hat band noise), Tone.js MembraneSynth/MetalSynth/NoiseSynth class.
3
+
4
+ export function membrane ({ freq = 55, drop = 3, duration = 0.5, fs = 44100, amp = 0.9 } = {}) {
5
+ let n = Math.round(duration * fs)
6
+ let out = new Float32Array(n)
7
+ let phase = 0
8
+ for (let i = 0; i < n; i++) {
9
+ let t = i / n
10
+ let f = freq * (1 + drop * Math.exp(-t * 9))
11
+ phase += 2 * Math.PI * f / fs
12
+ out[i] = amp * Math.exp(-t * 6) * Math.sin(phase)
13
+ }
14
+ return out
15
+ }
16
+
17
+ export function metal ({ freq = 200, duration = 0.6, fs = 44100, amp = 0.5 } = {}) {
18
+ const RATIOS = [1, 1.483, 1.932, 2.546, 2.63, 3.897] // classic FM-bell inharmonic set
19
+ let n = Math.round(duration * fs)
20
+ let out = new Float32Array(n)
21
+ for (let r of RATIOS) for (let i = 0; i < n; i++) out[i] += (amp / RATIOS.length) * Math.exp(-i / n * 8) * Math.sign(Math.sin(2 * Math.PI * freq * r * i / fs))
22
+ return out
23
+ }
24
+
25
+ export function noiseDrum ({ duration = 0.25, fs = 44100, amp = 0.7, seed = 9 } = {}) {
26
+ let n = Math.round(duration * fs)
27
+ let out = new Float32Array(n)
28
+ let s = seed
29
+ for (let i = 0; i < n; i++) { s = (s * 1103515245 + 12345) & 0x7fffffff; out[i] = amp * (s / 0x3fffffff - 1) * Math.exp(-i / n * 7) }
30
+ return out
31
+ }
32
+
33
+ export default membrane
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@audio/synth-drum",
3
+ "version": "1.0.0",
4
+ "description": "Drum synthesis — membrane kick, metal cymbal, noise snare (Tone.js class)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "drum.js",
8
+ "exports": {
9
+ ".": "./drum.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "drum.js"
14
+ ],
15
+ "keywords": [
16
+ "audio",
17
+ "dsp",
18
+ "synth",
19
+ "drum"
20
+ ],
21
+ "license": "MIT",
22
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ }
29
+ }