Add Micro auto
This commit is contained in:
@@ -101,6 +101,7 @@ export default function App() {
|
||||
const audioContextRef = useRef(null)
|
||||
const analyserRef = useRef(null)
|
||||
const sourceNodeRef = useRef(null)
|
||||
const monitorGainRef = useRef(null)
|
||||
const animationFrameRef = useRef(null)
|
||||
const silenceStartedAtRef = useRef(null)
|
||||
const hasSpeechInSegmentRef = useRef(false)
|
||||
@@ -282,6 +283,10 @@ export default function App() {
|
||||
sourceNodeRef.current.disconnect()
|
||||
sourceNodeRef.current = null
|
||||
}
|
||||
if (monitorGainRef.current) {
|
||||
monitorGainRef.current.disconnect()
|
||||
monitorGainRef.current = null
|
||||
}
|
||||
if (audioContextRef.current) {
|
||||
audioContextRef.current.close().catch(() => {})
|
||||
audioContextRef.current = null
|
||||
@@ -455,16 +460,25 @@ export default function App() {
|
||||
autoGainControl: true,
|
||||
},
|
||||
})
|
||||
const audioContext = new window.AudioContext()
|
||||
const AudioContextClass = window.AudioContext || window.webkitAudioContext
|
||||
const audioContext = new AudioContextClass()
|
||||
if (audioContext.state === 'suspended') {
|
||||
await audioContext.resume()
|
||||
}
|
||||
const analyser = audioContext.createAnalyser()
|
||||
analyser.fftSize = 2048
|
||||
analyser.smoothingTimeConstant = 0.85
|
||||
const sourceNode = audioContext.createMediaStreamSource(stream)
|
||||
const monitorGain = audioContext.createGain()
|
||||
monitorGain.gain.value = 0
|
||||
sourceNode.connect(analyser)
|
||||
analyser.connect(monitorGain)
|
||||
monitorGain.connect(audioContext.destination)
|
||||
|
||||
audioContextRef.current = audioContext
|
||||
analyserRef.current = analyser
|
||||
sourceNodeRef.current = sourceNode
|
||||
monitorGainRef.current = monitorGain
|
||||
mediaStreamRef.current = stream
|
||||
setIsAutoListening(true)
|
||||
setVoiceStatus('Micro actif. Parle quand tu veux, j’enverrai après 2,5 s de silence.')
|
||||
|
||||
Reference in New Issue
Block a user