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
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]
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’)
[/code]
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]
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()
[/code]