-
Notifications
You must be signed in to change notification settings - Fork 6
/
mix.html
265 lines (248 loc) · 9.31 KB
/
mix.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
265
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"/>
<meta name="format-detection" content="telephone=no"/>
<title>Web Audio API 移动端兼容性测试</title>
<style>
section.channels {
height: 40px;
}
section.channels label {
width: 80px;
display: inline-block;
height: 25px;
line-height: 25px;
}
section.channels input {
width: 200px;
vertical-align: middle;
}
</style>
</head>
<body>
<h1>Web Audio API 混音测试</h1>
<!--<p>
AudioContext:
<script type="text/javascript">
document.write(typeof AudioContext);
</script>
</p>
<p>
webkitAudioContext:
<script type="text/javascript">
document.write(typeof webkitAudioContext);
</script>
</p>-->
<!--<p>
<input type="button" id="load" value="Load"/>
</p>
<p>
<input type="button" id="mp3" disabled value="MP3"/>
<input type="button" id="aac_he_v1" disabled value="AAC_HE_v1"/>
<input type="button" id="aac_he_v2" disabled value="AAC_HE_v2"/>
<input type="button" id="aac_lc" disabled value="AAC_LC"/>
</p>-->
<div>
<section class="channels">
<label for="rectangle1">方形波1:</label>
<input id="rectangle1" type="range" value="1" step="0.01" max="1" min="0">
</section>
<section class="channels">
<label for="rectangle2">方形波2:</label>
<input id="rectangle2" type="range" value="1" step="0.01" max="1" min="0">
</section>
<section class="channels">
<label for="triangle">三角波:</label>
<input id="triangle" type="range" value="1" step="0.01" max="1" min="0">
</section>
<section class="channels">
<label for="noise">噪波:</label>
<input id="noise" type="range" value="1" step="0.01" max="1" min="0">
</section>
<p>
<input type="button" id="play" disabled value="加载中...0/4"/>
</p>
<p>
<input type="button" id="jump" disabled value="跳!"/>
<input type="button" id="gold" disabled value="金币!"/>
<input type="button" id="kick" disabled value="踩!"/>
</p>
</div>
<script type="text/javascript">
(function () {
var EXT_NAME = '.mp3';
var Ctx = window.webkitAudioContext ? window.webkitAudioContext : window.AudioContext;
var ctx = new Ctx();
var compressor = ctx.createDynamicsCompressor();
compressor.connect(ctx.destination);
var destinationNode = compressor;
var Sound = function () {
this.src = null;
this.buffer = null;
this.isLoaded = false;
this.load = function (src, callback) {
this.src = src;
var request = new XMLHttpRequest();
var that = this;
request.open('GET', this.src, true);
request.responseType = 'arraybuffer';
request.onload = function () {
ctx.decodeAudioData(request.response, function (buffer) {
that.buffer = buffer;
that.isLoaded = true;
if (typeof callback === 'function') {
callback(buffer);
}
}, function(){
//decode fail
alert('source not support');
});
};
request.send();
};
this.volume = 1;
this.loop = false;
this.loopStart = 0;
this.loopEnd = 0;
this.volumeNode = null;
this.sourceNode = null;
};
Sound.prototype.play = function () {
if (this.isLoaded) {
var source = ctx.createBufferSource();
source.buffer = this.buffer;
this.volumeNode = ctx.createGain();
this.volumeNode.gain.value = this.volume;
source.loopStart = this.loopStart;
source.loopEnd = this.loopEnd;
source.loop = this.loop;
source.connect(this.volumeNode);
this.volumeNode.connect(destinationNode);
source.start(0);
this.sourceNode = source;
}
};
Sound.prototype.setVolume = function (volume) {
this.volume = volume;
if (this.volumeNode) {
this.volumeNode.gain.value = volume;
}
};
Sound.prototype.setLoop = function (loopStart, loopEnd) {
if (loopStart === false) {
this.loop = false;
} else {
this.loop = true;
this.loopStart = loopStart;
this.loopEnd = loopEnd;
}
if (this.sourceNode) {
this.sourceNode.loopStart = this.loopStart;
this.sourceNode.loopEnd = this.loopEnd;
this.sourceNode.loop = this.loop;
}
};
(function (){
var rectangle1, rectangle2, triangle, noise;
var loadedNum = 0;
var loadedCallback = function(){
loadedNum ++;
document.getElementById('play').value = '加载中...' + loadedNum + '/4';
if (loadedNum >= 4) {
document.getElementById('play').removeAttribute('disabled');
document.getElementById('play').value = '播放';
}
};
rectangle1 = new Sound();
rectangle2 = new Sound();
triangle = new Sound();
noise = new Sound();
rectangle1.load('res/rectangle1' + EXT_NAME, loadedCallback);
rectangle2.load('res/rectangle2' + EXT_NAME, loadedCallback);
triangle .load('res/triangle' + EXT_NAME, loadedCallback);
noise .load('res/noise' + EXT_NAME, loadedCallback);
rectangle1.setLoop(2.40, 88.8);
rectangle2.setLoop(2.40, 88.8);
triangle .setLoop(2.40, 88.8);
noise .setLoop(2.40, 88.8);
document.getElementById('play').onclick = function () {
rectangle1.play();
rectangle2.play();
triangle .play();
noise .play();
document.getElementById('play').setAttribute('disabled', 'disabled');
};
document.getElementById('rectangle1').oninput = function () {
rectangle1.setVolume(this.value);
};
document.getElementById('rectangle2').oninput = function () {
rectangle2.setVolume(this.value);
};
document.getElementById('triangle').oninput = function () {
triangle.setVolume(this.value);
};
document.getElementById('noise').oninput = function () {
noise.setVolume(this.value);
};
})();
(function () {
var jump, gold, kick;
gold = new Sound();
gold.load('res/gold.mp3', function () {
document.getElementById('gold').removeAttribute('disabled');
});
document.getElementById('gold').onclick = function () {
gold.play();
};
jump = new Sound();
jump.load('res/jump.mp3', function () {
document.getElementById('jump').removeAttribute('disabled');
});
document.getElementById('jump').onclick = function () {
jump.play();
};
kick = new Sound();
kick.load('res/kick.mp3', function () {
document.getElementById('kick').removeAttribute('disabled');
});
document.getElementById('kick').onclick = function () {
kick.play();
};
})();
/*var sound_mp3, sound_aac_he_v2, sound_aac_he_v1, sound_aac_lc;
document.getElementById('load').onclick = function () {
sound_mp3 = new Sound();
sound_mp3.load('res/gold.mp3', function () {
document.getElementById('mp3').removeAttribute('disabled');
});
sound_aac_he_v2 = new Sound();
sound_aac_he_v2.load('res/gold_hev2.mp4', function () {
document.getElementById('aac_he_v2').removeAttribute('disabled');
});
sound_aac_he_v1 = new Sound();
sound_aac_he_v1.load('res/gold_hev1.mp4', function () {
document.getElementById('aac_he_v1').removeAttribute('disabled');
});
sound_aac_lc = new Sound();
sound_aac_lc.load('res/jump_lc.mp4', function () {
document.getElementById('aac_lc').removeAttribute('disabled');
});
};
document.getElementById('mp3').onclick = function () {
sound_mp3.play();
};
document.getElementById('aac_he_v2').onclick = function () {
sound_aac_he_v2.play();
};
document.getElementById('aac_he_v1').onclick = function () {
sound_aac_he_v1.play();
};
document.getElementById('aac_lc').onclick = function () {
sound_aac_lc.play();
};*/
})();
</script>
</body>
</html>