diff --git a/vite/src/pages/flow_free.jsx b/vite/src/pages/flow_free.jsx index 709aa9a..2e7acdd 100644 --- a/vite/src/pages/flow_free.jsx +++ b/vite/src/pages/flow_free.jsx @@ -58,6 +58,7 @@ export function FreeFlow(){ const refPauseTimer=useRef(); const refSpeechPaused=useRef(false); + const refChatCueEnd=useRef(false); const refContainer=useRef(); @@ -78,12 +79,26 @@ export function FreeFlow(){ function resetData() { setSummary(null); + reset(); } function playAudio(url){ if(!url) return; + //TODO: if cue end, don't play audio + if(refCurrentCue.current?.type=='chat'){ + if(refChatCueEnd.current) { + console.log('Chat cue has ended, not playing audio:', url); + setChatStatus(ChatStatus.Clear); // Reset chat status to Clear + onCueEnd(); + return; + } + // if audio time larger than cue remaining time, don't play audio + + } + + console.log('Playing audio:', url); if(refAudio.current) { @@ -101,6 +116,15 @@ export function FreeFlow(){ }); + audio.addEventListener("loadedmetadata", () => { + if(refCurrentCue.current?.type!='chat' && refCurrentCue.current?.type!='user_input') { + refTimer.current?.restart(audio.duration*1000 || 0); + }else{ + if(refCurrentCue.current?.type=='chat') setChatStatus(()=>ChatStatus.System); + else setChatStatus(()=>ChatStatus.Clear); + } + }); + audio.onended = () => { if(refCurrentCue.current?.type!='chat') onCueEnd(); else{ @@ -119,30 +143,32 @@ export function FreeFlow(){ if(raw_prompt && raw_prompt.trim() !== '') { const prompt = `${data?.sd_prompt_prefix || ''}${raw_prompt}${data?.sd_prompt_suffix || ''}`; - + updatePrompt(prompt); sendOsc(OSC_ADDRESS.PROMPT, prompt); } + + if(refChatCueEnd.current){ + console.log('Talking ended, ending current cue'); + onCueEnd(); // End the current cue if chat cue has ended + } } } refAudio.current = audio; // Store the new audio reference - audio.addEventListener("loadedmetadata", () => { - if(refCurrentCue.current?.type!='chat' && refCurrentCue.current?.type!='user_input') { - refTimer.current?.restart(audio.duration*1000 || 0); - }else{ - if(refCurrentCue.current?.type=='chat') setChatStatus(()=>ChatStatus.System); - else setChatStatus(()=>ChatStatus.Clear); - } - }); + } function playCue(cue) { if(!cue) return; console.log('Playing cue:', cue); + + setCurrentCue(cue); refCurrentCue.current = cue; // Store the current cue in ref + + if(parseFloat(cue.id)<=4.2){ // Special case for starting a conversation @@ -158,6 +184,7 @@ export function FreeFlow(){ switch(cue.type){ case 'chat': // Special case for starting a conversation + refChatCueEnd.current=false; resetTranscript(); console.log('Starting conversation...'); sendMessage(null, false, false, voice); // Send initial message with voice @@ -231,13 +258,22 @@ export function FreeFlow(){ } function onCueEnd() { - + refTimer.current?.stop(); // Stop the timer when cue ends - refAudio.current?.pause(); // Pause any playing audio - + if(!refCurrentCue.current) return; const cue= refCurrentCue.current; // Get the current cue from ref + if(cue.type=='chat'){ + if(chatStatus==ChatStatus.System) { + console.log('Still talking...'); + refChatCueEnd.current=true; + return; + } + } + + + refAudio.current?.pause(); // Pause any playing audio console.log('onCueEnd:', cue.id);