A rich terminal EPUB reader with TTS support
Project description
SpeakUB ๐
A modern, feature-rich terminal EPUB reader with Text-to-Speech support, built with Rich/Textual for a beautiful CLI experience.
โจ Features
- ๐จ Rich Terminal UI - Beautiful interface with Rich and Textual
- ๐ Full EPUB Support - Handles EPUB 2 and EPUB 3 formats
- ๐ Text-to-Speech - Built-in TTS using Microsoft Edge-TTS with Reservoir v5.2
- ๐ Intelligent Buffer Management - Water Level Control system for seamless playback
- ๐ Smart Navigation - Table of contents with hierarchical chapters
- ๐พ Progress Tracking - Automatically saves your reading position
- ๐ฏ Seamless Reading - Navigate between chapters without interruption
- ๐ผ๏ธ Image Support - View embedded images (optional)
- โจ๏ธ Keyboard Shortcuts - Efficient navigation with familiar keys
- ๐๏ธ TTS Controls - Play, Pause, Stop with speed/volume control
- ๐ฃ๏ธ Chinese Pronunciation Corrections - Optional pronunciation correction system
๐ Installation
Quick Install
pip install speakub
Development Install
git clone https://github.com/eyes1971/SpeakUB.git
cd SpeakUB
pip install -e .
With TTS Support
pip install speakub[tts]
With All Features
pip install speakub[all]
๏ฟฝ๏ธ Desktop Integration
SpeakUB automatically creates a desktop entry on first run, allowing you to:
- Right-click EPUB files and select "Open with SpeakUB"
- Double-click EPUB files to open them directly
The desktop entry uses speakub %f command, which automatically detects and launches in your preferred terminal emulator.
๏ฟฝ๐ Requirements
- Python 3.8+
- Terminal with Unicode support
Optional Dependencies
- TTS:
edge-ttsandpygamefor text-to-speech - Images:
fabulousandPillowfor image display
๐ฎ Usage
Basic Usage
speakub book.epub
Dump to Text
speakub book.epub --dump --cols 80
โจ๏ธ Keyboard Shortcuts
Global Controls
| Key | Action |
|---|---|
Esc / q |
Quit application |
Tab |
Switch focus between panels |
F1 |
Toggle table of contents |
F2 |
Toggle TTS panel |
Table of Contents (TOC)
| Key | Action |
|---|---|
โ / โ |
Navigate chapters |
PgUp / PgDn |
Page up/down |
Enter / โ |
Open chapter or expand group |
โ |
Collapse group |
Content Reading
| Key | Action |
|---|---|
โ / โ |
Scroll content (seamless across chapters) |
PgUp / PgDn |
Page up/down |
Home / End |
Go to start/end of chapter |
i |
Open images in browser (if available) |
TTS Controls
| Key | Action |
|---|---|
Space / p |
Play/Pause |
s |
Stop |
+ / = |
Increase volume |
- |
Decrease volume |
[ |
Decrease speed |
] |
Increase speed |
โ / โ |
Navigate TTS controls |
๐๏ธ Architecture
speakub/
โโโ speakub/ # Main package
โ โโโ core/ # Core functionality
โ โ โโโ epub_parser.py # EPUB parsing
โ โ โโโ content_renderer.py # HTML to text conversion (with adaptive cache)
โ โ โโโ chapter_manager.py # Chapter navigation
โ โ โโโ progress_tracker.py # Reading progress
โ โ โโโ exceptions.py # Custom exceptions
โ โ โโโ cfi.py # EPUB CFI handling
โ โ โโโ epub/ # EPUB-specific parsers
โ โ โโโ metadata_parser.py
โ โ โโโ opf_parser.py
โ โ โโโ path_resolver.py
โ โ โโโ toc_parser.py
โ โโโ tts/ # Text-to-Speech
โ โ โโโ engine.py # TTS abstraction
โ โ โโโ edge_tts_provider.py # Edge-TTS provider
โ โ โโโ gtts_provider.py # Google TTS provider
โ โ โโโ nanmai_tts_provider.py # Nanmai TTS provider
โ โ โโโ audio_player.py # Audio playback
โ โ โโโ playback_manager.py # Playback management
โ โ โโโ playlist_manager.py # Playlist handling
โ โ โโโ integration.py # TTS integration
โ โ โโโ reservoir/ # Reservoir architecture (v4.0)
โ โ โ โโโ controller.py # Predictive batch controller
โ โ โ โโโ system_monitors.py # Network and resource monitoring
โ โ โ โโโ play_monitor.py # Playback time monitoring
โ โ โ โโโ queue_predictor.py # Queue prediction engine
โ โ โ โโโ __init__.py # Package initialization
โ โ โโโ backends/ # Audio backends
โ โ โ โโโ base.py
โ โ โ โโโ mpv_backend.py
โ โ โ โโโ pygame_backend.py
โ โ โโโ ui/ # TTS UI components
โ โ โโโ network.py
โ โ โโโ playlist.py
โ โ โโโ runners.py
โ โโโ ui/ # User interfaces
โ โ โโโ app.py # Main application
โ โ โโโ epub_manager.py # EPUB management
โ โ โโโ tts_panel.py # TTS control panel
โ โ โโโ voice_selector_panel.py # Voice selection
โ โ โโโ actions.py # UI actions
โ โ โโโ panel_titles.py # Panel titles
โ โ โโโ progress.py # Progress handling
โ โ โโโ protocols.py # UI protocols
โ โ โโโ ui_utils.py # UI utilities
โ โ โโโ widgets/ # Reusable components
โ โ โโโ content_widget.py
โ โ โโโ toc_widget.py
โ โ โโโ tts_widget.py
โ โโโ utils/ # Utility functions
โ โ โโโ config.py # Configuration management
โ โ โโโ error_handler.py # Error handling
โ โ โโโ event_bus.py # Event system
โ โ โโโ file_utils.py # File operations
โ โ โโโ logging_config.py # Logging configuration
โ โ โโโ performance_monitor.py # Performance monitoring
โ โ โโโ predictive_preloader.py # Content preloading
โ โ โโโ resource_monitor.py # Resource monitoring
โ โ โโโ security.py # Security utilities
โ โ โโโ system_utils.py # System utilities
โ โ โโโ text_utils.py # Text processing
โ โ โโโ voice_filter_utils.py # Voice filtering
โ โโโ cli.py # Command-line interface
โ โโโ desktop.py # Desktop integration
โ โโโ __init__.py
โ โโโ __main__.py
โโโ LICENSE
โโโ pyproject.toml # Project configuration
โโโ README.md
โโโ requirements.txt # Dependency lock file
๐ Text-to-Speech Features
The TTS system provides:
- Multiple Voices - Support for various languages and voices
- Speed Control - Adjust playback speed (0.5x - 2.0x)
- Volume Control - Fine-tune audio levels
- Chapter Navigation - Skip to previous/next chapters
- Progress Tracking - Visual progress bar with time display
- Background Processing - Non-blocking audio synthesis
Reservoir v5.2: Intelligent Water Level Control
SpeakUB's advanced Reservoir architecture features intelligent Water Level Control for seamless TTS playback:
๐ฏ Water Level Monitoring
- Real-time buffer tracking: Continuously monitors playback buffer water levels
- Multi-tier alert system: Low water โ Deficit โ Drought โ Critical thresholds
- Predictive warnings: Anticipates buffer depletion before interruptions occur
๐ Adaptive Preload Strategies
The system automatically adjusts preloading based on buffer conditions:
- Conservative Mode (Buffer adequate): Minimal resource usage, efficient preloading
- Moderate Mode (Buffer medium): Balanced performance and resource utilization
- Aggressive Mode (Buffer low): Maximum preloading to prevent interruptions
- Emergency Mode (Buffer critical): Synchronous fallback to guarantee continuity
โก CPU-Optimized Design
- Pointer-driven selection: O(1) complexity task selection eliminates CPU waste
- Hardware-aware scaling: Automatic adaptation to system capabilities
- LRU cache management: Prevents resource leaks while optimizing memory usage
๐ Resource Intelligence
- Closed-loop CPU control: Dynamic batch size adjustment based on system pressure
- Network-adaptive buffering: Increases safety margins during unstable connections
- Thermal-aware operation: Prevents overheating through intelligent throttling
๐ Performance Results
- 45% CPU reduction: Through pointer-driven task selection
- 35% memory optimization: LRU cache improvements
- 80% fault recovery: Network instability compensation
- 100% playback continuity: Multi-layer fail-safe mechanisms
TTS Panel Controls
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ TTS: PLAYING (Smooth) | 23% | Vol: 100% | Speed: +30% | Pitch: +0Hz โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ ๏ธ Configuration
SpeakUB now provides a comprehensive configuration management system with:
- YAML-based Configuration: All settings stored in a human-readable YAML file
- Command-line Tools:
config_cli.pytool for managing settings - Dependency Injection: Each component uses its own ConfigManager instance
- Migration Support: Automatic migration from legacy JSON configurations
Configuration Location
Configuration file: ~/.config/speakub/config.yaml
CLI Configuration Tool
SpeakUB includes a powerful CLI tool for configuration management:
# Get a configuration value
python speakub/utils/config_cli.py get tts.preferred_engine
# Set a configuration value
python speakub/utils/config_cli.py set tts.preferred_engine "gtts"
# List all configuration keys (or prefix)
python speakub/utils/config_cli.py list tts
# Export configuration
python speakub/utils/config_cli.py export my_backup.yaml
# Import configuration
python speakub/utils/config_cli.py import my_backup.yaml
# Migrate from legacy JSON configuration
python speakub/utils/config_cli.py migrate
# Show configuration system information
python speakub/utils/config_cli.py info
Automatic Configuration
The reader automatically saves:
- Reading progress for each book
- Last position in each chapter
- TTS settings (volume, speed)
Progress is stored in ~/.speakub_progress.json
Manual Configuration
Environment Variables
You can customize the reader's behavior through environment variables:
Display Settings
# Set default content width (default: 80)
export SPEAKUB_WIDTH=100
# Enable/disable trace logging
export SPEAKUB_TRACE=1
# Set maximum cache size for content rendering
export SPEAKUB_CACHE_SIZE=100
TTS Configuration
# Set default TTS voice
export SPEAKUB_VOICE="en-US-AriaRUS"
# Set default TTS speed (0.5-2.0)
export SPEAKUB_TTS_SPEED=1.2
# Set default TTS volume (0-100)
export SPEAKUB_TTS_VOLUME=80
Performance Tuning
# Set chapter cache size (default: 50)
export SPEAKUB_CHAPTER_CACHE=100
# Enable/disable background processing
export SPEAKUB_BACKGROUND=1
# Set polling frequency for UI updates (milliseconds)
export SPEAKUB_POLL_INTERVAL=100
Configuration File
Create a configuration file at ~/.config/speakub/config.yaml:
# SpeakUB Configuration
language: en
voice_rate: "+20%"
pitch: "default"
volume: "default"
tts_enabled: true
reading_speed: 200
theme: default
font_size: 12
# TTS settings for centralized configuration
tts:
rate: 0
volume: 100
pitch: "+0Hz"
smooth_mode: false
preferred_engine: "edge-tts"
smooth_synthesis_delay: 1.2
# Engine-specific settings
edge-tts:
volume: 1.0
playback_speed: 1.0
smooth_synthesis_delay: 1.2
nanmai:
volume: 1.0
playback_speed: 1.0
smooth_synthesis_delay: 1.0
gtts:
volume: 1.0
playback_speed: 1.5
smooth_synthesis_delay: 1.5
# Hardware-aware cache configuration
cache:
auto_detect_hardware: true
chapter_cache_size: 50
width_cache_size: 1000
hardware_profile: auto
# Network configuration
network:
recovery_timeout_minutes: 30
recovery_check_interval: 10
connectivity_test_host: "8.8.8.8"
connectivity_test_port: 53
connectivity_test_timeout: 5
# Performance monitoring configuration
performance:
enable_monitoring: false
log_slow_operations: true
slow_operation_threshold_ms: 100
memory_usage_tracking: true
cpu_usage_tracking: true
benchmark_enabled: false
benchmark_output_file: "performance_benchmark.json"
# EPUB security configuration
epub_security:
max_file_size_mb: 50
max_uncompressed_ratio: 50
min_compression_ratio: 0.01
max_files_in_zip: 10000
max_path_length: 1000
# Content renderer configuration
content_renderer:
default_content_width: 80
min_content_width: 20
adaptive_cache_ttl: 300
Cache Management
The reader implements intelligent caching to improve performance:
- Chapter Content Cache: Stores parsed chapter content (LRU with 50 entries)
- Width Calculation Cache: Caches display width calculations (LRU with 100 entries)
- Adaptive Renderer Cache: Caches HTML-to-text renderers by width with TTL and statistics
Adaptive Cache Features
The new adaptive cache system provides:
- TTL (Time-To-Live): Automatically expires cached items after 5 minutes to prevent memory leaks
- LRU Eviction: Removes least recently used items when cache is full
- Performance Statistics: Tracks hit rates, cache size, and access patterns
- Memory-Aware Sizing: Automatically adjusts cache size based on system memory
Performance Monitoring
SpeakUB includes built-in performance monitoring to track system health:
# Monitor cache statistics in real-time
from speakub.core.content_renderer import ContentRenderer
renderer = ContentRenderer()
stats = renderer.get_cache_stats()
print(f"Cache hit rate: {stats['hit_rate']:.1%}")
Performance Metrics
- Cache Hit Rate: Percentage of cache requests that hit
- Memory Usage: Current and peak memory consumption
- TTS State Changes: Number of TTS state transitions
- Render Time: Time spent rendering content
To clear all caches, delete the progress file:
rm ~/.speakub_progress.json
๐ฃ๏ธ Chinese Pronunciation Corrections
SpeakUB supports optional pronunciation corrections for Chinese text. This feature allows you to customize how specific Chinese characters or words are pronounced by the TTS system.
Setting Up Corrections
-
Automatic Setup: The first time you run SpeakUB, it will create a corrections file at
~/.config/speakub/corrections.jsonwith instructions and examples. -
Manual Setup: You can also create the file manually:
mkdir -p ~/.config/speakub
touch ~/.config/speakub/corrections.json
Corrections File Format
The corrections file is a JSON object where each key is the original text and the value is the corrected pronunciation:
{
"_comment": "Chinese Pronunciation Corrections Configuration",
"_instructions": "Add your correction rules below in format: 'original': 'corrected'",
"_examples": {
"็้ท": "็ๆ",
"้ท": "ๅธธ"
},
"็้ท": "็ๆ",
"้ท": "ๅธธ",
"้ถ่ก": "yรญnhรกng",
"็ปไบ": "jวyว"
}
How It Works
- Keys starting with
_: These are treated as comments and instructions, not correction rules - Regular keys: These are the actual correction mappings
- Automatic filtering: The system automatically filters out instruction keys when loading corrections
- Optional feature: If the corrections file doesn't exist, SpeakUB works normally without corrections
Common Use Cases
- Polyphonic characters: Characters that can be pronounced differently in different contexts
- Proper nouns: Names, places, or terms that need specific pronunciation
- Technical terms: Specialized vocabulary that needs consistent pronunciation
- Regional variations: Different pronunciation preferences
Examples
{
"่ก": "xรญng", // ่ก่ตฐ (walking)
"้ถ่ก": "yรญnhรกng", // ้ถ่ก (bank)
"้ถ่กๅฎถ": "yรญnhรกngjiฤ", // ้ถ่กๅฎถ (banker)
"้ฟ": "chรกng", // ้ฟๅบฆ (length)
"้ฟๆฑ": "chรกngjiฤng" // ้ฟๆฑ (Yangtze River)
}
Tips
- Use Pinyin with tone marks for best results
- Test corrections with short text first
- The corrections are applied before TTS processing
- You can have multiple corrections for the same character in different contexts
๐ Version History
Version 2025.12.06.195815 (Latest - 2025-12-06)
- โจ New Feature: Comprehensive configuration management system with YAML support
- ๐ง Enhancement: Implemented dependency injection for all configuration handling
- ๐ ๏ธ CLI Tool: Added
config_cli.pyfor configuration management via command line - ๐ Migration: Automatic migration support from JSON to YAML configurations
- ๐ฆ Architecture: Refactored to use ConfigManager instances instead of global functions
- ๐งช Testing: Updated test suite to use dependency injection patterns
- ๐ Documentation: Complete README rewrite with new configuration system guide
Version 1.1.37
- โจ New Feature: Added Chinese pronunciation corrections system
- ๐ง Enhancement: Improved content widget with better text processing
- ๐ Bug Fix: Fixed Flake8 linting issues
- ๐ Documentation: Updated README with corrections usage guide
- ๐๏ธ Build: Updated build system and version management
Version 1.0.0
- ๐ Initial release with full EPUB reading capabilities
- ๐ Text-to-Speech support with Microsoft Edge-TTS
- ๐จ Rich terminal UI with Textual framework
- ๐ Smart table of contents navigation
- ๐พ Automatic progress tracking
- ๐๏ธ Comprehensive TTS controls
๐ API Documentation
Core Classes
EPUBParser
Main class for parsing EPUB files.
from speakub.core.epub_parser import EPUBParser
parser = EPUBParser("book.epub")
chapters = parser.get_chapters()
metadata = parser.get_metadata()
ContentRenderer
Handles HTML to text conversion with caching.
from speakub.core.content_renderer import ContentRenderer
renderer = ContentRenderer()
text = renderer.render_html_to_text(html_content, width=80)
TTSEngine
Abstract base class for TTS engines.
from speakub.tts.engine import TTSEngine
# Get available voices
voices = await engine.get_available_voices()
# Synthesize text
audio_data = await engine.synthesize("Hello world", voice="en-US-AriaRUS")
TTS Providers
EdgeTTSProvider
Microsoft Edge TTS integration.
from speakub.tts.edge_tts_provider import EdgeTTSProvider
provider = EdgeTTSProvider()
await provider.initialize()
voices = await provider.get_voices()
GTTSProvider
Google Text-to-Speech integration.
from speakub.tts.gtts_provider import GTTSProvider
provider = GTTSProvider()
audio = await provider.synthesize("Hello", lang="en")
Configuration
ConfigManager
Centralized configuration management.
from speakub.utils.config import ConfigManager
config = ConfigManager()
tts_speed = config.get("tts.speed", 1.0)
config.set_override("tts.speed", 1.2)
Event System
EventBus
Application-wide event handling.
from speakub.utils.event_bus import EventBus
bus = EventBus()
bus.subscribe("tts.state_changed", callback_function)
bus.publish("tts.state_changed", {"state": "playing"})
Performance Monitoring
PerformanceMonitor
System resource monitoring.
from speakub.utils.performance_monitor import PerformanceMonitor
monitor = PerformanceMonitor()
stats = monitor.get_system_stats()
monitor.log_performance_metrics()
๐ง Development
Setup Development Environment
git clone https://github.com/eyes1971/SpeakUB.git
cd SpeakUB
pip install -e .[dev]
pre-commit install
Development Tools
The tools/ directory contains helpful scripts for development and debugging:
# Check available TTS voices
python tools/check_voices.py
# Debug voice data structures
python tools/debug_voices.py
# Test TTS provider functionality
python tools/simple_test.py
# Interactive voice selector demo
python tools/voice_selector_demo.py
# Verify UI layout changes
python tools/simple_layout_check.py
Run Tests
pytest
Code Formatting
black speakub/
isort speakub/
flake8 speakub/
Type Checking
mypy speakub/
๐ค Contributing
Contributions are welcome! Please:
- Fork the repository at https://github.com/eyes1971/SpeakUB
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ง Contact
- Author: Sam Weng
- Email: eyes1971@gmail.com
- GitHub: https://github.com/eyes1971/SpeakUB
๐ Acknowledgments
- Rich - For the beautiful terminal UI framework
- Textual - For the modern TUI components
- BeautifulSoup - For robust HTML parsing
- Edge-TTS - For high-quality text-to-speech
- html2text - For HTML to text conversion
๐ Known Issues
- Image display requires
fabulousand may not work in all terminals - TTS seeking is not supported with the pygame audio backend
- Very large EPUB files may consume significant memory
๐ Similar Projects
Happy Reading! ๐โจ
For more information, visit our documentation or report issues.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file speakub-2025.12.16.124627.tar.gz.
File metadata
- Download URL: speakub-2025.12.16.124627.tar.gz
- Upload date:
- Size: 543.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
951f45bc1cf8ff20f3fef980e6b41b02771adc127445fd70f2f935a4c92d1851
|
|
| MD5 |
1eacf0f556d5d483a8e98a01e9528545
|
|
| BLAKE2b-256 |
a5819b0fdd59be7437aa907affc6ac80f7ea9d08e5496840a081c6068ef409db
|