-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10592aa
commit e6b1916
Showing
41 changed files
with
853 additions
and
311 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useAudioRecorder } from '@lobehub/tts'; | ||
import { Icon } from '@lobehub/ui'; | ||
import { Button } from 'antd'; | ||
import { Mic, StopCircle } from 'lucide-react'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
export default () => { | ||
const { isRecording, start, stop, url, formattedTime } = useAudioRecorder(); | ||
return ( | ||
<Flexbox gap={8}> | ||
{isRecording ? ( | ||
<Button block icon={<Icon icon={StopCircle} />} onClick={stop}> | ||
Stop {formattedTime} | ||
</Button> | ||
) : ( | ||
<Button block icon={<Icon icon={Mic} />} onClick={start} type={'primary'}> | ||
Record | ||
</Button> | ||
)} | ||
{url && <audio controls src={url} />} | ||
</Flexbox> | ||
); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
nav: Components | ||
group: STT | ||
title: useAudioRecorder | ||
--- | ||
|
||
## hooks | ||
|
||
<code src="./demos/index.tsx" center></code> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useCallback, useMemo, useState } from 'react'; | ||
|
||
import { secondsToMinutesAndSeconds } from '@/utils/secondsToMinutesAndSeconds'; | ||
|
||
export const useAudioRecorder = (onBolbAvailable?: (blob: Blob) => void) => { | ||
const [isRecording, setIsRecording] = useState(false); | ||
|
||
const [time, setTime] = useState(0); | ||
const [mediaRecorder, setMediaRecorder] = useState<MediaRecorder>(); | ||
// eslint-disable-next-line no-undef | ||
const [timerInterval, setTimerInterval] = useState<NodeJS.Timer>(); | ||
const [blob, setBlob] = useState<Blob>(); | ||
|
||
const _startTimer = useCallback(() => { | ||
const interval = setInterval(() => { | ||
setTime((time) => time + 1); | ||
}, 1000); | ||
setTimerInterval(interval); | ||
}, []); | ||
|
||
const _stopTimer = useCallback(() => { | ||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions | ||
timerInterval !== undefined && clearInterval(timerInterval); | ||
// @ts-ignore | ||
setTimerInterval(); | ||
}, [timerInterval]); | ||
|
||
const start = useCallback(() => { | ||
setBlob(undefined); | ||
if (timerInterval !== undefined) return; | ||
|
||
navigator.mediaDevices | ||
.getUserMedia({ audio: true }) | ||
.then((stream) => { | ||
setIsRecording(true); | ||
const recorder: MediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm' }); | ||
setMediaRecorder(recorder); | ||
recorder.start(); | ||
_startTimer(); | ||
|
||
recorder.addEventListener('dataavailable', (event) => { | ||
setBlob(event.data); | ||
onBolbAvailable?.(event.data); | ||
recorder.stream.getTracks().forEach((t) => t.stop()); | ||
// @ts-ignore | ||
setMediaRecorder(); | ||
}); | ||
}) | ||
.catch((error: DOMException) => { | ||
console.log(error.name, error.message, error.cause); | ||
}); | ||
}, [timerInterval, _startTimer]); | ||
|
||
const stop = useCallback(() => { | ||
mediaRecorder?.stop(); | ||
_stopTimer(); | ||
setTime(0); | ||
setIsRecording(false); | ||
}, [mediaRecorder, _stopTimer]); | ||
|
||
const url = useMemo(() => blob && URL.createObjectURL(blob), [blob]); | ||
|
||
return { | ||
blob, | ||
formattedTime: secondsToMinutesAndSeconds(time), | ||
isRecording, | ||
mediaRecorder, | ||
start, | ||
stop, | ||
time, | ||
url, | ||
}; | ||
}; |
This file contains 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
Oops, something went wrong.