Полностью кусок кода с CreateGainNode
//init audio
analyser = audioContext.createAnalyser();
analyser.smoothingTimeConstant = 0.1;
analyser.fftSize = 1024;
var gainNode = audioContext.createGain();
//init 3D scene
container = document.createElement('div');
document.body.appendChild(container);
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000000);
camera.position.z = 2000;
scene = new THREE.Scene();
scene.add(camera);
renderer = new THREE.WebGLRenderer({
antialias : false,
sortObjects : false
});
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
// stop the user getting a text cursor
document.onselectStart = function() {
return false;
};
//add stats
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild(stats.domElement);
//init listeners
$("#loadSample").loadSampleAudio;
$(document).mousemove(onDocumentMouseMove);
container.addEventListener( 'touchstart', onDocumentTouchStart, false );
container.addEventListener( 'touchmove', onDocumentTouchMove, false );
onWindowResize(null);
LoopVisualizer.init();
/////////////////////////
// do it with volume!
/////////////////////////
console.log(gainNode);
document.addEventListener("keydown", function (e) {
if (e.keyCode == 40){
//alert('keydown');
gainNode.gain.value = gainNode.gain.value - 0.2;
console.log(gainNode.gain.value);
}
}, false);
gainNode.connect(audioContext.destination);
}
//////////////////////////////
/**
* Эмуляция аудио объекта
* @param {string} src
* @constructor
*/
var Audio = function (src) {
this._time = Audio.randomInt(1000, 4000);
this._timer = null;
this._timeStart = null;
this._events = {};
this.src = src;
var that = this;
setTimeout(function () {
that._trigger("canplay");
}, Audio.randomInt(0, 500));
};
/**
* подписка на события
* @param {string} event
* @param {function} handler
*/
Audio.prototype.addEventListener = function (event, handler) {
if (!this._events[event]) this._events[event] = [];
this._events[event].push(handler);
};
/**
* Запускаем проигрывание
*/
Audio.prototype.play = function () {
var that = this;
this._trigger("play");
this._timeStart = Date.now();
this._timer = setTimeout(function () {
that._trigger("ended")
}, this._time);
};
/**
* Ставим на паузу
*/
Audio.prototype.pause = function () {
if (this._timer) {
clearTimeout(this._timer);
this._timer = null;
this._time = Date.now() - this._timeStart;
this._timeStart = null;
}
};
/**
* Снимаем обработчики
* @param {string} event
* @param {function} handler
*/
Audio.prototype.removeEventListener = function (event, handler) {
if (this._events[event]) {
this._events[event] = this._events[event].filter(function ($handler) {
return $handler != handler;
});
}
};
/**
* Запускаем событие
* @param event
* @private
*/
Audio.prototype._trigger = function (event) {
if (this._events[event]) {
this._events[event].forEach(function (handler) {
handler.call(this);
}, this);
}
};
/**
* Генерируем целое рандомное число
* @param {number} min
* @param {number} max
* @returns {number}
*/
Audio.randomInt = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
/**
* Менеджер треков
* @param {string[]} tracks
* @constructor
*/
var TrackManager = function (tracks) {
this._active = 0;
this._collection = [];
this._createTracks(tracks);
};
console.log(i);
/**
* Запускаем проигрывание треков
*/
TrackManager.prototype.play = function () {
if (this._active in this._collection) {
var media = this;
var request = new XMLHttpRequest();
request.open("GET", this._collection[Audio.randomInt(0, this._collection.length-1)].src, true);
//console.log(request);
request.responseType = "arraybuffer";
request.onload = function() {
audioContext.decodeAudioData(request.response, function(buffer) {
audioBuffer = buffer;
//console.log(audioBuffer);
if (this.started){
source.stop(0.0);
source.disconnect();
}
// Connect audio processing graph
source = audioContext.createBufferSource();
source.connect(audioContext.destination);
source.connect(analyser);
source.buffer = audioBuffer;
source.loop = false;
source.start(0.0);
startViz();
source.onended = function(){
mediaplay = media;
//return mediaplay;
console.log(mediaplay._active);
TrackManager.prototype._onEnd(mediaplay);
};
}, function(e) {
$('#prompt').text("error loading mp3");
console.log(e);
});
};
request.send();
console.log(this);
//console.log(source);
};
};
console.log(audioContext);
/**
* Создаем треки и подписываемся на события
* @param {string[]} tracks
* @private
*/
TrackManager.prototype._createTracks = function (tracks) {
tracks.forEach(function (src) {
var audio = new Audio(src), that = this;
audio.addEventListener("canplay", function () {
console.log(this.src + " canplay");
});
audio.addEventListener("play", function () {
console.log(this.src + " play");
});
audio.addEventListener("ended", function () {
alert(this.src + " ended");
that._onEnd();
});
this._collection.push(audio);
}, this);
};
/**
* Запускаем следующий трек когда заканчивается предыдущий;
* @private
*/
TrackManager.prototype._onEnd = function () {
//var media = this;
mediaplay._active++;
if(mediaplay._active > mediaplay._collection.length - 1)
{
mediaplay._active = 0;
}
console.log(mediaplay);
var request = new XMLHttpRequest();
request.open("GET", mediaplay._collection[Audio.randomInt(0, mediaplay._collection.length-1)].src, true);
//console.log(request);
request.responseType = "arraybuffer";
request.onload = function() {
audioContext.decodeAudioData(request.response, function(buffer) {
audioBuffer = buffer;
//console.log(audioBuffer);
if (source){
source.stop(0.0);
source.disconnect();
}
// Connect audio processing graph
source = audioContext.createBufferSource();
source.connect(audioContext.destination);
source.connect(analyser);
source.buffer = audioBuffer;
source.loop = false;
source.start(0.0);
startViz();
playing = this;
//return playing;
console.log(playing);
source.onended = function() {
//console.log(mediaplay);
//console.log(mediaplay._collection.length);
TrackManager.prototype._onEnd(mediaplay);
}
}, function(e) {
$('#prompt').text("error loading mp3");
console.log(e);
});
};
request.send();
};
//console.log(media);
var playlist = new Array();
var musicarray = {<?=$playlist;?>};
var i;
for(i = 0; i < musicarray.playlist.length; i++)
{
playlist[i] = musicarray.playlist[i].url;
}
new TrackManager(playlist).play();
console.log(TrackManager);