respeaker/mic_array

ReSpeaker 4-Mic Linear Array, HELP using gcc-phat via Thonny

luksiooor opened this issue · 0 comments

I am getting ALSA and "No QuadMic found" error while using this code in Thonny.

`import pyaudio, sys
import numpy as np

def indx_getter():
quadmic_indx = []
for indx in range(audio.get_device_count()):
dev = audio.get_device_info_by_index(indx) # get device
if dev['maxInputChannels']==4 and
len([ii for ii in dev['name'].split('-') if ii=='4mic'])>=1:
print('-'*30)
print('Found QuadMic!')
print('Device Index: {}'.format(indx)) # device index
print('Device Name: {}'.format(dev['name'])) # device name
print('Device Input Channels: {}'.format(dev['maxInputChannels'])) # channels
quadmic_indx = int(indx)
channels = dev['maxInputChannels']
if quadmic_indx == []:
print('No QuadMic Found')
sys.exit() # exit the script if no QuadMic found
return quadmic_indx,channels # return index, if found

##############################################

pyaudio Streaming Object

##############################################

def audio_dev_formatter():
stream = audio.open(format=pyaudio_format,rate=samp_rate,
channels=chans,input_device_index=quadmic_indx,
input=True,frames_per_buffer=CHUNK) # audio stream
stream.stop_stream() # stop streaming to prevent overloa
return stream

##############################################

Grabbing Data from Buffer

##############################################

def data_grabber():

stream.start_stream()                                                                 # start data stream
channel_data = [[]]*chans                                                             # data array
[stream.read(CHUNK,exception_on_overflow=False) for ii in range(0,1)]                 # clears buffer

for frame in range(0,int(np.ceil((samp_rate*record_length)/CHUNK))):                                                                                 
    stream_data = stream.read(CHUNK,exception_on_overflow=False)                    # grab data frames from buffer
    data = np.frombuffer(stream_data,dtype=buffer_format)                             # grab data from buffer
    
    for chan in range(chans):                                                         # loop through all channels
        channel_data[chan] = np.append(channel_data[chan],
                                    data[chan::chans])                                 # separate channels

return channel_data

GCC-PHAT function as defined in the original code

def gcc_phat(sig, refsig, fs, max_tau, interp=16):
# tutaj gcc phat

SIG = np.fft.rfft(sig, n=n)
REFSIG = np.fft.rfft(refsig, n=n)
R = SIG * np.conj(REFSIG)

cc = np.fft.irfft(R / np.abs(R), n=(interp * n))

max_shift = int(interp * n / 2)
if max_tau:
    max_shift = np.minimum(int(interp * fs * max_tau), max_shift)

cc = np.concatenate((cc[-max_shift:], cc[:max_shift+1]))

# find max cross correlation index
shift = np.argmax(np.abs(cc)) - max_shift

tau = shift / float(interp * fs)

return tau, cc


refsig = np.linspace(1, 10, 10)

for i in range(0, 10):
    sig = np.concatenate((np.linspace(0, 0, i), refsig, np.linspace(0, 0, 10 - i)))
    offset, _ = gcc_phat(sig, refsig)
    print(offset)

Main execution loop

if name == "main":
samp_rate = 16000 # audio sample rate [Hz]
CHUNK = 4000 # frames per buffer reading [per one mic]
buffer_format = np.int16 # 16-bit for buffer
pyaudio_format = pyaudio.paInt16 # bit depth of audio encoding

audio = pyaudio.PyAudio()                                                             # start pyaudio device
quadmic_indx,chans = indx_getter()                                                     # get QuadMic device index and channels

stream = audio_dev_formatter()                                                         # audio stream
max_tau = 0.05

while True:
    data_chunks = data_grabber()
    sig = data_chunks[0]  # Nagrywanie bieżącego sygnału do porównania
    refsig = data_chunks[0]
    tau, cc = gcc_phat(sig, refsig, samp_rate, max_tau)  # Porównanie sygnałów
    print(f"Time offset: {tau}")

`
Error:

ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib conf.c:5200:(snd_config_expand) Unknown parameters {AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0}
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0}
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_dmix.c:1009:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
No QuadMic Found

Process ended with exit code 0.

Can someone help me with why the ReSpeaker is not being detected in the provided code in Thonny? What should be fixed to detect the Respeaker ? This array is normally detected in Audacity and is connected correctly to Raspberry Pi2B. What should I do/change in code to make this work? Is it possible to use gcc-phat.py with 4-mic linear array ?