# audio_processing.py | |
import asyncio | |
import os | |
import tempfile | |
import edge_tts | |
# Lấy API key từ biến môi trường | |
EDGE_TTS_API_KEY = os.environ.get("EDGE_TTS_API_KEY", None) # Không cần API key cho edge-tts | |
def text_to_speech(text, voice, language): | |
""" | |
Chuyển đổi văn bản thành giọng nói bằng edge-tts. | |
""" | |
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
tts = edge_tts.Communicate(text, voice) | |
tts.save(output_file.name).get() | |
return output_file.name | |
async def async_text_to_speech(text, voice, language): | |
""" | |
Chuyển đổi văn bản thành giọng nói (bất đồng bộ). | |
""" | |
loop = asyncio.get_event_loop() | |
return await loop.run_in_executor(None, text_to_speech, text, voice, language) | |
if __name__ == "__main__": | |
# Ví dụ sử dụng | |
text = "Chào mừng bạn đến với công nghệ AI!" | |
voice = "en-US-JennyNeural" # Vôices available can be found at https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/language-support | |
language = "en-US" | |
output_file = async_text_to_speech(text, voice, language) | |
print(f"Tệp âm thanh đã được lưu tại: {output_file}") |