Foxes
Foxes

Reputation: 1237

How to make my Text-To-Speech program fully portable and usable on every Operating System?

I have a Text-To-Speech program that asks for a user input and then outputs that input as speech. It will then ask if the user wants to convert another input into speech or whether they want to exit the program. At the moment the program will only work on Windows as it is dependent on Windows Media Player to play the text-to-speech file. How could I make it so it plays the file from within Python, and, by extension, works on every operating system? If there are any other parts within the code that would prevent it from running on other Operating Systems, please tell me what they are and how I could change them as well. Thanks!

try:

    import os
    import time
    import sys
    import getpass
    import pip
    import subprocess
    from contextlib import contextmanager


    my_file = "Text To Speech.mp3"
    wmp = "C:\Program Files (x86)\Windows Media Player\wmplayer.exe"
    media_file = os.path.abspath(os.path.realpath(my_file))
    username = getpass.getuser()


    @contextmanager
    def suppress_output():

        with open(os.devnull, "w") as devnull:
            old_stdout = sys.stdout
            sys.stdout = devnull
            try:  
                yield
            finally:
                sys.stdout = old_stdout


    def check_and_remove_file():

        if os.path.isfile(my_file):
            os.remove(my_file)


    def input_for_tts(message):

        tts = gTTS(text = input(message))
        tts.save('Text To Speech.mp3')
        subprocess.Popen([wmp, media_file])
        audio = MP3(my_file)
        audio_length = audio.info.length
        time.sleep((audio_length) + 2) # Waits for the audio to finish playing before killing it off.
        os.system('TASKKILL /F /IM wmplayer.exe')
        time.sleep(0.5) # Waits for Windows Media Player to fully close before carrying on.


    with suppress_output():

        pkgs = ['mutagen', 'gTTS']
        for package in pkgs:
            if package not in pip.get_installed_distributions():
                pip.main(['install', package])


    from gtts import gTTS
    from mutagen.mp3 import MP3


    check_and_remove_file()


    input_for_tts("Hello there " + username + """. This program is
used to output the user's input as speech.
Please input something for the program to say: """)


    while True:

        answer = input("""
Do you want to repeat? (Y/N) """).strip().lower()
        if answer in ["yes", "y"]:
            input_for_tts("""
Please input something for the program to say: """)
        elif answer in ["no", "n"]:
            check_and_remove_file()
            sys.exit()
        else:
            print("""
Sorry, I didn't understand that. Please try again with either Y or N.""")


except KeyboardInterrupt:

    check_and_remove_file()
    print("""
Goodbye!""")
    sys.exit()

Upvotes: 0

Views: 353

Answers (1)

Stijn Goethals
Stijn Goethals

Reputation: 72

Instead of using Windows Media Player you can use an audio playing package. An good package that can do this is PyMedia.

Upvotes: 1

Related Questions