从pyaudio-stream获取音频样本作为浮点数

时间:2013-10-28 07:49:02

标签: python pyaudio

由于我目前正要构建一个基于Raspberry Pi的设备,用于测量声卡中记录的噪声(例如方差),并尝试在python中执行此操作,因此我陷入困境,想知道如何获取音频样本为float-number,用于进一步计算。

我做了什么:
采用Line-in-to-chinch适配器并触摸插头以产生某种测试信号 录制到例如Audacity或Matlab会显示合理的结果,例如

enter image description here

我想得到什么:
理想情况下,我希望从声卡中获取5帧×1024个样本,并将它们转换为列表,元组或numpy数组作为浮点数进行进一步计算。

当尝试使用python / pyaudio和本帖末尾的代码实现这一点时,我得到了这样的结果:

enter image description here

由于我在python中得到的值似乎与Matlab(和其他人)中的值相差大约两倍,我认为我已经监督了某些事情或做错了什么。 我想我在struct.unpack区域的某个地方犯了一个错误,但无法确定究竟在哪里或为什么。 我想请你帮忙,指出错误在哪里以及我做错了什么。

获取一些样本并绘制它们的小测试代码:

import pyaudio
import struct
import matplotlib.pyplot as plt

FORMAT = pyaudio.paFloat32
SAMPLEFREQ = 44100
FRAMESIZE = 1024
NOFFRAMES = 220
p = pyaudio.PyAudio()
print('running')

stream = p.open(format=FORMAT,channels=1,rate=SAMPLEFREQ,input=True,frames_per_buffer=FRAMESIZE)
data = stream.read(NOFFRAMES*FRAMESIZE)
decoded = struct.unpack(str(NOFFRAMES*FRAMESIZE)+'f',data)

stream.stop_stream()
stream.close()
p.terminate()
print('done')
plt.plot(decoded)
plt.show()

1 个答案:

答案 0 :(得分:11)

尝试使用“numpy.fromstring”函数替换“struct.unpack”:

import numpy
stream = p.open(format=FORMAT,channels=1,rate=SAMPLEFREQ,input=True,frames_per_buffer=FRAMESIZE)
data = stream.read(NOFFRAMES*FRAMESIZE)
decoded = numpy.fromstring(data, 'Float32');

让我知道这是否适合您