Update audio_processor.py
Browse files- audio_processor.py +7 -3
audio_processor.py
CHANGED
@@ -1,24 +1,28 @@
|
|
1 |
from gtts import gTTS
|
2 |
import io
|
3 |
|
4 |
-
# Function to convert translated text to speech with debugging
|
5 |
def text_to_speech(text, target_lang):
|
6 |
try:
|
7 |
if not text or not text.strip():
|
8 |
print("Error: Empty or invalid text for speech synthesis")
|
9 |
return None
|
|
|
|
|
|
|
10 |
lang_map = {"en": "en", "fr": "fr", "es": "es", "de": "de", "hi": "hi", "zh": "zh-cn", "ar": "ar", "ru": "ru", "ja": "ja"}
|
11 |
lang_code = lang_map.get(target_lang, "en")
|
12 |
if lang_code not in lang_map.values():
|
13 |
print(f"Error: Unsupported language code {lang_code} for speech synthesis")
|
14 |
return None
|
15 |
-
tts = gTTS(text=
|
16 |
audio_buffer = io.BytesIO()
|
17 |
tts.write_to_fp(audio_buffer)
|
18 |
audio_buffer.seek(0)
|
19 |
if audio_buffer.getbuffer().nbytes > 0:
|
|
|
20 |
return audio_buffer
|
21 |
-
print("Error: Audio buffer is empty")
|
22 |
return None
|
23 |
except Exception as e:
|
24 |
print(f"Audio error: {e}")
|
|
|
1 |
from gtts import gTTS
|
2 |
import io
|
3 |
|
4 |
+
# Function to convert translated text to speech with enhanced debugging
|
5 |
def text_to_speech(text, target_lang):
|
6 |
try:
|
7 |
if not text or not text.strip():
|
8 |
print("Error: Empty or invalid text for speech synthesis")
|
9 |
return None
|
10 |
+
# Clean and trim text to 200 characters
|
11 |
+
cleaned_text = text.strip()[:200]
|
12 |
+
print(f"Attempting to synthesize: '{cleaned_text}' for lang: {target_lang}")
|
13 |
lang_map = {"en": "en", "fr": "fr", "es": "es", "de": "de", "hi": "hi", "zh": "zh-cn", "ar": "ar", "ru": "ru", "ja": "ja"}
|
14 |
lang_code = lang_map.get(target_lang, "en")
|
15 |
if lang_code not in lang_map.values():
|
16 |
print(f"Error: Unsupported language code {lang_code} for speech synthesis")
|
17 |
return None
|
18 |
+
tts = gTTS(text=cleaned_text, lang=lang_code, slow=False)
|
19 |
audio_buffer = io.BytesIO()
|
20 |
tts.write_to_fp(audio_buffer)
|
21 |
audio_buffer.seek(0)
|
22 |
if audio_buffer.getbuffer().nbytes > 0:
|
23 |
+
print(f"Audio generated successfully, size: {audio_buffer.getbuffer().nbytes} bytes")
|
24 |
return audio_buffer
|
25 |
+
print("Error: Audio buffer is empty after synthesis")
|
26 |
return None
|
27 |
except Exception as e:
|
28 |
print(f"Audio error: {e}")
|