【问题标题】:How to set up sample rate using web audio API?如何使用网络音频 API 设置采样率?
【发布时间】:2015-07-12 17:35:01
【问题描述】:

我有由 webaudio API 生成的 blob 类型,但保存的文件必须具有高采样率。 我怎样才能将它转换为更低的可能像https://developer.mozilla.org/en-US/docs/Web/API/OfflineAudioContext 这样的东西可以提供帮助? 下面是一些代码示例:

  var xhr = new XMLHttpRequest();
   /* HERE IS SOME CONVERTATION TO LOWER RATE */

    var fd = new FormData();

    fd.append("randomname", bigBlob);
    xhr.open("POST",url,false);
    xhr.send(fd);

    xhr.onload=function(e) {
        alert(e.target.responseText);
    };

【问题讨论】:

  • 因此您正在使用 Web Audio API 录制声音,并且您希望在将其发送到服务器之前转换为较低的采样率。这是正确的吗?

标签: web-audio-api


【解决方案1】:
  • 最后用你想要的速率创建一个 OfflineAudioContext,最后会有多少帧
  • 从原始数据缓冲区创建 AudioBuffer
  • 创建一个AudioBufferSourceNode,将其buffer属性设置为刚刚创建的AudioBuffer,并将这个AudioBufferSourceNode连接到OfflineAudioContext的目的地
  • 在 0 处启动 AudioBufferSourceNode
  • 开始渲染

【讨论】:

    【解决方案2】:

    我找不到控制采样率的方法,但这里有一种重新采样(上/下采样)的方法

    function reSample(audioBuffer, targetSampleRate, onComplete) {
        var channel = audioBuffer.numberOfChannels;
        var samples = audioBuffer.length * targetSampleRate / audioBuffer.sampleRate;
    
        var offlineContext = new 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);
        })
    }
    

    摘自这里: https://github.com/notthetup/resampler

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 2014-08-15
      • 1970-01-01
      • 2011-05-08
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      相关资源
      最近更新 更多