chat-with-internet / app /hooks /useAudioManager.ts
matt HOFFNER
add voice chat
88cc829
raw
history blame
1.45 kB
"use client";
import { useState, useCallback } from 'react';
import constants from '../constants';
const useAudioManager = () => {
const [progress, setProgress] = useState<number | undefined>(undefined);
const [audioData, setAudioData] = useState<{
buffer: AudioBuffer;
url: string;
source: any;
mimeType: string;
} | undefined>(undefined);
// Reset the audio data
const resetAudio = useCallback(() => {
setAudioData(undefined);
}, []);
// Set audio from a Blob (e.g., from recording)
const setAudioFromRecording = useCallback(async (data: Blob) => {
resetAudio();
setProgress(0);
const blobUrl = URL.createObjectURL(data);
const audioCTX = new AudioContext({sampleRate: constants.SAMPLING_RATE, });
const arrayBuffer = await data.arrayBuffer();
const decoded = await audioCTX.decodeAudioData(arrayBuffer);
setProgress(undefined);
setAudioData({
buffer: decoded,
url: blobUrl,
source: "RECORDING",
mimeType: data.type,
});
}, [resetAudio]);
// Other functionalities (e.g., setAudioFromDownload, downloadAudioFromUrl)
// can be added similarly based on your requirements
return {
audioData,
progress,
setAudioFromRecording,
// Export other functions as needed
};
};
export default useAudioManager;