Skip to content

Instantly share code, notes, and snippets.

@indiejoseph
Created June 8, 2024 15:14
Show Gist options
  • Save indiejoseph/44370c98e6768e6e58c1ea2e0e47e423 to your computer and use it in GitHub Desktop.
Save indiejoseph/44370c98e6768e6e58c1ea2e0e47e423 to your computer and use it in GitHub Desktop.
const resampling = (audioBuffer: AudioBuffer, targetSampleRate: number): Promise<AudioBuffer> => {
const offlineAudioContext = new OfflineAudioContext(1, audioBuffer.length, targetSampleRate);
const source = offlineAudioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(offlineAudioContext.destination);
source.start();
return offlineAudioContext.startRendering();
};
const convertBlobToAudioBuffer = async (myBlob: Blob): Promise<AudioBuffer> => {
const audioContext = new AudioContext();
const fileReader = new FileReader();
return new Promise((resolve, reject) => {
fileReader.onloadend = () => {
let myArrayBuffer = fileReader.result;
if (!(myArrayBuffer instanceof ArrayBuffer)) {
return reject(new Error('Failed to convert Blob to ArrayBuffer'));
}
audioContext.decodeAudioData(myArrayBuffer, audioBuffer => {
resolve(audioBuffer);
});
};
//Load blob
fileReader.readAsArrayBuffer(myBlob);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment