Python3,收听麦克风阵列的特定通道

时间:2018-12-17 10:59:16

标签: python-3.x channel microphone pyaudio

我正在尝试使用Python 3.7监听麦克风阵列(Respeaker v2.0)的第一个(0)通道。

这时,我可以在6个频道上收听:

p = pyaudio.PyAudio()

stream = p.open(
            rate=16000,
            format=p.get_format_from_width(2),
            channels=6,
            input=True,
            input_device_index=5)

但是当我想在缓冲区中记录时:

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

如何选择通道“ 0”?

对于Respeaker V2.0,通道0包含检测到的语音(而通道5包含回放输出)

我看不到任何使用PyAudio做到这一点的选项:(

NB:如果我将流写入WAV文件,并使用Audacity打开它,则可以选择第一个通道,它实际上包含已清除的声音

谢谢!

2 个答案:

答案 0 :(得分:0)

这适用于两个用于本地文件和数据流的通道。

本地文件。

# audio channel balance local file
import numpy as np
import pyaudio
import wave
import time

wf = wave.open('2chan_audio.wav', 'r')
p = pyaudio.PyAudio()

# two channels
channel_balance = [0.1, 1]

def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    data_as_np = np.frombuffer(data, dtype=np.int16)
    data_as_np = data_as_np.reshape(frame_count, wf.getnchannels())
    data_as_np = np.int16(data_as_np * channel_balance)
    return data_as_np, pyaudio.paContinue

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():
    time.sleep(0.01)

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

数据流。

# audio channel balance stream
WIDTH = 2
CHANNELS = 2
RATE = 44100
p = pyaudio.PyAudio()

channel_balance = [0.1, 1]

def callback(in_data, frame_count, time_info, status):
    data_as_np = np.frombuffer(in_data, dtype=np.int16)
    data_as_np = data_as_np.reshape(frame_count, CHANNELS)
    data_as_np = np.int16(data_as_np * channel_balance)
    return data_as_np, pyaudio.paContinue

stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():
    time.sleep(0.01)

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

希望它能起作用!

答案 1 :(得分:0)

frames = []

for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    a0 = np.fromstring(data,dtype=np.int16)[0::6]
    # if you want to get channel 1 data
    # a1 = np.fromstring(data,dtype=np.int16)[1::6]

    a = a0.tostring()

    frames.append(a)

文档: http://wiki.seeedstudio.com/ReSpeaker_Mic_Array_v2.0/