我目前正在使用Chrome(60.0.3112.90)中的Web Audio API來通過FilerReader,AudioContext來構建給定文件的聲音波,createScriptProcessor和createAnalyser。我有以下代碼:Web Audio API getFloatFrequencyData函數將Float32Array參數數據設置爲-Infinity值的數組
const visualize = analyser => {
analyser.fftSize = 256;
let bufferLength = analyser.frequencyBinCount;
let dataArray = new Float32Array(bufferLength);
analyser.getFloatFrequencyData(dataArray);
}
loadAudio(file){
// creating FileReader to convert audio file to an ArrayBuffer
const fileReader = new FileReader();
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
fileReader.addEventListener('loadend',() => {
const fileArrayBuffer = fileReader.result;
let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
let processor = audioCtx.createScriptProcessor(4096, 1, 1);
let analyser = audioCtx.createAnalyser();
analyser.connect(processor);
let data = new Float32Array(analyser.frequencyBinCount);
let soundBuffer;
let soundSource = audioCtx.createBufferSource();
// loading audio track into buffer
audioCtx.decodeAudioData(
fileArrayBuffer,
buffer => {
soundBuffer = buffer;
soundSource.buffer = soundBuffer;
soundSource.connect(analyser);
soundSource.connect(audioCtx.destination);
processor.onaudioprocess =() => {
// data becomes array of -Infinity values after call below
analyser.getFloatFrequencyData(data);
};
visuaulize(analyser);
},
error => 'error with decoding audio data: ' + error.err
);
});
fileReader.readAsArrayBuffer(file);
}
在加載文件時,我一直到analyser.getFloatFrequencyData(data)
。在閱讀網頁音頻API文檔,它說,該參數是:
The Float32Array that the frequency domain data will be copied to.
For any sample which is silent, the value is -Infinity.
在我的情況,我同時擁有MP3和WAV文件,我使用來測試這一點,並調用analyser.getFloatFrequency(data)
後,這兩個文件結束給我data
這成爲一個`-Infinity'值的數組。
這可能是由於我對Web Audio API的無知,但我的問題是爲什麼這兩個包含大聲音的文件都給我一個表示無聲樣本的數組?
您使用的是Chrome嗎? –
不完全清楚你想做什麼 - 但是我沒有看到你真的調用了audiobuffersource的start(),除非它在iHateMyselfForThis.recordAudio(audioCtx,analyzer)中? – cwilso
@RaymondToy是的,我已更新我的問題與該信息 – antihero989