File size: 2,257 Bytes
07c72d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import streamlit as st
import pyttsx3

# Initialize TTS engine
engine = pyttsx3.init()

# Configure available voices
voices = engine.getProperty('voices')

# Streamlit App Title
st.title("Text-to-Speech Application")
st.markdown("""
A text-to-speech application supporting multiple languages and voices.  
Customize the speech synthesis settings as per your preferences!
""")

# User Input for Text
text = st.text_area("Enter the text you want to convert to speech:", placeholder="Type something here...")

# Language and Voice Selection
language = st.selectbox(
    "Select a Language:",
    options=["English (US)", "English (UK)", "Hindi", "French", "Spanish"]
)

voice_gender = st.radio(
    "Select Voice Gender:",
    options=["Male", "Female"]
)

# Customize Voice Rate and Volume
rate = st.slider("Adjust Speech Rate:", min_value=100, max_value=300, value=200, step=10)
volume = st.slider("Adjust Volume:", min_value=0.0, max_value=1.0, value=1.0, step=0.1)

# Generate Speech
if st.button("Convert to Speech"):
    if text.strip():
        # Set language and voice properties
        if language == "English (US)":
            selected_voice = voices[0]  # Adjust based on your system's available voices
        elif language == "English (UK)":
            selected_voice = voices[1]
        elif language == "Hindi":
            selected_voice = voices[2]  # Requires Hindi TTS setup in OS
        elif language == "French":
            selected_voice = voices[3]
        elif language == "Spanish":
            selected_voice = voices[4]
        else:
            selected_voice = voices[0]

        # Set voice
        engine.setProperty('voice', selected_voice.id)

        # Set gender
        if voice_gender == "Female":
            for v in voices:
                if "female" in v.name.lower():
                    engine.setProperty('voice', v.id)
                    break

        # Set rate and volume
        engine.setProperty('rate', rate)
        engine.setProperty('volume', volume)

        # Convert to speech
        engine.save_to_file(text, 'output.mp3')
        engine.runAndWait()
        st.audio('output.mp3', format="audio/mp3", start_time=0)
    else:
        st.warning("Please enter some text to convert to speech!")