-
Notifications
You must be signed in to change notification settings - Fork 0
/
julerytmeren.js
116 lines (97 loc) · 2.38 KB
/
julerytmeren.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
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
var BREAK;
var keynum;
var BPM, UPBEAT, DOWNBEAT = 80;
var TIMEQUEUE = [];
var sound = new Howl({
urls: ['JINGLES.wav'],
sprite: {
jingle: [0, 500],
slightjingle: [1000, 500]
},
volume: 0.8
});
var timeout0, timeout1;
Howl.prototype.lowerVolume = function() {
this.volume -= 0.1;
}
Howl.prototype.higherVolume = function() {
this.volume += 0.1;
}
function init(){
document.addEventListener('keydown', function(event) {
keynum = event.keyCode;
input(keynum);
$('#box').text(keynum);
});
document.addEventListener('keyup', function(event) {
if (event.keyCode == 16){
BREAK = false;
soundloop();
}
});
for(var i = 0; i<8; i++){
TIMEQUEUE.push(Date.now());
}
}
function soundloop(){
console.log(UPBEAT);
console.log(DOWNBEAT);
if (BREAK == true) {return;}
sound.play('jingle');
timeout0 = setTimeout(function(){sound.play('slightjingle')}, UPBEAT);
timeout1 = setTimeout(soundloop, DOWNBEAT);
}
function input(keynum){
switch(keynum) {
case 32: // space for tap!
time = Date.now();
TIMEQUEUE.push(time);
TIMEQUEUE.shift();
calculateBPM();
break;
case 38: // pil op
sound.volume(sound.volume() + 0.05);
break;
case 40: // pil ned
sound.volume(sound.volume() - 0.05);
break;
case 16: // BREAK-ned
BREAK = true;
break;
case 82: // BREAK-ned
BREAK = true;
break;
default: {}
}
}
function calculateBPM(){
var deltas = 0;
for(var i = 0; i < TIMEQUEUE.length - 1; i++){
deltas += TIMEQUEUE[i+1] - TIMEQUEUE[i]
}
deltas = deltas / 1000;
newBPM = (60.0 / deltas) * 7;
BPM = newBPM.toFixed(1);
DOWNBEAT = (60000 / BPM).toFixed(1);
UPBEAT = (60000 / BPM).toFixed(1);
$('#bpm').val(BPM.toString());
clearTimeout(timeout0);
clearTimeout(timeout1);
soundloop();
}
window.onload = init;
function setBPM(string){
switch(string) {
case "double":
BPM = (BPM * 2).toFixed(1);
break;
case "half":
BPM = (BPM / 2).toFixed(1);
break;
default:
BPM = parseFloat($('#bpm').val()).toFixed(1);
}
DOWNBEAT = (60000 / BPM * 2).toFixed(1);
UPBEAT = (60000 / BPM).toFixed(1);
$('#bpm').val(BPM.toString());
}