pyaudio中的数据中的数据是什么格式和数据类型?
我正在尝试将Pyaudio用于实时音频应用程序。我编写的作品的程序,但我正在尝试优化速度。此外,我遇到的主要问题是我正在尝试处理stream.read()数据。在执行此操作的一般方法中,数据存储在一个名为“帧”的数组中,您只需将每个块附加到数组。然后将此数组处理到.WAV文件中。我想将其翻译成具有其数字价值的含义的整数或浮点,但我不知道这样做的转换。为了更清楚,我使用的代码将帧数组转换为.wav文件。然后,我使用声音弹来将.WAV文件数据重新转换为点。然后,我将这些点用于频率处理,并且效果很好。我想切断需要转换为WAV并从WAV返回以获取可用数据的需要。我相信数据值以字节格式为单位,但代表16位整数,但是当我除以2^16时,我没有得到正确的值。对此有些确认会有所帮助。这与我没有频率处理的事情类似
import pyaudio
import wave
filename = "recorded.wav"
chunk = 1024
FORMAT = pyaudio.paInt16
channels = 1
sample_rate = 44100
record_seconds = 5
p = pyaudio.PyAudio()
# open stream object as input & output
stream = p.open(format=FORMAT,
channels=channels,
rate=sample_rate,
input=True,
output=True,
frames_per_buffer=chunk)
frames = []
print("Recording...")
for i in range(int(sample_rate / chunk * record_seconds)):
#what is this value?????????
data = stream.read(chunk)
frames.append(data)
print("Finished recording.")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(filename, "wb")
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(sample_rate)
wf.writeframes(b"".join(frames))
wf.close()
I am trying to use pyaudio for a real time audio application. The program that I wrote works, but I am trying to optimize the speed. Additionally, the main issue I have is that I am trying to process the stream.read() data. In the general method for doing this, the data is stored into an array called frames where you just append each chunk to the array. This array is then processed into a .wav file. I want to translate this over into integers or float that have meaning in terms of their digital value but I do not know the conversion to do so. To make this clearer, the code that I use translates the frames array into a .wav file. Then, I use soundfile to translate the .wav file data back into points. These points I then run for frequency processing and it works well.I want to cut out needing to convert to a wav and back from a wav to get usable data. I believe that the data values are in byte format but represent a 16 bit integer, but when I divide by 2^16 I do not get the correct value. Some confirmation on this would be helpful. Here is something similar to what i'm doing without the frequency processing
import pyaudio
import wave
filename = "recorded.wav"
chunk = 1024
FORMAT = pyaudio.paInt16
channels = 1
sample_rate = 44100
record_seconds = 5
p = pyaudio.PyAudio()
# open stream object as input & output
stream = p.open(format=FORMAT,
channels=channels,
rate=sample_rate,
input=True,
output=True,
frames_per_buffer=chunk)
frames = []
print("Recording...")
for i in range(int(sample_rate / chunk * record_seconds)):
#what is this value?????????
data = stream.read(chunk)
frames.append(data)
print("Finished recording.")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(filename, "wb")
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(sample_rate)
wf.writeframes(b"".join(frames))
wf.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其转换为整数列表,
它以字符字符串获取数据,您可以使用结果
我只知道字符的顺序使用了哪个字符串中的16位整数 -
big
或little
(big-endian
或Little-endian
)编辑:
您还可以使用标准模块 struct 。
h
表示16位unsigned整数
和>
表示big-endian
或使用迭代器(因此您可以运行它使用每个
块
,而不是使用完整data
)编辑:
您还可以使用标准模块 array
如果您使用
numpy
,则可以使用 numpy.frombuffer ,但这使用
little-endian
order。对于
big-endian
您可以使用dtype ='> u2'
或
It gets data as bytes string and you can convert it to list of integers using
Result
I only don't know what order of bytes uses 16bit integer in this string -
big
orlittle
(big-endian
orlittle-endian
)EDIT:
You can also use standard module struct for this.
H
means16 bit unsigned integer
and>
meansbig-endian
or using iterator (so you could run it with every
chunk
instead of using fulldata
)EDIT:
You can also use standard module array
If you use
numpy
then you can use numpy.frombufferbut this uses
little-endian
order.For
big-endian
you can usedtype='>u2'
or