如何同时 运行 Espeak 和 Pocketsphinx?

How to run Espeak and Pocketsphinx simultaneously?

我正在尝试 运行 Pocketsphinx 与 Espeak 一起使用,这样当一个词被识别时,它会用文本语音回复。

我已经在这个草图中工作了,但是一旦 Pocketsphinx 运行ning 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

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

--标准输出 | aplay -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