pythonpocketsphinxespeak

How to run Espeak and Pocketsphinx simultaneously?


I am trying to run Pocketsphinx together with Espeak, so that when a word is recognized it answers back with text to speech.

I have gotten both to work in this sketch, but once Pocketsphinx is running Espeak stops working. No error, just no sound.

So not sure what is going wrong? Do I need to run them in separated threads or does one block the other?

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

Solution

  • I found a solution to the problem. Espeak was not using the right audio card, since pocketsphinx installs pulseaudio. To specify where Espeak sends the audio to use the flag stdout:

    --stdout | aplay -D "sysdefault:CARD=seeed2micvoicec"

    use the command aplay -L to find the name of your card

    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