File size: 1,103 Bytes
17ed7d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from TTS.api import TTS
import simpleaudio as sa
import os

# Initialize the TTS object
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=True)
tts.to("cpu")  # Use CPU if GPU is not 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