зеркало из https://github.com/mozilla/deepdev.git
added audio test code
This commit is contained in:
Родитель
cded471187
Коммит
52b9932722
|
@ -0,0 +1,160 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<button class="zone1">Zone 1</button>
|
||||
<button class="zone2">Zone 2</button>
|
||||
<button class="zone3">Zone 3</button>
|
||||
<button class="zone4">Zone 4</button>
|
||||
<script>
|
||||
|
||||
var context;
|
||||
var fadeInCurve, fadeOutCurve;
|
||||
var audio;
|
||||
|
||||
function init() {
|
||||
window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
||||
context = new AudioContext();
|
||||
|
||||
bufferLoader = new BufferLoader(
|
||||
context,
|
||||
[
|
||||
'audio/quad1.ogg',
|
||||
'audio/quad2.ogg',
|
||||
'audio/quad3.ogg',
|
||||
'audio/quad4.ogg'
|
||||
],
|
||||
finishedLoading
|
||||
);
|
||||
|
||||
fadeInCurve = new Float32Array(11);
|
||||
fadeOutCurve = new Float32Array(11);
|
||||
for (var i = 0; i <= 10; i++) {
|
||||
fadeInCurve[i] = eqlerp(i/10);
|
||||
fadeOutCurve[i] = eqlerp(1-i/10);
|
||||
}
|
||||
|
||||
bufferLoader.load();
|
||||
}
|
||||
|
||||
function eqlerp(i) {
|
||||
i = Math.min(1, Math.max(0, i));
|
||||
return Math.cos((1.0 - i) * 0.5 * Math.PI);
|
||||
}
|
||||
|
||||
function Sound(buffer) {
|
||||
this.source = context.createBufferSource();
|
||||
this.source.buffer = buffer;
|
||||
this.gain = context.createGain();
|
||||
this.source.connect(this.gain);
|
||||
this.output = this.gain;
|
||||
this.output.connect(context.destination);
|
||||
}
|
||||
Sound.prototype.loop = function () {
|
||||
this.source.loop = true;
|
||||
this._play();
|
||||
}
|
||||
Sound.prototype.play = function () {
|
||||
this.source.loop = false;
|
||||
this._play();
|
||||
}
|
||||
Sound.prototype.fadeOut = function (duration) {
|
||||
this.gain.gain.setValueCurveAtTime(fadeOutCurve, context.currentTime, duration);
|
||||
}
|
||||
Sound.prototype.fadeIn = function (duration) {
|
||||
this.gain.gain.setValueCurveAtTime(fadeInCurve, context.currentTime, duration);
|
||||
}
|
||||
Sound.prototype._play = function () {
|
||||
this.gain.gain = 0;
|
||||
this.source.start(context.currentTime);
|
||||
this.fadeIn(2);
|
||||
}
|
||||
|
||||
|
||||
function finishedLoading(buffers) {
|
||||
var sounds = buffers.map(function (b) {
|
||||
return new Sound(b);
|
||||
});
|
||||
|
||||
audio = new AudioManager(sounds);
|
||||
audio.setBg(0);
|
||||
|
||||
document.querySelector('.zone1').addEventListener('click', setBg(0));
|
||||
document.querySelector('.zone2').addEventListener('click', setBg(1));
|
||||
document.querySelector('.zone3').addEventListener('click', setBg(2));
|
||||
document.querySelector('.zone4').addEventListener('click', setBg(3));
|
||||
}
|
||||
|
||||
|
||||
function AudioManager(sounds) {
|
||||
this.sounds = sounds;
|
||||
this.active = null;
|
||||
}
|
||||
AudioManager.prototype.setBg = function (n) {
|
||||
if (this.active) {
|
||||
this.active.fadeOut(2);
|
||||
}
|
||||
this.active = this.sounds[n];
|
||||
this.active.loop();
|
||||
}
|
||||
function setBg(n) {
|
||||
return function () {
|
||||
audio.setBg(n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function BufferLoader(context, urlList, callback) {
|
||||
this.context = context;
|
||||
this.urlList = urlList;
|
||||
this.onload = callback;
|
||||
this.bufferList = new Array();
|
||||
this.loadCount = 0;
|
||||
}
|
||||
|
||||
BufferLoader.prototype.loadBuffer = function(url, index) {
|
||||
// Load buffer asynchronously
|
||||
var request = new XMLHttpRequest();
|
||||
request.open("GET", url, true);
|
||||
request.responseType = "arraybuffer";
|
||||
|
||||
var loader = this;
|
||||
|
||||
request.onload = function() {
|
||||
// Asynchronously decode the audio file data in request.response
|
||||
loader.context.decodeAudioData(
|
||||
request.response,
|
||||
function(buffer) {
|
||||
if (!buffer) {
|
||||
alert('error decoding file data: ' + url);
|
||||
return;
|
||||
}
|
||||
loader.bufferList[index] = buffer;
|
||||
if (++loader.loadCount == loader.urlList.length)
|
||||
loader.onload(loader.bufferList);
|
||||
},
|
||||
function(error) {
|
||||
console.error('decodeAudioData error', error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
request.onerror = function() {
|
||||
alert('BufferLoader: XHR error');
|
||||
}
|
||||
|
||||
request.send();
|
||||
}
|
||||
|
||||
BufferLoader.prototype.load = function() {
|
||||
for (var i = 0; i < this.urlList.length; ++i)
|
||||
this.loadBuffer(this.urlList[i], i);
|
||||
}
|
||||
|
||||
window.onload = init;
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Загрузка…
Ссылка в новой задаче