我有以下功能
function calculateMFCC (wavFile, clipID) {
var audioData = loadAudioData(wavFile);
clipId = clipID;
passToWavReader(audioData);
}
函数loadAudioData(wavFile)应该使用Ajax加载wav文件并将结果作为arraybuffer返回。这就是它的样子:
function loadAudioData(wavFile) {
var request = new XMLHttpRequest();
request.open('GET', wavFile, false);
//request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = stringToArrayBuffer(request.response);
//passToWavReader(audioData);
return audioData;
}
request.send();
}
当我调试脚本时,我看到在loadAudioData(wavFile)函数中,变量audioData获取它应该获取的值,但是当它返回函数calculateMFCC(wavFile,clipID)时,值audioData是未定义的,所以当它调用passToWavReader(audioData)脚本中断。因此,来自calculateMFCC的audioData变量以某种方式没有从loadAudioData获取值。
答案 0 :(得分:0)
问题是XMLHttpRequest不会立即响应。您需要添加回调。尝试:
function calculateMFCC (wavFile, clipID) {
loadAudioData(wavFile, function(audioData) {
clipId = clipID;
passToWavReader(audioData);
});
}
function loadAudioData(wavFile, callback) {
var request = new XMLHttpRequest();
request.open('GET', wavFile, false);
//request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = stringToArrayBuffer(request.response);
//passToWavReader(audioData);
callback(audioData);
}
request.send();
}