coqui2 / play auto cuda.py
Adoetz's picture
Upload 833 files
17ed7d8 verified
raw
history blame
1.21 kB
from TTS.api import TTS
import simpleaudio as sa
import torch
# Check if GPU is available
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
# Initialize the TTS object
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=True)
tts.to(device) # Use GPU if available
# Define the output file path
output_audio_path = "output_audio.wav"
# Continuous loop for text input
print("Enter text to generate speech. Type 'exit' to quit.")
while True:
# Get user input
text = input("Enter text: ")
# Exit the loop if the user types 'exit'
if text.lower() == "exit":
print("Exiting...")
break
# Generate speech
tts.tts_to_file(
text=text,
file_path=output_audio_path,
speaker="Ana Florence", # Use a valid speaker ID
language="en" # Specify the language
)
print(f"Speech generated and saved to {output_audio_path}")
# Play the audio in the background using simpleaudio
wave_obj = sa.WaveObject.from_wave_file(output_audio_path)
play_obj = wave_obj.play()
play_obj.wait_done() # Wait until the audio finishes playing