aboutsummaryrefslogtreecommitdiff
path: root/app/converter
diff options
context:
space:
mode:
Diffstat (limited to 'app/converter')
-rw-r--r--app/converter/audio.py47
-rw-r--r--app/converter/clients/__init__.py68
-rw-r--r--app/converter/clients/audiocpp.py (renamed from app/converter/tts.py)783
-rw-r--r--app/converter/clients/base.py155
-rw-r--r--app/converter/clients/faster.py123
-rw-r--r--app/converter/clients/languages.py74
-rw-r--r--app/converter/clients/qwen.py322
-rw-r--r--app/converter/clients/speakers.py57
-rw-r--r--app/converter/clients/transcribe.py54
-rw-r--r--app/converter/converter.py21
10 files changed, 909 insertions, 795 deletions
diff --git a/app/converter/audio.py b/app/converter/audio.py
index eb970ff..90ce5ca 100644
--- a/app/converter/audio.py
+++ b/app/converter/audio.py
@@ -1,4 +1,9 @@
-"""Audio assembly: combining chunks, speed adjustment, cleanup."""
+"""Audio assembly: combining chunks, speed adjustment, cleanup.
+
+Every function that touches the run's scratch audio takes its folder as an
+explicit CHUNKS_DIR argument — the converter owns the folder constants and
+threads them through, so there is no module-global path to mutate.
+"""
import logging
import re
@@ -13,8 +18,6 @@ from . import config
logger = logging.getLogger(__name__)
-CHUNKS_FOLDER = Path(__file__).resolve().parent.parent.parent / "app" / "chunks"
-
# Tolerance for "is this speed 1.0?" comparisons (banner display, atempo
# filter elision); shared by every speed check.
SPEED_EPSILON = 1e-6
@@ -362,6 +365,7 @@ def _collect_chunk_files(total_chunks: int,
def combine_chunks(total_chunks: int, output_path: Path,
chunk_results: Dict[int, Optional[Path]],
+ *, chunks_dir: Path,
speed: float = 1.0, output_format: str = config.AUDIO_FORMAT,
intermediate: bool = False,
meta: Optional[TrackMeta] = None,
@@ -369,14 +373,15 @@ def combine_chunks(total_chunks: int, output_path: Path,
"""Combine audio chunks into the final audiobook using ffmpeg's concat demuxer.
``chunk_results`` maps chunk numbers to the audio file each chunk produced
- (None for failed chunks); failed and missing chunks are skipped. When
- ``speed`` differs from 1.0, an additional speed-adjusted copy is written
- next to the normal-speed file. ``meta``/``cover`` embed tags and cover
- art into the output (skipped for intermediate chapter scratch audio).
- Chunks are streamed by ffmpeg, so the whole book is never held in
- memory. Set ``intermediate`` for scratch chapter audio on the way to a
- larger output (e.g. a chaptered m4b) so save messages don't present it
- as the final audiobook.
+ (None for failed chunks); failed and missing chunks are skipped. The
+ concat scratch list is written to ``chunks_dir``. When ``speed`` differs
+ from 1.0, an additional speed-adjusted copy is written next to the
+ normal-speed file. ``meta``/``cover`` embed tags and cover art into the
+ output (skipped for intermediate chapter scratch audio). Chunks are
+ streamed by ffmpeg, so the whole book is never held in memory. Set
+ ``intermediate`` for scratch chapter audio on the way to a larger output
+ (e.g. a chaptered m4b) so save messages don't present it as the final
+ audiobook.
"""
if shutil.which("ffmpeg") is None or shutil.which("ffprobe") is None:
logger.error("ffmpeg and ffprobe are required to combine audio chunks (install ffmpeg)")
@@ -391,7 +396,7 @@ def combine_chunks(total_chunks: int, output_path: Path,
if missing_chunks:
logger.warning("Missing chunks: %s", missing_chunks)
- concat_list = CHUNKS_FOLDER / "_concat_list.txt"
+ concat_list = Path(chunks_dir) / "_concat_list.txt"
try:
with open(concat_list, "w", encoding="utf-8") as list_file:
for chunk_file in chunk_files:
@@ -463,12 +468,12 @@ def combine_chunks(total_chunks: int, output_path: Path,
pass
-def cleanup_chunks() -> None:
- """Remove temporary chunk and chapter files from the scratch folder."""
+def cleanup_chunks(chunks_dir: Path) -> None:
+ """Remove temporary chunk and chapter files from the CHUNKS_DIR scratch folder."""
try:
chunk_count = 0
for pattern in ("chunk_*", "chapter_*"):
- for chunk_file in CHUNKS_FOLDER.glob(pattern):
+ for chunk_file in Path(chunks_dir).glob(pattern):
try:
if chunk_file.is_file():
chunk_file.unlink()
@@ -532,13 +537,15 @@ def build_ffmetadata(chapters: List[tuple], path: Path) -> None:
def combine_chapters_to_m4b(chapter_files: List[Path], titles: List[str],
- output_path: Path, speed: float = 1.0,
+ output_path: Path, *, chunks_dir: Path,
+ speed: float = 1.0,
meta: Optional[TrackMeta] = None,
cover: Optional[Path] = None) -> bool:
"""Concatenate per-chapter audio into a single m4b with embedded chapter markers.
Chapter start/end times are derived from each chapter file's duration and
- written as ffmpeg chapter metadata. ``meta``/``cover`` embed tags and
+ written as ffmpeg chapter metadata; the concat list and metadata scratch
+ files are written to ``chunks_dir``. ``meta``/``cover`` embed tags and
cover art. When ``speed`` differs from 1.0, a speed-adjusted copy (with
rescaled chapter markers) is written alongside the normal-speed file.
"""
@@ -550,9 +557,9 @@ def combine_chapters_to_m4b(chapter_files: List[Path], titles: List[str],
logger.error("No chapter files provided")
return False
- concat_list = CHUNKS_FOLDER / "_concat_list.txt"
- metadata_file = CHUNKS_FOLDER / "_chapters.txt"
- speed_metadata_file = CHUNKS_FOLDER / "_chapters_speed.txt"
+ concat_list = Path(chunks_dir) / "_concat_list.txt"
+ metadata_file = Path(chunks_dir) / "_chapters.txt"
+ speed_metadata_file = Path(chunks_dir) / "_chapters_speed.txt"
try:
chapters = []
start_ms = 0
diff --git a/app/converter/clients/__init__.py b/app/converter/clients/__init__.py
new file mode 100644
index 0000000..e216011
--- /dev/null
+++ b/app/converter/clients/__init__.py
@@ -0,0 +1,68 @@
+"""TTS client implementations — one module per backend server.
+
+Public API: the three client classes (QwenTTSClient, FasterTTSClient,
+AudioCppTTSClient), the backend/voice-mode vocabulary, and the shared
+helpers (normalize_language, speaker tables, whisper transcription) that
+the UIs and setup wizards build on.
+"""
+
+# The TTS backends a conversion can use, in Convert-form order. Each has a
+# client module here; the backends package mirrors these keys for its
+# install/setup wizards.
+BACKEND_QWEN = "qwen"
+BACKEND_FASTER = "faster"
+BACKEND_AUDIOCPP = "audiocpp"
+BACKENDS = (BACKEND_AUDIOCPP, BACKEND_QWEN, BACKEND_FASTER)
+
+from .base import BaseTTSClient, ConversionCancelled, VOICE_MODE_CLONE, \
+ VOICE_MODE_CUSTOM, VOICE_MODES, resolve_request_seed
+from .languages import LANGUAGE_ISO_CODES, TTS_LANGUAGES, \
+ TTS_LANGUAGE_ALIASES, normalize_language
+from .speakers import QWEN3_TTS_SPEAKERS, SPEAKER_DISPLAY_NAMES, \
+ is_builtin_speaker, speaker_display_name, speaker_display_name_for
+from .transcribe import transcribe_reference_audio, whisper_backend_available
+from .qwen import CUSTOM_VOICE_MODEL_ID, MODEL_SIZE, QwenTTSClient
+from .faster import SAMPLE_RATE, FasterTTSClient
+from .audiocpp import (
+ AUDIOCPP_DEFAULT_FAMILY_PROFILE,
+ AUDIOCPP_FAMILY_PROFILES,
+ AUDIOCPP_FAMILY_QWEN3_TTS,
+ AUDIOCPP_LANG_DISPLAY,
+ AUDIOCPP_LANG_ISO,
+ AUDIOCPP_LANG_OMIT,
+ AUDIOCPP_SYNTHESIS_TASKS,
+ AUDIOCPP_TASK_TTS,
+ AUDIOCPP_TASK_VDES,
+ AUDIOCPP_VOICE_CLONE,
+ AUDIOCPP_VOICE_DESIGN,
+ AUDIOCPP_VOICE_SPEAKER,
+ AudioCppFamilyProfile,
+ AudioCppTTSClient,
+ audiocpp_entry_voice_capability,
+)
+
+__all__ = [
+ # vocabulary
+ "BACKEND_QWEN", "BACKEND_FASTER", "BACKEND_AUDIOCPP", "BACKENDS",
+ "VOICE_MODE_CUSTOM", "VOICE_MODE_CLONE", "VOICE_MODES",
+ # clients
+ "BaseTTSClient", "ConversionCancelled", "resolve_request_seed",
+ "QwenTTSClient", "FasterTTSClient", "AudioCppTTSClient",
+ # model facts
+ "MODEL_SIZE", "CUSTOM_VOICE_MODEL_ID", "SAMPLE_RATE",
+ # languages
+ "TTS_LANGUAGES", "TTS_LANGUAGE_ALIASES", "LANGUAGE_ISO_CODES",
+ "normalize_language",
+ # speakers
+ "QWEN3_TTS_SPEAKERS", "SPEAKER_DISPLAY_NAMES",
+ "speaker_display_name", "speaker_display_name_for", "is_builtin_speaker",
+ # transcription
+ "transcribe_reference_audio", "whisper_backend_available",
+ # audio.cpp family profiles
+ "AUDIOCPP_LANG_DISPLAY", "AUDIOCPP_LANG_ISO", "AUDIOCPP_LANG_OMIT",
+ "AUDIOCPP_FAMILY_QWEN3_TTS", "AUDIOCPP_TASK_TTS", "AUDIOCPP_TASK_VDES",
+ "AUDIOCPP_SYNTHESIS_TASKS", "AUDIOCPP_VOICE_SPEAKER",
+ "AUDIOCPP_VOICE_CLONE", "AUDIOCPP_VOICE_DESIGN",
+ "AudioCppFamilyProfile", "AUDIOCPP_DEFAULT_FAMILY_PROFILE",
+ "AUDIOCPP_FAMILY_PROFILES", "audiocpp_entry_voice_capability",
+]
diff --git a/app/converter/tts.py b/app/converter/clients/audiocpp.py
index 8130a44..4a161cb 100644
--- a/app/converter/tts.py
+++ b/app/converter/clients/audiocpp.py
@@ -1,121 +1,25 @@
-"""Client wrappers for the TTS backends.
+"""Client for the audio.cpp audiocpp_server (native ggml TTS families)."""
-QwenTTSClient talks to the Qwen3-TTS demo server (custom voice / voice clone).
-FasterTTSClient talks to the OpenAI-compatible server from the
-faster-qwen3-tts repository (voice cloning only; the reference voice is
-configured server-side — see the "Faster backend" section of the README).
-AudioCppTTSClient talks to the audiocpp_server from the audio.cpp
-repository, which can host any TTS model family audio.cpp supports
-(Qwen3-TTS, Higgs Audio, VoxCPM2, IndexTTS2, ...) through one OpenAI-style
-API; the family is detected from the server at startup (see the
-"audio.cpp backend" sections of the README).
-"""
-
-import contextlib
-import io
import json
import logging
-import random
import shutil
-import sys
import tempfile
-import threading
-import time
import urllib.error
import urllib.parse
import urllib.request
-import wave
from pathlib import Path
-from typing import Any, Dict, List, Optional, Tuple
+from typing import Any, Dict, List, Optional
-from . import config
-from .audio import concat_audio_files
-from .chunking import split_into_chunks
+from .. import config
+from ..audio import concat_audio_files
+from ..chunking import split_into_chunks
+from .base import BaseTTSClient, ConversionCancelled, resolve_request_seed
+from .languages import LANGUAGE_ISO_CODES, normalize_language
+from .speakers import (is_builtin_speaker, speaker_display_name,
+ speaker_display_name_for)
logger = logging.getLogger(__name__)
-
-class ConversionCancelled(Exception):
- """Raised inside a conversion whose cancel event was set.
-
- The TUI run view sets a ``threading.Event`` on the TTS client (and the
- converter checks it between chunks/chapters/books); the retry loops
- raise this so the cancellation propagates out of a sleeping or retrying
- request promptly instead of finishing the retry ladder.
- """
-
-
-# Voice modes (re-exported for the CLI and the converter orchestrator).
-VOICE_MODE_CUSTOM = "custom_voice"
-VOICE_MODE_CLONE = "voice_clone"
-VOICE_MODES = (VOICE_MODE_CUSTOM, VOICE_MODE_CLONE)
-
-# TTS backends (re-exported for the CLI and the converter orchestrator).
-BACKEND_QWEN = "qwen"
-BACKEND_FASTER = "faster"
-BACKEND_AUDIOCPP = "audiocpp"
-BACKENDS = (BACKEND_AUDIOCPP, BACKEND_QWEN, BACKEND_FASTER)
-
-# Languages understood by the Qwen3-TTS API. Display names must match the
-# demo dropdown exactly (the demo silently falls back to "Auto" for
-# unrecognized values, so languages are validated client-side first).
-TTS_LANGUAGES = (
- "Auto",
- "Chinese",
- "English",
- "German",
- "Italian",
- "Portuguese",
- "Spanish",
- "Japanese",
- "Korean",
- "French",
- "Russian",
-)
-
-# Short aliases accepted on the command line (ISO 639-1 codes and common
-# shorthands), mapped to the display names above.
-TTS_LANGUAGE_ALIASES = {
- "zh": "Chinese",
- "en": "English",
- "de": "German",
- "it": "Italian",
- "pt": "Portuguese",
- "es": "Spanish",
- "ja": "Japanese",
- "ko": "Korean",
- "fr": "French",
- "ru": "Russian",
- "zh-cn": "Chinese",
- "zh-tw": "Chinese",
- "pt-br": "Portuguese",
- "en-us": "English",
- "en-gb": "English",
-}
-
-# Qwen display names -> ISO 639-1 codes, for audio.cpp families whose
-# language request option takes a code instead of a display name. "Auto"
-# has no code and maps to None so the field is omitted and the server
-# applies its own default.
-LANGUAGE_ISO_CODES = {
- "Chinese": "zh",
- "English": "en",
- "German": "de",
- "Italian": "it",
- "Portuguese": "pt",
- "Spanish": "es",
- "Japanese": "ja",
- "Korean": "ko",
- "French": "fr",
- "Russian": "ru",
-}
-
-# --- audio.cpp model families ---------------------------------------------
-#
-# audiocpp_server exposes the same OpenAI-style API for every TTS family it
-# hosts; families only differ in a few request conventions, captured here as
-# profiles. Families that are not listed use the default profile below.
-
# How the "language" request field is expressed by a family.
AUDIOCPP_LANG_DISPLAY = "display" # Qwen display names, e.g. "English"
AUDIOCPP_LANG_ISO = "iso" # ISO 639-1 codes, e.g. "en"
@@ -210,664 +114,8 @@ def audiocpp_entry_voice_capability(family: str, task: str,
return AUDIOCPP_VOICE_SPEAKER
return AUDIOCPP_VOICE_CLONE
-# Built-in CustomVoice speaker names for the Qwen3-TTS family. Shared by the
-# qwen-tts demo backend (config.SPEAKER, the qwen setup/form) and the
-# audio.cpp audiocpp backend's CustomVoice entry (the Convert form's Speaker
-# picker). Entries are the canonical/config form; speaker_display_name()
-# maps them to the wire (display) form via SPEAKER_DISPLAY_NAMES below.
-QWEN3_TTS_SPEAKERS = ("Vivian", "Serena", "Uncle_Fu", "Dylan", "Eric",
- "Ryan", "Aiden", "Ono_Anna", "Sohee")
-
-# Canonical speaker names -> display names used by the qwen-tts demo.
-SPEAKER_DISPLAY_NAMES = {
- "ryan": "Ryan",
- "serena": "Serena",
- "vivian": "Vivian",
- "uncle_fu": "Uncle Fu",
- "aiden": "Aiden",
- "ono_anna": "Ono Anna",
- "sohee": "Sohee",
- "eric": "Eric",
- "dylan": "Dylan",
-}
-
-# Fixed model facts: both demos run the 1.7B model (the CustomVoice demo
-# takes its full HuggingFace id), and the 12Hz codec outputs 24 kHz audio.
-MODEL_SIZE = "1.7B"
-CUSTOM_VOICE_MODEL_ID = "Qwen/Qwen3-TTS-12Hz-1.7B-CustomVoice"
-SAMPLE_RATE = 24000
-
-CHUNKS_FOLDER = Path(__file__).resolve().parent.parent.parent / "app" / "chunks"
-
-
-def _resolve_request_seed() -> int:
- """Resolve the seed sent with every request.
-
- Returns config.SEED as-is, or (with CONSTANT_SEED and SEED < 0) one
- random value drawn per run, meant to be reused for every request so
- the voice stays consistent across chunk boundaries. Without
- CONSTANT_SEED, -1 is returned so the server re-samples the voice on
- every generation.
- """
- seed = config.SEED
- if config.CONSTANT_SEED and seed < 0:
- seed = random.randrange(2 ** 31)
- return seed
-
-
-def speaker_display_name_for(name: str) -> str:
- """Return the wire (display) form of a Qwen3-TTS CustomVoice speaker NAME.
-
- Accepts either the canonical/config form (e.g. "uncle_fu", "Uncle_Fu")
- or the display form ("Uncle Fu"), case-insensitively; unknown names pass
- through unchanged. Used by AudioCppTTSClient to normalize the --voice /
- Speaker-picker value into what audiocpp_server expects in the request's
- voice field.
- """
- return SPEAKER_DISPLAY_NAMES.get((name or "").lower(), name)
-
-
-def is_builtin_speaker(name: Optional[str]) -> bool:
- """True when NAME is one of the Qwen3-TTS CustomVoice built-in speakers.
-
- Matches case-insensitively across the canonical ("Uncle_Fu"), display
- ("Uncle Fu") and shorthand ("uncle_fu") forms, so the --voice flag and
- the Convert form's Speaker picker resolve to the same set.
- """
- if not name:
- return False
- norm = name.lower().replace("_", " ").replace("-", " ")
- return any(norm == speaker.lower().replace("_", " ")
- for speaker in QWEN3_TTS_SPEAKERS)
-
-
-def speaker_display_name() -> str:
- """Return the display name for the configured custom speaker."""
- return speaker_display_name_for(config.SPEAKER)
-
-
-def normalize_language(value: Optional[str]) -> str:
- """Normalize a user-provided language name to a Qwen3-TTS display name.
-
- Accepts the display names in TTS_LANGUAGES case-insensitively as
- well as the short aliases in TTS_LANGUAGE_ALIASES (ISO 639-1 codes
- and common shorthands). Raises ValueError for anything else, since the
- Qwen3-TTS demo silently falls back to "Auto" for unrecognized languages.
- """
- if value is None:
- raise ValueError("Language must not be None")
- candidate = value.strip()
- if not candidate:
- raise ValueError("Language must not be empty")
- for name in TTS_LANGUAGES:
- if candidate.lower() == name.lower():
- return name
- alias = TTS_LANGUAGE_ALIASES.get(candidate.lower())
- if alias:
- return alias
- raise ValueError(
- f"Unknown language: {value!r}. Expected one of "
- f"{', '.join(TTS_LANGUAGES)} (or an alias: "
- f"{', '.join(sorted(TTS_LANGUAGE_ALIASES))})."
- )
-
-
-def transcribe_reference_audio(audio_path: str, model_name: str = "base") -> Optional[str]:
- """Transcribe reference audio locally using an optional Whisper backend.
-
- The current qwen-tts demo does not expose a transcription endpoint, so
- transcription is done client-side when a Whisper package is available.
- Returns None if no backend is installed.
- """
- for backend in ("faster_whisper", "whisper"):
- try:
- if backend == "faster_whisper":
- from faster_whisper import WhisperModel
- model = WhisperModel(model_name, device="cpu", compute_type="int8")
- segments, _ = model.transcribe(audio_path)
- text = " ".join(seg.text.strip() for seg in segments).strip()
- else:
- import whisper
- model = whisper.load_model(model_name)
- result = model.transcribe(audio_path)
- text = (result.get("text") or "").strip()
- if text:
- logger.info("Transcription complete via %s: %s", backend, text)
- return text
- except ImportError:
- continue
- except Exception as exc:
- logger.warning("%s transcription failed: %s", backend, exc)
- logger.warning("No Whisper backend available; transcription skipped.")
- return None
-
-
-def whisper_backend_available() -> Optional[str]:
- """Return the name of an importable Whisper backend, or None.
-
- Checks faster_whisper first (preferred), then the openai-whisper
- package, without importing the heavy model code: a bare import probe
- is enough to tell whether the package is installed in the current
- environment. Used by the make_audiocpp_server_json tool to warn when
- neither is present (e.g. the wrong conda environment is active).
- """
- for backend in ("faster_whisper", "whisper"):
- try:
- __import__(backend)
- except ImportError:
- continue
- return backend
- return None
-
-
-class _BaseTTSClient:
- """Shared chunk retry logic, heartbeat, and chunk file bookkeeping."""
-
- # Set by the converter when the run is cancellable (the TUI run view):
- # a threading.Event that, once set, aborts the run between requests
- # (and interrupts retry back-off sleeps). ``quiet`` silences console
- # prints (the run view owns the screen).
- cancel = None
- quiet = False
-
- def _report(self, message: str) -> None:
- """Print a console line unless quiet (the run view owns the screen)."""
- if not self.quiet:
- print(message)
-
- def generate_chunk(self, text: str, chunk_num: int) -> Optional[str]:
- """Generate one audio chunk; returns its path in the chunks folder."""
- raise NotImplementedError
-
- def _cancel_requested(self) -> bool:
- """True when the run's cancel event has been set (if any)."""
- return isinstance(self.cancel, threading.Event) \
- and self.cancel.is_set()
-
- def _check_cancelled(self) -> None:
- """Raise ConversionCancelled when the cancel event is set."""
- if self._cancel_requested():
- raise ConversionCancelled("Cancelled by user")
-
- def _sleep(self, seconds: float) -> None:
- """Sleep SECONDS, cut short (raising) when the cancel event sets."""
- if isinstance(self.cancel, threading.Event):
- if self.cancel.wait(seconds):
- raise ConversionCancelled("Cancelled by user")
- else:
- time.sleep(seconds)
-
- def _chunk_path(self, chunk_num: int, suffix: str) -> Path:
- """Resolve the target path for a chunk, removing stale files first.
-
- Any stale chunk file for this index is removed so a retry or extension
- change can never leave two files matching chunk_NNNN.*.
- """
- for stale in CHUNKS_FOLDER.glob(f"chunk_{chunk_num:04d}.*"):
- try:
- stale.unlink()
- except OSError as exc:
- logger.debug("Could not remove stale chunk file %s: %s", stale, exc)
- return CHUNKS_FOLDER / f"chunk_{chunk_num:04d}{suffix}"
-
- def process_chunk_with_retry(self, chunk_num: int, text: str) -> Optional[Path]:
- """Process a chunk with retry logic.
-
- Returns the generated chunk file's path, or None when all attempts
- failed. Raises ConversionCancelled when the run was cancelled.
- """
- for attempt in range(config.MAX_RETRIES):
- self._check_cancelled()
- try:
- result = self.generate_chunk(text, chunk_num)
- if result and Path(result).exists():
- return Path(result)
- logger.warning("Chunk %d attempt %d failed", chunk_num, attempt + 1)
- except ConversionCancelled:
- raise
- except Exception as exc:
- logger.warning("Chunk %d attempt %d error: %s", chunk_num, attempt + 1, exc)
-
- if attempt < config.MAX_RETRIES - 1:
- sleep_time = 5 + (2 ** attempt)
- logger.info("Waiting %ds before retry...", sleep_time)
- self._sleep(sleep_time)
-
- logger.error("Chunk %d failed after %d attempts", chunk_num, config.MAX_RETRIES)
- return None
-
- @contextlib.contextmanager
- def _chunk_heartbeat(self, chunk_num: int):
- """Log a periodic "still working" record while a request generates."""
- stop = threading.Event()
- subject = f"Chunk {chunk_num}"
-
- def _beat():
- start = time.time()
- while not stop.wait(config.HEARTBEAT_INTERVAL_SECONDS):
- elapsed = time.time() - start
- if self.quiet:
- logger.info("%s still generating — %dm %ds elapsed",
- subject, int(elapsed // 60), int(elapsed % 60))
- else:
- print(f"[...] {subject} still generating — "
- f"{int(elapsed // 60)}m {int(elapsed % 60)}s elapsed",
- flush=True)
-
- thread = threading.Thread(target=_beat, daemon=True)
- thread.start()
- try:
- yield
- finally:
- stop.set()
- thread.join()
-
-
-class QwenTTSClient(_BaseTTSClient):
- """Generates audio chunks through a Qwen3-TTS demo server."""
-
- def __init__(self, voice_mode: str = "custom_voice", voice_clone_ref_audio: Optional[str] = None,
- voice_clone_ref_text: Optional[str] = None, skip_transcription: bool = False,
- language: Optional[str] = None, api_url: Optional[str] = None,
- quiet: bool = False):
- # Quiet before connecting so connect-time status lines never reach
- # a screen the TUI run view owns.
- self.quiet = bool(quiet)
- if voice_mode not in VOICE_MODES:
- raise ValueError(
- f"Unknown voice mode: {voice_mode!r} (expected one of {VOICE_MODES})"
- )
- self.voice_mode = voice_mode
- self.voice_clone_ref_audio = voice_clone_ref_audio
- self.voice_clone_ref_text = (voice_clone_ref_text or "").strip()
- self.skip_transcription = skip_transcription
- # api_url overrides the configured endpoint for the active voice mode
- # (used by the hub's "[remote]" backend entries and --api-url).
- self.api_url = (api_url or "").strip() or None
- # Seed sent with every request: config.SEED as-is, or (with
- # CONSTANT_SEED and SEED < 0) one random value drawn per run and
- # reused for every request so the voice stays consistent across
- # chunk boundaries. Without CONSTANT_SEED, -1 is forwarded so the
- # server re-samples the voice on every generation.
- self._seed = _resolve_request_seed()
- if language is None:
- language = config.LANGUAGE
- # Validate before connecting so bad values fail fast without a server.
- self.language = normalize_language(language)
- self.client = None
- self.api_info: Dict[str, Any] = {}
- self.clone_client = None
- self.clone_api_info: Dict[str, Any] = {}
- self._ref_audio_filedata: Optional[Dict[str, Any]] = None
- self._connect()
-
- # ------------------------------------------------------------------
- # Connection
- # ------------------------------------------------------------------
-
- def _connect(self) -> None:
- api_url = self.api_url or (
- config.CLONE_API_URL if self.voice_mode == VOICE_MODE_CLONE
- else config.QWEN_API_URL)
- try:
- if self.voice_mode == VOICE_MODE_CLONE:
- # Voice clone uses the Base-model demo, which is a separate server
- # from the CustomVoice demo (that one only exposes /run_instruct).
- self._init_client(api_url, clone=True)
- self._report(f"[OK] Connected to Voice Clone API at {api_url}")
- self._resolve_reference_text()
- else:
- self._init_client(api_url, clone=False)
- self._report("[OK] Connected to Qwen API")
- except Exception as exc:
- raise RuntimeError(
- f"Qwen API initialization failed at {api_url}: {exc}. "
- "Make sure the Qwen demo server is running and reachable, and that your "
- "installed Qwen3-TTS version matches this converter's API expectations "
- "(voice clone requires the Base-model demo: Qwen/Qwen3-TTS-12Hz-1.7B-Base)."
- ) from exc
-
- def _resolve_reference_text(self) -> None:
- """Resolve the reference transcript: explicit text, then local
- transcription, then x-vector-only mode."""
- if not self.voice_clone_ref_text and self.voice_clone_ref_audio:
- if self.skip_transcription:
- self._report("[INFO] Skipping reference audio transcription (--no-transcription).")
- else:
- self._report("[INFO] Transcribing reference audio for voice cloning...")
- self.voice_clone_ref_text = self.transcribe_audio(self.voice_clone_ref_audio) or ""
- if not self.voice_clone_ref_text:
- self._report("[WARNING] No reference text available; using "
- "x-vector-only clone mode (lower quality).")
- self._report(' Pass --transcription "..." for higher-quality in-context cloning.')
- else:
- self._report(f"[OK] Reference text:\n{self.voice_clone_ref_text}")
-
- def _init_client(self, url: str, clone: bool = False) -> None:
- """Initialize a Gradio client and store its API metadata.
-
- gradio_client prints its usage info directly to stdout while the
- client is created and its API metadata loaded, so stdout is swapped
- for a buffer for the whole process; the captured text is re-emitted
- at DEBUG level for troubleshooting.
- """
- from gradio_client import Client
-
- logger.info("Connecting to Qwen API at %s...", url)
- old_stdout = sys.stdout
- captured = io.StringIO()
- sys.stdout = captured
- try:
- try:
- client = Client(url, httpx_kwargs={"timeout": config.API_TIMEOUT})
- except TypeError:
- # Older gradio_client versions don't support httpx_kwargs.
- client = Client(url)
- if clone:
- self.clone_client = client
- self.clone_api_info = self._load_api_info(client)
- else:
- self.client = client
- self.api_info = self._load_api_info(client)
- finally:
- sys.stdout = old_stdout
- usage_info = captured.getvalue().strip()
- if usage_info:
- logger.debug("Gradio client output for %s:\n%s", url, usage_info)
- logger.info("Connected to Qwen API")
-
- @staticmethod
- def _load_api_info(client) -> Dict[str, Any]:
- """Load available API metadata from the Gradio app."""
- try:
- return client.view_api(return_format="dict")
- except Exception as exc:
- logger.warning("Unable to read API metadata: %s", exc)
- return {}
-
- def _resolve_api_name(self, *candidates: str, api_info: Optional[Dict[str, Any]] = None) -> str:
- """Return the first available api_name from candidate list."""
- info = api_info if api_info is not None else self.api_info
- named_endpoints = info.get("named_endpoints", {})
- for candidate in candidates:
- if candidate in named_endpoints:
- return candidate
- return candidates[0]
-
- def _endpoint_accepts_param(self, api_name: str, param_name: str,
- api_info: Optional[Dict[str, Any]] = None) -> bool:
- """Check whether endpoint input schema includes the given parameter."""
- info = api_info if api_info is not None else self.api_info
- endpoint = info.get("named_endpoints", {}).get(api_name, {})
- parameters = endpoint.get("parameters", [])
- return any(parameter.get("parameter_name") == param_name for parameter in parameters)
-
- # ------------------------------------------------------------------
- # Reference audio transcription (voice clone)
- # ------------------------------------------------------------------
-
- def transcribe_audio(self, audio_path: str) -> Optional[str]:
- """Transcribe reference audio locally using an optional Whisper backend."""
- return transcribe_reference_audio(audio_path)
-
- # ------------------------------------------------------------------
- # Chunk generation
- # ------------------------------------------------------------------
-
- def generate_chunk(self, text: str, chunk_num: int) -> Optional[str]:
- """Generate one audio chunk; returns its path in the chunks folder.
-
- The text is split into sub-requests of at most
- ``config.CHUNK_SIZE`` words each (the book-level chunker
- normally guarantees this already; the split is defense in depth
- against pathological input such as a punctuation-free run of
- text), and the audio files returned for the sub-requests are
- concatenated into one chunk file.
- """
- try:
- 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):
- part_paths = [
- self._generate_sub_request(sub_text, parts_dir, sub_num,
- len(sub_texts), chunk_num)
- for sub_num, sub_text in enumerate(sub_texts, 1)
- ]
- if len(part_paths) == 1:
- suffix = part_paths[0].suffix or ".wav"
- output_path = self._chunk_path(chunk_num, suffix)
- shutil.copy2(part_paths[0], output_path)
- else:
- output_path = self._chunk_path(chunk_num, ".wav")
- concat_audio_files(part_paths, output_path)
-
- logger.debug("Chunk %d generated successfully (%d sub-request(s))",
- chunk_num, len(sub_texts))
- return str(output_path)
-
- except ConversionCancelled:
- raise
- except Exception as exc:
- logger.error("Qwen chunk processing failed for chunk %d: %s", chunk_num, exc)
- return None
-
- def _generate_sub_request(self, text: str, parts_dir: str, sub_num: int,
- sub_total: int, chunk_num: int) -> Path:
- """Run one API generation for ``text``; returns the downloaded audio."""
- if sub_total > 1:
- logger.info("Chunk %d: oversized input split into %d requests "
- "(sub-request %d/%d)", chunk_num, sub_total, sub_num, sub_total)
- if self.voice_mode == VOICE_MODE_CUSTOM:
- result = self._generate_custom_voice(text)
- elif self.voice_mode == VOICE_MODE_CLONE:
- result = self._generate_voice_clone(text)
- else:
- raise ValueError(f"Unknown voice mode: {self.voice_mode}")
-
- if not isinstance(result, (tuple, list)) or not result:
- raise RuntimeError("Qwen API returned an invalid result")
-
- audio_path = result[0] # First element is the audio file path
- if not isinstance(audio_path, (str, Path)) or not audio_path:
- raise RuntimeError("Qwen API did not return an audio file path")
-
- source = Path(audio_path)
- if not source.exists():
- raise RuntimeError(f"Generated audio file not found: {audio_path}")
-
- destination = Path(parts_dir) / f"part_{sub_num:02d}{source.suffix or '.wav'}"
- shutil.copy2(source, destination)
-
- return destination
-
- # ------------------------------------------------------------------
- # API payloads
- # ------------------------------------------------------------------
-
- def _generate_custom_voice(self, text: str) -> Tuple:
- """Generate audio using CustomVoice mode."""
- custom_api = self._resolve_api_name("/run_instruct", "/run_custom_voice", "/generate_custom_voice")
- if custom_api == "/run_instruct":
- payload = dict(
- text=text,
- lang_disp=self.language,
- spk_disp=speaker_display_name(),
- instruct=config.INSTRUCT,
- )
- else:
- payload = dict(
- text=text,
- language=self.language,
- speaker=config.SPEAKER,
- instruct=config.INSTRUCT,
- )
- if self._endpoint_accepts_param(custom_api, "model_id_cv"):
- payload["model_id_cv"] = CUSTOM_VOICE_MODEL_ID
- elif self._endpoint_accepts_param(custom_api, "model_size"):
- payload["model_size"] = MODEL_SIZE
-
- if self._endpoint_accepts_param(custom_api, "seed"):
- payload["seed"] = self._seed
-
- return self.client.predict(**payload, api_name=custom_api)
-
- def _ref_audio_payload(self) -> Dict[str, Any]:
- """Gradio file payload for the reference audio (built once, reused)."""
- if self._ref_audio_filedata is None:
- from gradio_client import handle_file
- self._ref_audio_filedata = handle_file(self.voice_clone_ref_audio)
- return self._ref_audio_filedata
-
- def _generate_voice_clone(self, text: str) -> Tuple:
- """Generate audio using Voice Clone mode."""
- if not Path(self.voice_clone_ref_audio).exists():
- raise FileNotFoundError(f"Reference audio not found: {self.voice_clone_ref_audio}")
-
- if self.clone_client is None:
- raise RuntimeError("Voice Clone client is not initialized. Is the Base-model demo running?")
-
- clone_api = self._resolve_api_name("/run_voice_clone", "/generate_voice_clone",
- api_info=self.clone_api_info)
- use_xvector = config.XVECTOR_ONLY or not self.voice_clone_ref_text
-
- if clone_api == "/run_voice_clone":
- payload = dict(
- ref_aud=self._ref_audio_payload(),
- ref_txt=self.voice_clone_ref_text,
- use_xvec=use_xvector,
- text=text,
- lang_disp=self.language,
- )
- else:
- payload = dict(
- ref_audio=self._ref_audio_payload(),
- ref_text=self.voice_clone_ref_text,
- target_text=text,
- language=self.language,
- use_xvector_only=use_xvector,
- )
- optional_params = {
- "model_size": MODEL_SIZE,
- "seed": self._seed,
- }
- for name, value in optional_params.items():
- if self._endpoint_accepts_param(clone_api, name, api_info=self.clone_api_info):
- payload[name] = value
-
- return self.clone_client.predict(**payload, api_name=clone_api)
-
-
-class FasterTTSClient(_BaseTTSClient):
- """Generates audio chunks through a faster-qwen3-tts server.
-
- Talks to the OpenAI-compatible server shipped in the faster-qwen3-tts
- repository (examples/openai_server.py). The reference voice (ref audio,
- ref text) and language are configured on the server itself via
- --ref-audio/--ref-text or a --voices JSON file; this client only sends
- text. Unlike the Qwen demo, the server performs one generation per
- request, so long chunks are sub-chunked client-side.
- """
-
- def __init__(self, voice: Optional[str] = None, api_url: Optional[str] = None,
- quiet: bool = False):
- # Quiet before connecting so connect-time status lines never reach
- # a screen the TUI run view owns.
- self.quiet = bool(quiet)
- self.voice = voice or config.FASTER_VOICE
- self.api_url = (api_url or config.FASTER_API_URL).rstrip("/")
- self._check_health()
-
- def _check_health(self) -> None:
- """Verify the server is reachable and its model is loaded."""
- url = f"{self.api_url}/health"
- try:
- with urllib.request.urlopen(url, timeout=10) as response:
- payload = json.loads(response.read().decode("utf-8"))
- except Exception as exc:
- raise RuntimeError(
- f"Faster TTS server not reachable at {url}: {exc}. "
- "Start the faster-qwen3-tts OpenAI-compatible server first "
- "(see the 'Faster backend' section of the README)."
- ) from exc
- if not payload.get("model_loaded"):
- raise RuntimeError(
- "The faster TTS server is running but its model is not loaded yet; "
- "wait for model download and startup to finish, then retry."
- )
- self._report(f"[OK] Connected to faster TTS API at {self.api_url} (voice '{self.voice}')")
- self._report(f"[INFO] The server silently falls back to its first configured voice if "
- f"'{self.voice}' is not defined in its voice config (see README).")
-
- # ------------------------------------------------------------------
- # HTTP requests
- # ------------------------------------------------------------------
-
- def _request_pcm(self, text: str) -> bytes:
- """POST one sub-chunk and return raw 16-bit mono PCM bytes."""
- url = f"{self.api_url}/v1/audio/speech"
- payload = json.dumps({
- "model": "tts-1",
- "input": text,
- "voice": self.voice,
- "response_format": "pcm",
- }).encode("utf-8")
- request = urllib.request.Request(
- url, data=payload, headers={"Content-Type": "application/json"}, method="POST")
- try:
- with urllib.request.urlopen(request, timeout=config.API_TIMEOUT) as response:
- pcm = response.read()
- except urllib.error.HTTPError as exc:
- detail = ""
- try:
- detail = exc.read().decode("utf-8", errors="replace")[:200]
- except Exception:
- pass
- raise RuntimeError(f"Faster TTS server returned HTTP {exc.code}: {detail}") from exc
- except urllib.error.URLError as exc:
- raise RuntimeError(f"Faster TTS request failed: {exc.reason}") from exc
- if not pcm:
- raise RuntimeError("Faster TTS server returned empty audio")
- return pcm
-
- # ------------------------------------------------------------------
- # Chunk generation
- # ------------------------------------------------------------------
-
- def generate_chunk(self, text: str, chunk_num: int) -> Optional[str]:
- """Generate one audio chunk; returns its path in the chunks folder."""
- try:
- sub_chunks = split_into_chunks(text, max_words=config.CHUNK_SIZE)
- if not sub_chunks:
- raise RuntimeError("No text to synthesize")
-
- pcm_parts: List[bytes] = []
- with self._chunk_heartbeat(chunk_num):
- for sub_num, sub_text in enumerate(sub_chunks, 1):
- pcm = self._request_pcm(sub_text)
- pcm_parts.append(pcm)
-
- output_path = self._chunk_path(chunk_num, ".wav")
- with wave.open(str(output_path), "wb") as wav_file:
- wav_file.setnchannels(1)
- wav_file.setsampwidth(2)
- wav_file.setframerate(SAMPLE_RATE)
- wav_file.writeframes(b"".join(pcm_parts))
-
- logger.debug("Chunk %d generated (%d sub-chunks)", chunk_num, len(sub_chunks))
- return str(output_path)
-
- except ConversionCancelled:
- raise
- except Exception as exc:
- logger.error("Faster chunk processing failed for chunk %d: %s", chunk_num, exc)
- return None
-
-class AudioCppTTSClient(_BaseTTSClient):
+class AudioCppTTSClient(BaseTTSClient):
"""Generates audio chunks through an audio.cpp audiocpp_server.
Talks to the OpenAI-style HTTP API of audiocpp_server, which hosts TTS
@@ -928,15 +176,14 @@ class AudioCppTTSClient(_BaseTTSClient):
used for the Qwen client.
"""
- def __init__(self, voice: Optional[str] = None, language: Optional[str] = None,
+ def __init__(self, chunks_dir: Path,
+ voice: Optional[str] = None, language: Optional[str] = None,
api_url: Optional[str] = None,
model_id: Optional[str] = None,
instructions: Optional[str] = None,
request_options: Optional[Dict[str, str]] = None,
quiet: bool = False):
- # Quiet before connecting so connect-time status lines never reach
- # a screen the TUI run view owns.
- self.quiet = bool(quiet)
+ super().__init__(chunks_dir, quiet=quiet)
self.api_url = (api_url or config.AUDIOCPP_API_URL).rstrip("/")
# Per-run model selection: the --model CLI flag overrides config; an
# empty value is resolved at connect time when the server hosts exactly
@@ -948,10 +195,10 @@ class AudioCppTTSClient(_BaseTTSClient):
self.language = normalize_language(
language if language is not None else config.LANGUAGE)
# One seed value per run, reused for every request (see
- # _resolve_request_seed). Unlike the Qwen demo, audio.cpp has no
+ # resolve_request_seed). Unlike the Qwen demo, audio.cpp has no
# negative "randomize" seed, so a negative value means "send no seed
# at all" (see _request_wav) and the server randomizes.
- self._seed = _resolve_request_seed()
+ self._seed = resolve_request_seed()
# Voice selection (the --voice name). preset_mode / speaker_mode are
# resolved in _connect: a --voice that names a built-in CustomVoice
# speaker on a speaker-capable entry selects speaker mode; every
diff --git a/app/converter/clients/base.py b/app/converter/clients/base.py
new file mode 100644
index 0000000..a0f28cf
--- /dev/null
+++ b/app/converter/clients/base.py
@@ -0,0 +1,155 @@
+"""Shared TTS client plumbing: cancellation, retries, chunk bookkeeping."""
+
+import contextlib
+import logging
+import random
+import threading
+import time
+from pathlib import Path
+from typing import Optional
+
+from .. import config
+
+logger = logging.getLogger(__name__)
+
+
+class ConversionCancelled(Exception):
+ """Raised when the run's cancel event is set (between requests)."""
+
+
+# How a run supplies its voice: a built-in CustomVoice speaker, or by
+# cloning a reference audio clip (the faster and audiocpp backends always
+# clone server-side; only the Qwen client branches on this at request time).
+VOICE_MODE_CUSTOM = "custom_voice"
+VOICE_MODE_CLONE = "voice_clone"
+VOICE_MODES = (VOICE_MODE_CUSTOM, VOICE_MODE_CLONE)
+
+
+def resolve_request_seed() -> int:
+ """Resolve the seed sent with every request.
+
+ Returns config.SEED as-is, or (with CONSTANT_SEED and SEED < 0) one
+ random value drawn per run, meant to be reused for every request so
+ the voice stays consistent across chunk boundaries. Without
+ CONSTANT_SEED, -1 is returned so the server re-samples the voice on
+ every generation.
+ """
+ seed = config.SEED
+ if config.CONSTANT_SEED and seed < 0:
+ seed = random.randrange(2 ** 31)
+ return seed
+
+
+class BaseTTSClient:
+ """Shared chunk retry logic, heartbeat, and chunk file bookkeeping.
+
+ CHUNKS_DIR is the scratch folder the generated chunk files are written
+ to — provided by the converter that owns the run's folders, never a
+ module global, so concurrent runs (and tests) cannot step on each other.
+ """
+
+ # Class-level defaults so a partially-constructed instance behaves like
+ # a plain console run (tests build clients via __new__).
+ cancel = None
+ quiet = False
+
+ def __init__(self, chunks_dir: Path, quiet: bool = False):
+ self.chunks_dir = Path(chunks_dir)
+ # Quiet silences console prints (the run view owns the screen).
+ self.quiet = bool(quiet)
+ # Set by the converter when the run is cancellable (the TUI run
+ # view): a threading.Event that, once set, aborts the run between
+ # requests (and interrupts retry back-off sleeps).
+ self.cancel = None
+
+ def _report(self, message: str) -> None:
+ """Print a console line unless quiet (the run view owns the screen)."""
+ if not self.quiet:
+ print(message)
+
+ def generate_chunk(self, text: str, chunk_num: int) -> Optional[str]:
+ """Generate one audio chunk; returns its path in the chunks folder."""
+ raise NotImplementedError
+
+ def _cancel_requested(self) -> bool:
+ """True when the run's cancel event has been set (if any)."""
+ return isinstance(self.cancel, threading.Event) \
+ and self.cancel.is_set()
+
+ def _check_cancelled(self) -> None:
+ """Raise ConversionCancelled when the cancel event is set."""
+ if self._cancel_requested():
+ raise ConversionCancelled("Cancelled by user")
+
+ def _sleep(self, seconds: float) -> None:
+ """Sleep SECONDS, cut short (raising) when the cancel event sets."""
+ if isinstance(self.cancel, threading.Event):
+ if self.cancel.wait(seconds):
+ raise ConversionCancelled("Cancelled by user")
+ else:
+ time.sleep(seconds)
+
+ def _chunk_path(self, chunk_num: int, suffix: str) -> Path:
+ """Resolve the target path for a chunk, removing stale files first.
+
+ Any stale chunk file for this index is removed so a retry or extension
+ change can never leave two files matching chunk_NNNN.*.
+ """
+ for stale in self.chunks_dir.glob(f"chunk_{chunk_num:04d}.*"):
+ try:
+ stale.unlink()
+ except OSError as exc:
+ logger.debug("Could not remove stale chunk file %s: %s", stale, exc)
+ return self.chunks_dir / f"chunk_{chunk_num:04d}{suffix}"
+
+ def process_chunk_with_retry(self, chunk_num: int, text: str) -> Optional[Path]:
+ """Process a chunk with retry logic.
+
+ Returns the generated chunk file's path, or None when all attempts
+ failed. Raises ConversionCancelled when the run was cancelled.
+ """
+ for attempt in range(config.MAX_RETRIES):
+ self._check_cancelled()
+ try:
+ result = self.generate_chunk(text, chunk_num)
+ if result and Path(result).exists():
+ return Path(result)
+ logger.warning("Chunk %d attempt %d failed", chunk_num, attempt + 1)
+ except ConversionCancelled:
+ raise
+ except Exception as exc:
+ logger.warning("Chunk %d attempt %d error: %s", chunk_num, attempt + 1, exc)
+
+ if attempt < config.MAX_RETRIES - 1:
+ sleep_time = 5 + (2 ** attempt)
+ logger.info("Waiting %ds before retry...", sleep_time)
+ self._sleep(sleep_time)
+
+ logger.error("Chunk %d failed after %d attempts", chunk_num, config.MAX_RETRIES)
+ return None
+
+ @contextlib.contextmanager
+ def _chunk_heartbeat(self, chunk_num: int):
+ """Log a periodic "still working" record while a request generates."""
+ stop = threading.Event()
+ subject = f"Chunk {chunk_num}"
+
+ def _beat():
+ start = time.time()
+ while not stop.wait(config.HEARTBEAT_INTERVAL_SECONDS):
+ elapsed = time.time() - start
+ if self.quiet:
+ logger.info("%s still generating — %dm %ds elapsed",
+ subject, int(elapsed // 60), int(elapsed % 60))
+ else:
+ print(f"[...] {subject} still generating — "
+ f"{int(elapsed // 60)}m {int(elapsed % 60)}s elapsed",
+ flush=True)
+
+ thread = threading.Thread(target=_beat, daemon=True)
+ thread.start()
+ try:
+ yield
+ finally:
+ stop.set()
+ thread.join()
diff --git a/app/converter/clients/faster.py b/app/converter/clients/faster.py
new file mode 100644
index 0000000..df98546
--- /dev/null
+++ b/app/converter/clients/faster.py
@@ -0,0 +1,123 @@
+"""Client for the faster-qwen3-tts OpenAI-compatible server."""
+
+import json
+import logging
+import urllib.error
+import urllib.request
+import wave
+from pathlib import Path
+from typing import List, Optional
+
+from .. import config
+from ..chunking import split_into_chunks
+from .base import BaseTTSClient, ConversionCancelled
+
+logger = logging.getLogger(__name__)
+
+# The 12Hz codec the faster server synthesizes with outputs 24 kHz audio.
+SAMPLE_RATE = 24000
+
+
+class FasterTTSClient(BaseTTSClient):
+ """Generates audio chunks through a faster-qwen3-tts server.
+
+ Talks to the OpenAI-compatible server shipped in the faster-qwen3-tts
+ repository (examples/openai_server.py). The reference voice (ref audio,
+ ref text) and language are configured on the server itself via
+ --ref-audio/--ref-text or a --voices JSON file; this client only sends
+ text. Unlike the Qwen demo, the server performs one generation per
+ request, so long chunks are sub-chunked client-side.
+ """
+
+ def __init__(self, chunks_dir: Path,
+ voice: Optional[str] = None, api_url: Optional[str] = None,
+ quiet: bool = False):
+ super().__init__(chunks_dir, quiet=quiet)
+ self.voice = voice or config.FASTER_VOICE
+ self.api_url = (api_url or config.FASTER_API_URL).rstrip("/")
+ self._check_health()
+
+ def _check_health(self) -> None:
+ """Verify the server is reachable and its model is loaded."""
+ url = f"{self.api_url}/health"
+ try:
+ with urllib.request.urlopen(url, timeout=10) as response:
+ payload = json.loads(response.read().decode("utf-8"))
+ except Exception as exc:
+ raise RuntimeError(
+ f"Faster TTS server not reachable at {url}: {exc}. "
+ "Start the faster-qwen3-tts OpenAI-compatible server first "
+ "(see the 'Faster backend' section of the README)."
+ ) from exc
+ if not payload.get("model_loaded"):
+ raise RuntimeError(
+ "The faster TTS server is running but its model is not loaded yet; "
+ "wait for model download and startup to finish, then retry."
+ )
+ self._report(f"[OK] Connected to faster TTS API at {self.api_url} (voice '{self.voice}')")
+ self._report(f"[INFO] The server silently falls back to its first configured voice if "
+ f"'{self.voice}' is not defined in its voice config (see README).")
+
+ # ------------------------------------------------------------------
+ # HTTP requests
+ # ------------------------------------------------------------------
+
+ def _request_pcm(self, text: str) -> bytes:
+ """POST one sub-chunk and return raw 16-bit mono PCM bytes."""
+ url = f"{self.api_url}/v1/audio/speech"
+ payload = json.dumps({
+ "model": "tts-1",
+ "input": text,
+ "voice": self.voice,
+ "response_format": "pcm",
+ }).encode("utf-8")
+ request = urllib.request.Request(
+ url, data=payload, headers={"Content-Type": "application/json"}, method="POST")
+ try:
+ with urllib.request.urlopen(request, timeout=config.API_TIMEOUT) as response:
+ pcm = response.read()
+ except urllib.error.HTTPError as exc:
+ detail = ""
+ try:
+ detail = exc.read().decode("utf-8", errors="replace")[:200]
+ except Exception:
+ pass
+ raise RuntimeError(f"Faster TTS server returned HTTP {exc.code}: {detail}") from exc
+ except urllib.error.URLError as exc:
+ raise RuntimeError(f"Faster TTS request failed: {exc.reason}") from exc
+ if not pcm:
+ raise RuntimeError("Faster TTS server returned empty audio")
+ return pcm
+
+ # ------------------------------------------------------------------
+ # Chunk generation
+ # ------------------------------------------------------------------
+
+ def generate_chunk(self, text: str, chunk_num: int) -> Optional[str]:
+ """Generate one audio chunk; returns its path in the chunks folder."""
+ try:
+ sub_chunks = split_into_chunks(text, max_words=config.CHUNK_SIZE)
+ if not sub_chunks:
+ raise RuntimeError("No text to synthesize")
+
+ pcm_parts: List[bytes] = []
+ with self._chunk_heartbeat(chunk_num):
+ for sub_num, sub_text in enumerate(sub_chunks, 1):
+ pcm = self._request_pcm(sub_text)
+ pcm_parts.append(pcm)
+
+ output_path = self._chunk_path(chunk_num, ".wav")
+ with wave.open(str(output_path), "wb") as wav_file:
+ wav_file.setnchannels(1)
+ wav_file.setsampwidth(2)
+ wav_file.setframerate(SAMPLE_RATE)
+ wav_file.writeframes(b"".join(pcm_parts))
+
+ logger.debug("Chunk %d generated (%d sub-chunks)", chunk_num, len(sub_chunks))
+ return str(output_path)
+
+ except ConversionCancelled:
+ raise
+ except Exception as exc:
+ logger.error("Faster chunk processing failed for chunk %d: %s", chunk_num, exc)
+ return None
diff --git a/app/converter/clients/languages.py b/app/converter/clients/languages.py
new file mode 100644
index 0000000..079dead
--- /dev/null
+++ b/app/converter/clients/languages.py
@@ -0,0 +1,74 @@
+"""Language tables shared by the TTS clients and their UIs."""
+
+from typing import Optional
+
+# Languages the Qwen3-TTS demo accepts as display names (its API silently
+# falls back to "Auto" for anything else, so unknown names are rejected
+# before a run starts instead of mispronouncing a whole book).
+TTS_LANGUAGES = (
+ "Auto", "Chinese", "English", "German", "Italian", "Portuguese",
+ "Spanish", "Japanese", "Korean", "French", "Russian",
+)
+
+# Short aliases accepted on the command line (ISO 639-1 codes and common
+# shorthands), mapped to the display names above.
+TTS_LANGUAGE_ALIASES = {
+ "zh": "Chinese",
+ "en": "English",
+ "de": "German",
+ "it": "Italian",
+ "pt": "Portuguese",
+ "es": "Spanish",
+ "ja": "Japanese",
+ "ko": "Korean",
+ "fr": "French",
+ "ru": "Russian",
+ "zh-cn": "Chinese",
+ "zh-tw": "Chinese",
+ "pt-br": "Portuguese",
+ "en-us": "English",
+ "en-gb": "English",
+}
+
+# Qwen display names -> ISO 639-1 codes, for audio.cpp families whose
+# language request option takes a code instead of a display name. "Auto"
+# has no code and maps to None so the field is omitted and the server
+# applies its own default.
+LANGUAGE_ISO_CODES = {
+ "Chinese": "zh",
+ "English": "en",
+ "German": "de",
+ "Italian": "it",
+ "Portuguese": "pt",
+ "Spanish": "es",
+ "Japanese": "ja",
+ "Korean": "ko",
+ "French": "fr",
+ "Russian": "ru",
+}
+
+
+def normalize_language(value: Optional[str]) -> str:
+ """Normalize a user-provided language name to a Qwen3-TTS display name.
+
+ Accepts the display names in TTS_LANGUAGES case-insensitively as
+ well as the short aliases in TTS_LANGUAGE_ALIASES (ISO 639-1 codes
+ and common shorthands). Raises ValueError for anything else, since the
+ Qwen3-TTS demo silently falls back to "Auto" for unrecognized languages.
+ """
+ if value is None:
+ raise ValueError("Language must not be None")
+ candidate = value.strip()
+ if not candidate:
+ raise ValueError("Language must not be empty")
+ for name in TTS_LANGUAGES:
+ if candidate.lower() == name.lower():
+ return name
+ alias = TTS_LANGUAGE_ALIASES.get(candidate.lower())
+ if alias:
+ return alias
+ raise ValueError(
+ f"Unknown language: {value!r}. Expected one of "
+ f"{', '.join(TTS_LANGUAGES)} (or an alias: "
+ f"{', '.join(sorted(TTS_LANGUAGE_ALIASES))})."
+ )
diff --git a/app/converter/clients/qwen.py b/app/converter/clients/qwen.py
new file mode 100644
index 0000000..354ee04
--- /dev/null
+++ b/app/converter/clients/qwen.py
@@ -0,0 +1,322 @@
+"""Client for the qwen-tts Gradio demo servers (CustomVoice + Base)."""
+
+import io
+import logging
+import shutil
+import sys
+import tempfile
+from pathlib import Path
+from typing import Any, Dict, Optional, Tuple
+
+from .. import config
+from ..audio import concat_audio_files
+from ..chunking import split_into_chunks
+from .base import (BaseTTSClient, ConversionCancelled, resolve_request_seed,
+ VOICE_MODE_CLONE, VOICE_MODE_CUSTOM, VOICE_MODES)
+from .languages import normalize_language
+from .speakers import speaker_display_name
+
+logger = logging.getLogger(__name__)
+
+# Fixed model facts: both demos run the 1.7B model (the CustomVoice demo
+# takes its full HuggingFace id), and the 12Hz codec outputs 24 kHz audio.
+MODEL_SIZE = "1.7B"
+CUSTOM_VOICE_MODEL_ID = "Qwen/Qwen3-TTS-12Hz-1.7B-CustomVoice"
+
+
+class QwenTTSClient(BaseTTSClient):
+ """Generates audio chunks through a Qwen3-TTS demo server."""
+
+ def __init__(self, chunks_dir: Path,
+ voice_mode: str = "custom_voice", voice_clone_ref_audio: Optional[str] = None,
+ voice_clone_ref_text: Optional[str] = None, skip_transcription: bool = False,
+ language: Optional[str] = None, api_url: Optional[str] = None,
+ quiet: bool = False):
+ super().__init__(chunks_dir, quiet=quiet)
+ if voice_mode not in VOICE_MODES:
+ raise ValueError(
+ f"Unknown voice mode: {voice_mode!r} (expected one of {VOICE_MODES})"
+ )
+ self.voice_mode = voice_mode
+ self.voice_clone_ref_audio = voice_clone_ref_audio
+ self.voice_clone_ref_text = (voice_clone_ref_text or "").strip()
+ self.skip_transcription = skip_transcription
+ # api_url overrides the configured endpoint for the active voice mode
+ # (used by the hub's "[remote]" backend entries and --api-url).
+ self.api_url = (api_url or "").strip() or None
+ # Seed sent with every request: config.SEED as-is, or (with
+ # CONSTANT_SEED and SEED < 0) one random value drawn per run and
+ # reused for every request so the voice stays consistent across
+ # chunk boundaries. Without CONSTANT_SEED, -1 is forwarded so the
+ # server re-samples the voice on every generation.
+ self._seed = resolve_request_seed()
+ if language is None:
+ language = config.LANGUAGE
+ # Validate before connecting so bad values fail fast without a server.
+ self.language = normalize_language(language)
+ self.client = None
+ self.api_info: Dict[str, Any] = {}
+ self.clone_client = None
+ self.clone_api_info: Dict[str, Any] = {}
+ self._ref_audio_filedata: Optional[Dict[str, Any]] = None
+ self._connect()
+
+ # ------------------------------------------------------------------
+ # Connection
+ # ------------------------------------------------------------------
+
+ def _connect(self) -> None:
+ api_url = self.api_url or (
+ config.CLONE_API_URL if self.voice_mode == VOICE_MODE_CLONE
+ else config.QWEN_API_URL)
+ try:
+ if self.voice_mode == VOICE_MODE_CLONE:
+ # Voice clone uses the Base-model demo, which is a separate server
+ # from the CustomVoice demo (that one only exposes /run_instruct).
+ self._init_client(api_url, clone=True)
+ self._report(f"[OK] Connected to Voice Clone API at {api_url}")
+ self._resolve_reference_text()
+ else:
+ self._init_client(api_url, clone=False)
+ self._report("[OK] Connected to Qwen API")
+ except Exception as exc:
+ raise RuntimeError(
+ f"Qwen API initialization failed at {api_url}: {exc}. "
+ "Make sure the Qwen demo server is running and reachable, and that your "
+ "installed Qwen3-TTS version matches this converter's API expectations "
+ "(voice clone requires the Base-model demo: Qwen/Qwen3-TTS-12Hz-1.7B-Base)."
+ ) from exc
+
+ def _resolve_reference_text(self) -> None:
+ """Resolve the reference transcript: explicit text, then local
+ transcription, then x-vector-only mode."""
+ if not self.voice_clone_ref_text and self.voice_clone_ref_audio:
+ if self.skip_transcription:
+ self._report("[INFO] Skipping reference audio transcription (--no-transcription).")
+ else:
+ self._report("[INFO] Transcribing reference audio for voice cloning...")
+ self.voice_clone_ref_text = self.transcribe_audio(self.voice_clone_ref_audio) or ""
+ if not self.voice_clone_ref_text:
+ self._report("[WARNING] No reference text available; using "
+ "x-vector-only clone mode (lower quality).")
+ self._report(' Pass --transcription "..." for higher-quality in-context cloning.')
+ else:
+ self._report(f"[OK] Reference text:\n{self.voice_clone_ref_text}")
+
+ def _init_client(self, url: str, clone: bool = False) -> None:
+ """Initialize a Gradio client and store its API metadata.
+
+ gradio_client prints its usage info directly to stdout while the
+ client is created and its API metadata loaded, so stdout is swapped
+ for a buffer for the whole process; the captured text is re-emitted
+ at DEBUG level for troubleshooting.
+ """
+ from gradio_client import Client
+
+ logger.info("Connecting to Qwen API at %s...", url)
+ old_stdout = sys.stdout
+ captured = io.StringIO()
+ sys.stdout = captured
+ try:
+ try:
+ client = Client(url, httpx_kwargs={"timeout": config.API_TIMEOUT})
+ except TypeError:
+ # Older gradio_client versions don't support httpx_kwargs.
+ client = Client(url)
+ if clone:
+ self.clone_client = client
+ self.clone_api_info = self._load_api_info(client)
+ else:
+ self.client = client
+ self.api_info = self._load_api_info(client)
+ finally:
+ sys.stdout = old_stdout
+ usage_info = captured.getvalue().strip()
+ if usage_info:
+ logger.debug("Gradio client output for %s:\n%s", url, usage_info)
+ logger.info("Connected to Qwen API")
+
+ @staticmethod
+ def _load_api_info(client) -> Dict[str, Any]:
+ """Load available API metadata from the Gradio app."""
+ try:
+ return client.view_api(return_format="dict")
+ except Exception as exc:
+ logger.warning("Unable to read API metadata: %s", exc)
+ return {}
+
+ def _resolve_api_name(self, *candidates: str, api_info: Optional[Dict[str, Any]] = None) -> str:
+ """Return the first available api_name from candidate list."""
+ info = api_info if api_info is not None else self.api_info
+ named_endpoints = info.get("named_endpoints", {})
+ for candidate in candidates:
+ if candidate in named_endpoints:
+ return candidate
+ return candidates[0]
+
+ def _endpoint_accepts_param(self, api_name: str, param_name: str,
+ api_info: Optional[Dict[str, Any]] = None) -> bool:
+ """Check whether endpoint input schema includes the given parameter."""
+ info = api_info if api_info is not None else self.api_info
+ endpoint = info.get("named_endpoints", {}).get(api_name, {})
+ parameters = endpoint.get("parameters", [])
+ return any(parameter.get("parameter_name") == param_name for parameter in parameters)
+
+ # ------------------------------------------------------------------
+ # Reference audio transcription (voice clone)
+ # ------------------------------------------------------------------
+
+ def transcribe_audio(self, audio_path: str) -> Optional[str]:
+ """Transcribe reference audio locally using an optional Whisper backend."""
+ from .transcribe import transcribe_reference_audio
+ return transcribe_reference_audio(audio_path)
+
+ # ------------------------------------------------------------------
+ # Chunk generation
+ # ------------------------------------------------------------------
+
+ def generate_chunk(self, text: str, chunk_num: int) -> Optional[str]:
+ """Generate one audio chunk; returns its path in the chunks folder.
+
+ The text is split into sub-requests of at most
+ ``config.CHUNK_SIZE`` words each (the book-level chunker
+ normally guarantees this already; the split is defense in depth
+ against pathological input such as a punctuation-free run of
+ text), and the audio files returned for the sub-requests are
+ concatenated into one chunk file.
+ """
+ try:
+ 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):
+ part_paths = [
+ self._generate_sub_request(sub_text, parts_dir, sub_num,
+ len(sub_texts), chunk_num)
+ for sub_num, sub_text in enumerate(sub_texts, 1)
+ ]
+ if len(part_paths) == 1:
+ suffix = part_paths[0].suffix or ".wav"
+ output_path = self._chunk_path(chunk_num, suffix)
+ shutil.copy2(part_paths[0], output_path)
+ else:
+ output_path = self._chunk_path(chunk_num, ".wav")
+ concat_audio_files(part_paths, output_path)
+
+ logger.debug("Chunk %d generated successfully (%d sub-request(s))",
+ chunk_num, len(sub_texts))
+ return str(output_path)
+
+ except ConversionCancelled:
+ raise
+ except Exception as exc:
+ logger.error("Qwen chunk processing failed for chunk %d: %s", chunk_num, exc)
+ return None
+
+ def _generate_sub_request(self, text: str, parts_dir: str, sub_num: int,
+ sub_total: int, chunk_num: int) -> Path:
+ """Run one API generation for ``text``; returns the downloaded audio."""
+ if sub_total > 1:
+ logger.info("Chunk %d: oversized input split into %d requests "
+ "(sub-request %d/%d)", chunk_num, sub_total, sub_num, sub_total)
+ if self.voice_mode == VOICE_MODE_CUSTOM:
+ result = self._generate_custom_voice(text)
+ elif self.voice_mode == VOICE_MODE_CLONE:
+ result = self._generate_voice_clone(text)
+ else:
+ raise ValueError(f"Unknown voice mode: {self.voice_mode}")
+
+ if not isinstance(result, (tuple, list)) or not result:
+ raise RuntimeError("Qwen API returned an invalid result")
+
+ audio_path = result[0] # First element is the audio file path
+ if not isinstance(audio_path, (str, Path)) or not audio_path:
+ raise RuntimeError("Qwen API did not return an audio file path")
+
+ source = Path(audio_path)
+ if not source.exists():
+ raise RuntimeError(f"Generated audio file not found: {audio_path}")
+
+ destination = Path(parts_dir) / f"part_{sub_num:02d}{source.suffix or '.wav'}"
+ shutil.copy2(source, destination)
+
+ return destination
+
+ # ------------------------------------------------------------------
+ # API payloads
+ # ------------------------------------------------------------------
+
+ def _generate_custom_voice(self, text: str) -> Tuple:
+ """Generate audio using CustomVoice mode."""
+ custom_api = self._resolve_api_name("/run_instruct", "/run_custom_voice", "/generate_custom_voice")
+ if custom_api == "/run_instruct":
+ payload = dict(
+ text=text,
+ lang_disp=self.language,
+ spk_disp=speaker_display_name(),
+ instruct=config.INSTRUCT,
+ )
+ else:
+ payload = dict(
+ text=text,
+ language=self.language,
+ speaker=config.SPEAKER,
+ instruct=config.INSTRUCT,
+ )
+ if self._endpoint_accepts_param(custom_api, "model_id_cv"):
+ payload["model_id_cv"] = CUSTOM_VOICE_MODEL_ID
+ elif self._endpoint_accepts_param(custom_api, "model_size"):
+ payload["model_size"] = MODEL_SIZE
+
+ if self._endpoint_accepts_param(custom_api, "seed"):
+ payload["seed"] = self._seed
+
+ return self.client.predict(**payload, api_name=custom_api)
+
+ def _ref_audio_payload(self) -> Dict[str, Any]:
+ """Gradio file payload for the reference audio (built once, reused)."""
+ if self._ref_audio_filedata is None:
+ from gradio_client import handle_file
+ self._ref_audio_filedata = handle_file(self.voice_clone_ref_audio)
+ return self._ref_audio_filedata
+
+ def _generate_voice_clone(self, text: str) -> Tuple:
+ """Generate audio using Voice Clone mode."""
+ if not Path(self.voice_clone_ref_audio).exists():
+ raise FileNotFoundError(f"Reference audio not found: {self.voice_clone_ref_audio}")
+
+ if self.clone_client is None:
+ raise RuntimeError("Voice Clone client is not initialized. Is the Base-model demo running?")
+
+ clone_api = self._resolve_api_name("/run_voice_clone", "/generate_voice_clone",
+ api_info=self.clone_api_info)
+ use_xvector = config.XVECTOR_ONLY or not self.voice_clone_ref_text
+
+ if clone_api == "/run_voice_clone":
+ payload = dict(
+ ref_aud=self._ref_audio_payload(),
+ ref_txt=self.voice_clone_ref_text,
+ use_xvec=use_xvector,
+ text=text,
+ lang_disp=self.language,
+ )
+ else:
+ payload = dict(
+ ref_audio=self._ref_audio_payload(),
+ ref_text=self.voice_clone_ref_text,
+ target_text=text,
+ language=self.language,
+ use_xvector_only=use_xvector,
+ )
+ optional_params = {
+ "model_size": MODEL_SIZE,
+ "seed": self._seed,
+ }
+ for name, value in optional_params.items():
+ if self._endpoint_accepts_param(clone_api, name, api_info=self.clone_api_info):
+ payload[name] = value
+
+ return self.clone_client.predict(**payload, api_name=clone_api)
diff --git a/app/converter/clients/speakers.py b/app/converter/clients/speakers.py
new file mode 100644
index 0000000..eecd52a
--- /dev/null
+++ b/app/converter/clients/speakers.py
@@ -0,0 +1,57 @@
+"""Qwen3-TTS built-in speaker names and their wire (display) forms."""
+
+from typing import Optional
+
+from .. import config
+
+# Built-in CustomVoice speaker names for the Qwen3-TTS family. Shared by the
+# qwen-tts demo backend (config.SPEAKER, the qwen setup/form) and the
+# audio.cpp audiocpp backend's CustomVoice entry (the Convert form's Speaker
+# picker). Entries are the canonical/config form; speaker_display_name()
+# maps them to the wire (display) form via SPEAKER_DISPLAY_NAMES below.
+QWEN3_TTS_SPEAKERS = ("Vivian", "Serena", "Uncle_Fu", "Dylan", "Eric",
+ "Ryan", "Aiden", "Ono_Anna", "Sohee")
+
+# Canonical speaker names -> display names used by the qwen-tts demo.
+SPEAKER_DISPLAY_NAMES = {
+ "ryan": "Ryan",
+ "serena": "Serena",
+ "vivian": "Vivian",
+ "uncle_fu": "Uncle Fu",
+ "aiden": "Aiden",
+ "ono_anna": "Ono Anna",
+ "sohee": "Sohee",
+ "eric": "Eric",
+ "dylan": "Dylan",
+}
+
+
+def speaker_display_name_for(name: str) -> str:
+ """Return the wire (display) form of a Qwen3-TTS CustomVoice speaker NAME.
+
+ Accepts either the canonical/config form (e.g. "uncle_fu", "Uncle_Fu")
+ or the display form ("Uncle Fu"), case-insensitively; unknown names pass
+ through unchanged. Used by AudioCppTTSClient to normalize the --voice /
+ Speaker-picker value into what audiocpp_server expects in the request's
+ voice field.
+ """
+ return SPEAKER_DISPLAY_NAMES.get((name or "").lower(), name)
+
+
+def is_builtin_speaker(name: Optional[str]) -> bool:
+ """True when NAME is one of the Qwen3-TTS CustomVoice built-in speakers.
+
+ Matches case-insensitively across the canonical ("Uncle_Fu"), display
+ ("Uncle Fu") and shorthand ("uncle_fu") forms, so the --voice flag and
+ the Convert form's Speaker picker resolve to the same set.
+ """
+ if not name:
+ return False
+ norm = name.lower().replace("_", " ").replace("-", " ")
+ return any(norm == speaker.lower().replace("_", " ")
+ for speaker in QWEN3_TTS_SPEAKERS)
+
+
+def speaker_display_name() -> str:
+ """Return the display name for the configured custom speaker."""
+ return speaker_display_name_for(config.SPEAKER)
diff --git a/app/converter/clients/transcribe.py b/app/converter/clients/transcribe.py
new file mode 100644
index 0000000..d2db9f1
--- /dev/null
+++ b/app/converter/clients/transcribe.py
@@ -0,0 +1,54 @@
+"""Optional local Whisper transcription of reference audio."""
+
+import logging
+from typing import Optional
+
+logger = logging.getLogger(__name__)
+
+
+def transcribe_reference_audio(audio_path: str, model_name: str = "base") -> Optional[str]:
+ """Transcribe reference audio locally using an optional Whisper backend.
+
+ The current qwen-tts demo does not expose a transcription endpoint, so
+ transcription is done client-side when a Whisper package is available.
+ Returns None if no backend is installed.
+ """
+ for backend in ("faster_whisper", "whisper"):
+ try:
+ if backend == "faster_whisper":
+ from faster_whisper import WhisperModel
+ model = WhisperModel(model_name, device="cpu", compute_type="int8")
+ segments, _ = model.transcribe(audio_path)
+ text = " ".join(seg.text.strip() for seg in segments).strip()
+ else:
+ import whisper
+ model = whisper.load_model(model_name)
+ result = model.transcribe(audio_path)
+ text = (result.get("text") or "").strip()
+ if text:
+ logger.info("Transcription complete via %s: %s", backend, text)
+ return text
+ except ImportError:
+ continue
+ except Exception as exc:
+ logger.warning("%s transcription failed: %s", backend, exc)
+ logger.warning("No Whisper backend available; transcription skipped.")
+ return None
+
+
+def whisper_backend_available() -> Optional[str]:
+ """Return the name of an importable Whisper backend, or None.
+
+ Checks faster_whisper first (preferred), then the openai-whisper
+ package, without importing the heavy model code: a bare import probe
+ is enough to tell whether the package is installed in the current
+ environment. Used by the make_audiocpp_server_json tool to warn when
+ neither is present (e.g. the wrong conda environment is active).
+ """
+ for backend in ("faster_whisper", "whisper"):
+ try:
+ __import__(backend)
+ except ImportError:
+ continue
+ return backend
+ return None
diff --git a/app/converter/converter.py b/app/converter/converter.py
index 1abc85c..48d4987 100644
--- a/app/converter/converter.py
+++ b/app/converter/converter.py
@@ -15,7 +15,7 @@ from typing import Callable, Dict, List, Optional, Tuple
from . import audio, chunking, config, cover, extractors
from .audio import TrackMeta
-from .tts import (
+from .clients import (
BACKENDS,
BACKEND_AUDIOCPP,
BACKEND_FASTER,
@@ -228,7 +228,8 @@ class AudiobookConverter:
if backend == BACKEND_FASTER:
# The faster backend always voice-clones using a reference voice
# configured on the server, so no local reference audio is needed.
- self.tts = FasterTTSClient(voice=voice, api_url=api_url,
+ self.tts = FasterTTSClient(chunks_dir=CHUNKS_FOLDER,
+ voice=voice, api_url=api_url,
quiet=quiet)
elif backend == BACKEND_AUDIOCPP:
# --voice picks the voice: a built-in speaker name on the
@@ -237,13 +238,15 @@ class AudiobookConverter:
# multi-model servers; instructions describe or style the
# voice, request_options pass per-model controls through to
# the server.
- self.tts = AudioCppTTSClient(voice=voice, language=self.language,
+ self.tts = AudioCppTTSClient(chunks_dir=CHUNKS_FOLDER,
+ voice=voice, language=self.language,
model_id=model_id,
instructions=instructions,
request_options=self.request_options,
api_url=api_url, quiet=quiet)
else:
self.tts = QwenTTSClient(
+ chunks_dir=CHUNKS_FOLDER,
voice_mode=voice_mode,
voice_clone_ref_audio=voice_clone_ref_audio,
voice_clone_ref_text=voice_clone_ref_text,
@@ -398,7 +401,7 @@ class AudiobookConverter:
self.current_outputs = []
# Start from a clean scratch folder so a previous crash can never
# affect this run
- audio.cleanup_chunks()
+ audio.cleanup_chunks(CHUNKS_FOLDER)
logger.info("Extracting text...")
book = extractors.extract_book(file_path)
@@ -476,7 +479,7 @@ class AudiobookConverter:
return False
finally:
# Always cleanup, even on failure or interrupt
- audio.cleanup_chunks()
+ audio.cleanup_chunks(CHUNKS_FOLDER)
def _convert_m4b_with_chapters(self, sections, stem: str, start_time: float,
meta: Optional[TrackMeta] = None,
@@ -519,7 +522,9 @@ class AudiobookConverter:
return False
output_path = AUDIOBOOKS_FOLDER / f"{stem}.{self.output_format}"
- if not audio.combine_chapters_to_m4b(chapter_files, titles, output_path, speed=self.speed,
+ if not audio.combine_chapters_to_m4b(chapter_files, titles, output_path,
+ chunks_dir=CHUNKS_FOLDER,
+ speed=self.speed,
meta=meta, cover=cover):
return False
duration = time.time() - start_time
@@ -653,7 +658,9 @@ class AudiobookConverter:
successful_chunks, total_chunks)
return False
- success = audio.combine_chunks(total_chunks, output_path, chunk_results=results,
+ success = audio.combine_chunks(total_chunks, output_path,
+ chunk_results=results,
+ chunks_dir=CHUNKS_FOLDER,
speed=speed, output_format=output_format,
intermediate=chapter is not None,
meta=meta, cover=cover)