pythonpython-3.xubuntu-20.04

Wake word detection in porcupine


I'm trying to implement porcupine wakeword in python and have followed the instructions from here

I have the following code:

import pvporcupine

### Porcupine wakeword
handle = pvporcupine.create(keywords=['computer', 'jarvis'])

def get_next_audio_frame():
    pass

while True:
    keyword_index = handle.process(get_next_audio_frame())
    if keyword_index >= 0:
        # Insert detection event callback here
        print('Yes sir?')
        pass

but I receive the following error:

❯ python3 porcupine.py Traceback (most recent call last):   File "porcupine.py", line 10, in <module>
    keyword_index = handle.process(get_next_audio_frame())   File "/home/rupstar/Computer/lib/python3.8/site-packages/pvporcupine/porcupine.py", line 129, in process
    if len(pcm) != self.frame_length: TypeError: object of type 'NoneType' has no len()

Solution

  • This may not be perfect (it isn't), but it shows how I've created a personal voice assistant in python on WSL2 running Ubuntu 20.04 on a Windows 10 machine. The voice assistant responds to a wakeword (Jarvis or Computer) and then executes commands. Relevant to this post is how porcupine is invoked:

    #!/usr/bin/env python3
    #Porcupine wakeword includes
    import struct
    import pyaudio
    import pvporcupine
    
    porcupine = None
    pa = None
    audio_stream = None
    
    try:
        porcupine = pvporcupine.create(keywords=["computer", "jarvis"])
    
        pa = pyaudio.PyAudio()
    
        audio_stream = pa.open(
                        rate=porcupine.sample_rate,
                        channels=1,
                        format=pyaudio.paInt16,
                        input=True,
                        frames_per_buffer=porcupine.frame_length)
    
        while True:
            pcm = audio_stream.read(porcupine.frame_length)
            pcm = struct.unpack_from("h" * porcupine.frame_length, pcm)
    
            keyword_index = porcupine.process(pcm)
    
            if keyword_index >= 0:
                print("Hotword Detected")
                speak("Computer online")