[Solved] Micarray_alsa_pipes example and Matrix voice

Hi,
I used the micarray_alsa_pipes.py demo and with alsaaudio I can read data:
Looks like "H?H?G?I?E?H?J?I?F?G?G?G?H?H?J?D…"
What is the next step to get the right “Sound” values?

python code:
import alsaaudio

mic = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, device=‘mic_channel0’)
mic.setchannels(1)
mic.setrate(16000)
mic.setformat(alsaaudio.PCM_FORMAT_S16_LE)
mic.setperiodsize(160)

while True:
length, data = mic.read()
if length:
print data

what du you mean with right sound values? numeric values?

yes, numeric ones… im not sure about the pcm format of the piped data. Do you have any idea?

hmm eventually something like this:

import struct
...
while True:
    length, data = mic.read()
    if samples:
        values = struct.unpack('<%dh' % length, data)
        print(values)

here is an example of generating a spectrum with numpy, etc.:

 https://gist.github.com/sourceperl/c1444a99992a58a0ba6987beaa2daec0
1 Like

i wrote a quick and dirty (just for fun) example to implement a recorder starting to record, when it detects some sound and keeps recording while not idle for 2 seconds - saving the result to test.wav. i know the range of an audio value is signed so the max(…) is not nice here, but it works quite well. the check againts 2000 is totally random chosen.

the script uses pyalsaaudio and needs the matrixio-malos serivce running.

1 Like

Thanks! The examples are working!
Also spectrum.py after changing to the rigth device.
I’m happy to test the Matrix Voice now.
btw: matrixio-malos serivce was running but I had to start micarray_alsa_pipes.py to get it working…

Hi !

I ran into the same issue, I try your code and I got this error :
indent preformatted text by 4 spaces
Traceback (most recent call last):
File “voice.py”, line 8, in
mic = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, device=‘mic_channel8’)
TypeError: ‘device’ is an invalid keyword argument for this function

So I wonder if you can share your /etc/asound.conf file and if exist your ~/.asound.conf

Have you modify the micarray_alsa_pipes.cpp ?

Thank you

no you only need to start the matrixio-malos.service it should create the pipes in the /tmp folder.

# malos service is needed:
sudo apt install matrixio-malos

# service should be started by default, but to be sure:
sudo systemctl start matrixio-malos.service

# check if the pipes are created
ls /tmp

as alternative you can create those pipes by running the cpp programm an let it keep running. with the installation of the malos.serivce, the alsa sound config should be set up properly. so no need define your own ~/.asound.conf

Thank you for your answer.

ls /tmp
show:
matrix_micarray_channel_0
matrix_micarray_channel_1
matrix_micarray_channel_2
matrix_micarray_channel_3
matrix_micarray_channel_4
matrix_micarray_channel_5
matrix_micarray_channel_6
matrix_micarray_channel_7
matrix_micarray_channel_8

which version of alsaaudio are you using ?
I upgrade my version to 0.8.4 (pip install pyalsaaudio -U)

Now I got no such file or directory [mic_channel8]
it seems that the device= is invalid …

i have installed the pyalsaaudio in a virtualenv, but your pip install should work also.

can you try following steps:

  1. check if your /etc/asound.conf looks like this, this should be the default installed by matrix-creator-init:

  2. remove or rename ~/.asound.conf

    • sudo apt update
    • sudo apt dist-uprade
    • sudo apt install matrixio-malos
  3. reboot

  4. ps ax | grep should return:
    xxx ? Ssl 0:51 /usr/bin/malos

  5. you can list the pcm devices available with running this in your python shell

    import alsaaudio
    alsaaudio.__file__ # <-- check if the correct mpdule is used
    alsaaudio.pcms()   # <-- list devices
    
    this should return:
    '/home/pi/.virtualenvs/matrix/local/lib/python2.7/site-packages/alsaaudio.so' <--my virtualenv install path
    
    [u'null', u'sc', u'mic_channel0', u'mic_channel1', u'mic_channel2', u'mic_channel3', u'mic_channel4', u'mic_channel5', u'mic_channel6', u'mic_channel7', u'mic_channel8', u'default', u'sysdefault:CARD=ALSA', u'dmix:CARD=ALSA,DEV=0', u'dmix:CARD=ALSA,DEV=1', u'dsnoop:CARD=ALSA,DEV=0', u'dsnoop:CARD=ALSA,DEV=1', u'hw:CARD=ALSA,DEV=0', u'hw:CARD=ALSA,DEV=1', u'plughw:CARD=ALSA,DEV=0', u'plughw:CARD=ALSA,DEV=1', u'sysdefault:CARD=Dummy', u'dmix:CARD=Dummy,DEV=0', u'dsnoop:CARD=Dummy,DEV=0', u'hw:CARD=Dummy,DEV=0', u'plughw:CARD=Dummy,DEV=0']
    

that’s all i have done to get the example running on my creator and it works.

by the way i also updated my example script to use 20000 as condition for the recorder, because they increased the gain of the mics in their latest firmware release

Thanks you for your help.

I finally succeed. I remove everything I made before and create a virtual environnement and now it works.

I modified the modprob.d/alsa … file so I delete it and work instantly.
I remove all the matrix package reboot and reinstall only the one you mention.
sudo apt-get remove matrix-*

But now, I don’t know if it normal but after a while the script show that error (if its an error)

Traceback (most recent call last):
File "voice.py", line 26, in <module>
sound_detected = True if max(struct.unpack('<%dh' % length, data)) > 20000 else False
struct.error: bad char in struct format

the error appear also with 2000 instead of 20000.

Blockquote

is this happening before, or while recording, i could reproduce it one. it seems that the unpack format get’s generated wrong. eventually is the length value not a regular, or negative number, will test further.

It happens while recording, and it randomly appear. and sometime I can go through the 290 iterations without problems.

can you change this and test it again:

length, data = mic.read()
time.sleep(0.01)
sound_detected = True if max(struct.unpack('<%dh' % length, data)) > 20000 else False

if it still happens, i only got it once, so i can not test it myself.

I retry today and now everything work,

if I add the time.sleep(0.01).
it stop after a second.

Finally I use arecord directly and mic_demo as a service and my project works as I expected

Thank you for your time.

You saved me, thank you so very much!!

1 Like