Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
9c4eabfc
authored
Nov 20, 2023
by
heheer
Committed by
GitHub
Nov 20, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix audio input infinite rendering (#496)
parent
b05dd0fd
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
8 deletions
+9
-8
projects/app/src/web/common/hooks/useSpeech.ts
+9
-8
No files found.
projects/app/src/web/common/hooks/useSpeech.ts
View file @
9c4eabfc
import
{
useEffect
,
useMemo
,
useRef
,
useState
}
from
'react'
;
import
{
use
Callback
,
use
Effect
,
useMemo
,
useRef
,
useState
}
from
'react'
;
import
{
POST
}
from
'../api/request'
;
import
{
useToast
}
from
'./useToast'
;
import
{
useTranslation
}
from
'next-i18next'
;
...
...
@@ -8,7 +8,8 @@ export const useSpeech = (props?: { shareId?: string }) => {
const
{
shareId
}
=
props
||
{};
const
{
t
}
=
useTranslation
();
const
mediaRecorder
=
useRef
<
MediaRecorder
>
();
const
mediaStream
=
useRef
<
MediaStream
>
();
// const mediaStream = useRef<MediaStream>();
const
[
mediaStream
,
setMediaStream
]
=
useState
<
MediaStream
>
();
const
{
toast
}
=
useToast
();
const
[
isSpeaking
,
setIsSpeaking
]
=
useState
(
false
);
const
[
isTransCription
,
setIsTransCription
]
=
useState
(
false
);
...
...
@@ -24,7 +25,7 @@ export const useSpeech = (props?: { shareId?: string }) => {
return
`
${
formattedMinutes
}
:
${
formattedSeconds
}
`
;
},
[
audioSecond
]);
const
renderAudioGraph
=
(
analyser
:
AnalyserNode
,
canvas
:
HTMLCanvasElement
)
=>
{
const
renderAudioGraph
=
useCallback
(
(
analyser
:
AnalyserNode
,
canvas
:
HTMLCanvasElement
)
=>
{
const
bufferLength
=
analyser
.
frequencyBinCount
;
const
backgroundColor
=
'white'
;
const
dataArray
=
new
Uint8Array
(
bufferLength
);
...
...
@@ -47,12 +48,12 @@ export const useSpeech = (props?: { shareId?: string }) => {
canvasCtx
.
fillRect
(
x
,
height
-
adjustedBarHeight
,
barWidth
,
adjustedBarHeight
);
x
+=
barWidth
+
1
;
}
};
}
,
[])
;
const
startSpeak
=
async
(
onFinish
:
(
text
:
string
)
=>
void
)
=>
{
try
{
const
stream
=
await
navigator
.
mediaDevices
.
getUserMedia
({
audio
:
true
});
mediaStream
.
current
=
stream
;
setMediaStream
(
stream
)
;
mediaRecorder
.
current
=
new
MediaRecorder
(
stream
);
const
chunks
:
Blob
[]
=
[];
setIsSpeaking
(
true
);
...
...
@@ -121,8 +122,8 @@ export const useSpeech = (props?: { shareId?: string }) => {
if
(
mediaRecorder
.
current
&&
mediaRecorder
.
current
.
state
!==
'inactive'
)
{
mediaRecorder
.
current
.
stop
();
}
if
(
mediaStream
.
current
)
{
mediaStream
.
current
.
getTracks
().
forEach
((
track
)
=>
track
.
stop
());
if
(
mediaStream
)
{
mediaStream
.
getTracks
().
forEach
((
track
)
=>
track
.
stop
());
}
};
},
[]);
...
...
@@ -133,7 +134,7 @@ export const useSpeech = (props?: { shareId?: string }) => {
isSpeaking
,
isTransCription
,
renderAudioGraph
,
stream
:
mediaStream
.
current
,
stream
:
mediaStream
,
speakingTimeString
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment