Web Audio Offline Context and Analyser Node

时光总嘲笑我的痴心妄想 提交于 2020-01-28 11:01:11

问题


Is it possible to use the Analyser node in the offlineAudioContext to do frequency analysis?

I found out that ScriptProcessor 's onaudioprocess event still fires in the offlineAudioContext and this was the only event source I could use to call getByteFrequencyData of the Analyser Node. As below:

var offline = new offlineAudioContext(1, buffer.length, 44100);
var bufferSource = offline.createBufferSource();
bufferSource.buffer = buffer;

var analyser = offline.createAnalyser();
var scp = offline.createScriptProcessor(256, 0, 1);

bufferSource.connect(analyser);
scp.connect(offline.destination); // this is necessary for the script processor to start

var freqData = new Uint8Array(analyser.frequencyBinCount);
scp.onaudioprocess = function(){
    analyser.getByteFrequencyData(freqData);
    console.log(freqData);
   // freqData is always the same.
};

bufferSource.start(0);
offline.startRendering();

The problem here is that freqData array is always the same and never changes. Seem like as if it is only analysing one section of the buffer.

Am I doing anything wrong here? Or the Analyser is not intended to be used in the offlineContext.

And here is the fiddle with the same code.


回答1:


The analyser isn't really intended to be used in the offlineContext; in fact, it was originally named "RealtimeAnalyser". But even more importantly, right now you won't get consistent functionality from script processors in offline contexts, either.




回答2:


An alternative is to use the suspend and resume methods available for an OfflineAudioContext. After suspending, you can use your analyser node to get the desired time and/or frequency domain data. Since the context is stopped, this works just fine. If you're going to do this several times, you'll need to schedule a stop for the next time before resuming.

Unfortunately, AFAIK, only Chrome has implemented suspend for an OfflineAudioContext



来源:https://stackoverflow.com/questions/25368596/web-audio-offline-context-and-analyser-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!