-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch.html
More file actions
147 lines (123 loc) · 3.9 KB
/
scratch.html
File metadata and controls
147 lines (123 loc) · 3.9 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>scratch</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css">
<!-- <script src="beat.js" ></script> -->
<script>
var count = 0;
var bpm = 70
var stop = false;
var wrAF = window.requestAnimationFrame
window['beatMark'] = '0'
</script>
<script>
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: 2000}) => {
if(!start) start = timestamp;
let progress = timestamp - start;
cb(progress);
if(progress < options.dur){ id = window.requestAnimationFrame(t => st(t, cb, options)) }
}
//
const sixteenths = ["", "e", "&", "a"]
let phaseLen = 16
let currentValue = 0
let eanda = null
let onBpms = bpms => progress => {
// console.log(Math.round(progress) % Math.round(bpms))
let beat = progress / bpms
let nextValue = Math.round( progress / bpms ) + 1
if (nextValue <= phaseLen) {
if (currentValue !== Math.round( progress / bpms ) + 1){
currentValue = nextValue
}
let sixteenth = beat === 0 ? 0 :Math.round( progress / (bpms / 4) + 1 ) % 4
if ( eanda !== sixteenth ) {
// console.log(nextValue + sixteenths[sixteenth] )
window['beatMark'] = `${nextValue}${sixteenths[sixteenth]}`
eanda = sixteenth
return nextValue + sixteenths[sixteenth]
}
}
}
</script>
<script>
const handleChange = ({target: {id, value}}) => {
document.getElementById(id).value = value
bpm = value
}
</script>
<script>
/**
* Promise thrower
* options= { bpm: number, phaseLength }
*/
const promiseThrower = (options={bpm}) => async (currentMark, mark, cb) => {
const sixteenthMs = bpmToMs(bpm) / 4
findMarkTime = m => {
let [ q, s ] = m.split('')
let quarterNote = parseInt(q)
let sixteenthNote = s ? 0 : sixteenths.indexOf(s)
return (bpmToMs(bpm) * quarterNote + sixteenthMs * sixteenthNote )
}
let timeTilMark = findMarkTime(mark) - findMarkTime(currentMark)
const promise1 = new Promise(function(resolve, reject) {
setTimeout(() => {
resolve(() => cb('here', timeTilMark))
}, timeTilMark);
});
if (currentMark === mark ) {
return promise1
}
}
</script>
</head>
<body>
<h1 style="color:white; text-align: center" id="heading" ></h1>
<h1 style="color:white; text-align: center" id="heading" ></h1>
<input id="bpm_input" type="number" value="140" onchange="handleChange(event)"></input>
<script>
num = document.getElementById('heading')
num.innerText = count
let filter = async (progress) => {
let onBeat = onBpms(bpmToMs(bpm))(progress)
if ( onBeat ) {
beat = onBeat
num.innerText = beat
let abc = await promiseThrower({bpm: bpm})(beat, '3e', console.log)
if ( abc ) {
abc()
}
}
};
window.requestAnimationFrame(t => st(t, filter, {dur: (bpmToMs(bpm) * phaseLen)}))
const promiseStep = (mark, cb) => new Promise((resolve, reject) => {
console.log( 'promiseStep' )
let step = () => {
if (window['beatMark'] === mark) {
cb(`_${mark}_`)
resolve()
}
if(window['beatMark'] !== mark || stop){
window.requestAnimationFrame(step)
}
}
window.requestAnimationFrame(step)
})
promiseStep('4a', console.log).then(function(cb) {
})
</script>
</body>
</html>