-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtempo.js
More file actions
461 lines (411 loc) · 14.7 KB
/
tempo.js
File metadata and controls
461 lines (411 loc) · 14.7 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Copyright (c) 2016-21 Walter Bender
// Copyright (c) 2016 Hemant Kasat
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
// This widget enable us to manipulate the beats per minute. It
// behaves like a metronome and updates the master BPM block.
/*
global
_, getDrumSynthName, Singer, TONEBPM
*/
/*
Global locations
js/utils/musicutils.js
getDrumSynthName
js/turtle-singer.js
Singer
js/utils/utils.js
_
js/logoconstants.js
TONEBPM
*/
/* exported Tempo */
class Tempo {
static TEMPOSYNTH = "bottle";
static TEMPOINTERVAL = 5;
static BUTTONDIVWIDTH = 476; // 8 buttons 476 = (55 + 4) * 8
static BUTTONSIZE = 53;
static ICONSIZE = 32;
static TEMPOWIDTH = 700;
static TEMPOHEIGHT = 100;
static YRADIUS = 75;
constructor() {
this._xradius = Tempo.YRADIUS / 3;
this.BPMs = [];
this.BPMInputs = [];
this.BPMBlocks = [];
this.tempoCanvases = [];
}
init(activity) {
this.activity = activity;
this._directions = [];
this._widgetFirstTimes = [];
this._widgetNextTimes = [];
this._firstClickTimes = null;
this._intervals = [];
this.isMoving = true;
if (this._intervalID != undefined && this._intervalID != null) {
clearInterval(this._intervalID);
}
this._intervalID = null;
this.activity.logo.synth.loadSynth(0, getDrumSynthName(Tempo.TEMPOSYNTH));
if (this._intervalID != null) {
clearInterval(this._intervalID);
}
const widgetWindow = window.widgetWindows.windowFor(this, "tempo", "tempo", true);
this.widgetWindow = widgetWindow;
widgetWindow.clear();
widgetWindow.show();
widgetWindow.onclose = () => {
if (this._intervalID != null) {
clearInterval(this._intervalID);
}
widgetWindow.destroy();
};
const pauseBtn = widgetWindow.addButton("pause-button.svg", Tempo.ICONSIZE, _("Pause"));
pauseBtn.onclick = () => {
if (this.isMoving) {
this.pause();
pauseBtn.innerHTML = `<img
src="header-icons/play-button.svg"
title="${_("Play")}"
alt="${_("Play")}"
height="${Tempo.ICONSIZE}"
width="${Tempo.ICONSIZE}"
vertical-align="middle"
>`;
this.isMoving = false;
} else {
this.resume();
pauseBtn.innerHTML = `<img
src="header-icons/pause-button.svg"
title="${_("Pause")}"
alt="${_("Pause")}"
height="${Tempo.ICONSIZE}"
width="${Tempo.ICONSIZE}"
vertical-align="middle"
>`;
this.isMoving = true;
}
};
this._save_lock = false;
widgetWindow.addButton("export-chunk.svg", Tempo.ICONSIZE, _("Save tempo"), "").onclick =
() => {
// Debounce button
if (!this._get_save_lock()) {
this._save_lock = true;
this._saveTempo();
setTimeout(() => (this._save_lock = false), 1000);
}
};
this.bodyTable = document.createElement("table");
this.widgetWindow.getWidgetBody().appendChild(this.bodyTable);
let r1, r2, r3, tcCell;
for (let i = 0; i < this.BPMs.length; i++) {
this._directions.push(1);
this._widgetFirstTimes.push(this.activity.logo.firstNoteTime);
if (this.BPMs[i] <= 0) {
this.BPMs[i] = 30;
}
this._intervals.push((60 / this.BPMs[i]) * 1000);
this._widgetNextTimes.push(this._widgetFirstTimes[i] - this._intervals[i]);
r1 = this.bodyTable.insertRow();
r2 = this.bodyTable.insertRow();
r3 = this.bodyTable.insertRow();
widgetWindow.addButton(
"up.svg",
Tempo.ICONSIZE,
_("speed up"),
r1.insertCell()
).onclick = (
i => () =>
this.speedUp(i)
)(i);
widgetWindow.addButton(
"down.svg",
Tempo.ICONSIZE,
_("slow down"),
r2.insertCell()
).onclick = (
i => () =>
this.slowDown(i)
)(i);
this.BPMInputs[i] = widgetWindow.addInputButton(this.BPMs[i], r3.insertCell());
this.tempoCanvases[i] = document.createElement("canvas");
this.tempoCanvases[i].style.width = Tempo.TEMPOWIDTH + "px";
this.tempoCanvases[i].style.height = Tempo.TEMPOHEIGHT + "px";
this.tempoCanvases[i].style.margin = "1px";
this.tempoCanvases[i].style.background = "rgba(255, 255, 255, 1)";
tcCell = r1.insertCell();
tcCell.appendChild(this.tempoCanvases[i]);
tcCell.setAttribute("rowspan", "3");
// The tempo can be set from the interval between successive clicks on the canvas.
this.tempoCanvases[i].onclick = (id => () => {
const d = new Date();
let newBPM, BPMInput;
if (this._firstClickTime == null) {
this._firstClickTime = d.getTime();
} else {
newBPM = parseInt((60 * 1000) / (d.getTime() - this._firstClickTime));
if (newBPM > 29 && newBPM < 1001) {
this.BPMs[id] = newBPM;
this._updateBPM(id);
BPMInput = this.BPMInputs[id];
BPMInput.value = this.BPMs[id];
this._firstClickTime = null;
} else {
this._firstClickTime = d.getTime();
}
}
})(i);
this.BPMInputs[i].addEventListener(
"keyup",
(id => e => {
if (e.keyCode === 13) {
this._useBPM(id);
}
})(i)
);
}
activity.textMsg(_("Adjust the tempo with the buttons."), 3000);
this.resume();
widgetWindow.sendToCenter();
}
/**
* @private
* @param {number} i
* @returns {void}
*/
_updateBPM(i) {
this._intervals[i] = (60 / this.BPMs[i]) * 1000;
if (this.BPMBlocks[i] == null) return;
const bpmBlock = this.activity.blocks.blockList[this.BPMBlocks[i]];
const blockNumber = bpmBlock.connections[1];
if (blockNumber != null) {
this.activity.blocks.blockList[blockNumber].value = parseFloat(this.BPMs[i]);
this.activity.blocks.blockList[blockNumber].text.text = this.BPMs[i];
this.activity.blocks.blockList[blockNumber].updateCache();
this.activity.refreshCanvas();
this.activity.saveLocally();
}
const bpmValue = parseFloat(this.BPMs[i]);
if (bpmBlock.name === "setmasterbpm2" || bpmBlock.name === "setmasterbpm") {
Singer.masterBPM = bpmValue;
Singer.defaultBPMFactor = TONEBPM / bpmValue;
} else if (bpmBlock.name === "setbpm3" || bpmBlock.name === "setbpm2") {
for (const tur of this.activity.turtles.turtleList) {
if (tur.singer.bpm.length > 0) {
tur.singer.bpm[tur.singer.bpm.length - 1] = bpmValue;
}
}
}
}
/**
* @public
* @returns {void}
*/
pause() {
clearInterval(this._intervalID);
}
/**
* @public
* @returns {void}
*/
resume() {
// Reset widget time since we are restarting. We will no longer keep synch with the turtles.
const d = new Date();
for (let i = 0; i < this.BPMs.length; i++) {
this._widgetFirstTimes[i] = d.getTime();
this._widgetNextTimes[i] = this._widgetFirstTimes[i] + this._intervals[i];
this._directions[i] = 1;
}
// Restart the interval.
if (this._intervalID !== null) {
clearInterval(this._intervalID);
}
this._intervalID = setInterval(() => {
this._draw();
}, Tempo.TEMPOINTERVAL);
}
/**
* @private
* @param {number} i
* @returns {void}
*/
_useBPM(i) {
const input = this.BPMInputs[i].value;
if (isNaN(input)) {
activity.errorMsg(_("Please enter a number between 30 and 1000"), 3000);
return;
}
this.BPMs[i] = this.BPMInputs[i].value;
if (this.BPMs[i] > 1000) {
this.BPMs[i] = 1000;
activity.errorMsg(_("The beats per minute must be between 30 and 1000."), 3000);
} else if (this.BPMs[i] < 30) {
this.BPMs[i] = 30;
activity.errorMsg(_("The beats per minute must be between 30 and 1000."), 3000);
}
this._updateBPM(i);
this.BPMInputs[i].value = this.BPMs[i];
}
/**
* @public
* @param {number} i
* @returns {void}
*/
speedUp(i) {
this.BPMs[i] = parseFloat(this.BPMs[i]) + Math.round(0.1 * this.BPMs[i]);
if (this.BPMs[i] > 1000) {
activity.errorMsg(_("The beats per minute must be below 1000."), 3000);
this.BPMs[i] = 1000;
}
this._updateBPM(i);
this.BPMInputs[i].value = this.BPMs[i];
}
/**
* @public
* @param {number} i
* @returns {void}
*/
slowDown(i) {
this.BPMs[i] = parseFloat(this.BPMs[i]) - Math.round(0.1 * this.BPMs[i]);
if (this.BPMs[i] < 30) {
activity.errorMsg(_("The beats per minute must be above 30"), 3000);
this.BPMs[i] = 30;
}
this._updateBPM(i);
this.BPMInputs[i].value = this.BPMs[i];
}
/**
* @private
* @returns {void}
*/
_draw() {
// First thing to do is figure out where we are supposed to be based on the elapsed time.
const d = new Date();
let tempoCanvas, deltaTime, dx, x, ctx;
for (let i = 0; i < this.BPMs.length; i++) {
tempoCanvas = this.tempoCanvases[i];
if (!tempoCanvas) continue;
// We start the music clock as the first note is being played.
if (this._widgetFirstTimes[i] == null) {
this._widgetFirstTimes[i] = d.getTime();
this._widgetNextTimes[i] = this._widgetFirstTimes[i] + this._intervals[i];
}
// How much time has gone by?
deltaTime = this._widgetNextTimes[i] - d.getTime();
// Are we done yet?
if (d.getTime() > this._widgetNextTimes[i]) {
// Play a tone.
this.activity.logo.synth.trigger(
0,
["C2"],
0.0625,
Tempo.TEMPOSYNTH,
null,
null,
false
);
this._widgetNextTimes[i] += this._intervals[i];
// Ensure we are at the edge.
if (this._directions[i] === -1) {
this._directions[i] = 1;
} else {
this._directions[i] = -1;
}
} else {
// Determine new x position based on delta time.
if (this._intervals[i] !== 0) {
dx = tempoCanvas.width * (deltaTime / this._intervals[i]);
} else {
dx = 0;
}
// Set this._xradius based on the dx to achieve the compressing effect
if (tempoCanvas.width - dx <= Tempo.YRADIUS / 3) {
this._xradius = tempoCanvas.width - dx;
} else if (dx <= Tempo.YRADIUS / 3) {
this._xradius = dx;
} else {
this._xradius = Tempo.YRADIUS / 3;
}
// Set x based on dx and direction
if (this._directions[i] === -1) {
x = tempoCanvas.width - dx;
} else {
x = dx;
}
}
// Set x value if it is undefined
if (x === undefined) {
if (this._directions[i] === -1) {
x = 0;
} else {
x = tempoCanvas.width;
}
}
ctx = tempoCanvas.getContext("2d");
ctx.clearRect(0, 0, tempoCanvas.width, tempoCanvas.height);
ctx.beginPath();
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.ellipse(
x,
Tempo.YRADIUS,
Math.max(this._xradius, 1),
Tempo.YRADIUS,
0,
0,
Math.PI * 2
);
ctx.fill();
ctx.closePath();
}
}
/**
* @private
* @param {number} i
* @returns {void}
*/
__save(i) {
setTimeout(() => {
// console.debug("saving a BPM block for " + this.BPMs[i]);
const delta = i * 42;
const newStack = [
[0, ["setbpm3", {}], 100 + delta, 100 + delta, [null, 1, 2, 5]],
[1, ["number", { value: this.BPMs[i] }], 0, 0, [0]],
[2, ["divide", {}], 0, 0, [0, 3, 4]],
[3, ["number", { value: 1 }], 0, 0, [2]],
[4, ["number", { value: 4 }], 0, 0, [2]],
[5, ["vspace", {}], 0, 0, [0, null]]
];
this.activity.blocks.loadNewBlocks(newStack);
activity.textMsg(_("New action block generated."), 3000);
}, 200 * i);
}
/**
* @private
* @returns {void}
*/
_saveTempo() {
// Save a BPM block for each tempo.
for (let i = 0; i < this.BPMs.length; i++) {
this.__save(i);
}
}
/**
* @private
* @returns {HTMLElement}
*/
_get_save_lock() {
return this._save_lock;
}
}
if (typeof module !== "undefined") {
module.exports = Tempo;
}