Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 239
trevorKirkby Avatar asked Oct 21 '22 18:10

trevorKirkby


2 Answers

This error says that it can't find a executable of MPlayer since you just use a mplayer wrapper.

File ".\program.py", line 1681, in playsound
        player = mplayer.Player()
      File "C:\Users\Student\Documents\notes\mplayer.py", line 109, in __init__
        self.spawn()
      File "C:\Users\Student\Documents\notes\mplayer.py", line 319, in spawn
        close_fds=(not subprocess.mswindows))
      File "C:\Python27\lib\subprocess.py", line 711, in __init__
        errread, errwrite)
      File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
        startupinfo)
    WindowsError: [Error 2] The system cannot find the file specified

You can get a MPlayer binary from here: http://www.mplayerhq.hu/design7/dload.html

Most other python programs use ffmpeg and writing some wrapper scripts to play mp3 files.

Maybe you can get inspired by music-player

Also there is a page at the official Python site where you could find some libraries. https://wiki.python.org/moin/PythonInMusic But be ware, most of them are outdated.

The problem with pygame is, that you need to check if the music is playing and hold your program open as long as music is playing.

This could be done like this:

FRAMERATE = 30
clock = pygame.time.Clock()
pygame.mixer.music.load(soundfile)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
    clock.tick(FRAMERATE)

Edit: and to your bounty, there is not a really simple solution if you won't use pygame and the included sdl library. it's better to swap out py2exe / create your own "exe" without py2exe.

like image 194
Christian Schmitt Avatar answered Oct 23 '22 11:10

Christian Schmitt


You could try the mp3play module for python. If it works for you, and works with py2exe, then it's a very easy module to use. The docs page I like to says it needs win xp to run, but I use it successfully on win7

For example:

import mp3play

filename = r'C:\Documents and Settings\Michael\Desktop\music.mp3'
mp3 = mp3play.load(filename)

mp3.play()

# Let it play for up to 30 seconds, then stop it.
import time
time.sleep(min(30, mp3.seconds()))
mp3.stop()

Check it out here

like image 33
Totem Avatar answered Oct 23 '22 11:10

Totem