Skip to content

Commit 839d979

Browse files
committed
Add advanced examples for text processing, batch processing, ML integration, and real-time WebRTC TTS
- Introduced advanced text processing capabilities with emotional expressions, pauses, and voice effects in `advanced_text_processing.py`. - Implemented batch processing and queue management features in `batch_processing_queue.py`, including priority-based tasks and error handling. - Added machine learning integration for content analysis and voice selection in `ml_integration.py`. - Developed real-time TTS with WebRTC integration for live applications in `realtime_webrtc_integration.py`. These examples enhance the functionality and usability of the Edge-TTS library for various applications.
1 parent 8db06b7 commit 839d979

10 files changed

Lines changed: 3892 additions & 0 deletions

docs/ENHANCED_FEATURES.md

Lines changed: 652 additions & 0 deletions
Large diffs are not rendered by default.

docs/QUICK_REFERENCE.md

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# Enhanced Edge-TTS Quick Reference
2+
3+
## 🚀 Quick Start
4+
5+
```python
6+
import edge_tts
7+
8+
# Simple AI-powered TTS
9+
result = await edge_tts.speak_intelligently("Hello world!", "output.mp3")
10+
11+
# Advanced control
12+
enhanced = edge_tts.EnhancedCommunicate("Text with [pause:medium] effects!")
13+
await enhanced.save("output.mp3")
14+
```
15+
16+
## 🎭 Text Effects
17+
18+
### Pause Effects
19+
```python
20+
"[pause:short]" # 0.5s pause
21+
"[pause:medium]" # 1s pause
22+
"[pause:long]" # 2s pause
23+
"[pause:extra_long]" # 3s pause
24+
```
25+
26+
### Emotion Effects
27+
```python
28+
"[emotion:happy]" # Happy voice
29+
"[emotion:sad]" # Sad voice
30+
"[emotion:excited]" # Excited voice
31+
"[emotion:calm]" # Calm voice
32+
"[emotion:angry]" # Angry voice
33+
"[emotion:surprised]" # Surprised voice
34+
"[emotion:neutral]" # Neutral voice
35+
```
36+
37+
### Sound Effects
38+
```python
39+
"[laugh]" # Laughter
40+
"[sigh]" # Sigh
41+
"[whisper]" # Whisper
42+
"[shout]" # Shout
43+
```
44+
45+
### Voice Parameters
46+
```python
47+
"[speed:+50%]" # 50% faster
48+
"[speed:-30%]" # 30% slower
49+
"[pitch:+100Hz]" # Higher pitch
50+
"[pitch:-50Hz]" # Lower pitch
51+
"[volume:+20%]" # Louder
52+
"[volume:-15%]" # Quieter
53+
```
54+
55+
## 📊 Batch Processing
56+
57+
```python
58+
# Simple batch processing
59+
texts = ["Text 1", "Text 2", "Text 3"]
60+
results = await edge_tts.batch_speak(texts, output_prefix="batch")
61+
62+
# Advanced batch processing
63+
processor = edge_tts.BatchProcessor(max_concurrent=5)
64+
results = await processor.process_batch(texts, voices, "output")
65+
```
66+
67+
## 🧠 Content Analysis
68+
69+
```python
70+
# Analyze content
71+
analyzer = edge_tts.ContentAnalyzer()
72+
analysis = analyzer.analyze_content("Breaking news!")
73+
74+
print(f"Type: {analysis.content_type.value}") # news
75+
print(f"Emotion: {analysis.emotion.value}") # excited
76+
print(f"Voice: {analysis.recommended_voice}") # en-US-AriaNeural
77+
```
78+
79+
## 🎵 Voice Profiles
80+
81+
```python
82+
# Access voice characteristics
83+
profile = edge_tts.VoiceProfile(
84+
voice_name='Aria',
85+
gender='Female',
86+
characteristics={'warm': 0.8, 'professional': 0.9}
87+
)
88+
```
89+
90+
## ⚡ Real-time Features
91+
92+
```python
93+
# WebRTC integration
94+
class RealtimeTTS:
95+
async def speak_live(self, text: str):
96+
enhanced = edge_tts.EnhancedCommunicate(text)
97+
# Stream audio data
98+
return audio_data
99+
```
100+
101+
## 🎯 Common Use Cases
102+
103+
### Podcast Production
104+
```python
105+
script = "Welcome [pause:medium] to our [emotion:excited] show!"
106+
result = await edge_tts.speak_intelligently(script, "podcast.mp3")
107+
```
108+
109+
### Educational Content
110+
```python
111+
lesson = "Today we'll learn [speed:-20%] about photosynthesis [pause:short]"
112+
enhanced = edge_tts.EnhancedCommunicate(lesson)
113+
await enhanced.save("lesson.mp3")
114+
```
115+
116+
### News Broadcasting
117+
```python
118+
news = "Breaking news [pause:short] [emotion:surprised] [volume:+10%]!"
119+
result = await edge_tts.speak_intelligently(news, "news.mp3")
120+
```
121+
122+
### Interactive Dialogue
123+
```python
124+
dialogue = """
125+
Character A: Hello [emotion:happy]! How are you?
126+
Character B: I'm great [laugh]! Thanks for asking.
127+
"""
128+
enhanced = edge_tts.EnhancedCommunicate(dialogue)
129+
await enhanced.save("dialogue.mp3")
130+
```
131+
132+
## 🔧 Advanced Features
133+
134+
### Custom Voice Parameters
135+
```python
136+
enhanced = edge_tts.EnhancedCommunicate("Custom text")
137+
params = enhanced.get_voice_parameters()
138+
print(f"Rate: {params['rate']}")
139+
print(f"Volume: {params['volume']}")
140+
print(f"Pitch: {params['pitch']}")
141+
```
142+
143+
### Effect Processing
144+
```python
145+
enhanced = edge_tts.EnhancedCommunicate("Text with [pause:medium] effects!")
146+
print(f"Effects: {len(enhanced.effects)}")
147+
for effect in enhanced.effects:
148+
print(f"- {effect.effect_type}: {effect.parameters}")
149+
```
150+
151+
### Error Handling
152+
```python
153+
try:
154+
result = await edge_tts.speak_intelligently(text, "output.mp3")
155+
except Exception as e:
156+
# Fallback to basic TTS
157+
communicate = edge_tts.Communicate(text, "en-US-AriaNeural")
158+
await communicate.save("output.mp3")
159+
```
160+
161+
## 📈 Performance Tips
162+
163+
### Memory Management
164+
```python
165+
# Process in chunks for large datasets
166+
chunk_size = 10
167+
for i in range(0, len(texts), chunk_size):
168+
chunk = texts[i:i + chunk_size]
169+
results = await edge_tts.batch_speak(chunk, f"chunk_{i//chunk_size}")
170+
```
171+
172+
### Caching
173+
```python
174+
# Cache repeated content
175+
cache_key = hashlib.md5(text.encode()).hexdigest()
176+
if os.path.exists(f"cache/{cache_key}.mp3"):
177+
return f"cache/{cache_key}.mp3"
178+
```
179+
180+
## 🎉 Complete Example
181+
182+
```python
183+
import edge_tts
184+
import asyncio
185+
186+
async def main():
187+
# Professional podcast with effects
188+
script = """
189+
Welcome to Tech Talk [pause:medium] with your host [emotion:excited] Sarah!
190+
Today we're discussing [emotion:surprised] artificial intelligence [pause:short]
191+
and its impact on [emotion:calm] our daily lives.
192+
"""
193+
194+
# Generate with AI voice selection
195+
result = await edge_tts.speak_intelligently(script, "podcast.mp3")
196+
197+
print(f"Voice used: {result['voice_used']}")
198+
print(f"Content type: {result['analysis'].content_type.value}")
199+
print(f"Emotion: {result['analysis'].emotion.value}")
200+
print(f"Parameters: {result['parameters']}")
201+
202+
if __name__ == "__main__":
203+
asyncio.run(main())
204+
```
205+
206+
## 📚 API Reference
207+
208+
### Core Classes
209+
- `EnhancedCommunicate`: Main enhanced TTS class
210+
- `ContentAnalyzer`: ML-powered content analysis
211+
- `AdvancedTextProcessor`: Text effects processing
212+
- `BatchProcessor`: Enterprise batch processing
213+
- `VoiceProfile`: Voice characteristics
214+
215+
### Convenience Functions
216+
- `speak_intelligently()`: Simple AI-powered TTS
217+
- `batch_speak()`: Batch processing with AI
218+
219+
### Enums
220+
- `ContentType`: News, Story, Technical, Educational, etc.
221+
- `EmotionType`: Happy, Sad, Excited, Calm, etc.
222+
- `PauseType`: Short, Medium, Long, Extra Long
223+
224+
## 🚨 Troubleshooting
225+
226+
### Common Issues
227+
1. **403 Error**: Update edge-tts to latest version
228+
2. **ML Dependencies**: Install scikit-learn, transformers
229+
3. **Memory Issues**: Use chunked processing
230+
4. **Voice Selection**: Check content analysis results
231+
232+
### Debug Mode
233+
```python
234+
import logging
235+
logging.basicConfig(level=logging.DEBUG)
236+
237+
# Enhanced TTS with debug info
238+
enhanced = edge_tts.EnhancedCommunicate("Debug text")
239+
print(f"Analysis: {enhanced.get_analysis()}")
240+
print(f"Effects: {enhanced.get_effects()}")
241+
print(f"Parameters: {enhanced.get_voice_parameters()}")
242+
```
243+
244+
## 🎯 Best Practices
245+
246+
1. **Use AI Voice Selection**: Let the library choose optimal voices
247+
2. **Combine Effects**: Mix multiple effects for rich audio
248+
3. **Batch Processing**: Use for large-scale applications
249+
4. **Error Handling**: Always implement fallbacks
250+
5. **Performance**: Use caching and chunked processing
251+
6. **Testing**: Test with different content types and emotions
252+
253+
## 📞 Support
254+
255+
- **Documentation**: [ENHANCED_FEATURES.md](ENHANCED_FEATURES.md)
256+
- **Examples**: [examples/](examples/)
257+
- **Issues**: GitHub Issues
258+
- **Community**: GitHub Discussions
259+
260+
---
261+
262+
**Happy TTS Generation! 🎉**

0 commit comments

Comments
 (0)