-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
264 lines (222 loc) · 6.66 KB
/
index.html
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
<html>
<head>
<title>Midi Player 3D</title>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script src="./js/soundfont/acoustic_grand_piano-mp3.js" type="text/javascript"></script>
<script src="./js/inc/shim/Base64.js" type="text/javascript"></script>
<script src="./js/inc/shim/Base64binary.js" type="text/javascript"></script>
<script src="./js/inc/shim/WebAudioAPI.js" type="text/javascript"></script>
<script src="./js/inc/shim/WebMIDIAPI.js" type="text/javascript"></script>
<!-- jasmid package -->
<script src="./js/inc/jasmid/stream.js"></script>
<script src="./js/inc/jasmid/midifile.js"></script>
<script src="./js/inc/jasmid/replayer.js"></script>
<!-- midi.js package -->
<script src="./js/midi/audioDetect.js" type="text/javascript"></script>
<script src="./js/midi/gm.js" type="text/javascript"></script>
<script src="./js/midi/loader.js" type="text/javascript"></script>
<script src="./js/midi/plugin.audiotag.js" type="text/javascript"></script>
<script src="./js/midi/plugin.webaudio.js" type="text/javascript"></script>
<script src="./js/midi/plugin.webmidi.js" type="text/javascript"></script>
<script src="./js/midi/player.js" type="text/javascript"></script>
<script src="./js/midi/synesthesia.js" type="text/javascript"></script>
<!-- utils -->
<script src="./js/util/dom_request_xhr.js" type="text/javascript"></script>
<script src="./js/util/dom_request_script.js" type="text/javascript"></script>
<script src="./js/song.js" type="text/javascript"></script>
<style>
body { margin: 0; }
canvas {
width: 1024px;
height: 768px;
}
</style>
</head>
<body>
<script src="js/three.min.js"></script>
<script src="js/OrbitControls.js"></script>
<script>
var scene, worldScene, camera, renderer;
var geometry, material, mesh;
var slidingKeys;//for keeping sliding keys
//88 key piano
var lowKey = 21;
var highKey = 108;
var keyboardWidth = 10;
var keyboardHeight = 1;
var keys = [];
init();
animate();
function init()
{
scene = new THREE.Scene();
worldScene = new THREE.Scene();
slidingKeys = new THREE.Group();
camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
//var mycanvas = document.getElementById("keycanvas")
renderer = new THREE.WebGLRenderer();
renderer.setSize( 1280, 720 );
renderer.setClearColor( 0xf0f0f0 );
document.body.appendChild( renderer.domElement );
//create scene
createScene();
//createLighting
createLighting();
worldScene.add(slidingKeys);
scene.add(worldScene);
worldScene.translateX(-(keyboardWidth/2));
worldScene.rotateZ(-90*3.1516/180);
worldScene.rotateY(-30*3.1516/180);
camera.position.z = 5;
// Add the orbit controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target = new THREE.Vector3(0, 0, 0);
}
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
function createKeyboard()
{
//keyboard size
w = keyboardWidth;
h = keyboardHeight;
whiteKeyCount = 0;
for(i = lowKey; i <= highKey; i++)
{
switch(i % 12)
{
case 0:
case 2:
case 4:
case 5:
case 7:
case 9:
case 11:
{
++whiteKeyCount;
}
break;
}
}
var whiteKeyWidth = (w) / whiteKeyCount;
var blackKeyWidth = whiteKeyWidth * .66666;
var blackKeyHeight = h * .66666;
var offset = -(whiteKeyWidth - blackKeyWidth) - ((whiteKeyWidth - blackKeyWidth) / 2);
var width = whiteKeyWidth;
var widthBlack = width * .6666;
var wnum = 0;
for(i = lowKey; i <= highKey; i++)
{
switch(i % 12)
{
case 0:
case 2:
case 4:
case 5:
case 7:
case 9:
case 11:
geometry = new THREE.BoxGeometry( h, whiteKeyWidth*.95, .1 );
geometry.translate(0,wnum*width,0);
material = new THREE.MeshPhongMaterial( { color: 0xffffff} );
mesh = new THREE.Mesh( geometry, material );
worldScene.add( mesh );
keys.push(mesh);
wnum++;
break;
case 1:
case 3:
case 6:
case 8:
case 10:
geometry = new THREE.BoxGeometry( blackKeyHeight, blackKeyWidth*.95, .1 );
geometry.translate((blackKeyHeight-h)/2-.01,offset + wnum*width,.05);
material = new THREE.MeshPhongMaterial( { color: 0x000000} );
mesh = new THREE.Mesh( geometry, material );
worldScene.add( mesh );
keys.push(mesh);
break;
}
}
}
function createScene()
{
createKeyboard();
}
function createLighting()
{
var light = new THREE.PointLight(0xffffff);
light.position.set(-5,10,5);
scene.add(light);
var light = new THREE.PointLight(0xffffff);
light.position.set(5,-10,-5);
scene.add(light);
}
function colorKey(note)
{
key = note - lowKey;
if(key < 0 || key >= keys.length)
return;
keys[key].material = new THREE.MeshPhongMaterial( { color: 0xff0000} );
}
function uncolorKey(note)
{
key = note - lowKey;
if(key < 0 || key >= keys.length)
return;
switch(note % 12)
{
//white key
case 0:
case 2:
case 4:
case 5:
case 7:
case 9:
case 11:
keys[key].material = new THREE.MeshPhongMaterial( { color: 0xffffff} );
break;
//black key
case 1:
case 3:
case 6:
case 8:
case 10:
keys[key].material = new THREE.MeshPhongMaterial( { color: 0x000000} );
break;
}
}
</script>
<script>
var player; //MIDI.Player
function initMIDI()
{
MIDI.loadPlugin({
soundfontUrl: "./soundfont/",
onprogress: function(state, progress) {
},
onsuccess: function() {
/// this sets up the MIDI.player and gets things going...
player = MIDI.Player;
player.timeWarp = 1;
player.loadFile(song[1]);
/// change the piano keys colors
player.addListener(function(event) {
if (event.message === 144) {
colorKey(event.note);
} else {
uncolorKey(event.note);
}
});
}
});
}
$(function()
{
initMIDI();
});
</script>
<div><button onclick="player.start()">Start</button><button onclick="player.stop()">Stop</button></div>
</body>
</html>