我正在使用python处理音频信号。我想记录一个信号并保持其SPL级别。我正在使用pyaudio Library来做到这一点。这是我找到的例子。但是,我发现信号大约比它应该大18db。有没有办法改变文件wav中记录的信号幅度?第二个问题是对于wav文件,数据以带符号的16位编码。我的印象是负值是使用两个补码编码的,任何人都可以确认吗?
非常感谢!
enter code here def record(output_file_name, rec_device_id=None):
# Sampling = Data is recorded per batches of 1024 bits
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
# Sampling = 48kHz
RATE = 48000
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = output_file_name
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
input_device_index=rec_device_id)
# Sampling = 48kHz
print("*** recording start ***")
frames = []
rms=np.zeros(RATE / CHUNK * RECORD_SECONDS+1)
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
#print("*** data ***",data)
#decoded = struct.unpack('h',data)
print("*** decoded ***",decoded)
frames.append(data)
rms[i] = audioop.rms(data, 2)
print("*** recording done ***")
print("*** rms ***",rms)
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(output_file_name, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()