我有通过简单的pyAudio录音获得的数据:
"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
我需要在numpy处理这些(并且不要转换回来)。因此,我需要将pyaudio.paInt16(或24等等)转换为更易于管理的内容,如float或double。
有简单的方法吗?
答案 0 :(得分:0)
我认为最简单的方法是使用sounddevice模块(免责声明:我有偏见,因为我是作者):
import sounddevice as sd
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
myrecording = sd.rec(int(RECORD_SECONDS * RATE), samplerate=RATE,
channels=CHANNELS, blocking=True, dtype='float64')
默认情况下,这会使用float32
,但您可以更改dtype
,如上所示。
或者,如果您确实想将16位值转换为浮点值,请查看my tutorial about this topic。