-
Notifications
You must be signed in to change notification settings - Fork 0
/
drumz.htm
349 lines (300 loc) · 8.92 KB
/
drumz.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>drum machine / Severák's synths</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://tilde.town/~severak/flogiston-tui.css">
<meta property="og:title" content="Severák's synths" />
<meta property="og:image" content="https://www.gravatar.com/avatar/21621c02d6a6ab7b07b4a6982b6ec68e.jpg?s=300" />
<meta property="og:type" content="website" />
<meta property="og:description" content="various synths in javascript / html5 / webmidi" />
<script src="uboot.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jzz"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/javascript/JZZ.synth.Tiny.min.js"></script>
</head>
<body>
<div class="flogiston-page">
<div class="flogiston-article">
<h1>8*3 drum machine</h1>
<div class="keyboard" id="keyboard">
<div class="row qwer">
<button id="q">Q</button>
<button id="w">W</button>
<button id="e">E</button>
<button id="r">R</button>
<button id="t">T</button>
<button id="y">Y</button>
<button id="u">U</button>
<button id="i">I</button>
<select class="instrument" data-ord="0">
<option value="36">bass</option>
<option value="40">snare</option>
<option value="45" selected>low tom</option>
<option value="64">low conga</option>
<option value="37">rimshot</option>
<option value="75">claves</option>
<option value="70">maraca</option>
<option value="39">handclap</option>
<option value="56">cowbell</option>
<option value="49">cymbal</option>
<option value="46">open hihat</option>
<option value="42">closed hihat</option>
</select>
</div>
<div class="row asdf">
<button id="a">A</button>
<button id="s">S</button>
<button id="d">D</button>
<button id="f">F</button>
<button id="g">G</button>
<button id="h">H</button>
<button id="j">J</button>
<button id="k">K</button>
<select class="instrument" data-ord="1">
<option value="36">bass</option>
<option value="40" selected>snare</option>
<option value="45">low tom</option>
<option value="64">low conga</option>
<option value="37">rimshot</option>
<option value="75">claves</option>
<option value="70">maraca</option>
<option value="39">handclap</option>
<option value="56">cowbell</option>
<option value="49">cymbal</option>
<option value="46">open hihat</option>
<option value="42">closed hihat</option>
</select>
</div>
<div class="row zxcv">
<button id="z">Z</button>
<button id="x">X</button>
<button id="c">C</button>
<button id="v">V</button>
<button id="b">B</button>
<button id="n">N</button>
<button id="m">M</button>
<button id="comma">,</button>
<select class="instrument" data-ord="2">
<option value="36" selected>bass</option>
<option value="40">snare</option>
<option value="45">low tom</option>
<option value="64">low conga</option>
<option value="37">rimshot</option>
<option value="75">claves</option>
<option value="70">maraca</option>
<option value="39">handclap</option>
<option value="56">cowbell</option>
<option value="49">cymbal</option>
<option value="46">open hihat</option>
<option value="42">closed hihat</option>
</select>
</div>
</div>
<div class="keyboard">
<div class="row">
<button id="space">play / pause</button>
</div>
</div>
<div>
<label>BPM: <input id="bpm" type="number" min="1" max="600" value="120"></label>
<label>metronome <input id="metronome" type="checkbox" value="1"></label>
<label>MIDI Out: <select id=selectMidiOut></select></label>
</div>
</div>
<hr>
<p>just one of <a href="index.htm">Severák's synths</a></p>
<p>Created with wonderful <a href="https://www.jazz-soft.net/doc/JZZ/">JZZ lib from jazz soft</a>.</p>
<style>
.keyboard .row { padding-bottom: 0.5em; }
.keyboard button { border: 1px solid lightgrey; color: lightgrey; background-color: darkblue; border-radius: 1em; width: 2em; height: 2em; }
.keyboard button.on { background-color: green; }
.keyboard button.current { border: 1px solid red; }
.keyboard #space { width: 20em; }
select, input { border: 1px solid lightgrey; color: lightgrey; background-color: darkblue; }
button:focus { border-style: double; }
</style>
<script>
// fallback synth
JZZ.synth.Tiny.register('JZZ Tiny Synth');
whenReady(function(){
// key on keyboard -> letter
var keymap = {
KeyQ: 'q',
KeyW: 'w',
KeyE: 'e',
KeyR: 'r',
KeyT: 't',
KeyY: 'y',
KeyU: 'u',
KeyI: 'i',
KeyA: 'a',
KeyS: 's',
KeyD: 'd',
KeyF: 'f',
KeyG: 'g',
KeyH: 'h',
KeyJ: 'j',
KeyK: 'k',
KeyZ: 'z',
KeyX: 'x',
KeyC: 'c',
KeyV: 'v',
KeyB: 'b',
KeyN: 'n',
KeyM: 'm',
Comma: 'comma',
Space: 'space',
};
keyord = [
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k'],
['z', 'x', 'c', 'v', 'b', 'n', 'm', 'comma'],
];
instr = [45, 40, 36];
// what is toggled
toggled = {};
var _keyElem = {};
function toggle(k)
{
toggled[k] = !toggled[k];
if (!_keyElem[k]) _keyElem[k] = gebi(k);
if (toggled[k]) {
addClass(_keyElem[k] ,'on');
} else {
delClass(_keyElem[k] ,'on');
}
}
on('keyboard', 'click', 'button', function(e){
toggle(e.target.id);
e.target.blur();
});
on(document, 'keyup', function(e){
if (keymap[e.code]) {
var k = keymap[e.code];
toggle(k);
return false; // preventDefault
}
});
on('instrument', 'change', 'select', function(e){
instr[e.target.getAttribute('data-ord')] = e.target.value;
});
var metronome = false;
on('metronome', 'change', function(){
metronome = gebi('metronome').checked;
});
// MIDI through
through = JZZ.Widget();
// from https://stackoverflow.com/a/44337628 :
/**
* Self-adjusting interval to account for drifting
*
* @param {function} workFunc Callback containing the work to be done
* for each interval
* @param {int} interval Interval speed (in milliseconds) - This
* @param {function} errorFunc (Optional) Callback to run if the drift
* exceeds interval
*/
function AdjustingInterval(workFunc, interval, errorFunc) {
var that = this;
var expected, timeout;
this.interval = interval;
this.start = function() {
expected = Date.now() + this.interval;
timeout = setTimeout(step, this.interval);
}
this.stop = function() {
clearTimeout(timeout);
}
function step() {
var drift = Date.now() - expected;
if (drift > that.interval) {
// You could have some default stuff here too...
if (errorFunc) errorFunc();
}
workFunc();
expected += that.interval;
timeout = setTimeout(step, Math.max(0, that.interval-drift));
}
}
var intervalError = function() {
console.warn('The drift exceeded the interval.');
};
var step = 0;
// scope problems - keyord + toggled + instr + through
function tick() {
delClass(keyord[0][step], 'current');
step = (step + 1) % 8;
if (toggled.space) {
addClass(keyord[0][step], 'current');
if (toggled[keyord[0][step]]) {
through.noteOn(9, instr[0]);
}
if (toggled[keyord[1][step]]) {
through.noteOn(9, instr[1]);
}
if (toggled[keyord[2][step]]) {
through.noteOn(9, instr[2]);
}
if (metronome) {
through.noteOn(9, 34);
}
}
}
var ticker = new AdjustingInterval(tick, 1000 / (120/60), intervalError);
on('space', 'click', function(){
// TODO - we start on no 0!
// TODO - starting stopnig by keyboard
toggle('space');
gebi('space').blur();
ticker.start();
});
on('bpm', 'change', function(){
var bpm = gebi('bpm').value;
ticker.stop();
ticker.interval = 1000 / (bpm / 60 );
ticker.start();
});
// populate MIDI output select
JZZ().and(function(){
var i;
for (i = 0; i < this.info().outputs.length; i++) {
selectMidiOut[i] = new Option(this.info().outputs[i].name);
}
if (!i) {
selectMidiOut[i] = new Option('Not available');
}
});
// changing MIDI output
var midiOutName = 'Not available';
var midiOutPort;
function setListbox(lb, s) {
for (var i = 0; i < lb.options.length; i++) if (lb.options[i].value == s) lb.options[i].selected = 1;
}
function onMidiOutSuccess() {
if (midiOutPort) {
midiOutPort.close();
}
midiOutPort = this;
through.connect(this);
midiOutName = this.name();
setListbox(selectMidiOut, midiOutName);
}
function onMidiOutFail() {
if (midiOutPort) through.connect(midiOutPort);
setListbox(selectMidiOut, midiOutName);
}
JZZ().openMidiOut().or(onMidiOutFail).and(onMidiOutSuccess);
function changeMidiOut() {
var name = selectMidiOut.options[selectMidiOut.selectedIndex].value;
if (name == midiOutName) return;
if (midiOutPort) through.disconnect(midiOutPort);
JZZ().openMidiOut(name).or(onMidiOutFail).and(onMidiOutSuccess);
}
selectMidiOut.addEventListener('change', changeMidiOut);
});
</script>
</div>
</div>
</body>
</html>