[LÖST]JS Web Audio API Chrome, createScriptProcessor fungerar inte?
Hej, jag får inte "createScriptProcessor" att fungerar i Chrome, det fungerar dock i Firefox.
Inget händer i "onaudioprocess" eventet som ska köras när ljudet spelas upp.
Det måste vara något fel hur jag har anslutit alla noder för jag får i alla fall ut ljudet i Chrome.
Vet någon vad felet är i metoden _prepare()?
function PHAudio(el,options)
{
this.el = el;
var options = options || {};
options.volume = options.volume || 1.0;
options.loop = options.loop || false;
this.volume = options.volume;
this.loop = options.loop;
this.audioContext = null;
this.file = null;
this.buffer = null;
this.gainNode = null;
this.splitter = null;
this.source = null;
this.processor = [];
this.analyser = [];
this.frequencyBinCount = [];
this.frequencyArray = [];
this.timeDomainArray = [];
this.playing = false;
this.startOffset = 0;
this.startTime = 0;
this._init();
}
PHAudio.prototype = {
_init:function()
{
try
{
this.audioContext = new AudioContext();
this.file = this.el.src;
this._load();
}
catch (e)
{
console.log("No Audio API support.");
console.log(e);
}
},
_loadFromFile:function(file)
{
var self = this
var fr = new FileReader();
fr.onload = function(e)
{
var fileResult = fr.result;
self._decode(fileResult);
}
fr.onerror = function(e)
{
console.log(e);
}
fr.readAsArrayBuffer(file);
},
_loadFromRequest:function()
{
var self = this;
var request = new XMLHttpRequest();
request.open('GET', this.file, true);
request.responseType = 'arraybuffer';
request.onload = function()
{
var audioData = request.response;
self._decode(audioData);
}
request.send();
},
_load:function()
{
this._loadFromRequest();
},
_decode:function(audioData)
{
var self = this;
this.audioContext.decodeAudioData(audioData,
function(buffer)
{
self.buffer = buffer;
if(self.bufferLoaded)
self.bufferLoaded();
},
function(e){"Error with decoding audio data" + e.err}
);
},
_disconnect:function()
{
for(var i = 0;i < this.analyser.length;i++)
{
this.analyser[i].disconnect();
this.processor[i].disconnect();
}
this.splitter.disconnect();
this.gainNode.disconnect();
this.source.disconnect();
},
_prepare:function()
{
if(this.buffer === null) throw "No buffer to prepare.";
this.source = this.audioContext.createBufferSource();
this.source.buffer = this.buffer;
this.source.loop = this.loop;
if(!this.source.start)
{
this.source.start = this.source.noteOn
this.source.stop = this.source.noteOff
}
this.gainNode = this.audioContext.createGain();
this.processor[0] = this.audioContext.createScriptProcessor(2048, 1, 0);
this.processor[1] = this.audioContext.createScriptProcessor(2048, 1, 0);
this.splitter = this.audioContext.createChannelSplitter();
this.analyser[0] = this.audioContext.createAnalyser();
this.analyser[1] = this.audioContext.createAnalyser();
this.source.connect(this.gainNode);
this.gainNode.connect(this.splitter);
for(var i = 0;i < this.analyser.length;i++)
{
this.analyser[i].smoothingTimeConstant = 0.3;
this.analyser[i].fftSize = 1024;
this.splitter.connect(this.analyser[i],i,0);
this.analyser[i].connect(this.processor[i]);
}
this.gainNode.connect(this.audioContext.destination);
var self = this;
this.source.onended = function(e)
{
self.end();
}
for(var i = 0;i < this.processor.length;i++)
{
this.processor[i].onaudioprocess = function(e)
{
console.log(true);
var index = self.processor.indexOf(this);
if(index == 0)
{
self.gainNode.gain.value = self.volume;
}
self.frequencyBinCount[index] = self.analyser[index].frequencyBinCount;
self.frequencyArray[index] = new Uint8Array(self.frequencyBinCount[index]);
self.analyser[index].getByteFrequencyData(self.frequencyArray[index]);
self.timeDomainArray[index] = new Uint8Array(self.frequencyBinCount[index]);
self.analyser[index].getByteTimeDomainData(self.timeDomainArray[index]);
}
}
},
getRemainingTime:function()
{
if(this.buffer === null) return 0;
return parseInt(this.buffer.duration - this.audioContext.currentTime, 10);
},
getRemainingMin:function()
{
return Math.floor(this.getRemainingTime()/60,10);
},
getRemainingSec:function()
{
return this.getRemainingTime() - this.getRemainingMin()*60;
},
printTimeLeft:function()
{
return '-' + this.getRemainingMin() + ':' +
(this.getRemainingSec() > 9 ? this.getRemainingSec() : '0' + this.getRemainingSec());
},
end:function(time)
{
if(this.source != null || this.playing)
{
var time = time || 0;
this.startOffset += this.audioContext.currentTime - this.startTime;
this.source.stop(time);
this.playing = false;
this.frequencyBinCount = [];
this.frequencyArray = [];
this.timeDomainArray = [];
this._disconnect();
}
},
stop:function(time)
{
return this.end(time);
},
play:function(time,bufferOffset)
{
var time = time || 0;
var bufferOffset = bufferOffset || 0;
try
{
this.end();
this._prepare();
this.startTime = this.audioContext.currentTime;
this.source.start(time,bufferOffset);
this.playing = true;
}
catch(e){};
},
resume:function()
{
this.play(0,this.startOffset % this.buffer.duration)
},
setVolume:function(factor)
{
this.volume = factor;
return this;
},
getAverageVolume:function(channel)
{
var average = 0;
if(this.playing && this.frequencyArray.length > 0 && this.frequencyArray[channel])
{
var values = 0;
var length = this.frequencyArray[channel].length;
for(var i = 0; i < length; i++)
{
values += this.frequencyArray[channel][i];
}
average = values / length;
}
return average;
}
};
Hela källkoden, http://www.xuniver.se/projects/progresshero/pub/javascripts.p....