首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Microsoft中重采样音频,因为不支持copyToChannel

在Microsoft中重采样音频,因为不支持copyToChannel
EN

Stack Overflow用户
提问于 2016-01-15 15:50:08
回答 1查看 124关注 0票数 3

我正试着重拍一些音频。我有一个在Chrome和Firefox中工作的函数,但是它在语句的Edge中崩溃

代码语言:javascript
复制
audioBuffer.copyToChannel(sampleArray,0,0);

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

不管怎样,我想找个解决办法。在开发人员工具中检查audioBuffer对象并没有给我带来任何线索。

谢谢!

这是我的代码:

代码语言:javascript
复制
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);
    });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-21 13:35:34

我和边缘一起工作:

代码语言:javascript
复制
    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();
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34814866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档