【发布时间】:2014-03-03 15:09:49
【问题描述】:
每当我有一个新缓冲区进入我的客户端时,我想将该音频实例重绘到我的画布上。我从http://webaudioapi.com/samples/visualizer/ 获取示例代码并尝试对其进行更改以适应我在现场环境中的需求。我似乎有一些工作,因为当我调用 .draw() 时确实看到画布在更新,但它并没有达到应有的速度。我可能看到大约 1 fps。如何加快我的 fps 并仍然为新缓冲区的每个实例调用 draw?
完整代码: https://github.com/grkblood13/web-audio-stream/tree/master/visualizer
这是为每个缓冲区调用 .draw() 的部分:
function playBuffer(audio, sampleRate) {
var source = context.createBufferSource();
var audioBuffer = context.createBuffer(1, audio.length , sampleRate);
source.buffer = audioBuffer;
audioBuffer.getChannelData(0).set(audio);
source.connect(analyser);
var visualizer = new Visualizer(analyser);
visualizer.analyser.connect(context.destination);
visualizer.draw(); // Draw new canvas for every new set of data
if (nextTime == 0) {
nextTime = context.currentTime + 0.05; /// add 50ms latency to work well across systems - tune this if you like
}
source.start(nextTime);
nextTime+=source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played
}
这是 .draw() 方法:
Visualizer.prototype.draw = function() {
function myDraw() {
this.analyser.smoothingTimeConstant = SMOOTHING;
this.analyser.fftSize = FFT_SIZE;
// Get the frequency data from the currently playing music
this.analyser.getByteFrequencyData(this.freqs);
this.analyser.getByteTimeDomainData(this.times);
var width = Math.floor(1/this.freqs.length, 10);
// Draw the time domain chart.
this.drawContext.fillStyle = 'black';
this.drawContext.fillRect(0, 0, WIDTH, HEIGHT);
for (var i = 0; i < this.analyser.frequencyBinCount; i++) {
var value = this.times[i];
var percent = value / 256;
var height = HEIGHT * percent;
var offset = HEIGHT - height - 1;
var barWidth = WIDTH/this.analyser.frequencyBinCount;
this.drawContext.fillStyle = 'green';
this.drawContext.fillRect(i * barWidth, offset, 1, 2);
}
}
requestAnimFrame(myDraw.bind(this));
}
【问题讨论】:
标签: javascript html streaming html5-canvas web-audio-api