gpt4 book ai didi

javascript - 由于不支持 copyToChannel,因此在 Microsoft Edge 中重新采样音频

转载 作者:行者123 更新时间:2023-11-29 15:31:05 25 4
gpt4 key购买 nike

我正在尝试对一些音频重新采样。我有一个在 Chrome 和 Firefox 中工作的函数,但它在 Edge 中崩溃了声明

audioBuffer.copyToChannel(sampleArray,0,0);

表示未定义 copyToChannel。这很好奇,因为 Microsoft 文档专门定义了它:https://dev.windows.com/en-us/microsoft-edge/platform/documentation/apireference/interfaces/audiobuffer/

无论如何,我正在寻找解决方法。在开发人员工具中检查 audioBuffer 对象没有为我提供任何线索。

谢谢!

这是我的代码:

function reSample(sampleArray, targetSampleRate, onComplete) {
// sampleArray is a Float32Array
// targetSampleRate is an int (22050 in this case)
// onComplete is called with the new buffer when the operation is complete
var audioCtx = new window.AudioContext();

var audioBuffer = audioCtx.createBuffer(1, sampleArray.length, audioCtx.sampleRate);
audioBuffer.copyToChannel(sampleArray,0,0); // Not supported by Microsoft Edge 12, evidently.

var channel = audioBuffer.numberOfChannels;
var samples = audioBuffer.length * targetSampleRate / audioBuffer.sampleRate;

var offlineContext = new window.OfflineAudioContext(channel, samples, targetSampleRate);
var bufferSource = offlineContext.createBufferSource();
bufferSource.buffer = audioBuffer;

bufferSource.connect(offlineContext.destination);
bufferSource.start(0);
offlineContext.startRendering().then(function(renderedBuffer){
onComplete(renderedBuffer);
});
}

最佳答案

我在 Edge 上使用了这个:

    const offlineCtx = new OfflineAudioContext(sourceBuffer.numberOfChannels, sourceBuffer.duration *
this.sampleRate, this.sampleRate);
const cloneBuffer = offlineCtx.createBuffer(sourceBuffer.numberOfChannels, sourceBuffer.length, sourceBuffer.sampleRate);
cloneBuffer.copyToChannel(sourceBuffer.getChannelData(0), 0);
const source = offlineCtx.createBufferSource();
source.buffer = cloneBuffer;
offlineCtx.oncomplete = (e) => {
const left = e.renderedBuffer.getChannelData(0);
this.onAudioProcess(this.float32ToInt16(left, left.length), e.renderedBuffer.duration * 1000);
};
source.connect(offlineCtx.destination);
source.start(0);
offlineCtx.startRendering();
}

关于javascript - 由于不支持 copyToChannel,因此在 Microsoft Edge 中重新采样音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34814866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com