export function encodePcm(buffer: ArrayBuffer) { const bytes = new Uint8Array(buffer); let binary = ""; for (let index = 0; index < bytes.length; index += 0x8000) { binary += String.fromCharCode(...bytes.subarray(index, index + 0x8000)); } return btoa(binary); } export function decodePcm(base64: string) { const binary = atob(base64); const buffer = new ArrayBuffer(binary.length); const bytes = new Uint8Array(buffer); for (let index = 0; index < binary.length; index++) { bytes[index] = binary.charCodeAt(index); } const view = new DataView(buffer); const samples = new Float32Array(Math.floor(binary.length / 2)); for (let index = 0; index < samples.length; index++) { const value = view.getInt16(index * 2, true); samples[index] = value < 0 ? value / 0x8000 : value / 0x7fff; } return samples; }