Skip to content

Commit a1390df

Browse files
committed
🚸 Pause TTS on 3 errors
1 parent 20b13ba commit a1390df

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎composables/use-text-to-speech.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export function useTextToSpeech(options: TTSOptions = {}) {
6161
const shouldResumeWhenOnline = ref(false)
6262
const currentBufferIndex = ref<0 | 1>(0)
6363
const idleBufferIndex = computed<0 | 1>(() => (currentBufferIndex.value === 0 ? 1 : 0))
64+
const consecutiveAudioErrors = ref(0)
65+
const MAX_CONSECUTIVE_ERRORS = 3
6466
const currentTTSSegmentIndex = ref(0)
6567
const ttsSegments = ref<TTSSegment[]>([])
6668
const currentTTSSegment = computed(() => {
@@ -210,6 +212,7 @@ export function useTextToSpeech(options: TTSOptions = {}) {
210212

211213
audio.onended = () => {
212214
isTextToSpeechPlaying.value = false
215+
consecutiveAudioErrors.value = 0
213216
playNextElement()
214217
}
215218

@@ -243,6 +246,12 @@ export function useTextToSpeech(options: TTSOptions = {}) {
243246
}
244247

245248
options.onError?.(error)
249+
consecutiveAudioErrors.value += 1
250+
if (consecutiveAudioErrors.value >= MAX_CONSECUTIVE_ERRORS) {
251+
console.warn(`TTS paused after ${MAX_CONSECUTIVE_ERRORS} consecutive audio errors`)
252+
pauseTextToSpeech()
253+
return
254+
}
246255
setTimeout(() => {
247256
if (isTextToSpeechOn.value && isTextToSpeechPlaying.value) {
248257
playNextElement()
@@ -342,6 +351,7 @@ export function useTextToSpeech(options: TTSOptions = {}) {
342351
}
343352

344353
function resetAudio() {
354+
consecutiveAudioErrors.value = 0
345355
currentBufferIndex.value = 0
346356
audioBuffers.value.forEach((audio) => {
347357
if (audio) {

0 commit comments

Comments
 (0)