-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
330 lines (301 loc) · 9.98 KB
/
run.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
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
328
329
330
"use strict";
const pianoPath = "M4.57764e-05 112.676C3.86599e-05 90.6737 0 73 8.5 52.5C17 32 33.9306 20.4349 39 17.5C48.5 12 70 -3.05176e-05 110 -1.52587e-05C150 0 192 16 216.5 46.5C235 69.5 244.5 88 247 133.5C249.5 179 253.5 188.5 268.5 213C283.5 237.5 347.5 265 355.5 284.5C363.5 304 360 394 360 394H4.57764e-05C4.57764e-05 394 6.10352e-05 159.851 4.57764e-05 112.676Z";
document.body.append(
GSUcreateDiv( { id: "head" },
GSUcreateElement( "gsui-analysertime", { color: "#ddd", pinch: 1, amp: 10 } ),
GSUcreateDiv( { id: "title" },
GSUcreateSpan( null, "Piano" ),
GSUcreateSpan( null, "by GridSound" ),
),
),
GSUcreateDiv( { id: "myPiano" },
GSUcreateDiv( { id: "pianoTop" },
GSUcreateElementSVG( "svg", { id: "pianoSVGGradients" },
GSUcreateElementSVG( "defs", null,
GSUcreateElementSVG( "linearGradient", { id: "pianoGradient", x1: "0%", x2: "0%", y1: "0%", y2: "100%" },
GSUcreateElementSVG( "stop", { offset: "50%", "stop-color": "#00000040" } ),
GSUcreateElementSVG( "stop", { offset: "100%", "stop-color": "#00000000" } ),
),
GSUcreateElementSVG( "linearGradient", { id: "pianoGradient2", x1: "100%", x2: "0%", y1: "0%", y2: "0%" },
GSUcreateElementSVG( "stop", { offset: "50%", "stop-color": "#00000030" } ),
GSUcreateElementSVG( "stop", { offset: "100%", "stop-color": "#00000000" } ),
),
),
),
GSUcreateElementSVG( "svg", { viewBox: "0 0 361 394", preserveAspectRatio: "none" },
GSUcreateElementSVG( "path", { d: pianoPath } ),
),
GSUcreateElementSVG( "svg", { viewBox: "0 0 361 394", preserveAspectRatio: "none" },
GSUcreateElementSVG( "path", { d: pianoPath } ),
),
),
GSUcreateDiv( { id: "velocityLabel" },
GSUcreateI( { class: "gsuiIcon", "data-icon": "arrow-up" } ),
GSUcreateSpan( null, "velocity" ),
GSUcreateI( { class: "gsuiIcon", "data-icon": "arrow-down" } ),
),
GSUcreateDiv( { id: "niceBorder" },
GSUcreateElement( "gsui-keys", { orient: "horizontal", rootoctave: 2, octaves: "1 4" } ),
GSUcreateDiv( { id: "octaves-loaders" },
GSUcreateDiv( { class: "octave-loader", style: { animationDelay: ".0s" } } ),
GSUcreateDiv( { class: "octave-loader", style: { animationDelay: ".05s" } } ),
GSUcreateDiv( { class: "octave-loader", style: { animationDelay: ".10s" } } ),
GSUcreateDiv( { class: "octave-loader", style: { animationDelay: ".15s" } } ),
GSUcreateDiv( { class: "octave-loader", style: { animationDelay: ".20s" } } ),
GSUcreateDiv( { class: "octave-loader", style: { animationDelay: ".25s" } } ),
GSUcreateDiv( { class: "octave-loader", style: { animationDelay: ".30s" } } ),
),
),
),
GSUcreateDiv( { id: "analyserBottom" } ),
GSUcreateDiv( { id: "foot" },
GSUcreateDiv( { id: "readme" },
GSUcreateSpan( null, "The raw audio samples come from a Piano " ),
GSUcreateI( null, "Steinway & Sons model B" ),
GSUcreateSpan( null, " recorded at " ),
GSUcreateAExt( { href: "https://theremin.music.uiowa.edu/MISpiano.html" }, "the university of Iowa" ),
GSUcreateSpan( null, " — " ),
GSUcreateAExt( { href: "https://github.com/gridsound/piano" }, "source code" ),
),
GSUcreateSpan( { id: "copyright" },
`© ${ ( new Date() ).getFullYear() } `,
GSUcreateA( { href: "https://gridsound.com" }, "gridsound.com" ),
" all rights reserved",
),
),
);
const root = document.querySelector( "#myPiano" );
const head = document.querySelector( "#head" );
const uiKeys = document.querySelector( "gsui-keys" );
const uiAnTime = document.querySelector( "gsui-analysertime" );
const anBottom = document.querySelector( "#analyserBottom" );
const octavesLoadersWrap = document.querySelector( "#octaves-loaders" );
const octavesLoaders = octavesLoadersWrap.getElementsByClassName( "octave-loader" );
const midiManager = new gswaMIDIControllersManager();
const szKey = GSUonMobile ? 24 : 16;
const octaveBuffers = {};
const keyboardDown = {};
const octaveURLs = {
1: "assets/🎹/21-35.96k.mp3",
2: "assets/🎹/36-47.96k.mp3",
3: "assets/🎹/48-59.96k.mp3",
4: "assets/🎹/60-71.96k.mp3",
5: "assets/🎹/72-83.96k.mp3",
6: "assets/🎹/84-95.96k.mp3",
7: "assets/🎹/96-108.96k.mp3",
};
const absnMap = new Map();
const ctx = new AudioContext();
const analyserNode = ctx.createAnalyser();
const analyserFFTSize = 512;
const analyserData = new Uint8Array( analyserFFTSize / 2 );
let nbOct = 0;
const keysMap88 = {
// assets/🎹/21-35
21: [ 1, 14, 14 * 0 ],
22: [ 1, 14, 14 * 1 ],
23: [ 1, 14, 14 * 2 ],
24: [ 1, 14, 14 * 3 ],
25: [ 1, 14, 14 * 4 ],
26: [ 1, 14, 14 * 5 ],
27: [ 1, 14, 14 * 6 ],
28: [ 1, 14, 14 * 7 ],
29: [ 1, 14, 14 * 8 ],
30: [ 1, 14, 14 * 9 ],
31: [ 1, 14, 14 * 10 ],
32: [ 1, 14, 14 * 11 ],
33: [ 1, 14, 14 * 12 ],
34: [ 1, 14, 14 * 13 ],
35: [ 1, 14, 14 * 14 ],
// assets/🎹/36-47
36: [ 2, 12, 12 * 0 ],
37: [ 2, 12, 12 * 1 ],
38: [ 2, 12, 12 * 2 ],
39: [ 2, 12, 12 * 3 ],
40: [ 2, 12, 12 * 4 ],
41: [ 2, 12, 12 * 5 ],
42: [ 2, 12, 12 * 6 ],
43: [ 2, 12, 12 * 7 ],
44: [ 2, 12, 12 * 8 ],
45: [ 2, 12, 12 * 9 ],
46: [ 2, 12, 12 * 10 ],
47: [ 2, 12, 12 * 11 ],
// assets/🎹/48-59
48: [ 3, 10, 10 * 0 ],
49: [ 3, 10, 10 * 1 ],
50: [ 3, 10, 10 * 2 ],
51: [ 3, 10, 10 * 3 ],
52: [ 3, 10, 10 * 4 ],
53: [ 3, 10, 10 * 5 ],
54: [ 3, 10, 10 * 6 ],
55: [ 3, 10, 10 * 7 ],
56: [ 3, 10, 10 * 8 ],
57: [ 3, 10, 10 * 9 ],
58: [ 3, 10, 10 * 10 ],
59: [ 3, 10, 10 * 11 ],
// assets/🎹/60-71
60: [ 4, 8, 8 * 0 ],
61: [ 4, 8, 8 * 1 ],
62: [ 4, 8, 8 * 2 ],
63: [ 4, 8, 8 * 3 ],
64: [ 4, 8, 8 * 4 ],
65: [ 4, 8, 8 * 5 ],
66: [ 4, 8, 8 * 6 ],
67: [ 4, 8, 8 * 7 ],
68: [ 4, 8, 8 * 8 ],
69: [ 4, 8, 8 * 9 ],
70: [ 4, 8, 8 * 10 ],
71: [ 4, 8, 8 * 11 ],
// assets/🎹/72-83
72: [ 5, 6, 6 * 0 ],
73: [ 5, 6, 6 * 1 ],
74: [ 5, 6, 6 * 2 ],
75: [ 5, 6, 6 * 3 ],
76: [ 5, 6, 6 * 4 ],
77: [ 5, 6, 6 * 5 ],
78: [ 5, 5, 6 * 6 + 5 * 0 ],
79: [ 5, 5, 6 * 6 + 5 * 1 ],
80: [ 5, 5, 6 * 6 + 5 * 2 ],
81: [ 5, 5, 6 * 6 + 5 * 3 ],
82: [ 5, 5, 6 * 6 + 5 * 4 ],
83: [ 5, 5, 6 * 6 + 5 * 5 ],
// assets/🎹/84-95
84: [ 6, 4, 4 * 0 ],
85: [ 6, 4, 4 * 1 ],
86: [ 6, 4, 4 * 2 ],
87: [ 6, 4, 4 * 3 ],
88: [ 6, 4, 4 * 4 ],
89: [ 6, 4, 4 * 5 ],
90: [ 6, 3, 4 * 6 + 3 * 0 ],
91: [ 6, 3, 4 * 6 + 3 * 1 ],
92: [ 6, 3, 4 * 6 + 3 * 2 ],
93: [ 6, 3, 4 * 6 + 3 * 3 ],
94: [ 6, 3, 4 * 6 + 3 * 4 ],
95: [ 6, 3, 4 * 6 + 3 * 5 ],
// assets/🎹/96-108
96: [ 7, 2, 2 * 0 ],
97: [ 7, 2, 2 * 1 ],
98: [ 7, 2, 2 * 2 ],
99: [ 7, 2, 2 * 3 ],
100: [ 7, 2, 2 * 4 ],
101: [ 7, 2, 2 * 5 ],
102: [ 7, 2, 2 * 6 ],
103: [ 7, 2, 2 * 7 ],
104: [ 7, 2, 2 * 8 ],
105: [ 7, 2, 2 * 9 ],
106: [ 7, 2, 2 * 10 ],
107: [ 7, 2, 2 * 11 ],
108: [ 7, 2, 2 * 12 ],
};
analyserNode.fftSize = analyserFFTSize;
analyserNode.smoothingTimeConstant = 0;
function frame() {
analyserNode.getByteTimeDomainData( analyserData );
uiAnTime.$draw( analyserData );
requestAnimationFrame( frame );
}
frame();
uiKeys.style.fontSize = `${ szKey }px`;
gsuiKeys.$keyNotation( "CDEFGAB" );
navigator.requestMIDIAccess( { sysex: true } )
.then( midiAccess => {
midiManager.$initMidiAccess( midiAccess );
midiManager.$setPianorollKeys( uiKeys );
} );
document.body.onresize = onresize;
document.body.onclick = () => {
if ( ctx.state !== "running" ) {
ctx.resume();
}
};
document.body.onkeydown = e => {
const key = uiKeys.$getMidiKeyFromKeyboard( e );
if ( key !== false && !( key in keyboardDown ) ) {
keyboardDown[ key ] = true;
uiKeys.$midiKeyDown( key );
}
};
document.body.onkeyup = e => {
const key = uiKeys.$getMidiKeyFromKeyboard( e );
if ( key !== false ) {
delete keyboardDown[ key ];
uiKeys.$midiKeyUp( key );
}
};
GSUlistenEvents( uiKeys, {
gsuiKeys: {
keyDown: d => startKey( ...d.args ),
keyUp: d => stopKey( ...d.args ),
}
} );
onresize();
function onresize() {
const sz = document.body.clientWidth - 80;
const nbOct2 = Math.max( 1, Math.min( Math.floor( sz / ( 12 * szKey ) ), 7 ) );
if ( nbOct !== nbOct2 ) {
if ( ( nbOct === 1 ) !== ( nbOct2 === 1 ) ) {
( nbOct2 === 1 ? anBottom : head ).prepend( uiAnTime );
}
nbOct = nbOct2;
GSUsetAttribute( uiKeys, "octaves", `1 ${ nbOct }` );
for ( let i = 0; i < octavesLoaders.length; ++i ) {
const needed = i < nbOct;
octavesLoaders[ i ].classList.toggle( "octave-needed", needed );
if ( needed && !octaveBuffers[ i + 1 ] ) {
dlOctave( i + 1 );
}
}
head.style.width =
anBottom.style.width = `${ uiKeys.parentNode.clientWidth }px`;
}
}
function dlOctave( oct ) {
const url = octaveURLs[ oct ];
const loadCl = octavesLoaders[ oct - 1 ].classList;
loadCl.add( "octave-loading" );
return fetch( url )
.then( res => res.arrayBuffer() )
.then( arr => ctx.decodeAudioData( arr ) )
.then( buf => {
octaveBuffers[ oct ] = buf;
loadCl.remove( "octave-loading" );
return [ buf, ...GSUsplitNums( url.split( "/" )[ 2 ].split( "." )[ 0 ], "-" ) ];
} );
}
function startKey( key, vel ) {
const key2 = key + 12;
const [ octave, bufDur, bufOff ] = keysMap88[ key2 ];
const buf = octaveBuffers[ octave ];
const vel2 = Math.max( .001, GSUeaseOutCirc( ( .2 + vel ) * ( 1 / 1.2 ) ) );
const absn = ctx.createBufferSource();
const gain = ctx.createGain();
const lowp = ctx.createBiquadFilter();
const freq = 440 * 2 ** ( ( ( key2 ) - 69 ) / 12 );
const filt = ( freq + 5000 * GSUeaseInCirc( vel2 ) ) / ( 1 / vel2 );
// lg(vel, vel2)
absn.buffer = buf;
lowp.type = "lowpass";
lowp.Q.setValueAtTime( 0, ctx.currentTime );
lowp.frequency.setValueAtTime( filt, ctx.currentTime );
gain.gain.setValueAtTime( vel2, ctx.currentTime );
gain.connect( analyserNode );
absn
.connect( lowp )
.connect( gain )
.connect( ctx.destination );
absn.start( 0, bufOff, bufDur );
absnMap.set( key2, [ absn, gain, lowp, vel2 ] );
}
function stopKey( key ) {
const key2 = key + 12;
const absnData = absnMap.get( key2 );
if ( absnData ) {
const [ absn, gain, lowp, vel ] = absnData;
gain.gain.setValueCurveAtTime( new Float32Array( [ vel, 0 ] ), ctx.currentTime, .35 );
setTimeout(() => {
lowp.disconnect();
gain.disconnect();
absn.stop();
}, .6 * 1000 );
}
}