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

问题描述 投票:0回答:2

我正在尝试使用Python 3.7收听麦克风阵列的第一个(0)通道(Respeaker v2.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做任何选项:(

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

谢谢 !

python-3.x channel microphone pyaudio
2个回答
1
投票
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/


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()

希望它有效!

© www.soinside.com 2019 - 2024. All rights reserved.