I have a AudioBuffer from AudioContext, and I get Float32Array from AudioBuffer.getChannelData,It's in range -1 to 1, and I dont know how to convert the data into Uint8Array, then I can encode it into base64 string.
const sampleRate = 16000
const sampleBit = 16
const context = new AudioContext({
sampleRate: sampleRate
})
const audioSliceMs = 160
let streamEnd = false
const audioSliceByte = sampleRate * sampleBit / 8 / 1000 * audioSliceMs
const recorderScriptNode = context.createScriptProcessor(1024, 1, 1);
let bufferOffset = 0
let bufferFloat32Array = new Float32Array(audioSliceByte)
recorderScriptNode.onaudioprocess = function (audioProcessingEvent) {
// Float32Array -1.0 to 1.0
const float32Array = audioProcessingEvent.inputBuffer.getChannelData(0)
// convert here ...
}
expecting convert code snipet
To convert the Float32Array data (which is in the range of -1.0 to 1.0) to Uint8Array and then encode it into a base64 string, you need to follow these steps:
Here's a code snippet that demonstrates these steps: