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