How to do text to speech with python on a Toshiba laptop and Windows 7?

Viewed 1131

I am trying to find a way to create text to speech in python (I am on windows 7). I am using pyinstaller to compile this program. I have tried a large number of approaches, including using Google's unofficial text to speech program accessed through the urllib2 module. This ends up creating an mp3 file. For details on the code, much of this code is from http://glowingpython.blogspot.com/2012/11/text-to-speech-with-correct-intonation.html. I have then needed to play the mp3 file that this generates. I have used mplayer, ffmpeg, mp3play, audiere, pydub, and pygame all with the same results: no sound was played, yet no exceptions were raised. I have even used the same pygame code on a raspberry pi and successfully played an mp3 file. I have also tried converting it to a wav file, which has worked fine, only when I try to play it with pygame or winsound, the same thing happens. No sound, no exceptions. My current code uses winsound, playing a wav file that I can successfully play in the windows media player (I can even open it in windows media player from python, using os.startfile()). Here it is:

winsound.PlaySound("file.wav", winsound.SND_FILENAME)    #the wav file is in the same directory as the program

I am also trying to use pygame mixer an music modules. For example:

init()                            #this is pygame.init(), I only imported init and the mixer module
pygame.mixer.init()               #initializes pygame.mixer
pygame.mixer.music.load(filename) #loads it in music
pygame.mixer.music.play()         #plays it in music
time.sleep(20)

I have even played sounds from python successfully with the winsound and win32api Beep() functions. However, this obviously cannot play an mp3 or wav file. I have also tried a completely different text to speech engine, which plays the sound without an mp3 file in the mix, using pyttsx:

import pyttsx

engine = pyttsx.init()

def tts(mytext):
    engine.say(mytext)
    engine.runAndWait()

This has also failed to create sound, or raise an exception. Because of this pattern, I have a feeling that this has something to do with the system, but it doesn't seem like it is something obvious.

Because this almost definitely has something to do with the hardware (pygame.mixer has worked this way on different hardware, and I am sure it usually works on windows) it may be important to know I am using a Toshiba laptop. Also, I am using python 2.7.

Ideally, I would like to do this with pygame, because I have the most experience using it and there are some sound editing features I would like to access in pygame if at all possible.

I also tried using 64 bit python (I was using 32 bit python on 64 bit windows 7). It still failed to work.

I also tried playing an mp3 file inside a Ubuntu virtual box environment, but on the same device. It still didn't work. This isn't particularly surprising because virtualbox uses a lot of the resources (like screen and wifi) from the host operating system, hence it wouldn't necessarily play sounds any differently. Any way around this would be helpful. Some sounds play fine, just not specifically mp3 or wav files in python, so there is probably a solution.

3 Answers
Related