Record Audio Channels seperately

info about ReSpeaker series products

Moderators: lily.li, violet, seth.welday

Post Reply
vidura2013
Pre-kindergarten
Pre-kindergarten
Posts: 3
Joined: Fri Aug 10, 2018 9:43 am

Record Audio Channels seperately

Post by vidura2013 » Sun Nov 04, 2018 11:24 am

Hi, I have recently bought an Re-speaker Mic array v2.0 and installed 6-channel firmware. I would like to know how to record each channel separately(Especially channel0 which has processed audio for ASR). A python implementation would be great!!

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

Re: Record Audio Channels seperately

Post by bill.yu » Mon Nov 05, 2018 2:45 pm

Hi there,

here is the python example, http://wiki.seeedstudio.com/ReSpeaker_M ... ract-voice thanks.

vidura2013
Pre-kindergarten
Pre-kindergarten
Posts: 3
Joined: Fri Aug 10, 2018 9:43 am

Re: Record Audio Channels seperately

Post by vidura2013 » Thu Nov 08, 2018 8:41 am

Thanks for your reply. I already went through that link but didn't help my scenario. But I was finally able to solve my issue :-)

rui.zhou
Pre-kindergarten
Pre-kindergarten
Posts: 2
Joined: Mon Nov 19, 2018 4:08 pm

Re: Record Audio Channels seperately

Post by rui.zhou » Mon Nov 19, 2018 4:10 pm

hi
how to record channel seperately? the python example only show record all channels

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

Re: Record Audio Channels seperately

Post by bill.yu » Thu Nov 29, 2018 2:45 pm

Hi there,

Here is the code and configure the channel. thanks.

# deinterleave, select 1 channel
channel0 = data_array[0::CHANNELS]
#channel1 = data_array[1::CHANNELS]

# convert numpy array to string
data = channel0.tostring()
frames.append(data)


Code: Select all

import sys
import pyaudio
import wave
import numpy as np

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 6
RATE = 16000
RECORD_SECONDS = 5

pyaudio_instance = pyaudio.PyAudio()


# find the index of respeaker usb device
def find_device_index():
    found = -1

    for i in range(pyaudio_instance.get_device_count()):
        dev = pyaudio_instance.get_device_info_by_index(i)
        name = dev['name'].encode('utf-8')
        print(i, name, dev['maxInputChannels'], dev['maxOutputChannels'])
        if name.lower().find(b'respeaker') >= 0 and dev['maxInputChannels'] > 0:
            found = i
            break

    return found


device_index = find_device_index()
if device_index < 0:
    print('No ReSpeaker USB device found')
    sys.exit(1)


stream = pyaudio_instance.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                input_device_index=device_index,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)

    # convert string to numpy array
    data_array = np.fromstring(data, dtype='int16')

    # deinterleave, select 1 channel
    channel0 = data_array[0::CHANNELS]
    #channel1 = data_array[1::CHANNELS]

    # convert numpy array to string 
    data = channel0.tostring()
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
pyaudio_instance.terminate()

wf = wave.open('out.wav', 'wb')
wf.setnchannels(1)
wf.setsampwidth(pyaudio_instance.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

Post Reply