-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_to_speech.py
More file actions
46 lines (38 loc) · 1.33 KB
/
text_to_speech.py
File metadata and controls
46 lines (38 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import time
from io import BytesIO
import pygame
from elevenlabs import ElevenLabs
from elevenlabs import VoiceSettings
def text_to_speech_elevenlabs(text):
"""
Convert text to speech using ElevenLabs API and play the audio.
"""
pygame.mixer.init()
try:
elevenclient = ElevenLabs(api_key=os.getenv("ELEVEN_API_KEY"))
response = elevenclient.text_to_speech.convert(
voice_id="MF3mGyEYCl7XYWbV9V6O",
output_format="mp3_22050_32",
text=text,
model_id="eleven_turbo_v2",
voice_settings=VoiceSettings(stability=0.0, similarity_boost=1.0, style=0.0, use_speaker_boost=True),
)
# Write the audio data to a BytesIO object
audio_data = BytesIO()
for chunk in response:
if chunk:
audio_data.write(chunk)
if audio_data.getbuffer().nbytes == 0:
raise ValueError("Audio data is empty.")
# Play the audio using pygame
audio_data.seek(0)
pygame.mixer.music.load(audio_data)
pygame.mixer.music.play()
# Wait for the audio to finish playing
while pygame.mixer.music.get_busy():
time.sleep(0.1)
return "Audio played successfully"
except Exception as e:
print(f"An error occurred: {str(e)}")
raise