-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path005_keyboard.html
More file actions
327 lines (255 loc) · 8.16 KB
/
Copy path005_keyboard.html
File metadata and controls
327 lines (255 loc) · 8.16 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
<!DOCTYPE HTML>
<html>
<head>
<script src="MidiConvert.js"></script>
<script src="soundfont-player.js"></script>
<script src="audio.js"></script>
<script src="player.js"></script>
<script src="keyboard.js"></script>
<!--
Vendor sources:
https://raw.githubusercontent.com/Tonejs/MidiConvert/master/build/MidiConvert.js
-->
</head>
<body>
<style>
body { background: #000; color: #eee; }
#controls {
position: absolute;
z-index: 10;
}
input {
width: 50px;
}
</style>
<div id="controls">
Size: <input type="range" value="1" min="0" max="8" step="0.1" onchange="BALL_MULTIPLIER = this.value"></input>
Time: <span id="playing_time"></span>
Progress: <input id="progress" type="range" value="0" min="0" max="1" step="0.0001"
onmousedown="PROGRESS_CHANGE = true"
onmouseup="player.seek(this.value * duration); lastPlayed = -1; PROGRESS_CHANGE = false"
onchange="this.value"></input>
Speed: <input type="range" value="1" min="0.2" max="4" step="0.2" onchange="player.speed(this.value)"></input>
<button onclick="player.pause()">Pause</button>
</div>
<script>
keyboard = new Keyboard(1, 10);
// transform-origin: center;
// transform: rotate(90deg)
/*
position: absolute;
top: 0;
left: -100px;
transform: scale(0.5, 0.5);
*/
canvas = document.createElement('canvas')
ctx = canvas.getContext('2d')
canvas.width = innerWidth
canvas.height = innerHeight
document.body.appendChild(canvas)
ctx.globalCompositeOperation = 'lighter' // lighter
min = 100000
max = 0
m = null
lastPlayed = -1
allNotes = []
NOTE_HEIGHT = 8
NOTE_RANGE = 110
// PARAMETERS
TIME_IN_Y = 40
START_X = 400 // + 400
BALL_MULTIPLIER = 1
DISPLAY_GRID = false
DISPLAY_HIT_GRID = false
PROGRESS_CHANGE = false
// move prepTime etc...
// bigger
NOTE_HEIGHT = 10; TIME_IN_Y = 200
buffer = 50
// defaults https://github.com/danigb/sample-player/blob/ebe9f25cf512f78cd338965ba524d6b47ccece38/lib/player.js#L8
_attack = 0.01 // 0.01
_decay = 0.1 // 0.01
_sustain = 0.9 // 4
_release = 0.3 // 0.01
_velMul = 1.5
_durMul = 1.5
// file = 'Prelude1.mid'
file = 'rachmaninov3.mid'
// file = 'minute_waltz.mid'
// file = 'chpn-p15.mid' // http://www.piano-midi.de/chopin.htm
// minute_waltz.mid rachmaninov3
// 0 &&
const startup = () => MidiConvert.load(file, function(midi) {
console.log(midi)
m = midi
colors = {}
const c = 0.35
const len = midi.tracks.length
midi.tracks.forEach((track, trackNo) => {
track.notes.forEach((note) => {
const { midi, time, duration, velocity } = note;
min = Math.min(midi, min)
max = Math.max(midi, max)
allNotes.push(Object.assign({ trackNo }, note))
})
const color = ((trackNo / len / 1.2 + c) * 0xffffff | 0).toString(16)
colors[trackNo] = `#${color}`
})
allNotes.sort((an, bn) => {
const a = an.time
const b = bn.time
return a < b ? -1 : a > b ? 1 : 0
})
endNote = allNotes[allNotes.length - 1]
duration = endNote.time + endNote.duration
player.play()
console.log(`${min} -> ${max}`)
})
instrument = null
// ac = new AudioContext()
// Soundfont.instrument(ac, 'acoustic_grand_piano', { soundfont: 'MusyngKite' })
// .then(function (instrument) {
// window.instrument = instrument
// })
// .then(startup)
startup();
const easings = {
easeOut(t) {
return t*(2-t)
},
easeIn(t) {
return t*t
},
cubicIn(k) {
return k * k * k;
},
cubicOut(k) {
return --k * k * k + 1;
},
linear(t) {
return t;
},
easeInOut(t) {
// quad
return t<.5 ? 2*t*t : -1+(4-2*t)*t
},
cubicInOut(t) {
return t <.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1
},
exponentialOut(k) {
return k === 1 ? 1 : 1 - Math.pow(2, - 10 * k)
},
exponentialIn(k) {
return k === 0 ? 0 : Math.pow(1024, k - 1);
},
sinInOut(k) {
return 0.5 * (1 - Math.cos(Math.PI * k))
}
};
var PREP_EASE = easings.linear;
PREP_EASE = easings.easeIn
function formatSeconds(s) {
var min = s / 60 | 0;
var secs = (s % 60).toFixed(2);
return min + ':' + secs;
}
function render() {
requestAnimationFrame(render)
keyboard.paint();
ctx.save()
// background
ctx.clearRect(0, 0, canvas.width, canvas.height)
if (typeof duration !== 'undefined') {
playing_time.innerHTML = formatSeconds(lapse) + ' ' + speed.toFixed(1) + 'x'
if (!PROGRESS_CHANGE) progress.value = lapse / duration
}
// proceed after midi is loaded
if (!playing) return ctx.restore()
const midi = m
player.update()
translateY = -lapse * TIME_IN_Y + START_X
// Math.max(lastPlayed - 50, 0)
for (let i = 0; i < allNotes.length; i++) {
const note = allNotes[i];
if (
i > lastPlayed && note.time <= lapse + 0.00) {
// allow scheduling up to
if (note.time >= lapse - 0.05) { // up until missed by 100ms, we drop it.
var drift = note.time - lapse;
setTimeout(() => keyboard.toggleNote(note.midi, true), drift * 1000)
setTimeout(() => {
keyboard.toggleNote(note.midi, false)
}, note.duration * 1000 + drift)
if (!instrument)
noteOn(note.midi, note.velocity, drift)
else
instrument.play(note.midi, ac.currentTime + drift, {
gain: note.velocity * _velMul,
duration: note.duration * _durMul,
attack: _attack,
decay: _decay,
sustain: _sustain,
release: _release
})
}
lastPlayed = i;
}
var bufferTime = 4;
// if (note.time >= lapse - 0.05 && note.time <= lapse)
// if (note.time >= lapse - 0.05 && note.time <= lapse + 0.05)
if (note.time >= lapse - bufferTime && note.time <= lapse + bufferTime)
drawNote(note)
if (i > lastPlayed + buffer) break;
}
ctx.restore()
}
player.init()
render()
function trackInY(midi) {
return NOTE_HEIGHT * (NOTE_RANGE - midi)
}
function frac(t, unit) {
return t / unit;
}
function reverseFrac(t, unit) {
return 1 - t / unit;
}
var easeInTime = 4;
var prepTime = 0.4;
function drawNote(note) {
const { midi, time, duration, velocity } = note;
// Math.max(...allNotes.map(({ velocity }) => velocity))
// quick optimization by filtering
if (time < lapse - 60 * 2 || time > lapse + 60 * 2) return
ctx.fillStyle = colors[note.trackNo]
ctx.save()
// ctx.globalAlpha = velocity
var diff = time - lapse
const f = diff > 0 ? (1 - PREP_EASE(1 - diff / easeInTime)) * easeInTime : diff
const x0 = f * TIME_IN_Y + START_X
const y0 = trackInY(midi)
const w = duration * TIME_IN_Y
// Impact 1 + size
let h = diff <= 0 ?
// on note on
velocity * 2 *
(Math.max(duration * 4, 0.1)) *
NOTE_HEIGHT * Math.min(Math.max(easings.easeIn(2 + diff / 0.5), 0), 4) :
diff < prepTime ?
// prep time
(easings.easeIn(reverseFrac(diff, prepTime)) + 1) * NOTE_HEIGHT
:
NOTE_HEIGHT
h *= BALL_MULTIPLIER
if (diff < 0) {
ctx.globalAlpha = Math.max(easings.easeIn(1 + diff / 1.4), 0);
}
// Circles
ctx.beginPath()
ctx.arc(x0, y0 + NOTE_HEIGHT / 2, h /2, 0, Math.PI * 2)
ctx.fill()
ctx.restore()
}
</script>
</body>
</html>