我用pyaudio用python从电脑上的麦克风录制了声音。当声音以16位整数作为数据类型记录时,它可以正常工作。但是,当它以32位浮点数作为数据类型记录时,它不起作用。
请查看以下代码。如果FORMAT设置为pyaudio.paInt16,则它可以按我的要求工作。但是,按如下所示将其设置为pyaudio.paFloat32时,它不起作用;
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paFloat32
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "path/to/output_file.wav"
p = pyaudio.PyAudio()
frames = []
stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK)
print("* recording")
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()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
非常感谢您的建议和帮助!
更新;
我已经测试了sounddevice,下面是代码;
import sounddevice as sd
import wave
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "path/to/output.wav"
myrecording = sd.rec(int(RECORD_SECONDS * RATE), samplerate=RATE,
channels=CHANNELS, blocking=True, dtype='int16')
print(myrecording, 'myrecording')
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(2)
wf.setframerate(RATE)
wf.writeframes(myrecording)
wf.close()
结果与pyaudio相同。它在int16时有效,但当32位元作为dtype浮动时会产生噪音和声音失真。