From 86452fa1c7fc8792d983554f26e2ed1ea2ab1036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Tue, 3 Sep 2024 10:19:12 +0200 Subject: [PATCH] Fix stack overflow with TTS --- CHANGELOG.md | 9 ++++++++- .../TTS/PublicationSpeechSynthesizer.swift | 15 +++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 635d00aa5..03ec0d5a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file. Take a look **Warning:** Features marked as *alpha* may change or be removed in a future release without notice. Use with caution. - +## [Unreleased] + +### Fixed + +#### Navigator + +* [#459](https://github.com/readium/swift-toolkit/issues/459) Fixed the stack overflow issue that occurred when running the text-to-speech on an EPUB file with many empty resources. + ## [3.0.0-alpha.2] diff --git a/Sources/Navigator/TTS/PublicationSpeechSynthesizer.swift b/Sources/Navigator/TTS/PublicationSpeechSynthesizer.swift index 64bf83025..b5dff0963 100644 --- a/Sources/Navigator/TTS/PublicationSpeechSynthesizer.swift +++ b/Sources/Navigator/TTS/PublicationSpeechSynthesizer.swift @@ -339,15 +339,14 @@ public class PublicationSpeechSynthesizer: Loggable { /// Loads the utterances for the next publication `ContentElement` item in the given `direction`. private func loadNextUtterances(_ direction: Direction) async -> Bool { do { - guard let content = try await publicationIterator?.next(direction) else { - return false - } - - let nextUtterances = try tokenize(content) - .flatMap { utterances(for: $0) } + var nextUtterances: [Utterance] = [] + while nextUtterances.isEmpty { + guard let content = try await publicationIterator?.next(direction) else { + return false + } - if nextUtterances.isEmpty { - return await loadNextUtterances(direction) + nextUtterances = try tokenize(content) + .flatMap { utterances(for: $0) } } utterances = CursorList(