|
"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); |
|
|
|
|
|
const resetAudio = useCallback(() => { |
|
setAudioData(undefined); |
|
}, []); |
|
|
|
|
|
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]); |
|
|
|
|
|
|
|
|
|
return { |
|
audioData, |
|
progress, |
|
setAudioFromRecording, |
|
|
|
}; |
|
}; |
|
|
|
export default useAudioManager; |
|
|