ReSpeaker 4-Mics Pi HAT API to starting recording or choosing channel

info about ReSpeaker series products

Moderators: lily.li, violet, seth.welday

Post Reply
954562905
Pre-kindergarten
Pre-kindergarten
Posts: 1
Joined: Fri Mar 22, 2019 4:49 pm

ReSpeaker 4-Mics Pi HAT API to starting recording or choosing channel

Post by 954562905 » Fri Mar 22, 2019 5:06 pm

I hope there is some solutions to this problem I'm dealing with. I have been working on a program of DoA algorithm in C++ recently, and now I need to test if it works out well. Thus I bought a ReSpeaker 4-Mics Pi HAT and a raspberry 3B to capture voice and run my program.I'm looking for a method to control the mics directly using my code based on the driver i have installed but no luck till now. I've searched all over seeed website, librespeaker.io ,
forums and got nothing useful.
Someone help!!!PLZ

bill.yu
Junior-6
Junior-6
Posts: 644
Joined: Mon Apr 02, 2018 11:05 am

Re: ReSpeaker 4-Mics Pi HAT API to starting recording or choosing channel

Post by bill.yu » Thu Mar 28, 2019 11:57 am

Hi there,

We also can use python script to extract specific channel data.

Step 1, We need to run the following script to get the device index number of 4 mics pi hat:

sudo pip install pyaudio
cd ~
nano get_index.py

Step 2, copy below code and paste on get_index.py.

Code: Select all


import pyaudio

p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')

for i in range(0, numdevices):
        if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print "Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name')

Step 3, press Ctrl + X to exit and press Y to save.

Step 4, run 'sudo python get_index.py' and we will see the device ID as below.

ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
Input Device id 2 - seeed-4mic-voicecard: - (hw:1,0)
Input Device id 5 - ac108
Input Device id 6 - SoftVolume
Input Device id 8 - default

Step 5, change RESPEAKER_INDEX = 2 to index number. Run python script record.py to record a speech. If you want to extract channel 0 data from 4 channels, please follow below code. For other channel X, please change [0::4] to [X::4].

Code: Select all


import pyaudio
import wave
import numpy as np

RESPEAKER_RATE = 16000
RESPEAKER_CHANNELS = 4 
RESPEAKER_WIDTH = 2
# run getDeviceInfo.py to get index
RESPEAKER_INDEX = 2  # refer to input device id
CHUNK = 1024
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(
            rate=RESPEAKER_RATE,
            format=p.get_format_from_width(RESPEAKER_WIDTH),
            channels=RESPEAKER_CHANNELS,
            input=True,
            input_device_index=RESPEAKER_INDEX,)

print("* recording")

frames = [] 

for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    # extract channel 0 data from 4 channels, if you want to extract channel 1, please change to [1::4]
    a = np.fromstring(data,dtype=np.int16)[0::4]
    frames.append(a.tostring())

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(p.get_format_from_width(RESPEAKER_WIDTH)))
wf.setframerate(RESPEAKER_RATE)
wf.writeframes(b''.join(frames))
wf.close()


Post Reply