问题描述
我的应用程序中有一个预先录制的音频文件的音频缓冲区.我正在尝试获取整个音轨的频域数据,这是我尝试过的:
I have the audio buffer of a prerecorded audio file in my application.I'm trying to get the frequency domain data of the ENTIRE audio track, this is what I've tried:
getAudioDataFromBuffer: function(buf){
var src = g.audioContext.createBufferSource();
src.buffer = buf;
var anal = src.context.createAnalyser();
src.connect(anal);
var dataArray = new Uint8Array(buf.length);
anal.fftSize = 2048;
anal.getByteFrequencyData(dataArray);
return dataArray;
},
但这只会给我一个充满零的数组.
But this only gives me an array full of zeros.
我需要这个来比较两个音轨,一个是预先录制的,另一个是在应用程序中录制的.我想我可以测量它们的频域之间的相关性.
I need this to compare two audio tracks, one is prerecorded and the other is recorded in the application. I'm thinking I could measure the correlation between their frequency domains.
推荐答案
I arrived to the solution seeing this answer and this discussion.
基本上你需要使用一个 OfflineAudioContext.这里的代码从已经加载的音频缓冲区开始:
Basically you need to use an OfflineAudioContext. Here the code staring from an already loaded audio buffer:
var offline = new OfflineAudioContext(2, 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);
};
bufferSource.start(0);
offline.oncomplete = function(e){
console.log('analysed');
};
offline.startRendering();
这篇关于网络音频分析整个缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!