How to make a list of hot words/wake words execute different functions in Python snowboy module?

189 Views Asked by At
from snowboy import snowboydecoder
import threading

def hello():
    print('Hello')

def greeting():
    print('Im doing good.')

def det_hi():
    detector = snowboydecoder.HotwordDetector('Hi.pmdl', sensitivity=0.5, audio_gain=1)
    detector.start(hello)

def det_greeting():
    d2 = snowboydecoder.HotwordDetector('HowAreYou.pmdl', sensitivity=0.5, audio_gain=1)      
    d2.start(greeting)    

thread1 = threading.Thread(target=det_hi)
thread2 = threading.Thread(target=det_greeting)
thread1.start()
thread2.start()

^^^^ I tried running multiple instances of snowboy in different threads that are always listening, and it works for a little bit, but the threads end up fighting over mic access after a while and it stops working

from snowboy import snowboydecoder
import threading

wake_words = ['Hi.pmdl', 'HowAreYou.pmdl']

def hello():
    print('Hello')

def greeting():
    print('Im doing good.')


detector = snowboydecoder.HotwordDetector(wake_words, sensitivity=0.5, audio_gain=1)
detector.start(hello)

^^^^ I tried putting the different wake word files in a list and running it then. It works, but now i cant figure out how have it execute a separate function for each wake word.

I also noticed a detector.num_hotwords attribute, but i couldnt find any reference to it in the docs or any way to use that attribute.

Any suggestions?

1

There are 1 best solutions below

0
On

I just managed to figure it out, I'm posting this in case anyone needs this in the future.

from snowboy import snowboydecoder
import threading

def hello():
    print('Hello')

def greeting():
    print('Im doing good.')

wake_words = ['Hi.pmdl', 'HowAreYou.pmdl']
callbacks = [hello, greetings]  # For some reason does work if you include the () on the function

detector = snowboydecoder.HotwordDetector(wake_words, sensitivity=0.5, audio_gain=1)
detector.start(detected_callback=callbacks)

The key was in making a list of callbacks the same length as the list of wake words.