1

我正在将 Microsoft Azure Kinect DK 传感器用于大学研究项目。我正在尝试访问从每个通道听到的麦克风数据(以 dB 为单位)。我需要这些数据来进一步编写延迟和求和算法。

我目前已经尝试了很多东西。这是我所拥有的基础知识:

FORMAT = pyaudio.paInt16 # We use 16bit format per sample
CHANNELS =7
RATE =16000
CHUNK = 1024 # 1024bytes of data red from a buffer
RECORD_SECONDS = 0.1
WAVE_OUTPUT_FILENAME = "output.wav"


p = pyaudio.PyAudio()

stream = p.open(
                format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True)

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    a0 = np.fromstring(data,dtype=np.int16)[0::7]

    a = a0.tostring()

    frames.append(a)

理论上这会从第一个通道中获取数据,但我不断收到错误“无效的通道数”。

任何帮助,将不胜感激,

谢谢!

4

1 回答 1

1

我认为您遇到了错误,因为您正在从默认麦克风读取音频,在您的情况下,默认麦克风不是 Microsoft Azure Kinect DK。确保将 Microsoft Azure Kinect DK 设置为默认值。如果不是,那么您可以将其设置为默认值,然后尝试。PyAudio.get_device_count()或者,您可以在运行时使用and找出 Azure Kinect DK 的索引,PyAudio.get_device_info_by_index()并将其作为参数传递给p.open()调用。

我能够使用以下代码从 Azure Kinect DK 录制单通道(7 个通道中的第一个通道)音频。

import pyaudio
import wave
import numpy as np

p = pyaudio.PyAudio()

# Find out the index of Azure Kinect Microphone Array
azure_kinect_device_name = "Azure Kinect Microphone Array"
index = -1
for i in range(p.get_device_count()):
    print(p.get_device_info_by_index(i))
    if azure_kinect_device_name in p.get_device_info_by_index(i)["name"]:
        index = i
        break
if index == -1:
    print("Could not find Azure Kinect Microphone Array. Make sure it is properly connected.")
    exit()

# Open the stream for reading audio
input_format = pyaudio.paInt32
input_sample_width = 4
input_channels = 7
input_sample_rate = 48000

stream = p.open(format=input_format, channels=input_channels, rate=input_sample_rate, input=True, input_device_index=index)

# Read frames from microphone and write to wav file
with wave.open("output.wav", "wb") as outfile:
    outfile.setnchannels(1) # We want to write only first channel from each frame
    outfile.setsampwidth(input_sample_width)
    outfile.setframerate(input_sample_rate)

    time_to_read_in_seconds = 5
    frames_to_read = time_to_read_in_seconds * input_sample_rate
    total_frames_read = 0
    while total_frames_read < frames_to_read:
        available_frames = stream.get_read_available()
        read_frames = stream.read(available_frames)
        first_channel_data = np.fromstring(read_frames, dtype=np.int32)[0::7].tobytes()
        outfile.writeframesraw(first_channel_data)
        total_frames_read += available_frames

stream.stop_stream()
stream.close()

p.terminate()
于 2020-12-03T03:25:36.130 回答