How to record with PyAudio

Can you please post your code for snowboy hotword detection?
I am working with snowboy hotword detection and the python speech_recognition package. Both work with PyAudio and had trouble configuring them.
I would like to see how I could get these two working with the arecord process and then maybe stream sound from the mic to google cloud speech.
Thanks

I promised snowboy’s owner that I’ll commit a python sample there. I hope I’ll get time to do it today. Will share a link here then ;-).

That sound great! Looking forward to it :slight_smile:

Hi, I’ve sent the PR for the arecord based python example. You can take a look:
https://github.com/Kitt-AI/snowboy/pull/120

1 Like

Thanks! Will definetely give it a try

Hi @duoduo999 thanks so much for the github code.
I tested your pull request on a Matrix Creator.

I renamed your file to snowboydecoder.py so could test using demo.py and demo2.py in the same directory.

Upon running the demos I get:

Listening... Press Ctrl+C to exit
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/pi/snowboy/examples/Python/snowboydecoder.py", line 115, in record_proc
    wav = wave.open(process.stdout, 'rb')
  File "/usr/lib/python2.7/wave.py", line 509, in open
    return Wave_read(f)
  File "/usr/lib/python2.7/wave.py", line 164, in __init__
    self.initfp(f)
  File "/usr/lib/python2.7/wave.py", line 129, in initfp
    self._file = Chunk(file, bigendian = 0)
  File "/usr/lib/python2.7/chunk.py", line 63, in __init__
    raise EOFError
EOFError

I am sure I am doing something wrong. How did you setup and test?

To make sure it wasn’t something with my base system, I verified am able to successfully record audio from the commandline with arecord --device=mic_channel1 -q -r 16000 -f S16_LE test.wav.

I notice on line 113 of your PR the “arecord” cmd has no “--device” specified? Should there be? My commandline tests don’t run without it. Or are you using a config file or patch that somehow uses the matrix microphone array. Somehow using the array would be ideal.

I didn’t want to comment directly in github without these clarifications first, since some of these issues may be matrix.one specific, and in order to not slow down the PR if so.

Thanks and please advise,
Marc

HI marc,

I didn’t include “–device” parameter in the PR because I didn’t want it to be bound to Matrix Creator. In my local environment I’ve set mic_channel8 to be default recording device so I didn’t need to pass it in. You can try adding that switch in python code and see if the problem can be resolved.

Btw, I’ve only tested with python3, although I think python2 should work too. Let me know if you still run into problems.

Hi everyone,

Just wanted to let you know that PortAudio support is on our roadmap.

Thanks!
Sean

Thanks! Can’t wait to see it.

has there been any update on this?

Is this roadmap public somehow? I am struggling too with my initial setup using PyAudio.

I declared mic8 to be the defaut one for recording, arecord works well.

How i would proceed to replace the well known PyAudio code snippets to record using your method? Thank you.

@duoduo999: Hello,

can you please share your code for replacing portaudio / pyaudio replacement which you mentioned above? I am not interested in the snowboy part yet.

Thank you

Okay, seems to be part of your Snowboy pull request here https://github.com/Kitt-AI/snowboy/pull/120/commits/9549d8412043973ccf61ecc2687ef9c38716faf0

Is this code suitable to detect complete sentences and corresponding audio silence or does it focus on single word recording?

It only changed the recording approach but didn’t modify how snowboy works
… I guess snowboy just listens on continuous stream of frames and does
its detection.

I hope Matrix Labs will support PyAudio soon as it is the Python quasi standard.

We do not have any feedback when this might happen. Therefore I am currently searching the internet for software (and hardware) alternatives. Maybe gstreamer can do the job, don’t know. Maybe we can utilize the nodejs micarray code within Python using subprocess and threading as you did.

2 Likes

Not sure if there has been any solution through python code thats been found.
But I was able to get it working by directly accessing the fifo socket of the channel in the mic array.
I tried with pyaudio but just couldn’t get it to work. This code snippet records on channel 8 for 10 seconds and saves the audio file as a .wav
You can replace channel 8 with your channel of interest.

CHUNK = 1024

FORMAT = 8

CHANNELS = 1

RATE = 16000 #sample rate

RECORD_SECONDS = 10

currentDir = "audio/"

 

if not os.path.exists(currentDir):

    os.makedirs(currentDir)

clipFile = os.path.join(currentDir, "vallie"  + "-" + time.strftime("%Y%m%d-%H%M%S") + ".wav")

 

print("* start recording")

frames = []

timeout = time.time()  + RECORD_SECONDS + 1

while time.time() < timeout:

    with open('/tmp/matrix_micarray_channel_8') as fifo:

        print("FIFO opened")

        while True:

            data = fifo.read()

            if len(data) == 0:

                continue

            frames.append(data)

            break

print("* done recording")

 

wf = wave.open(clipFile, 'wb')

wf.setnchannels(CHANNELS)

wf.setsampwidth(2)

wf.setframerate(RATE)

wf.writeframes(b''.join(frames))

wf.close()

Check this last post here :

1 Like

I would mark a thread as solved once the fix is available for all. Currently, i see the term ‘we are close’ replaced by ‘we are really close’, no more or less. But i am positive.

Any updates on PyAudio support?
Seems like this issue is blocking a lot of open source projects using voice recognition.