-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
187c412
commit aedb0ee
Showing
6 changed files
with
167 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,54 @@ | ||
import { | ||
hasAudioStarted, | ||
isAudioLoading, | ||
isPlaying, | ||
pauseResumeAudio, | ||
playFullDebate, | ||
resetAudioState, | ||
isSynthesizing | ||
} from 'lib/audioUtils.ts'; | ||
import type { Personality } from 'lib/debate/personalities.ts'; | ||
import { useCallback, useEffect, useState } from 'preact/hooks'; | ||
import { pauseResumeAudio, playFullDebate, processQueue } from "lib/audioUtils.ts"; | ||
import { Personality } from "lib/debate/personalities.ts"; | ||
import { useCallback, useEffect } from "preact/hooks"; | ||
import { useAudioState } from "./useAudioState.ts"; | ||
|
||
export function useAudioControls(debate: Array<{ role: string; content: string }>, agentDetails: Required<Personality>[]) { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isSynthesizingAudio, setIsSynthesizingAudio] = useState(false); | ||
export const useAudioControls = ( | ||
debate: Array<{ role: string; content: string }>, | ||
agentDetails: Required<Personality>[] | ||
) => { | ||
const audioState = useAudioState(); | ||
const { | ||
isProcessingQueue, | ||
isPaused, | ||
isLoading, | ||
isSynthesizingAudio, | ||
audioQueue, | ||
currentQueueIndex, | ||
resetAudioState, | ||
setIsPaused, | ||
setIsProcessingQueue, | ||
} = audioState; | ||
|
||
const isPlaying = isProcessingQueue && !isPaused; | ||
|
||
useEffect(() => { | ||
const checkStatus = () => { | ||
setIsLoading(isAudioLoading()); | ||
setIsSynthesizingAudio(isSynthesizing()); | ||
return () => { | ||
resetAudioState(); | ||
}; | ||
|
||
const intervalId = setInterval(checkStatus, 100); | ||
return () => clearInterval(intervalId); | ||
}, []); | ||
|
||
const handlePlayPause = useCallback(async () => { | ||
if (isPlaying()) { | ||
pauseResumeAudio(); | ||
useEffect(() => { | ||
if (audioQueue.length > 0 && !isProcessingQueue && !isPaused) { | ||
processQueue(audioState); | ||
} | ||
}, [audioQueue, isProcessingQueue, isPaused]); | ||
|
||
const handlePlayPause = useCallback(() => { | ||
if (audioQueue.length === 0 && currentQueueIndex === 0) { | ||
playFullDebate(debate, agentDetails, audioState).then(() => { | ||
setIsProcessingQueue(true); | ||
processQueue(audioState); | ||
}); | ||
} else { | ||
setIsLoading(true); | ||
setIsSynthesizingAudio(true); | ||
if (!hasAudioStarted()) { | ||
resetAudioState(); | ||
await playFullDebate(debate, agentDetails); | ||
} else { | ||
pauseResumeAudio(); | ||
} | ||
setIsLoading(false); | ||
setIsSynthesizingAudio(false); | ||
pauseResumeAudio(audioState); | ||
} | ||
}, [debate, agentDetails]); | ||
}, [debate, agentDetails, audioState, audioQueue, currentQueueIndex]); | ||
|
||
return { | ||
isPlaying: isPlaying(), | ||
isPlaying, | ||
isLoading, | ||
isSynthesizingAudio, | ||
handlePlayPause, | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useState } from "preact/hooks"; | ||
|
||
export const useAudioState = () => { | ||
const [audioQueue, setAudioQueue] = useState<Array<{ content: string; voice: string }>>([]); | ||
const [isProcessingQueue, setIsProcessingQueue] = useState(false); | ||
const [currentAudio, setCurrentAudio] = useState<HTMLAudioElement | null>(null); | ||
const [isPaused, setIsPaused] = useState(false); | ||
const [currentPlaybackPosition, setCurrentPlaybackPosition] = useState(0); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [currentQueueIndex, setCurrentQueueIndex] = useState(0); | ||
const [isSynthesizingAudio, setIsSynthesizingAudio] = useState(false); | ||
|
||
const resetAudioState = () => { | ||
setAudioQueue([]); | ||
setIsProcessingQueue(false); | ||
setCurrentAudio(null); | ||
setIsPaused(false); | ||
setCurrentPlaybackPosition(0); | ||
setCurrentQueueIndex(0); | ||
setIsLoading(false); | ||
}; | ||
|
||
return { | ||
audioQueue, | ||
setAudioQueue, | ||
isProcessingQueue, | ||
setIsProcessingQueue, | ||
currentAudio, | ||
setCurrentAudio, | ||
isPaused, | ||
setIsPaused, | ||
currentPlaybackPosition, | ||
setCurrentPlaybackPosition, | ||
isLoading, | ||
setIsLoading, | ||
currentQueueIndex, | ||
setCurrentQueueIndex, | ||
isSynthesizingAudio, | ||
setIsSynthesizingAudio, | ||
resetAudioState, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.