@vibuca/synth8-player 0.1.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/LICENSE +21 -0
- package/README.md +73 -0
- package/dist/drum/drum-synths.d.ts +10 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +102 -0
- package/package.json +26 -0
- package/src/index.ts +92 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sven Hemmer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @vibuca/synth8-player
|
|
2
|
+
|
|
3
|
+
Tone.js player for Synt8 patterns.
|
|
4
|
+
|
|
5
|
+
This package plays patterns compiled by `@vibuca/synth8-core`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @vibuca/synth8-core @vibuca/synth8-player
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { compile } from "@vibuca/synth8-core";
|
|
17
|
+
import { play, stop } from "@vibuca/synth8-player";
|
|
18
|
+
|
|
19
|
+
const pattern = compile(`
|
|
20
|
+
song(
|
|
21
|
+
beat("kick+hihat snare hihat snare"),
|
|
22
|
+
melody("c4 e4 g4 e4")
|
|
23
|
+
)
|
|
24
|
+
`);
|
|
25
|
+
|
|
26
|
+
await play(pattern, { bpm: 120 });
|
|
27
|
+
|
|
28
|
+
// later
|
|
29
|
+
stop();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
### `play(pattern, options)`
|
|
35
|
+
|
|
36
|
+
Plays a compiled Synt8 pattern.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
play(pattern, { bpm: 120 });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Options:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
type PlayOptions = {
|
|
46
|
+
bpm: number;
|
|
47
|
+
};
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### `stop()`
|
|
51
|
+
|
|
52
|
+
Stops playback.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
stop();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Supported events
|
|
59
|
+
|
|
60
|
+
`@vibuca/synth8-player` supports:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
{ type: "drum", value: "kick" }
|
|
64
|
+
{ type: "note", value: "c4" }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Drums are synthesized with Tone.js percussion voices.
|
|
68
|
+
|
|
69
|
+
Notes are played with a Tone.js synth.
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as Tone from "tone";
|
|
2
|
+
declare const kick: Tone.MembraneSynth;
|
|
3
|
+
declare const snare: Tone.NoiseSynth;
|
|
4
|
+
declare const hihat: Tone.MetalSynth;
|
|
5
|
+
declare const clap: Tone.NoiseSynth;
|
|
6
|
+
declare const openhat: Tone.MetalSynth;
|
|
7
|
+
declare const tom: Tone.MembraneSynth;
|
|
8
|
+
declare const rim: Tone.MetalSynth;
|
|
9
|
+
declare const cowbell: Tone.AmplitudeEnvelope;
|
|
10
|
+
export { kick, snare, hihat, clap, openhat, tom, rim, cowbell };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import * as e from "tone";
|
|
2
|
+
//#region src/drum/drum-synths.ts
|
|
3
|
+
var t = new e.MembraneSynth().toDestination(), n = new e.NoiseSynth({
|
|
4
|
+
noise: { type: "white" },
|
|
5
|
+
envelope: {
|
|
6
|
+
attack: .001,
|
|
7
|
+
decay: .15,
|
|
8
|
+
sustain: 0
|
|
9
|
+
}
|
|
10
|
+
}).toDestination(), r = new e.MetalSynth({
|
|
11
|
+
envelope: {
|
|
12
|
+
attack: .001,
|
|
13
|
+
decay: .05,
|
|
14
|
+
release: .01
|
|
15
|
+
},
|
|
16
|
+
harmonicity: 5.1,
|
|
17
|
+
modulationIndex: 32,
|
|
18
|
+
resonance: 4e3,
|
|
19
|
+
octaves: 1.5
|
|
20
|
+
});
|
|
21
|
+
r.frequency.value = 300, r.toDestination();
|
|
22
|
+
var i = new e.NoiseSynth({
|
|
23
|
+
noise: { type: "white" },
|
|
24
|
+
envelope: {
|
|
25
|
+
attack: .001,
|
|
26
|
+
decay: .12,
|
|
27
|
+
sustain: 0
|
|
28
|
+
}
|
|
29
|
+
}).toDestination(), a = new e.MetalSynth({
|
|
30
|
+
envelope: {
|
|
31
|
+
attack: .001,
|
|
32
|
+
decay: .35,
|
|
33
|
+
release: .1
|
|
34
|
+
},
|
|
35
|
+
harmonicity: 5.1,
|
|
36
|
+
modulationIndex: 24,
|
|
37
|
+
resonance: 5e3,
|
|
38
|
+
octaves: 1.5
|
|
39
|
+
});
|
|
40
|
+
a.frequency.value = 350, a.toDestination();
|
|
41
|
+
var o = new e.MembraneSynth().toDestination(), s = new e.MetalSynth({ envelope: {
|
|
42
|
+
attack: .001,
|
|
43
|
+
decay: .08,
|
|
44
|
+
release: .01
|
|
45
|
+
} });
|
|
46
|
+
s.frequency.value = 800, s.toDestination();
|
|
47
|
+
var c = new e.AmplitudeEnvelope({
|
|
48
|
+
attack: .001,
|
|
49
|
+
decay: .18,
|
|
50
|
+
sustain: 0,
|
|
51
|
+
release: .03
|
|
52
|
+
}).toDestination(), l = new e.Oscillator(540, "square").connect(c), u = new e.Oscillator(800, "square").connect(c);
|
|
53
|
+
l.start(), u.start();
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/index.ts
|
|
56
|
+
var d = new e.PolySynth(e.Synth).toDestination(), f = (e, l) => {
|
|
57
|
+
switch (e) {
|
|
58
|
+
case "kick":
|
|
59
|
+
t.triggerAttackRelease("C1", "8n", l);
|
|
60
|
+
break;
|
|
61
|
+
case "snare":
|
|
62
|
+
n.triggerAttackRelease("16n", l);
|
|
63
|
+
break;
|
|
64
|
+
case "clap":
|
|
65
|
+
i.triggerAttackRelease("16n", l);
|
|
66
|
+
break;
|
|
67
|
+
case "hihat":
|
|
68
|
+
r.triggerAttackRelease("32n", l);
|
|
69
|
+
break;
|
|
70
|
+
case "openhat":
|
|
71
|
+
a.triggerAttackRelease("8n", l);
|
|
72
|
+
break;
|
|
73
|
+
case "tom":
|
|
74
|
+
o.triggerAttackRelease("G1", "8n", l);
|
|
75
|
+
break;
|
|
76
|
+
case "rim":
|
|
77
|
+
s.triggerAttackRelease("32n", l);
|
|
78
|
+
break;
|
|
79
|
+
case "cowbell":
|
|
80
|
+
c.triggerAttackRelease("16n", l);
|
|
81
|
+
break;
|
|
82
|
+
default: console.warn(`Unknown drum sound: ${e}`);
|
|
83
|
+
}
|
|
84
|
+
}, p = async (t, n = {}) => {
|
|
85
|
+
let r = n.bpm ?? 120;
|
|
86
|
+
await e.start();
|
|
87
|
+
let i = e.getTransport();
|
|
88
|
+
i.stop(), i.cancel(), i.bpm.value = r;
|
|
89
|
+
let a = 60 / r, o = t.length * a;
|
|
90
|
+
for (let e of t.events) {
|
|
91
|
+
let t = e.time * a;
|
|
92
|
+
i.scheduleRepeat((t) => {
|
|
93
|
+
e.type === "drum" && f(e.value, t), e.type === "note" && d.triggerAttackRelease(e.value, e.dur, t);
|
|
94
|
+
}, o, t);
|
|
95
|
+
}
|
|
96
|
+
i.start();
|
|
97
|
+
}, m = () => {
|
|
98
|
+
let t = e.getTransport();
|
|
99
|
+
t.stop(), t.cancel();
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
102
|
+
export { p as play, m as stop };
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vibuca/synth8-player",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.ts",
|
|
11
|
+
"import": "./src/index.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"tone": "^15.1.22",
|
|
16
|
+
"@vibuca/synth8-core": "0.1.1"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "vite build"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as Tone from "tone";
|
|
2
|
+
import { kick, snare, hihat, clap, openhat, tom, cowbell, rim } from "./drum/drum-synths";
|
|
3
|
+
import type { Pattern } from "@vibuca/synth8-core";
|
|
4
|
+
|
|
5
|
+
export type PlayOptions = {
|
|
6
|
+
bpm?: number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const synth = new Tone.PolySynth(Tone.Synth).toDestination();
|
|
10
|
+
|
|
11
|
+
const playDrum = (value: string, time: number) => {
|
|
12
|
+
switch (value) {
|
|
13
|
+
case "kick":
|
|
14
|
+
kick.triggerAttackRelease("C1", "8n", time);
|
|
15
|
+
break;
|
|
16
|
+
|
|
17
|
+
case "snare":
|
|
18
|
+
snare.triggerAttackRelease("16n", time);
|
|
19
|
+
break;
|
|
20
|
+
|
|
21
|
+
case "clap":
|
|
22
|
+
clap.triggerAttackRelease("16n", time);
|
|
23
|
+
break;
|
|
24
|
+
|
|
25
|
+
case "hihat":
|
|
26
|
+
hihat.triggerAttackRelease("32n", time);
|
|
27
|
+
break;
|
|
28
|
+
|
|
29
|
+
case "openhat":
|
|
30
|
+
openhat.triggerAttackRelease("8n", time);
|
|
31
|
+
break;
|
|
32
|
+
|
|
33
|
+
case "tom":
|
|
34
|
+
tom.triggerAttackRelease("G1", "8n", time);
|
|
35
|
+
break;
|
|
36
|
+
|
|
37
|
+
case "rim":
|
|
38
|
+
rim.triggerAttackRelease("32n", time);
|
|
39
|
+
break;
|
|
40
|
+
|
|
41
|
+
case "cowbell":
|
|
42
|
+
cowbell.triggerAttackRelease("16n", time);
|
|
43
|
+
break;
|
|
44
|
+
|
|
45
|
+
default:
|
|
46
|
+
console.warn(`Unknown drum sound: ${value}`);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const play = async (
|
|
51
|
+
pattern: Pattern,
|
|
52
|
+
options: PlayOptions = {}
|
|
53
|
+
): Promise<void> => {
|
|
54
|
+
const bpm = options.bpm ?? 120;
|
|
55
|
+
|
|
56
|
+
await Tone.start();
|
|
57
|
+
|
|
58
|
+
const transport = Tone.getTransport();
|
|
59
|
+
transport.stop();
|
|
60
|
+
transport.cancel();
|
|
61
|
+
transport.bpm.value = bpm;
|
|
62
|
+
|
|
63
|
+
const secondsPerBeat = 60 / bpm;
|
|
64
|
+
const loopDuration = pattern.length * secondsPerBeat;
|
|
65
|
+
|
|
66
|
+
for (const event of pattern.events) {
|
|
67
|
+
const eventOffset = event.time * secondsPerBeat;
|
|
68
|
+
|
|
69
|
+
transport.scheduleRepeat(
|
|
70
|
+
(time) => {
|
|
71
|
+
if (event.type === "drum") {
|
|
72
|
+
playDrum(event.value, time);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (event.type === "note") {
|
|
76
|
+
synth.triggerAttackRelease(event.value, event.dur, time);
|
|
77
|
+
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
loopDuration,
|
|
81
|
+
eventOffset
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
transport.start();
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const stop = (): void => {
|
|
89
|
+
const transport = Tone.getTransport();
|
|
90
|
+
transport.stop();
|
|
91
|
+
transport.cancel();
|
|
92
|
+
};
|