python - 如何同时运行 Espeak 和 Pocketsphinx?
问题描述
我正在尝试将 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
解决方案
我找到了解决问题的方法。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
推荐阅读
- linux - curl 从一个 docker 容器到另一个在 Jenkins 服务器上给出 302,但在本地工作
- .net - 带有认证的 WCF 消息安全性,没有主键
- r - 通过保持连续三天的行日期来过滤数据框,最好使用 dplyr
- python - 如何在 python 脚本中使用带有密码命令的“sudo”?
- php - 使用 Wordpress 向 sql 插入数据的问题
- vue.js - 无法将 json 推送到 vue.js 中的数据
- html - 如何垂直和水平对齐DIV
- node.js - 完全清除生成
- angular - 如何为 ngTemplateOutput 分配函数返回值?
- botframework - 用户和机器人消息出现在聊天容器的同一侧