How to adjust sample rate using web audio API?

I have a blob type created by the webaudio API, but the saved file has a high sampling rate. How can I convert it to lower, maybe something like https://developer.mozilla.org/en-US/docs/Web/API/OfflineAudioContext can help? Here are some sample code:

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); }; 
+2
source share
2 answers
  • Create a standalone audio context at the speed you want at the end and the number of frames at the end will be
  • Create an AudioBuffer from the source data buffer
  • Create an AudioBufferSourceNode, set its buffer attribute to the newly created AudioBuffer, and connect this AudioBufferSourceNode to the destination of the stand-alone stand-alone interface
  • Run AudioBufferSourceNode with 0
  • Run rendering
+2
source

I could not find a way to control the sampling rate, but here is a way to re-sample (up / down sampling)

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

Extracted from here: https://github.com/notthetup/resampler

+1
source

All Articles