如何同时运行Espeak和Pocketsphinx?

时间:2019-04-12 22:41:49

标签: python pocketsphinx espeak

我正尝试将Pocketsphinx与Espeak一起运行,以便在识别出一个单词时,它会以文本到语音的方式回答。

在此草图中我俩都可以工作,但是一旦Pocketsphinx运行,Espeak就会停止工作。没有错误,只有声音。

所以不确定是怎么回事?我需要在单独的线程中运行它们还是一个阻塞另一个线程?

from pocketsphinx import LiveSpeech
from subprocess import call
import pyttsx3
import os

def speak(txt):
    print("speaking: "+txt)
    call(["sudo", "espeak", txt])

def init_speech():
    return LiveSpeech(
        audio_device = None,
        sampling_rate=16000,
        lm=False,
        kws=keyword_dir)

speak('hello world one') # this works

root_dir = os.path.dirname(os.path.abspath(__file__))
keyword_dir = os.path.join(root_dir, 'data/keyphrase.list')

speech = init_speech()

speak('hello world two') # and now it does not work

while True:
    for phrase in speech:
        topWord = phrase.segments()[0]
        print(topWord) #this works
        speak(topWord) # this is the goal

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的办法。 Espeak没有使用正确的声卡,因为Pocketsphinx安装了pulseaudio。要指定Espeak将音频发送到何处以使用标志stdout:

-标准输出|播放-D“ sysdefault:CARD = seeed2micvoicec”

使用命令aplay -L查找卡的名称

from pocketsphinx import LiveSpeech
import os

def speak(txt):
    os.system('espeak "'+txt+'" --stdout | aplay -D "sysdefault:CARD=seeed2micvoicec"')
    print("TtS: " + txt)

def init_speech():
    return LiveSpeech(
        audio_device = None,
        sampling_rate=16000,
        lm=False,
        kws=keyword_dir)

speak('hello world one') # this works

root_dir = os.path.dirname(os.path.abspath(__file__))
keyword_dir = os.path.join(root_dir, 'keyphrase.list')

speech = init_speech()
print("running")
speak('hello world two') # and now it does not work

while True:
    for phrase in speech:
        topWord = phrase.segments()[0]
        print(phrase) #this works
        speak(topWord) # this is the goal