如何在东芝笔记本电脑和Windows 7上使用python进行文本语音转换? [英] How to do text to speech with python on a Toshiba laptop and Windows 7?

查看:133
本文介绍了如何在东芝笔记本电脑和Windows 7上使用python进行文本语音转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种在python中创建文本到语音的方法(我在Windows 7上).我正在使用pyinstaller编译该程序.我尝试了许多方法,包括使用Google的非官方文本语音程序,通过urllib2模块访问语音程序.最终将创建一个mp3文件.有关该代码的详细信息,大部分代码来自 http://glowingpython.blogspot.com/2012/11/text-to-speech-with-correct-intonation.html .然后,我需要播放由此生成的mp3文件.我使用了mplayer,ffmpeg,mp3play,audiere,pydub和pygame,它们的结果相同:没有声音播放,但没有异常.我什至在树莓派上使用了相同的pygame代码,并成功播放了mp3文件.我还尝试将其转换为wav文件,该文件运行良好,仅当我尝试使用pygame或winsound播放该文件时,会发生相同的情况.没有声音,没有例外.我当前的代码使用winsound,播放一个wav文件,我可以在Windows Media Player中成功播放该文件(甚至可以使用os.startfile()从python在Windows Media Player中打开它).在这里:

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

我也正在尝试使用pygame混音器和音乐模块.例如:

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)

我什至已经使用winsound和win32api Beep()函数成功播放了python的声音.但是,这显然无法播放mp3或wav文件.我还尝试了一种与语音引擎完全不同的文本,该引擎使用pyttsx播放没有混合mp3文件的声音:

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.

因为这几乎与硬件有关(pygame.mixer以这种方式在不同的硬件上工作,并且我确信它通常可以在Windows上工作),所以知道我正在使用东芝笔记本电脑可能很重要.另外,我正在使用python 2.7.

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.

理想情况下,我想使用pygame进行此操作,因为我拥有最丰富的使用经验,并且希望尽可能在pygame中使用一些声音编辑功能.

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.

我也尝试使用64位python(我在64位Windows 7上使用32位python).它仍然无法正常工作.

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

我还尝试在Ubuntu虚拟盒子环境中但在同一设备上播放mp3文件.它仍然没有用.这并不特别令人惊讶,因为virtualbox使用了主机操作系统中的大量资源(例如屏幕和wifi),因此播放声音不一定会有所不同.解决此问题的任何方法都将有所帮助.某些声音可以正常播放,但不是专门用于python中的mp3或wav文件,因此可能有解决方案.

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.

推荐答案

碰巧的是,最近我有一个主意,促使我对此进行了另一番尝试.该解决方案非常骇人,不需要,但不幸的是.基本上,我要做的是在Windows Media Player中打开文件,但使用子进程隐藏窗口.我已经有了这个问题的基本答案,所以在这里,因为我不太需要重写完全相同的东西.

As it happens I recently had an idea that led me to take another shot at this. This solution is extremely hackish and shouldn't be necessary but unfortunately it is. Basically what I do is open the file in windows media player but suppress the window using subprocess. I already have the basic answer at this question, so here it is because I don't much care to rewrite the exact same thing.

这篇关于如何在东芝笔记本电脑和Windows 7上使用python进行文本语音转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆