从 pyaudio-stream 获取音频样本作为浮点数 [英] Get an audio sample as float number from pyaudio-stream
问题描述
由于我目前正准备构建一个基于 Raspberry Pi 的设备,用于测量用声卡记录的噪声(例如方差)中的一些东西,并尝试在 python 中执行此操作,我陷入了弄清楚如何获得音频样本作为浮点数以供进一步计算.
As I am currently about to build a device based on a Raspberry Pi for measuring some stuff from noise recorded with a sound card (e.g. variance), and trying to do this within python, I got stuck figuring out how to get a an audiosample as float-number for further calculations.
我做了什么:
使用 Line-In-to-chinch-Adapter 并接触插头以生成某种测试信号.
录制到例如 Audacity 或 Matlab 会显示合理的结果,例如
What did I do:
Took a Line-In-to-chinch-adapter and touching the plugs for generating some sort of test signal.
Recording to for example Audacity or Matlab shows plausible results, like
我想得到什么:
理想情况下,我想从声卡中获取例如 5 帧和 1024 个样本,并将它们转换为列表、元组或 numpy 数组作为浮点数以供进一步计算.
What I want to get:
In ideal, I want to get for example 5 frames á 1024 samples from the sound card, and convert them into a list, tuple or numpy array as a float number for further calculations.
当试图用 python/pyaudio 和本文末尾的代码实现这一点时,我得到了这样的东西:
When trying to achieve this with python/pyaudio with the code at the end of this post, I got something like this:
由于我使用 python 获得的值似乎与 Matlab(和其他)中的值相差大约两倍,我想我已经监督了某些事情或做错了什么.我想我在 struct.unpack 区域的某个地方犯了一个错误,但无法弄清楚究竟在哪里或为什么.我想请你帮忙,指出错误在哪里,我做错了什么.
Due to the fact that the values I got with python seem to differ from them in Matlab (and others) by the factor of about two, I think I've overseen something or did something wrong. I think I made a mistake somewhere at the struct.unpack region, but can't figure out where exactly or why. I'd like to ask you for help, pointing out where the error is and what I did wrong.
用于获取一些样本并绘制它们的小测试代码:
Little testcode for getting some samples and plotting them:
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()
推荐答案
尝试使用numpy.fromstring"函数替换struct.unpack":
Try use "numpy.fromstring" function to replace "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');
让我知道这是否适合您
这篇关于从 pyaudio-stream 获取音频样本作为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!