2015-03-02 134 views
2

我有由webaudio API生成的blob類型,但保存的文件必須採用高採樣率。 如何將它轉換爲較低的值,或許可以幫助像https://developer.mozilla.org/en-US/docs/Web/API/OfflineAudioContext這樣的東西? 下面是一些代碼示例:如何使用網絡音頻API設置採樣率?

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); 
    }; 
+0

因此,您使用Web Audio API錄製聲音,並且希望在將其發送到服務器之前轉換爲較低的採樣率。它是否正確? – aldel 2015-03-03 16:24:00

回答

2
  • 創建你想在年底速率的OfflineAudioContext和幀數會有在年底
  • 從原始數據創建AudioBuffer緩衝
  • 創建AudioBufferSourceNode,設置爲AudioBuffer剛剛創建的緩衝屬性,這AudioBufferSourceNode的AudioBufferSourceNode在0
  • 連接到OfflineAudioContext
  • 開始的目的地
  • 開始渲染
1

我無法找到一個方法來控制採樣率,但這裏是重樣的方式(上/下采樣)

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