-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
284 lines (254 loc) · 7.79 KB
/
index.html
File metadata and controls
284 lines (254 loc) · 7.79 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>リアルタイム通信して複数デバイス上で音声を再生する | Node.js(express) + Socket.IO + Web Audio API on Heroku</title>
<style>
html { height: 100%; }
body {
font-family: ArialMT,"Hiragino Kaku Gothic ProN","ヒラギノ角ゴ ProN W3","メイリオ",sans-serif;
margin: 0;
padding: 0;
height: 100%;
-webkit-text-size-adjust: 100%;
text-size-adjust: 100%;
-webkit-tap-highlight-color: transparent;
tap-highlight-color: transparent;
position: relative;
}
button {
display: block;
-webkit-appearance: none;
appearance: none;
background: #ddd;
width: 100px;
height: 100px;
-webkit-border-radius: 10px;
border-radius: 10px;
border: 1px solid #aaa;
-webkit-user-select: none;
user-select: none;
}
button:focus {
outline: none;
}
.tap {
background: #bbb;
}
#kick {
position: absolute;
top: 260px;
left: 50%;
margin-left: -50px;
}
#cymbal {
position: absolute;
top: 10px;
left: 10px;
}
#hat {
position: absolute;
top: 130px;
left: 10px;
}
#snare {
position: absolute;
top: 130px;
right: 10px;
}
#result {
opacity: 0;
color: #ffff00;
text-align: center;
width: 200px;
margin: -29px 0 0 -100px;
padding: 20px 0;
position: absolute;
top: 50%;
left: 50%;
pointer-events: none;
background-color: rgba(0,0,0,0.6);
-webkit-border-radius: 5px;
border-radius: 5px;
}
#result.popup {
-webkit-animation: popup 0.6s linear 1;
animation: popup 0.6s linear 1;
}
@-webkit-keyframes popup {
0% { opacity: 0; -webkit-transform: scale3d(0.9, 0.9, 0.9); -webkit-transform: scale(0.9, 0.9); }
5% { opacity: 1; -webkit-transform: scale3d(1, 1, 1); -webkit-transform: scale(1, 1); }
100% { opacity: 0; -webkit-transform: scale3d(1.1, 1.1, 1.1); -webkit-transform: scale(1.1, 1.1); }
}
@keyframes popup {
0% { opacity: 0; transform: scale3d(0.9, 0.9, 0.9); transform: scale(0.9, 0.9); }
5% { opacity: 1; transform: scale3d(1, 1, 1); transform: scale(1, 1); }
100% { opacity: 0; transform: scale3d(1.1, 1.1, 1.1); transform: scale(1.1, 1.1); }
}
#splash-image {
text-align: center;
width: 100%;
height: 100%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: rgba(255,255,255,0.8);
background-image: url(logo.png);
background-position: 50% 50%;
background-repeat: no-repeat;
-webkit-background-size: 200px 35px;
background-size: 200px 35px;
}
#splash-image.none { display: none; }
#splash-image.fade-out {
-webkit-animation: fade-out 0.3s linear 1;
animation: fade-out 0.3s linear 1;
}
@-webkit-keyframes fade-out {
0% { opacity: 1; -webkit-transform: scale(1); }
80% { opacity: 1; -webkit-transform: scale(0.97); }
100% { opacity: 0; -webkit-transform: scale(2); }
}
@keyframes fade-out {
0% { opacity: 1; transform: scale(1); }
80% { opacity: 1; transform: scale(0.97); }
100% { opacity: 0; transform: scale(2); }
}
</style>
</head>
<body>
<button id="kick"> kick </button>
<button id="cymbal"> cymbal </button>
<button id="hat"> hat </button>
<button id="snare"> snare </button>
<div id="result"></div>
<div id="splash-image"></div>
<script src="/socket.io/socket.io.js"></script>
<script>
;(function (window, undefined) {
// socket.ioに接続する
// var socketio = io.connect('http://localhost:8000'); // localで試す時
var socketio = io.connect('https://nodejs-socketio-webaudio.herokuapp.com/'); // Herokuで試す時
var drumObj = {}; // ドラムのbutton、音声情報を格納するObject
var isTouchDevice = 'ontouchend' in document;
var eventNames = {
start: isTouchDevice ? 'touchstart' : 'mousedown',
move: isTouchDevice ? 'touchmove' : 'mousemove',
end: isTouchDevice ? 'touchend' : 'mouseup',
click: 'click'
};
var result = document.getElementById('result'), // ポップアップ用
splashImage = document.getElementById('splash-image'); // スプラッシュ画像
function AudioBufferLoader(ele, url) {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
try {
this.context = new AudioContext;
}
catch(e) {
alert('Web Audio API is not supported in this browser');
}
this.ele = document.getElementById(ele);
this.url = url;
this.buffer = null;
}
AudioBufferLoader.prototype = {
loadBuffer: function(url) {
var _this = this;
var request = new XMLHttpRequest();
request.open('get', url, true);
request.responseType = 'arraybuffer';
request.onload = function() {
_this.context.decodeAudioData(
request.response,
function(buffer) {
if (! buffer) {
alert('error decodeing file data: ' + url);
return;
}
_this.buffer = buffer;
_this.events();
},
function(error) {
alert('decodeAudioData error', error);
}
);
};
request.onerror = function() {
alert('AudioBufferLoader: XHR error');
};
request.send();
},
events: function() {
var _this = this;
var context = _this.context;
_this.ele.addEventListener(eventNames.start, function() {
this.className = '';
this.className = 'tap';
result.className = "";
// socket.ioで全デバイスで同期させる為、タップイベントでは再生せずサーバーからの返答時に再生
// var source = context.createBufferSource();
// source.buffer = _this.buffer;
// source.connect(context.destination);
// source.start(0);
// クライアントからサーバーにボタンidを送る
socketio.emit("from_client", this.id);
});
_this.ele.addEventListener(eventNames.end, function() {
this.className = '';
});
},
load: function() {
var _this = this;
_this.loadBuffer(_this.url);
},
// 音声データをセット
set: function () {
var _this = this;
_this.source = _this.context.createBufferSource();
_this.source.buffer = _this.buffer;
_this.source.connect(_this.context.destination);
}
};
drumObj.kick = new AudioBufferLoader('kick', '/sound/kick.m4a');
drumObj.kick.load();
drumObj.cymbal = new AudioBufferLoader('cymbal', '/sound/cymbal.m4a');
drumObj.cymbal.load();
drumObj.hat = new AudioBufferLoader('hat', '/sound/hat.m4a');
drumObj.hat.load();
drumObj.snare = new AudioBufferLoader('snare', '/sound/snare.m4a');
drumObj.snare.load();
// スプラッシュ画像を押した時に音声データをロード
// iPhone/Androidで最初の音声ロードだけはユーザーアクションが必要な制約がある為(PCにはこの制約はなさそう)
splashImage.addEventListener(eventNames.start, function(){
// 音声データをセット
drumObj.kick.set();
drumObj.cymbal.set();
drumObj.hat.set();
drumObj.snare.set();
// スプラッシュ画像をフェードアウト
this.className = 'fade-out';
this.addEventListener("webkitAnimationEnd", function(){ this.className = "none"; });
this.addEventListener("animationend", function(){ this.className = "none"; });
});
// サーバーから "from_server" が送られてくるのを監視
socketio.on("from_server", function(id){
// サーバーから返されたid(buttonのid名)から音声を再生
// start()だけを複数回実行出来ない仕様のようなので、毎回createBufferSource()してから再生
drumObj[id].source = drumObj[id].context.createBufferSource();
drumObj[id].source.buffer = drumObj[id].buffer;
drumObj[id].source.connect(drumObj[id].context.destination);
drumObj[id].source.start(0);
// ポップアップを表示
result.className = "popup";
result.innerHTML = id + '!';
result.addEventListener("webkitAnimationEnd", function(){ this.className = ""; });
result.addEventListener("animationend", function(){ this.className = ""; });
});
}(this));
</script>
</body>
</html>