aboutsummaryrefslogtreecommitdiff
path: root/app/converter
diff options
context:
space:
mode:
authorhistoria <historiavg@proton.me>2026-08-24 13:50:50 -0400
committerhistoria <historiavg@proton.me>2026-08-24 13:50:50 -0400
commitdff790664389d60d16729092a58d9c0dc490a953 (patch)
tree7c29996d78b0c1ae82fa9c4dc71ed39bb70d180e /app/converter
parentaac8febbdb45b7994e209bc74a44f8fc98fc745d (diff)
downloadtts-audiobook-generator-dff790664389d60d16729092a58d9c0dc490a953.tar.gz
remove: --chunk flag (always force client-side chunking)
Diffstat (limited to 'app/converter')
-rw-r--r--app/converter/config.py3
-rw-r--r--app/converter/converter.py76
-rw-r--r--app/converter/tts.py61
3 files changed, 29 insertions, 111 deletions
diff --git a/app/converter/config.py b/app/converter/config.py
index 6a98136..5e35ee3 100644
--- a/app/converter/config.py
+++ b/app/converter/config.py
@@ -8,9 +8,6 @@ MAX_RETRIES = 3 # Attempts per chunk request
HEARTBEAT_INTERVAL_SECONDS = 30 # Print "still working" in console logs every N seconds
# Words per TTS generation request (client-side chunking).
-# The qwen and faster backends always chunk with this size
-# The audio.cpp backend chunks long text itself, so this is ignored
-# by default with that backend. Force chunking with --chunk
CHUNK_SIZE = 250
# Default TTS backend.
diff --git a/app/converter/converter.py b/app/converter/converter.py
index cef4808..4c1ffad 100644
--- a/app/converter/converter.py
+++ b/app/converter/converter.py
@@ -140,7 +140,7 @@ class AudiobookConverter:
speed: float = 1.0, single_file: bool = False, output_format: str = config.AUDIO_FORMAT,
language: Optional[str] = None, backend: str = config.BACKEND,
voice: Optional[str] = None, debug: bool = False,
- chunk: bool = False, model_id: Optional[str] = None,
+ model_id: Optional[str] = None,
instructions: Optional[str] = None,
request_options: Optional[Dict[str, str]] = None):
if speed <= 0:
@@ -162,12 +162,6 @@ class AudiobookConverter:
self.backend = backend
self.voice = voice
self.debug = bool(debug)
- # Client-side chunking: the qwen and faster backends always chunk
- # (their servers do one generation per request and silently truncate
- # long text). The audio.cpp server chunks long text itself, so it
- # defaults to one request per chapter; --chunk forces client-side
- # chunking on top (possible needless double-chunking).
- self.client_chunks = bool(chunk) or backend != BACKEND_AUDIOCPP
# Voice design / style instruction and free-form request options
# (audio.cpp only): forwarded to AudioCppTTSClient, which validates
# them against the server-hosted model at connect time.
@@ -185,7 +179,6 @@ class AudiobookConverter:
# instructions describe or style the voice, request_options pass
# per-model controls through to the server.
self.tts = AudioCppTTSClient(voice=voice, language=self.language,
- chunk_text=self.client_chunks,
model_id=model_id,
instructions=instructions,
request_options=self.request_options)
@@ -437,10 +430,9 @@ class AudiobookConverter:
and every request/response is logged.
"""
total_chunks = len(chunks)
- if self.client_chunks:
- print(f"\n{'=' * 50}")
- print(f"PROCESSING {total_chunks} CHUNKS")
- print(f"{'=' * 50}")
+ print(f"\n{'=' * 50}")
+ print(f"PROCESSING {total_chunks} CHUNKS")
+ print(f"{'=' * 50}")
results: Dict[int, Optional[Path]] = {}
for chunk_num, chunk_text in enumerate(chunks, 1):
@@ -461,8 +453,7 @@ class AudiobookConverter:
destination = f" -> {copied.name}" if copied else ""
logger.debug("Chunk %d/%d response in %.1fs%s",
chunk_num, total_chunks, elapsed, destination)
- if self.client_chunks:
- print(f"[OK] Chunk {chunk_num:3d}/{total_chunks} completed")
+ print(f"[OK] Chunk {chunk_num:3d}/{total_chunks} completed")
logger.info("+ Chunk %d/%d completed", chunk_num, total_chunks)
else:
logger.error("Chunk %d/%d failed; aborting the remaining chunks",
@@ -476,25 +467,16 @@ class AudiobookConverter:
break
successful_chunks = sum(1 for path in results.values() if path)
- if self.client_chunks:
- print(f"\n{'=' * 50}")
- print("CHUNK PROCESSING COMPLETE")
- print(f"Successful: {successful_chunks}/{total_chunks}")
- print(f"{'=' * 50}")
+ print(f"\n{'=' * 50}")
+ print("CHUNK PROCESSING COMPLETE")
+ print(f"Successful: {successful_chunks}/{total_chunks}")
+ print(f"{'=' * 50}")
logger.info("Chunk processing completed: %d/%d chunks", successful_chunks, total_chunks)
return results
def _chapter_chunks(self, text: str) -> List[str]:
- """Split chapter text into TTS requests.
-
- Client-side chunking splits into CHUNK_SIZE-word chunks (qwen and
- faster always; audio.cpp only with --chunk). Otherwise (audio.cpp
- default) the whole text is one request and the server does its own
- long-form chunking.
- """
- if self.client_chunks:
- return chunking.split_into_chunks(text)
- return [text] if text.strip() else []
+ """Split chapter text into CHUNK_SIZE-word TTS requests."""
+ return chunking.split_into_chunks(text)
def _convert_text(self, text: str, output_path: Path, start_time: float,
speed: Optional[float] = None,
@@ -530,31 +512,14 @@ class AudiobookConverter:
chunk_sizes = [len(chunk.split()) for chunk in chunks]
avg_chunk_size = sum(chunk_sizes) / len(chunk_sizes)
- if len(chunks) == 1:
- logger.info("Sending the whole text as one request (%d words; "
- "the server chunks long text itself)",
- chunk_sizes[0])
- else:
- logger.info("Split into %d chunks (avg %.0f words per chunk)",
- total_chunks, avg_chunk_size)
+ logger.info("Split into %d chunks (avg %.0f words per chunk)",
+ total_chunks, avg_chunk_size)
backend_labels = {
BACKEND_FASTER: "faster TTS API",
BACKEND_AUDIOCPP: "audio.cpp server",
}
backend = backend_labels.get(self.backend, "Qwen API")
- if self.client_chunks:
- print(f"[INFO] Processing {total_chunks} chunks via {backend}...")
- else:
- # The whole request is sent at once and the server does its
- # own long-form chunking, so the chunk vocabulary does not
- # apply; warn that this one request can take a very long time.
- subject = (f"chapter {chapter[0]}/{chapter[1]}"
- if chapter is not None else "text")
- print(f"[INFO] Sending the {subject} to the {backend} as a "
- "single request...")
- print("[NOTE] It is expected for this to take a very long "
- "time: the server synthesizes the entire request before "
- "returning any audio.")
+ print(f"[INFO] Processing {total_chunks} chunks via {backend}...")
results = self._synthesize_chunks(chunks, debug_dir=debug_dir)
successful_chunks = sum(1 for path in results.values() if path)
@@ -578,11 +543,8 @@ class AudiobookConverter:
logger.info("Chapter %d/%d converted in %dm %ds (%d/%d chunks)",
chapter[0], chapter[1], minutes, seconds,
successful_chunks, total_chunks)
- if self.client_chunks:
- print(f"[INFO] Chapter {chapter[0]}/{chapter[1]} converted "
- f"({successful_chunks}/{total_chunks} chunks)")
- else:
- print(f"[INFO] Chapter {chapter[0]}/{chapter[1]} converted")
+ print(f"[INFO] Chapter {chapter[0]}/{chapter[1]} converted "
+ f"({successful_chunks}/{total_chunks} chunks)")
else:
logger.info("Conversion completed in %dm %ds: %s", minutes, seconds, output_path)
else:
@@ -621,12 +583,6 @@ class AudiobookConverter:
print(f"Speaker: {config.SPEAKER}")
if self.request_options:
print(f"Request options: {self.request_options}")
- if self.client_chunks:
- print("Chunking: client-side (--chunk; the server also chunks "
- "long text itself, so this may double-chunk)")
- else:
- print("Chunking: server-side (one request per chapter; "
- "--chunk forces client-side chunking)")
print(f"Language: {self.language}")
else:
api_url = (config.CLONE_API_URL if self.voice_mode == VOICE_MODE_CLONE
diff --git a/app/converter/tts.py b/app/converter/tts.py
index 6ccef56..842cc0c 100644
--- a/app/converter/tts.py
+++ b/app/converter/tts.py
@@ -278,11 +278,6 @@ def whisper_backend_available() -> Optional[str]:
return None
-# 150 wpm is a typical spoken pace; used only to size the HTTP request
-# timeout for long audio.cpp generations (not as a correctness check).
-_ESTIMATED_WORDS_PER_MINUTE = 150
-
-
class _BaseTTSClient:
"""Shared chunk retry logic, heartbeat, and chunk file bookkeeping."""
@@ -327,15 +322,10 @@ class _BaseTTSClient:
return None
@contextlib.contextmanager
- def _chunk_heartbeat(self, chunk_num: int, label: Optional[str] = None):
- """Print a periodic "still working" message while a request generates.
-
- ``label`` overrides the default "Chunk {chunk_num}" subject, for
- backends that send one request per chapter without client-side
- chunking (the audio.cpp default) where "chunk" would be misleading.
- """
+ def _chunk_heartbeat(self, chunk_num: int):
+ """Print a periodic "still working" message while a request generates."""
stop = threading.Event()
- subject = label if label is not None else f"Chunk {chunk_num}"
+ subject = f"Chunk {chunk_num}"
def _beat():
start = time.time()
@@ -796,20 +786,14 @@ class AudioCppTTSClient(_BaseTTSClient):
the request's "options" object, which is the server's generic
pass-through for per-model controls.
- Chunking: the server does its own long-form text chunking for every
- family (its ``text_chunk_size`` option, with a per-family default), so
- by default each chapter is sent as a single request and the audio
- comes back already stitched. With ``chunk_text=True`` (the --chunk CLI
- flag), text is instead split client-side into CHUNK_SIZE-word
- sub-requests, which may needlessly double-chunk — the warning is
- printed by the CLI.
-
- Each response is a complete WAV file, so sub-request audio is
- concatenated with the same lossless path used for the Qwen client.
+ Chunking: text is split client-side into sub-requests of at most
+ config.CHUNK_SIZE words each; each sub-request returns a complete
+ WAV file and the parts are concatenated with the same lossless path
+ used for the Qwen client.
"""
def __init__(self, voice: Optional[str] = None, language: Optional[str] = None,
- api_url: Optional[str] = None, chunk_text: bool = False,
+ api_url: Optional[str] = None,
model_id: Optional[str] = None,
instructions: Optional[str] = None,
request_options: Optional[Dict[str, str]] = None):
@@ -844,10 +828,6 @@ class AudioCppTTSClient(_BaseTTSClient):
# speakers gets its voice from the instruction alone (no voice field).
self.design_mode = False
self.instruction_voice = False
- # When False (default), each chapter is sent as one request and the
- # server does its own long-form chunking (text_chunk_size); when True,
- # text is split client-side into CHUNK_SIZE-word sub-requests first.
- self.chunk_text = bool(chunk_text)
# Family and task of the selected model entry and the family's request
# profile; all are resolved from GET /v1/models during _connect.
self.family = ""
@@ -1217,11 +1197,6 @@ class AudioCppTTSClient(_BaseTTSClient):
url, data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json"}, method="POST")
timeout = config.API_TIMEOUT
- if not self.chunk_text:
- # Estimated audio duration at 150 wpm, doubled plus a minute of
- # slack, bounded below by the configured per-request timeout.
- estimated_seconds = 60.0 * len(text.split()) / _ESTIMATED_WORDS_PER_MINUTE
- timeout = max(timeout, int(estimated_seconds * 2) + 60)
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
wav = response.read()
@@ -1259,28 +1234,18 @@ class AudioCppTTSClient(_BaseTTSClient):
def generate_chunk(self, text: str, chunk_num: int) -> Optional[str]:
"""Generate one audio chunk; returns its path in the chunks folder.
- By default the whole text goes out as a single request and the
- server does its own long-form chunking (see the class docstring).
- With ``chunk_text=True`` (--chunk), the text is split into
- sub-requests of at most ``config.CHUNK_SIZE`` words each; each
- sub-request returns a complete WAV file and the parts are
- concatenated into one chunk file.
+ The text is split into sub-requests of at most ``config.CHUNK_SIZE``
+ words each; each sub-request returns a complete WAV file and the
+ parts are concatenated into one chunk file.
"""
try:
- if self.chunk_text:
- sub_texts = split_into_chunks(text, max_words=config.CHUNK_SIZE)
- elif text.strip():
- sub_texts = [text]
- else:
- sub_texts = []
+ sub_texts = split_into_chunks(text, max_words=config.CHUNK_SIZE)
if not sub_texts:
raise RuntimeError("No text to synthesize")
output_path: Optional[Path] = None
with tempfile.TemporaryDirectory(prefix="tts_parts_") as parts_dir, \
- self._chunk_heartbeat(
- chunk_num,
- label=None if self.chunk_text else "Request"):
+ self._chunk_heartbeat(chunk_num):
part_paths = []
for sub_num, sub_text in enumerate(sub_texts, 1):
wav = self._request_wav_with_retry(