IT News

음성을 연구 해보자, 파이썬 코

posttstory 2023. 3. 24.
728x90

코딩을 공부하면서

이런저런 것들을 시도 해보는대 오늘은 음성을 만들어 봅니다.

계속해서 에러가 군데군데 나와서 수정 작업을 해보면서 만들기 때문에 완전 하진 않아요

 

다양한 방법으로 시도를 해보는대 원하는 만큼 나오지는 않는다.

자꾸만 더 나은 것을 찾아보려고 하는대 쉽지가 않네 

비 전공자라 접근하기가 어려웠지만 차근차근 해보니 나름 해결의 길도 보이는 거 같기도 하다.

import tkinter as tk
import pyttsx3
import os

# Initialize the text-to-speech engine
engine = pyttsx3.init()

# Get a list of available voices
voices = engine.getProperty("voices")

# Define a list of speed values
speed_values = ["50", "100", "150", "200", "250", "300"]

# Create the GUI window
root = tk.Tk()

# Create a label widget for the text
text_label = tk.Label(root, text="Type here:")
text_label.pack()

# Create a text entry widget with a scrollbar
text_scrollbar = tk.Scrollbar(root)
text_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

text_entry = tk.Text(root, wrap=tk.WORD, yscrollcommand=text_scrollbar.set, height=10)
text_entry.pack()

# Set a limit of 5000 characters using the bind method
def validate_text(event):
    text = text_entry.get("1.0", tk.END) # Get the text from the text entry widget
    if len(text) > 5000: # If the text is too long, remove the last character
        text_entry.delete("end-2c") # Delete the last character
        return "break" # Prevent the character from being entered

text_entry.bind("<Key>", validate_text)

# Configure the scrollbar to work with the text entry widget
text_scrollbar.config(command=text_entry.yview)

# Define a function to read the text
def read_text():
    # Get the selected voice and speed from the option menus
    voice_index = voice_menu.current()
    speed_index = speed_menu.current()
    voice = voices[voice_index].id
    speed = int(speed_values[speed_index])
   
    # Set the voice and speed of the engine
    engine.setProperty("voice", voice)
    engine.setProperty("rate", speed)
   
    text = text_entry.get("1.0", tk.END) # Get the text from the text entry widget
    engine.say(text) # Use the text-to-speech engine to read the text
    engine.runAndWait() # Wait for the speech to finish

# Create a button widget to trigger the reading
read_button = tk.Button(root, text="Read", command=read_text)
read_button.pack()

# Define a function to save the text to a file
def save_text():
    text = text_entry.get("1.0", tk.END) # Get the text from the text entry widget
    folder = "saved_text" # Define the folder name
    if not os.path.exists(folder): # Create the folder if it doesn't exist
        os.makedirs(folder)
    file_name = "text.txt" # Define the file name
    file_path = os.path.join(folder, file_name) # Combine the folder and file paths
    with open(file_path, "w") as f: # Write the text to the file
        f.write(text)
    print("Text saved successfully.")

# Create a button widget to trigger the saving
save_button = tk.Button(root, text="Save", command=save_text)
save_button.pack()

# Create an option menu for the voices
voice_label = tk.Label(root, text="Select a voice:")
voice_label.pack()

voice_menu = tk.OptionMenu(root, *voices)
voice_menu.pack()

# Create an option menu for the speeds
speed_label = tk.Label(root, text="Select a speed:")
speed_label.pack()

speed_menu = tk.OptionMenu(root, *speed_values)
speed_menu.pack()

# Start the main event loop
root.mainloop()

 

728x90

댓글

💲 추천 글