File size: 1,243 Bytes
776566f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import subprocess

def speak_text_festival(text):
    """
    Uses Festival to speak the given text.
    """
    command = f'(SayText "{text}")'
    try:
        # Popen is used to run the Festival command.
        # We pass the command to Festival's standard input.
        process = subprocess.Popen(['festival', '--pipe'], 
                                   stdin=subprocess.PIPE, 
                                   stdout=subprocess.PIPE, 
                                   stderr=subprocess.PIPE,
                                   text=True) # text=True for string input/output
        stdout, stderr = process.communicate(input=command)
        
        if process.returncode != 0:
            print(f"Error speaking text with Festival: {stderr}")
        # else:
        #     print(f"Festival output: {stdout}") # Uncomment to see Festival's stdout
            
    except FileNotFoundError:
        print("Error: Festival executable not found. Make sure Festival is installed and in your PATH.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

# Example usage:
speak_text_festival("Good morning, welcome to Festival.")
speak_text_festival("This is an example of Python interacting with Festival.")