Skip to content

Commit 69cf85c

Browse files
author
Jason Belcher
committed
initial commit
0 parents  commit 69cf85c

180 files changed

Lines changed: 219 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# General
2+
.DS_Store
3+
.AppleDouble
4+
.LSOverride
5+
6+
# Icon must end with two \r
7+
Icon
8+
9+
10+
# Thumbnails
11+
._*
12+
13+
# Files that might appear in the root of a volume
14+
.DocumentRevisions-V100
15+
.fseventsd
16+
.Spotlight-V100
17+
.TemporaryItems
18+
.Trashes
19+
.VolumeIcon.icns
20+
.com.apple.timemachine.donotpresent
21+
22+
# Directories potentially created on remote AFP share
23+
.AppleDB
24+
.AppleDesktop
25+
Network Trash Folder
26+
Temporary Items
27+
.apdisk
28+
29+
node_modules

drum-machine.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
let player = require("play-sound")();
2+
3+
// For each tick in a sequence this object will be checked
4+
// to determine what instruments need
5+
// to be triggered for the current tick.
6+
7+
function DrumMachineState(id) {
8+
this.id = id;
9+
this.kick = {
10+
on: false,
11+
name: "kick",
12+
location: "./samples/Deep House Drum Samples/bd_kick/bd_deephouser.wav"
13+
};
14+
this.clap = {
15+
on: false,
16+
name: "clap",
17+
location: "./samples/Deep House Drum Samples/clap/clp_analogue.wav"
18+
};
19+
this.hat = {
20+
on: false,
21+
name: "hat",
22+
location: "./samples/Deep House Drum Samples/hats/hat_analog.wav"
23+
};
24+
this.shaker = {
25+
on: false,
26+
name: "shaker",
27+
location:
28+
"./samples/Deep House Drum Samples/shaker_tambourine/shaker_bot.wav"
29+
};
30+
this.perc = {
31+
on: false,
32+
name: "perc",
33+
location: "./samples/Deep House Drum Samples/percussion/prc_bongodrm.wav"
34+
};
35+
this.perc2 = {
36+
on: false,
37+
name: "perc2",
38+
location: "./samples/Deep House Drum Samples/percussion/prc_congaz.wav"
39+
};
40+
}
41+
42+
DrumMachineState.prototype.getState = function(name) {
43+
return this[name];
44+
};
45+
DrumMachineState.prototype.setState = function(name, on, volume) {
46+
this[name].on = on;
47+
this[name].volume = volume || 1;
48+
this[name].name = name;
49+
};
50+
51+
// dependency inject the drum machine state into sequencer
52+
function Sequencer(drumMachineState, length) {
53+
this.drumMachineState = drumMachineState;
54+
this.length = length;
55+
this.sequence = [];
56+
}
57+
58+
Sequencer.prototype.initSeq = function() {
59+
for (let i = 0; i < this.length; i += 1) {
60+
this.sequence.push(new this.drumMachineState(i + 1));
61+
}
62+
63+
return this.sequence;
64+
};
65+
66+
// drum machine player
67+
const dm = {
68+
playingSeq: null,
69+
playHead: 0,
70+
tempo: 800,
71+
setTempo: function(tempo, ticksPerBeat = 1) {
72+
let ms;
73+
switch (ticksPerBeat) {
74+
case 1:
75+
ms = 60000 / tempo;
76+
break;
77+
case 2:
78+
ms = 30000 / tempo;
79+
break;
80+
case 4:
81+
ms = 15000 / tempo;
82+
default:
83+
ms = 15000 / tempo;
84+
}
85+
this.tempo = ms;
86+
},
87+
start: function start(initializedSequence) {
88+
this.playingSeq = setInterval(() => {
89+
this.triggerSounds(initializedSequence[this.playHead]);
90+
this.playHead++;
91+
if (this.playHead === initializedSequence.length) {
92+
this.playHead = 0;
93+
}
94+
}, this.tempo);
95+
},
96+
stop: function stop(m) {
97+
if (m === undefined) {
98+
clearInterval(this.playingSeq);
99+
}
100+
setTimeout(() => {
101+
clearInterval(this.playingSeq);
102+
}, m);
103+
},
104+
triggerSounds: function triggerSounds(dmState) {
105+
for (let instrument in dmState) {
106+
if (dmState.hasOwnProperty(instrument)) {
107+
if (dmState[instrument].on) {
108+
console.log(dmState.getState(instrument).location);
109+
player.play(dmState.getState(instrument).location, {
110+
afplay: ["-v", dmState.getState(instrument).volume]
111+
});
112+
}
113+
}
114+
}
115+
}
116+
};
117+
118+
const sequencer = new Sequencer(DrumMachineState, 8);
119+
const seq = sequencer.initSeq();
120+
121+
seq[0].setState("kick", true, 1);
122+
seq[4].setState("kick", true, 0.8);
123+
seq[4].setState("clap", true);
124+
seq[2].setState("hat", true, 0.2);
125+
seq[6].setState("hat", true, 0.24);
126+
seq[0].setState("shaker", true, 0.2);
127+
seq[1].setState("shaker", true, 0.1);
128+
seq[2].setState("shaker", true, 0.2);
129+
seq[3].setState("shaker", true, 0.1);
130+
seq[4].setState("shaker", true, 0.2);
131+
seq[5].setState("shaker", true, 0.1);
132+
seq[6].setState("shaker", true, 0.2);
133+
seq[7].setState("shaker", true, 0.1);
134+
seq[1].setState("perc", true, 0.3);
135+
seq[4].setState("perc", true, 0.4);
136+
seq[5].setState("perc", true, 0.3);
137+
seq[2].setState("perc2", true, 0.3);
138+
seq[3].setState("perc2", true, 0.5);
139+
seq[7].setState("perc2", true, 0.25);
140+
141+
dm.setTempo(127, 4);
142+
dm.start(seq);

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "drum-machine-v0.1",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "drum-machine.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"play-sound": "^1.1.3"
13+
}
14+
}
44.6 KB
Binary file not shown.
128 KB
Binary file not shown.
133 KB
Binary file not shown.
130 KB
Binary file not shown.
64 KB
Binary file not shown.
40.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)