You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1003 lines
34 KiB

3 months ago
import { invoke } from '@tauri-apps/api/core';
5 months ago
import { useEffect, useRef, useState } from "react";
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition';
import { Countdown } from "../comps/timer";
import { Status, useChat } from "../util/useChat";
import { getSummary } from "../util/chat";
import { saveHistory } from "../util/output";
4 months ago
import NumPad, { NUMPAD_TYPE } from "../comps/numpad";
5 months ago
import { Light } from "../comps/light";
import { useData } from "../util/useData";
import VoiceAnalysis from "../comps/voiceanalysis";
3 months ago
import { sendOsc, OSC_ADDRESS, updatePrompt, onOscMessageReceived, sendOscStatus } from "../util/osc";
5 months ago
import { DebugControl, TEST_PROMPT } from "../comps/debug";
4 months ago
import { useUser } from "../util/useUser";
5 months ago
const CUELIST_FILE = 'cuelist_0923.json';
3 months ago
const AUDIO_FADE_TIME=3000; // in ms
5 months ago
const EmojiType={
phone: '📞',
headphone: '🎧',
speaker: '🔊',
chat: '🤖',
5 months ago
chat_end: '🤖',
5 months ago
user_input: '💬',
announce: '📢',
5 months ago
}
const ChatStatus={
System: 'system',
User: 'user',
Processing: 'processing',
5 months ago
Clear: 'clear',
4 months ago
End: 'end',
Playing: 'playing',
4 months ago
Message: 'message',
5 months ago
}
const Voice={
ONYX: 'onyx',
SHIMMER: 'shimmer',
}
5 months ago
export function FreeFlow(){
const { data }=useData();
const [cuelist, setCuelist] = useState([]);
const [currentCue, setCurrentCue] = useState(null);
4 months ago
const [nextCue, setNextCue] = useState(null);
3 months ago
const [localIP, setLocalIP] = useState(null);
4 months ago
5 months ago
const [chatWelcome, setChatWelcome] = useState(null);
const [audioInput, setAudioInput] = useState(true);
const [autoSend, setAutoSend] = useState(true);
const [chatStatus, setChatStatus] = useState(ChatStatus.System); // System, User, Processing
3 months ago
const [padInput, setPadInput] = useState(null);
2 months ago
const [inputReady, setInputReady] = useState(false);
3 months ago
const { userId, setUserId, getFileId, setPassword, reset:resetUser, uploadHistory, setSummary, summary,setChoice,choice, getUploadFolder,getDataId, writeSheet } = useUser();
5 months ago
const refTimer=useRef();
const refAudio=useRef();
4 months ago
const refAudioPrompt=useRef();
const refAudioAnnounce=useRef();
5 months ago
const refInput=useRef();
3 months ago
// const refLight=useRef();
5 months ago
5 months ago
const refPauseTimer=useRef();
const refSpeechPaused=useRef(false);
5 months ago
const refChatCueEnd=useRef(false);
5 months ago
5 months ago
const refContainer=useRef();
const refCurrentCue= useRef(null);
3 months ago
const refData=useRef(data);
5 months ago
3 months ago
const refHintTimeout=useRef();
2 months ago
const refInputTimeout=useRef();
3 months ago
const refFadeOutInterval=useRef();
3 months ago
const refVolDownInterval=useRef();
const [lastOsc, setLastOsc]=useState();
4 months ago
const { history, status, reset, sendMessage, setStatus, audioOutput, setAudioOutput, stop:stopChat,
audioUrl }=useChat();
5 months ago
5 months ago
5 months ago
const {
transcript,
finalTranscript,
listening,
resetTranscript,
browserSupportsSpeechRecognition,
isMicrophoneAvailable,
}=useSpeechRecognition();
5 months ago
function resetData() {
setSummary(null);
5 months ago
5 months ago
reset();
3 months ago
resetUser();
4 months ago
sendOsc(OSC_ADDRESS.CHOICE, 'reset');
3 months ago
sendOsc(OSC_ADDRESS.SPEECH, 'stop');
3 months ago
setPadInput();
3 months ago
setChoice();
5 months ago
}
5 months ago
4 months ago
function onOsc(payload){
console.log('onOsc', payload);
const address=payload.addr;
const message=payload.args[0];
3 months ago
const params=message.split('#');
4 months ago
if(params[0]!='all'){
console.log('set lastOsc', {address, params});
setLastOsc(()=>({address, params}));
return;
}
4 months ago
switch(address){
3 months ago
case OSC_ADDRESS.PLAY_CUE:
setNextCue(()=>params[1]);
4 months ago
break;
3 months ago
case OSC_ADDRESS.STOP_CUE:
onStop();
4 months ago
break;
3 months ago
case OSC_ADDRESS.RESET_CUE:
sendOsc(OSC_ADDRESS.STATUS, 'reset');
onStop();
resetData();
3 months ago
break;
4 months ago
}
// Handle OSC messages here
}
3 months ago
function sendPrompt(){
// send prompt
let raw_prompt=history[history.length-1]?.prompt || '';
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);
// play audio for prompt
refAudioPrompt.current?.play().catch(error => {
console.error("Audio prompt playback error:", error);
});
3 months ago
3 months ago
refAudioPrompt.current.onended = () => {
console.log('Audio prompt ended, setting chat status to User');
setChatStatus(ChatStatus.User); // Set chat status to User after audio ends
3 months ago
const user_input = history.find(msg => msg.role === 'user');
if(user_input && user_input.content.trim() !== '') {
sendOsc(OSC_ADDRESS.STATUS, 'go'); // Send OSC status message
}
3 months ago
}
5 months ago
3 months ago
}else{
setChatStatus(()=>ChatStatus.User); // Reset chat status to User after audio ends
5 months ago
}
3 months ago
}
function playAudio(url){
if(!url) return;
5 months ago
5 months ago
console.log('Playing audio:', url);
if(refCurrentCue.current?.layer=='announce'){
if(refAudioAnnounce.current) {
refAudioAnnounce.current.pause(); // Stop any currently playing announce audio
}
refAudioAnnounce.current = new Audio(url);
refAudioAnnounce.current.loop=refCurrentCue.current?.loop || false; // Set loop if defined in cue
refAudioAnnounce.current.play().catch(error => {
console.error("Audio announce playback error:", error);
});
3 months ago
// lower audio
if(refAudio.current) {
// fade out current audio
if(refVolDownInterval.current){
clearInterval(refVolDownInterval.current);
}
const dest=0.3;
3 months ago
let fadeOutInterval = setInterval(() => {
if (refAudio.current.volume > dest) {
refAudio.current.volume =Math.max(dest, refAudio.current.volume - 1.0/(AUDIO_FADE_TIME/100)); // Decrease volume gradually
} else {
clearInterval(fadeOutInterval);
}
}, 100);
refVolDownInterval.current=fadeOutInterval;
}
return;
}
if(refAudioAnnounce.current) {
refAudioAnnounce.current.pause(); // Stop any currently playing announce audio
refAudioAnnounce.current = null;
}
5 months ago
if(refAudio.current) {
refAudio.current.pause(); // Stop any currently playing audio
}
let audioUrl = url;
// if(voice==Voice.SHIMMER) audioUrl = url.replace(Voice.ONYX, Voice.SHIMMER);
// console.log('Using voice:', voice, 'for audio:', audioUrl);
const audio = new Audio(audioUrl);
3 months ago
5 months ago
3 months ago
//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;
// }
}
audio.loop=refCurrentCue.current?.loop || false; // Set loop if defined in cue
5 months ago
5 months ago
audio.addEventListener("loadedmetadata", () => {
if(refCurrentCue.current?.type!='chat' && refCurrentCue.current?.type!='user_input') {
refTimer.current?.restart(audio.duration*1000 || 0);
3 months ago
audio.play().catch(error => {
console.error("Audio playback error:", error);
});
5 months ago
}else{
3 months ago
if(refCurrentCue.current?.type=='chat'){
if(refTimer.current?.remainingTime < audio.duration*1000) {
console.log('Audio duration is longer than remaining cue time, not playing audio:', url);
// send propmpt
sendPrompt();
return;
}else{
setChatStatus(()=>ChatStatus.System);
audio.play().catch(error => {
console.error("Audio playback error:", error);
});
}
}else{
setChatStatus(()=>ChatStatus.Playing);
audio.play().catch(error => {
console.error("Audio playback error:", error);
});
}
5 months ago
}
});
5 months ago
audio.onended = () => {
4 months ago
if(refCurrentCue.current?.type!='chat'){
setChatStatus(ChatStatus.End);
4 months ago
onCueEnd();
console.log('Audio ended, ending current cue');
3 months ago
}else{
5 months ago
// if history contains user input, send it
3 months ago
3 months ago
sendPrompt();
5 months ago
}
}
refAudio.current = audio; // Store the new audio reference
5 months ago
5 months ago
}
3 months ago
function fadeOutAudio(callback){
2 months ago
if(refVolDownInterval.current) clearInterval(refVolDownInterval.current);
if(refAudio.current || refAudioAnnounce.current){
3 months ago
console.log('Fading out audio');
let audio = refAudio.current;
let announce = refAudioAnnounce.current;
if(refFadeOutInterval.current){
clearInterval(refFadeOutInterval.current);
refFadeOutInterval.current=null;
}
3 months ago
let fadeOutInterval = setInterval(() => {
if(audio){
if (audio.volume > 0) {
audio.volume =Math.max(0, audio.volume - 1.0/(AUDIO_FADE_TIME/100)); // Decrease volume gradually
} else {
audio.pause();
audio.volume = 0; // Reset volume for next play
2 months ago
}
}
2 months ago
if(announce){
if (announce.volume > 0) {
announce.volume = Math.max(0, announce.volume - 1.0/(AUDIO_FADE_TIME/100)); // Decrease volume gradually
} else {
//clearInterval(fadeOutInterval);
announce.pause();
announce.volume = 0; // Reset volume for next play
}
3 months ago
}
2 months ago
2 months ago
if((audio==null || audio.volume==0) && (announce==null || announce.volume==0)){
2 months ago
clearInterval(fadeOutInterval);
if(callback) callback();
}
}, 100); // Decrease volume every 100ms
refFadeOutInterval.current=fadeOutInterval;
3 months ago
}else{
if(callback) callback();
}
}
5 months ago
function playCue(cue) {
if(!cue) return;
console.log('Playing cue:', cue);
3 months ago
// stop audio
// if(refAudio.current) refAudio.current.pause();
5 months ago
5 months ago
setCurrentCue(cue);
refCurrentCue.current = cue; // Store the current cue in ref
5 months ago
5 months ago
if(parseFloat(cue.id)<=4.2){
// Special case for starting a conversation
console.log('clear conversation...');
reset();
5 months ago
5 months ago
const prompt = `${data?.sd_prompt_prefix || ''}${TEST_PROMPT}${data?.sd_prompt_suffix || ''}`;
5 months ago
updatePrompt(prompt);
5 months ago
}
4 months ago
// clear unity hint
3 months ago
if(refHintTimeout.current) clearTimeout(refHintTimeout.current);
4 months ago
sendOsc(OSC_ADDRESS.HINT, ''); // Clear hint message
sendOsc(OSC_ADDRESS.INPUT, ''); // Clear input message
3 months ago
sendOsc(OSC_ADDRESS.SPEECH, 'stop');
5 months ago
3 months ago
setPadInput();
5 months ago
switch(cue.type){
case 'chat':
// Special case for starting a conversation
5 months ago
refChatCueEnd.current=false;
5 months ago
resetTranscript();
console.log('Starting conversation...');
setChatStatus(ChatStatus.User);
//sendMessage(null, false, false, null); // Send initial message with voice
//setChatWelcome(true);
3 months ago
//resetData(); // Reset data for new conversation
3 months ago
break;
3 months ago
4 months ago
case 'summary':
console.log('Getting summary...');
5 months ago
4 months ago
setChatStatus(ChatStatus.Clear); // Set chat status to Processing
4 months ago
let message=refInput.current?.value?.trim() || history.map(el=>`${el.role}:${el.content}`).join('\n');
3 months ago
console.log('Summary input message:', message);
if(!message || message.length==0) {
5 months ago
3 months ago
setSummary();
console.log('no message input, clear summary');
4 months ago
onCueEnd(); // End the current cue after getting summary
5 months ago
3 months ago
}else{
getSummary(message, data).then(summary_ => {
console.log('Summary:', summary_);
onCueEnd(); // End the current cue after getting summary
setSummary(()=>summary_?.result);
refContainer.current.scrollTop = refContainer.current.scrollHeight; // Scroll to bottom
}).catch(error => {
console.error('Error getting summary:', error);
});
}
5 months ago
4 months ago
4 months ago
break;
5 months ago
case 'user_input':
4 months ago
setChatStatus(ChatStatus.Message); // Set chat status to User
5 months ago
resetTranscript(); // Reset transcript for user input
4 months ago
break;
default:
setChatStatus(ChatStatus.Clear);
break;
5 months ago
}
5 months ago
3 months ago
// if(cue.callback=='fade_in_light') refLight.current.fadeIn(); // Fade in light for conversation start
// if(cue.callback=='fade_out_light') refLight.current.fadeOut(); // Fade out light for conversation end
5 months ago
3 months ago
if(cue.hint!=null && cue.hint_time!=null){
3 months ago
refHintTimeout.current=setTimeout(()=>{
sendOsc(OSC_ADDRESS.HINT, cue.hint); // Send OSC hint message
2 months ago
}, cue.hint_time);
}
5 months ago
2 months ago
setInputReady(false);
if(cue.input_time!=null){
if(refInputTimeout.current) clearTimeout(refInputTimeout.current);
refInputTimeout.current=setTimeout(()=>{
setInputReady(()=>true);
}, cue.input_time);
}
5 months ago
if(cue.audioFile){
playAudio(cue.audioFile);
}
if(cue.duration){
refTimer.current.restart(cue.duration*1000, ()=>{
onCueEnd(cue);
});
}
5 months ago
switch(cue.callback){
case 'exportFile':
exportFile();
break;
// case 'fadeout':
// fadeOutAudio();
// break;
3 months ago
}
5 months ago
// control unity
5 months ago
if(cue.status && cue.status!='go') {
3 months ago
if(cue.status_delay) {
setTimeout(()=>{
sendOsc(OSC_ADDRESS.STATUS, cue.status); // Send OSC status message
}, cue.status_delay);
}else{
sendOsc(OSC_ADDRESS.STATUS, cue.status); // Send OSC status message
}
5 months ago
if(cue.status=='reset') {
3 months ago
// refLight.current.set(1);
4 months ago
resetData();
5 months ago
}
5 months ago
}
5 months ago
if(cue.type=='chat' || cue.type=='user_input') {
sendOsc(OSC_ADDRESS.COUNTDOWN, cue.duration || '0'); // Send OSC countdown message
5 months ago
}else{
5 months ago
sendOsc(OSC_ADDRESS.COUNTDOWN, '0'); // Reset countdown for non-chat cues
5 months ago
}
3 months ago
if(cue.numpad_type=='choice'){
setChoice();
}
5 months ago
3 months ago
sendOscStatus(OSC_ADDRESS.CLIENT_STATUS,`${data.id}#playcue#${cue.id}`);
5 months ago
console.log('~~~~ clear pause timer');
5 months ago
if(refPauseTimer.current) clearTimeout(refPauseTimer.current);
5 months ago
// refSpeechPaused.current=false;
5 months ago
}
function onCueEnd() {
5 months ago
5 months ago
refTimer.current?.stop(); // Stop the timer when cue ends
5 months ago
5 months ago
if(!refCurrentCue.current) return;
const cue= refCurrentCue.current; // Get the current cue from ref
5 months ago
if(cue.type=='chat'){
3 months ago
// if(chatStatus==ChatStatus.System) {
// console.log('Still talking...');
// refChatCueEnd.current=true;
// return;
// }
4 months ago
console.log('save chat history:', history);
4 months ago
uploadHistory(history); // Save chat history when cue ends
5 months ago
}
3 months ago
4 months ago
2 months ago
sendOsc(OSC_ADDRESS.HINT, ''); // Clear hint message
3 months ago
sendOsc(OSC_ADDRESS.SPEECH, 'stop');
4 months ago
3 months ago
if(cue.numpad_type=='choice'){
3 months ago
if(!choice){
console.log('set default choice to save');
setChoice('save');
sendOsc(OSC_ADDRESS.CHOICE, 'save'); // Send OSC save choice message
3 months ago
}else{
3 months ago
// sendOsc(OSC_ADDRESS.CHOICE, choice); // Send OSC save choice message
3 months ago
}
}
4 months ago
refAudio.current?.pause(); // Pause any playing audio
5 months ago
console.log('onCueEnd:', cue.id);
5 months ago
5 months ago
resetTranscript(); // Reset transcript after cue ends
3 months ago
sendOscStatus(OSC_ADDRESS.CLIENT_STATUS, `${data.id}#endcue#${cue.id}`);
3 months ago
if(cue.auto || cue.callback=='numpad'){
5 months ago
playCue(cuelist.find(c => c.id === cue.nextcue));
}
}
function onStop(){
console.log('Stopping current cue');
if(refAudio.current) {
refAudio.current.pause();
refAudio.current = null;
}
if(refAudioAnnounce.current) {
refAudioAnnounce.current.pause();
refAudioAnnounce.current = null;
}
5 months ago
setCurrentCue(null);
refCurrentCue.current = null; // Clear the current cue reference
refTimer.current.restart(0);
stopChat(); // Stop chat processing
}
function onNumpad(mess){
3 months ago
console.log('onNumPad',mess);
setPadInput(()=>mess);
3 months ago
}
3 months ago
function exportFile(){
const user_input = history.find(msg => msg.role === 'user');
const default_image=!(user_input && user_input.content.trim() !== '');
sendOsc(OSC_ADDRESS.EXPORT, `${getUploadFolder()}#${getDataId()}#${summary||''}#${getFileId(padInput)}#${choice||''}#${default_image?'default':'generated'}`); // Send OSC export message
writeSheet();
3 months ago
}
useEffect(()=>{
if(!lastOsc) return;
console.log('Process last OSC:', lastOsc);
if(lastOsc.params[0]!=data.id) return;
switch(lastOsc.address){
case OSC_ADDRESS.PLAY_CUE:
setNextCue(()=>lastOsc.params[1]);
break;
case OSC_ADDRESS.STOP_CUE:
onStop();
break;
case OSC_ADDRESS.RESET_CUE:
sendOsc(OSC_ADDRESS.STATUS, 'reset');
onStop();
resetData();
break;
}
},[lastOsc]);
3 months ago
useEffect(()=>{
if(padInput==null) return;
console.log('Numpad input:', padInput);
5 months ago
if(refCurrentCue.current?.callback!='numpad') return;
let cue=refCurrentCue.current;
let next=cue.nextcue;
4 months ago
switch(cue.numpad_type){
case NUMPAD_TYPE.USERID:
3 months ago
console.log('set id', padInput);
3 months ago
setUserId(()=>padInput);
break;
4 months ago
case NUMPAD_TYPE.CHOICE:
3 months ago
next=cue.branch[padInput.toString()].nextcue;
3 months ago
setChoice(()=>cue.branch[padInput.toString()].description); // Set choice for user input
sendOsc(OSC_ADDRESS.CHOICE, cue.branch[padInput.toString()].description);
break;
4 months ago
case NUMPAD_TYPE.PASSWORD:
3 months ago
setPassword(padInput);
4 months ago
// sendOsc(OSC_ADDRESS.PASSWORD, mess); // Send OSC password message
3 months ago
// sendOsc(OSC_ADDRESS.CHOICE, choice); // Send OSC save choice message
break;
}
if(next){
4 months ago
onStop();
console.log('Finish enter number, next cue:', next);
playCue(cuelist.find(c => c.id === next));
}
3 months ago
},[padInput]);
5 months ago
useEffect(()=>{
if(userId>=1 && userId<=24) {
console.log('User ID set:', userId);
4 months ago
//playCue(cuelist.find(c => c.id === refCurrentCue.current.nextcue)); // Play cue 5 when userId is set
5 months ago
}
},[userId]);
function onSpeechEnd(){
5 months ago
3 months ago
sendOsc(OSC_ADDRESS.SPEECH, 'stop');
5 months ago
if(currentCue?.type!='chat') return; // Only process if current cue is user input
5 months ago
if(chatStatus!=ChatStatus.User) return; // Only process if chat status is User
5 months ago
5 months ago
console.log('~~~ on speech end, start pause timer',data.speech_idle_time);
// refSpeechPaused.current=true;
5 months ago
if(refPauseTimer.current) clearTimeout(refPauseTimer.current);
refPauseTimer.current=setTimeout(()=>{
5 months ago
console.log('~~~ pause timer ended, process speech');
// if(refSpeechPaused.current)
processSpeech();
5 months ago
}, data.speech_idle_time);
}
function processSpeech(){
if(currentCue?.type!='chat') return; // Only process if current cue is user input
console.log('processSpeech:', finalTranscript);
4 months ago
if(refChatCueEnd.current) {
console.log('Chat cue has ended, do not processing speech');
onCueEnd();
return;
}
5 months ago
if(autoSend && transcript.trim().length > 0) {
console.log('Auto sending transcript:', transcript);
// onCueEnd();
const message= refInput.current?.value?.trim();
if(message && message.length>0) {
console.log('Ending conversation with message:', message);
sendMessage(message, false, false, null);
5 months ago
setChatWelcome(false);
4 months ago
5 months ago
setChatStatus(ChatStatus.Processing); // Set chat status to Processing
}
resetTranscript();
}
}
4 months ago
function manualSendMessage() {
if(currentCue?.type!='chat') return; // Only process if current cue is user input
if(chatStatus!=ChatStatus.User) return; // Only process if chat status is User
const message= refInput.current?.value?.trim();
if(message && message.length>0) {
console.log('Manual sending message:', message);
sendMessage(message, false, false, null);
setChatWelcome(false);
setChatStatus(ChatStatus.Processing); // Set chat status to Processing
}
resetTranscript();
}
5 months ago
useEffect(()=>{
2 months ago
console.log('Final transcript changed:', finalTranscript);
5 months ago
if(finalTranscript.trim().length > 0) {
onSpeechEnd();
}
5 months ago
},[finalTranscript]);
5 months ago
function startRecognition() {
SpeechRecognition.startListening({ continuous: true, language: 'zh-TW' }).then(() => {
console.log("Speech recognition started.");
}).catch(error => {
console.error("Error starting speech recognition:", error);
});
}
3 months ago
function blurText(text) {
if(!text) return '';
return text.replace(/./g, '*');
}
5 months ago
useEffect(()=>{
if(audioInput && isMicrophoneAvailable) {
5 months ago
startRecognition();
5 months ago
const recognition= SpeechRecognition.getRecognition();
5 months ago
5 months ago
recognition.onspeechstart=(e)=>{
5 months ago
console.log('Speech start:', e);
5 months ago
};
5 months ago
// recognition.onspeechend=(e)=>{
// console.log('Speech end:', e);
// startRecognition();
// };
5 months ago
}else{
console.log('Stopping speech recognition...');
SpeechRecognition.stopListening();
}
},[audioInput]);
useEffect(()=>{
5 months ago
if((currentCue?.type=='chat' && chatStatus==ChatStatus.User) || currentCue?.type=='user_input') {
5 months ago
5 months ago
// console.log('transcript state changed:', transcript);
if(transcript!=finalTranscript){
5 months ago
refInput.current.value = transcript;
5 months ago
// clear pause timer
// console.log('~~~~ clear pause timer');
if(refPauseTimer.current) clearTimeout(refPauseTimer.current);
refSpeechPaused.current=false;
5 months ago
}
3 months ago
sendOsc(OSC_ADDRESS.SPEECH, 'start');
3 months ago
sendOscStatus(OSC_ADDRESS.CLIENT_INPUT, `${data.id}#${transcript}`);
// Send current input via OSC
3 months ago
3 months ago
}
5 months ago
},[transcript]);
useEffect(()=>{
3 months ago
if(refCurrentCue.current?.type!='chat') return;
5 months ago
if(audioUrl) playAudio(audioUrl);
},[audioUrl]);
useEffect(()=>{
resetTranscript();
4 months ago
let text='';
switch(chatStatus) {
case ChatStatus.System:
text = '等我一下\n換我說囉';
break;
case ChatStatus.User:
text = '換你說了';
break;
case ChatStatus.Processing:
text = '記憶讀取中';
break;
case ChatStatus.Message:
text = '請留言';
break;
case ChatStatus.Clear:
default:
text = '';
break;
}
3 months ago
sendOsc(OSC_ADDRESS.SPEECH, 'stop');
4 months ago
sendOsc(OSC_ADDRESS.INPUT, text);
5 months ago
},[chatStatus]);
useEffect(()=>{
switch(status) {
case Status.SUCCESS:
5 months ago
console.log('Success!');
5 months ago
setStatus(Status.IDLE);
refInput.current.value = '';
resetTranscript();
refContainer.current.scrollTop = refContainer.current.scrollHeight;
break;
}
},[status]);
4 months ago
useEffect(()=>{
if(!nextCue) return;
console.log('Next cue:', nextCue);
3 months ago
const next=cuelist.find(c => c.name === nextCue);
if(currentCue?.fadeout){
3 months ago
// fade out audio
fadeOutAudio(()=>{
console.log('fade out then play next cue:', next);
playCue(next);
setNextCue(()=>null);
});
}else{
3 months ago
3 months ago
playCue(next);
setNextCue(null);
}
4 months ago
},[nextCue]);
5 months ago
useEffect(()=>{
fetch(CUELIST_FILE)
5 months ago
.then(response => response.json())
.then(data => {
console.log('Cuelist data:', data);
setCuelist(data.cuelist);
})
.catch(error => {
console.error('Error fetching cuelist:', error);
});
4 months ago
refAudioPrompt.current = new Audio('assets/sfx/sfx-05.mp3'); // Load audio prompt if available
4 months ago
onOscMessageReceived(onOsc); // Set up OSC message listener
3 months ago
invoke('get_ip').then((ip)=>{
console.log('Local IP address:', ip);
setLocalIP(ip);
});
5 months ago
},[]);
return (
5 months ago
<main className="items-start">
<section className="flex-1 flex flex-col gap-2 self-stretch overflow-hidden">
3 months ago
<div className="bg-purple-300 text-3xl flex flex-row justify-between px-4 items-center gap-1">
<span className='font-bold'>PC {data?.id}</span>
<span className="">{localIP || '...'}</span>
</div>
3 months ago
<DebugControl onReset={resetData}/>
2 months ago
<div className="w-full p-2 grid grid-cols-3 gap-2 items-stretch justify-center *:max-h-[5rem]">
5 months ago
<div className="bg-gray-100 text-4xl font-bold mb-4 flex justify-center items-center">
{refCurrentCue.current?.name}
</div>
3 months ago
<Countdown ref={refTimer} clientId={data?.id}/>
5 months ago
<button className="!bg-red-300" onClick={onStop}>Stop</button>
{/* <button onClick={saveImage}>Save image</button> */}
4 months ago
<NumPad onSend={onNumpad}
2 months ago
disabled={currentCue?.callback !== 'numpad' || !inputReady}
4 months ago
type={currentCue?.numpad_type}
3 months ago
clientId={data?.id}
4 months ago
/>
3 months ago
{/* <Light ref={refLight} /> */}
2 months ago
{/* <VoiceAnalysis/> */}
3 months ago
5 months ago
{/* <div className="flex flex-col">
<label className="text-center">Voice</label>
<button className={`${voice==Voice.ONYX && 'bg-gray-300'}`} onClick={() => setVoice(Voice.ONYX)}>Onyx</button>
<button className={`${voice==Voice.SHIMMER && 'bg-gray-300'}`} onClick={() => setVoice(Voice.SHIMMER)}>Shimmer</button>
5 months ago
</div> */}
4 months ago
<div className="grid grid-cols-[1fr_2fr] relative text-xs">
<label className="col-span-1">user</label>
<span></span>
<span>UserId</span><span>{userId}</span>
<span>FileId</span><span>{getFileId()}</span>
3 months ago
<span>Choice</span><span>{choice || ''}</span>
4 months ago
</div>
3 months ago
5 months ago
</div>
5 months ago
<div className="flex-1 overflow-y-auto">
5 months ago
<table className="border-collapse **:border-y w-full **:p-2 text-sm">
5 months ago
<thead>
<tr className="text-left">
{/* <th>ID</th> */}
5 months ago
<th></th>
5 months ago
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Auto</th>
3 months ago
<th>Audio</th>
<th>Duration</th>
5 months ago
<th>Action</th>
5 months ago
</tr>
5 months ago
</thead>
<tbody>
{cuelist?.map(({id, name, description, type, auto, audioFile,...props}, index) => (
<tr key={id}>
{/* <td>{id}</td> */}
<td>
<button className="rounded-full !bg-green-200"
onClick={()=>{
// playCue({id, name, description, type, auto, audioFile, ...props});
setNextCue(()=>name);
5 months ago
}}>go</button>
</td>
5 months ago
<td>{name}</td>
<td>{description}</td>
<td>{EmojiType[type]}</td>
<td>{auto ? '⤵' : ''}</td>
3 months ago
<td>{audioFile || ""}</td>
<td>{props.duration || ''}</td>
5 months ago
<td>{props.callback && `<${props.callback}>`}{props.status && `(${props.status})`}</td>
5 months ago
</tr>
))}
</tbody>
</table>
</div>
</section>
3 months ago
<section className="flex-1 self-stretch overflow-y-auto flex flex-col justify-end gap-2 ">
<div ref={refContainer} className="flex-1 flex flex-col overflow-y-auto gap-2 blur-sm" >
5 months ago
{history?.map((msg, index) => (
<div key={index} className={`w-5/6 ${msg.role=='user'? 'self-end':''}`}>
3 months ago
<div className={`${msg.role=='user'? 'bg-green-300':'bg-pink-300'} px-2`}>{blurText(msg.content)}</div>
{msg.prompt && <div className="text-xs bg-gray-200">{blurText(msg.prompt)}</div>}
5 months ago
</div>
))}
3 months ago
{summary && <div className="w-full self-center bg-blue-200 px-2">{summary}</div>}
5 months ago
</div>
<textarea ref={refInput} name="message" rows={2}
3 months ago
className={`w-full border-1 resize-none p-2 disabled:bg-gray-500 blur-sm`}
4 months ago
disabled={chatStatus!=ChatStatus.User && chatStatus!=ChatStatus.Message}></textarea>
5 months ago
<div className="flex flex-row justify-end gap-2 flex-wrap">
<span className="flex flex-row gap-1">
<label>audio_output</label>
<input type='checkbox' checked={audioOutput} onChange={(e) => setAudioOutput(e.target.checked)} />
</span>
<span className="flex flex-row gap-1">
<label>audio_input</label>
<input type='checkbox' checked={audioInput} onChange={(e) => setAudioInput(e.target.checked)} />
</span>
<span className="flex flex-row gap-1">
<label>auto_send</label>
<input type='checkbox' checked={autoSend} onChange={(e) => setAutoSend(e.target.checked)} />
</span>
<button onClick={resetTranscript}>reset transcript</button>
4 months ago
<button onClick={manualSendMessage} disabled={chatStatus!=ChatStatus.User && chatStatus!=ChatStatus.Message}>Send</button>
5 months ago
<div className="rounded-2xl bg-gray-300 self-end px-4 tracking-widest">api_status= {status}</div>
<div className="rounded-2xl bg-gray-300 self-end px-4 tracking-widest">chat_status= {chatStatus}</div>
</div>
5 months ago
</section>
5 months ago
</main>
);
}