def save_audio_file(filename, quantized_signal, quantization_steps=256, format="16bit_pcm", sampling_rate=48000):
quantized_signal = quantized_signal.astype(float)
normalized_signal = (quantized_signal / quantization_steps - 0.5) * 2.0
# inv mu-law companding transformation (ITU-T, 1988)
mu = quantization_steps - 1
signals_1d = np.sign(normalized_signal) * ((1 + mu) ** np.absolute(normalized_signal)) / mu
if format == "16bit_pcm":
max = 1<<15
type = np.int16
elif format == "32bit_pcm":
max = 1<<31
type = np.int32
elif format == "8bit_pcm":
max = 1<<8 - 1
type = np.uint8
signals_1d *= max
audio = signals_1d.reshape((-1, 1)).astype(type)
audio = np.repeat(audio, 2, axis=1)
wavfile.write(filename, sampling_rate, audio)
# convert signal to 1xW image
评论列表
文章目录