import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import pyttsx3

def speak_text():
    text = text_area.get("1.0", tk.END).strip()
    if not text:
        messagebox.showwarning("Input Error", "Please enter some text to convert to speech.")
        return
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    selected_voice = voice_var.get()
    if selected_voice == "Male":
        engine.setProperty('voice', voices[0].id)
    else:
        engine.setProperty('voice', voices[1].id)
        
    engine.setProperty('rate', rate_var.get())
    engine.setProperty('volume', volume_var.get() / 100)
    engine.say(text)
    engine.runAndWait()
    engine.stop()
    
    
def save_audio():
    text = text_area.get("1.0", tk.END).strip()
    if not text:
        messagebox.showwarning("Input Error", "Please enter some text to convert to speech.")
        return
    file_path = filedialog.asksaveasfilename(defaultextension=".mp3",filetypes=[("MP3 Files", "*.mp3"), ("WAV Files", "*.wav")])
    
    if file_path:
        engine = pyttsx3.init()
        voices = engine.getProperty('voices')
        selected_voice = voice_var.get()
        if selected_voice == "Male":
            engine.setProperty('voice', voices[0].id)
        else:
            engine.setProperty('voice', voices[1].id)
            
        engine.setProperty('rate', rate_var.get())
        engine.setProperty('volume', volume_var.get() / 100)
        engine.save_to_file(text, file_path)
        engine.runAndWait()
        engine.stop()
        messagebox.showinfo("Success", f"Audio saved successfully at {file_path}")



# ---------------- Tkinter UI ----------------

root = tk.Tk()
root.title("Text to Speech Converter")
root.geometry("600x500")
root.resizable(False, False)

# Title
title_label = ttk.Label(root, text="Text-to-Speech Converter", font=("Arial", 20, "bold"))
title_label.pack(pady=10)

# Textarea
text_area = tk.Text(root, height=12, width=60, font=("Arial", 12))
text_area.pack(pady=10)
# Frame for options
options_frame = ttk.Frame(root)
options_frame.pack(pady=10)

# Voice Selector
ttk.Label(options_frame, text="Voice:").grid(row=0, column=0, pady=5)
voice_var = tk.StringVar(value="Male")
voice_menu = ttk.Combobox(options_frame, textvariable=voice_var, values=["Male","Female"], state="readonly")
voice_menu.grid(row=0, column=1, padx=5)
# Rate Slider

ttk.Label(options_frame, text="Rate:").grid(row=1, column=0, padx=5, pady=5)
rate_var = tk.IntVar(value=150)
rate_slider = ttk.Scale(options_frame, from_=50, to=300, orient="horizontal", variable=rate_var)
rate_slider.grid(row=1, column=1, padx=5)

# Volume Slider
ttk.Label(options_frame, text="Volume:").grid(row=2, column=0, padx=5, pady=5)
volume_var = tk.IntVar(value=100)
volume_slider = ttk.Scale(options_frame, from_=10, to=100, orient="horizontal", variable=volume_var)
volume_slider.grid(row=2, column=1, padx=5)

# Buttons
button_frame = ttk.Frame(root)
button_frame.pack(pady=15)

speak_btn = ttk.Button(button_frame, text="Speak", command=speak_text)
speak_btn.grid(row=0, column=0, padx=10)

save_btn = ttk.Button(button_frame , text="Save as audio",command=save_audio)
save_btn.grid(row=0, column=1, padx=10)

exit_btn = ttk.Button(button_frame, text="❌ Exit", command=root.destroy)
exit_btn.grid(row=0, column=2, padx=10)

root.mainloop()