Created
June 8, 2024 15:14
-
-
Save indiejoseph/44370c98e6768e6e58c1ea2e0e47e423 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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