Untitled

 avatar
unknown
python
a year ago
1.3 kB
2
Indexable
import pyttsx3

def text_to_speech(text, voice_index=0, rate=150, volume=1.0):
    """
    Convert text to speech with customizable voice, rate, and volume.
    
    :param text: The text to convert to speech
    :param voice_index: Index of the voice to use (default 0)
    :param rate: Speaking rate (default 150)
    :param volume: Volume of the speech (0.0 to 1.0, default 1.0)
    """
    engine = pyttsx3.init()
    
    # Get available voices
    voices = engine.getProperty('voices')
    
    # Set the voice
    if voice_index < len(voices):
        engine.setProperty('voice', voices[voice_index].id)
    else:
        print(f"Voice index {voice_index} not available. Using default voice.")
    
    # Set the rate
    engine.setProperty('rate', rate)
    
    # Set the volume
    engine.setProperty('volume', volume)
    
    # Say the text
    engine.say(text)
    engine.runAndWait()

# Example usage
text = "Hello, this is a test of the Text-to-Speech engine."

# List available voices
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for i, voice in enumerate(voices):
    print(f"Voice {i}: {voice.name}")

# Use the function
text_to_speech(text, voice_index=1, rate=150, volume=0.8)
Editor is loading...
Leave a Comment