-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeat.js
More file actions
72 lines (57 loc) · 1.91 KB
/
beat.js
File metadata and controls
72 lines (57 loc) · 1.91 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
/*
1. set up a reliable timing system that can be set to a bpm
- performance.now, requestAnimationFrames etc.
2. pass a callback that is triggered on the beat.
- return a promise
3. incorporate Symbols somewhere.
*/
//console.clear()
const bpmToMs = (bpm, options={decimals: 2}) => {
if (options.decimals > 14) { console.warn('max decimals is 14')}
let beatsPerMs = 60 / bpm * 1000
let decimalPlaces = 10**options.decimals
return Math.round(beatsPerMs * decimalPlaces) / decimalPlaces
}
//
let id=0;
let start=null;
let st = (timestamp, cb, options={dur: 3428.56}) => {
if(!start) start = timestamp;
let progress = timestamp - start;
cb(progress);
if(progress < options.dur){ id = window.requestAnimationFrame(t => st(t, cb)) }
}
//
const sixteenths = ["", "e", "&", "a"]
let phaseLen = 8
let currentValue = 0
let eanda = null
let onBpms = bpms => progress => {
// console.log(Math.round(progress) % Math.round(bpms))
let nextValue = Math.round( progress / bpms ) + 1
if (nextValue <= phaseLen) {
if (currentValue !== Math.round( progress / bpms ) + 1){
currentValue = nextValue
}
let sixteenth = Math.round( progress / (bpms / 4) + 1) % 4
if ( eanda !== sixteenth || nextValue === 1 ) {
console.log(nextValue + sixteenths[sixteenth] )
eanda = sixteenth
return nextValue + sixteenths[sixteenth]
}
}
}
let print = onBpms(bpmToMs(140))
window.requestAnimationFrame(t => st(t, print, {dur: bpmToMs(140) * phaseLen}))
var promiseStep = new Promise((resolve, reject) => {
let step = (timestamp, cb, options={dur: 2000}) => {
if(!start) start = timestamp;
let progress = timestamp - start;
if (window['beatMark'] === '4e') {
console.log('4e!!')
resolve('resolve 4e!')
}
if(window['beatMark'] !== '4e' || progress < options.dur){ id = window.requestAnimationFrame(t => st(t, cb)) }
}
window.requestAnimationFrame(t => step(t, cb))
})