Skip to content

Commit

Permalink
Fix saved speed + voice
Browse files Browse the repository at this point in the history
  • Loading branch information
richardr1126 committed Jan 27, 2025
1 parent ced82d7 commit 610167f
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/contexts/TTSContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
// Audio cache using LRUCache with a maximum size of 50 entries
const audioCacheRef = useRef(new LRUCache<string, AudioBuffer>({ max: 50 }));

// Update local state when config changes
useEffect(() => {
setSpeed(voiceSpeed);
setVoice(configVoice);
}, [voiceSpeed, configVoice]);

const setText = useCallback((text: string) => {
setCurrentText(text);
console.log('Setting page text:', text);
Expand Down Expand Up @@ -352,12 +358,7 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
setIsProcessing(false);
//setIsPlaying(false); // Stop playback on error

if (error instanceof Error &&
(error.message.includes('Unable to decode audio data') ||
error.message.includes('EncodingError'))) {
console.log('Skipping problematic sentence:', sentence);
advance(); // Skip problematic sentence
}
advance(); // Skip problematic sentence
}
}, [isPlaying, processSentence, advance]);

Expand Down Expand Up @@ -434,12 +435,11 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
audioCacheRef.current.clear();

if (isPlaying) {
const currentIdx = currentIndex;
stop();
setCurrentIndex(currentIdx);
setIsPlaying(false);
abortAudio();
setIsPlaying(true);
}
}, [isPlaying, currentIndex, stop, updateConfigKey]);
}, [isPlaying, abortAudio, updateConfigKey]);

const setVoiceAndRestart = useCallback((newVoice: string) => {
setVoice(newVoice);
Expand All @@ -448,12 +448,11 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
audioCacheRef.current.clear();

if (isPlaying) {
const currentIdx = currentIndex;
stop();
setCurrentIndex(currentIdx);
setIsPlaying(false);
abortAudio();
setIsPlaying(true);
}
}, [isPlaying, currentIndex, stop, updateConfigKey]);
}, [isPlaying, abortAudio, updateConfigKey]);

const value = {
isPlaying,
Expand Down

0 comments on commit 610167f

Please sign in to comment.