forked from ralfpopescu/Jammercan
-
Notifications
You must be signed in to change notification settings - Fork 3
/
loops.js
54 lines (47 loc) · 1.13 KB
/
loops.js
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
var bpm = 140;
var delayPerBeat = 60 * 1000 / bpm;
var looping = {};
var chordProg1 = require('./loops/chordprog1.json');
var loopNames = ['chordProg1'];
var loops = {
chordProg1: chordProg1
};
function startLoop(loopName, emitAll) {
looping[loopName] = true;
if (loops[loopName]) {
nextNote(loops[loopName], 0, emitAll);
}
}
function stopLoop(loopName) {
looping[loopName] = false;
}
function nextNote(loop, i, emitAll) {
var name = loop.name;
if (looping[name]) {
var sequence = loop.sequence;
if (i >= sequence.length) {
i = 0;
}
var noteData = sequence[i];
var delayMs = delayPerBeat * noteData.delay;
setTimeout(function() {
emitAll(noteData.type, {id: name, note: noteData.note});
nextNote(loop, i+1, emitAll);
}, delayMs);
}
}
function getAllLoops() {
var list = [];
for (var i=0; i<loopNames.length; i++) {
var currentName = loopNames[i];
var listItem = {
name: currentName,
playing: looping[currentName]
}
list.push(listItem);
}
return list;
}
module.exports = {startLoop: startLoop,
stopLoop: stopLoop,
getAllLoops: getAllLoops};