转换python espeak+;直接播放输出音频的子流程代码

转换python espeak+;直接播放输出音频的子流程代码,python,linux,subprocess,alsa,espeak,Python,Linux,Subprocess,Alsa,Espeak,我正在使用一个现有的程序,该程序从套接字读取xml,将文本转换为wav文件,然后通过音频输出设备播放 我想把它去掉,这样它就可以直接播放文本到音频中 现在,我很难弄清楚我是否有正确的代码,并且理解它是否真正创建了wav文件 调用文本到语音功能的函数 def generate_audio(self, language, voice=None): info = self.get_first_info(language, bestmatch=False) if info is None

我正在使用一个现有的程序,该程序从套接字读取xml,将文本转换为wav文件,然后通过音频输出设备播放

我想把它去掉,这样它就可以直接播放文本到音频中

现在,我很难弄清楚我是否有正确的代码,并且理解它是否真正创建了wav文件

调用文本到语音功能的函数

def generate_audio(self, language, voice=None):
    info = self.get_first_info(language, bestmatch=False)
    if info is None:
        self.media_info[language] = None
        return False

    truncate = not self.broadcast_immediately() and bcastplayer.Config.setting('alerts_truncate')
    message_text = info.get_message_text(truncate)

    location = bcastplayer.ObData.get_datadir() + "/alerts"
    if os.access(location, os.F_OK) == False:
        os.mkdir(location)
    filename = self.reference(self.sent, self.identifier) + "-" + language + ".wav"

    resources = info.get_resources('audio')
    if resources:
        if resources[0].write_file(os.path.join(location, filename)) is False:
            return False

    elif message_text:
        self.write_tts_file(os.path.join(location, filename), message_text, voice)

    else:
        return False
可以修改此选项以直接播放音频吗?

def write_tts_file(self, path, message_text, voice=None):
    if not voice:
        voice = 'en'
    proc = subprocess.Popen([ 'espeak', '-m', '-v', voice, '-s', '130', '--stdout' ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
    (stdout, stderr) = proc.communicate(message_text.encode('utf-8') + b" <break time=\"2s\" /> " + message_text.encode('utf-8') + b" <break time=\"3s\" /> ")
    proc.wait()

    with open(path, 'wb') as f:
        f.write(stdout)
def write_tts_文件(self、path、message_text、voice=None):
如果不是声音:
声音='en'
proc=subprocess.Popen(['espeak'、'-m'、'-v',voice'-s'、'130'、'-stdout'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,close_fds=True)
(stdout,stderr)=过程通信(消息文本编码('utf-8')+b”“+消息文本编码('utf-8')+b”“)
进程等待()
将open(路径“wb”)作为f:
f、 写入(标准输出)
我从未见过这样的代码使用
进程
子进程
标准输出
管道

在不创建wav文件的情况下,是否可以轻松地将子流程代码更改为只将输出管道或重定向到
aplay

还有另一个答案可能会提供线索,但我的新手理解不确定如何将此代码转换为该答案


您可以使用
子流程管道
将这两个流程链接在一起。以下是
写入tts\u文件
功能的修改版本:

def write_tts_file(self, path, message_text, voice=None):
    if not voice:
        voice = 'en'
    proc = subprocess.Popen(['espeak', '-m', '-v', voice, '-s', '130', '--stdout' ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
    aplay = subprocess.Popen(['aplay', '-D', 'sysdefault'], stdin=proc.stdout)
    proc.stdin.write(message_text.encode('utf-8') + b" <break time=\"2s\" /> " + message_text.encode('utf-8') + b" <break time=\"3s\" /> \n")
    proc.stdin.close()
    proc.wait()
def write_tts_文件(self、path、message_text、voice=None):
如果不是声音:
声音='en'
proc=subprocess.Popen(['espeak'、'-m'、'-v',voice'-s'、'130'、'-stdout'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,close_fds=True)
aplay=subprocess.Popen(['aplay','-D',sysdefault',stdin=proc.stdout)
过程标准写入(消息文本编码('utf-8')+b”“+消息文本编码('utf-8')+b”\n)
程序标准关闭()
进程等待()

发送要讲话的消息后,请务必关闭
程序的
stdin
。这将使
proc
在发送数据后退出,并将其输出关闭到
aplay
,而aplay将在播放结束后退出。如果
proc
的输入未关闭,则两个进程都不会退出。

您可以使用
子进程管道将两个进程链接在一起。以下是
写入tts\u文件
功能的修改版本:

def write_tts_file(self, path, message_text, voice=None):
    if not voice:
        voice = 'en'
    proc = subprocess.Popen(['espeak', '-m', '-v', voice, '-s', '130', '--stdout' ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
    aplay = subprocess.Popen(['aplay', '-D', 'sysdefault'], stdin=proc.stdout)
    proc.stdin.write(message_text.encode('utf-8') + b" <break time=\"2s\" /> " + message_text.encode('utf-8') + b" <break time=\"3s\" /> \n")
    proc.stdin.close()
    proc.wait()
def write_tts_文件(self、path、message_text、voice=None):
如果不是声音:
声音='en'
proc=subprocess.Popen(['espeak'、'-m'、'-v',voice'-s'、'130'、'-stdout'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,close_fds=True)
aplay=subprocess.Popen(['aplay','-D',sysdefault',stdin=proc.stdout)
过程标准写入(消息文本编码('utf-8')+b”“+消息文本编码('utf-8')+b”\n)
程序标准关闭()
进程等待()

发送要讲话的消息后,请务必关闭
程序的
stdin
。这将使
proc
在发送数据后退出,并将其输出关闭到
aplay
,而aplay将在播放结束后退出。如果
proc
的输入未关闭,他们两人都不会退出。

谢谢-我会尝试一下。我真的很难理解espeak和子流程hanks-我会尝试一下。我真的很难理解espeak和子流程