@ -130,23 +130,35 @@ export function FreeFlow(){
/ / H a n d l e O S C m e s s a g e s h e r e
}
function sendPrompt ( ) {
/ / s e n d p r o m p t
let raw _prompt = history [ history . length - 1 ] ? . prompt || '' ;
function playAudio ( url ) {
if ( ! url ) return ;
if ( raw _prompt && raw _prompt . trim ( ) !== '' ) {
const prompt = ` ${ data ? . sd _prompt _prefix || '' } ${ raw _prompt } ${ data ? . sd _prompt _suffix || '' } ` ;
/ / T O D O : i f c u e e n d , d o n ' t p l a y a u d i o
if ( refCurrentCue . current ? . type == 'chat' ) {
/ / i f ( r e f C h a t C u e E n d . c u r r e n t ) {
/ / c o n s o l e . l o g ( ' C h a t c u e h a s e n d e d , n o t p l a y i n g a u d i o : ' , u r l ) ;
/ / s e t C h a t S t a t u s ( C h a t S t a t u s . C l e a r ) ; / / R e s e t c h a t s t a t u s t o C l e a r
/ / o n C u e E n d ( ) ;
/ / r e t u r n ;
/ / }
/ / i f a u d i o t i m e l a r g e r t h a n c u e r e m a i n i n g t i m e , d o n ' t p l a y a u d i o
updatePrompt ( prompt ) ;
sendOsc ( OSC _ADDRESS . PROMPT , prompt ) ;
/ / p l a y a u d i o f o r p r o m p t
refAudioPrompt . current ? . play ( ) . catch ( error => {
console . error ( "Audio prompt playback error:" , error ) ;
} ) ;
refAudioPrompt . current . onended = ( ) => {
console . log ( 'Audio prompt ended, setting chat status to User' ) ;
setChatStatus ( ChatStatus . User ) ; / / S e t c h a t s t a t u s t o U s e r a f t e r a u d i o e n d s
}
} else {
setChatStatus ( ( ) => ChatStatus . User ) ; / / R e s e t c h a t s t a t u s t o U s e r a f t e r a u d i o e n d s
}
}
function playAudio ( url ) {
if ( ! url ) return ;
console . log ( 'Playing audio:' , url ) ;
if ( refAudio . current ) {
@ -158,18 +170,52 @@ export function FreeFlow(){
/ / c o n s o l e . l o g ( ' U s i n g v o i c e : ' , v o i c e , ' f o r a u d i o : ' , a u d i o U r l ) ;
const audio = new Audio ( audioUrl ) ;
/ / T O D O : i f c u e e n d , d o n ' t p l a y a u d i o
if ( refCurrentCue . current ? . type == 'chat' ) {
/ / i f ( r e f C h a t C u e E n d . c u r r e n t ) {
/ / c o n s o l e . l o g ( ' C h a t c u e h a s e n d e d , n o t p l a y i n g a u d i o : ' , u r l ) ;
/ / s e t C h a t S t a t u s ( C h a t S t a t u s . C l e a r ) ; / / R e s e t c h a t s t a t u s t o C l e a r
/ / o n C u e E n d ( ) ;
/ / r e t u r n ;
/ / }
}
audio . loop = refCurrentCue . current ? . loop || false ; / / S e t l o o p i f d e f i n e d i n c u e
audio . play ( ) . catch ( error => {
console . error ( "Audio playback error:" , error ) ;
} ) ;
audio . addEventListener ( "loadedmetadata" , ( ) => {
if ( refCurrentCue . current ? . type != 'chat' && refCurrentCue . current ? . type != 'user_input' ) {
refTimer . current ? . restart ( audio . duration * 1000 || 0 ) ;
audio . play ( ) . catch ( error => {
console . error ( "Audio playback error:" , error ) ;
} ) ;
} else {
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 ) ;
/ / s e n d p r o p m p t
sendPrompt ( ) ;
return ;
} else {
setChatStatus ( ( ) => ChatStatus . System ) ;
audio . play ( ) . catch ( error => {
console . error ( "Audio playback error:" , error ) ;
} ) ;
}
} else {
if ( refCurrentCue . current ? . type == 'chat' ) setChatStatus ( ( ) => ChatStatus . System ) ;
else setChatStatus ( ( ) => ChatStatus . Playing ) ;
setChatStatus ( ( ) => ChatStatus . Playing ) ;
audio . play ( ) . catch ( error => {
console . error ( "Audio playback error:" , error ) ;
} ) ;
}
}
} ) ;
@ -186,28 +232,7 @@ export function FreeFlow(){
sendOsc ( OSC _ADDRESS . STATUS , 'go' ) ; / / S e n d O S C s t a t u s m e s s a g e
}
/ / s e n d p r o m p t
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 ) ;
/ / p l a y a u d i o f o r p r o m p t
refAudioPrompt . current ? . play ( ) . catch ( error => {
console . error ( "Audio prompt playback error:" , error ) ;
} ) ;
refAudioPrompt . current . onended = ( ) => {
console . log ( 'Audio prompt ended, setting chat status to User' ) ;
setChatStatus ( ChatStatus . User ) ; / / S e t c h a t s t a t u s t o U s e r a f t e r a u d i o e n d s
}
} else {
setChatStatus ( ( ) => ChatStatus . User ) ; / / R e s e t c h a t s t a t u s t o U s e r a f t e r a u d i o e n d s
}
sendPrompt ( ) ;
}
}
@ -385,11 +410,11 @@ export function FreeFlow(){
const cue = refCurrentCue . current ; / / G e t t h e c u r r e n t c u e f r o m r e f
if ( cue . type == 'chat' ) {
if ( chatStatus == ChatStatus . System ) {
console . log ( 'Still talking...' ) ;
refChatCueEnd . current = true ;
return ;
}
/ / i f ( c h a t S t a t u s = = C h a t S t a t u s . S y s t e m ) {
/ / c o n s o l e . l o g ( ' S t i l l t a l k i n g . . . ' ) ;
/ / r e f C h a t C u e E n d . c u r r e n t = t r u e ;
/ / r e t u r n ;
/ / }
console . log ( 'save chat history:' , history ) ;
uploadHistory ( history ) ; / / S a v e c h a t h i s t o r y w h e n c u e e n d s
}
@ -631,6 +656,8 @@ export function FreeFlow(){
useEffect ( ( ) => {
if ( refCurrentCue . current ? . type != 'chat' ) return ;
if ( audioUrl ) playAudio ( audioUrl ) ;
} , [ audioUrl ] ) ;