我有一些原始的二进制int16数据,我正在使用以下代码将其转换为numpy数组:
audio = np.fromstring(raw_data, dtype=np.int16)
这些数据是音频数据。当我将数据转换为float32时,音频被失真了:
audio = audio.astype(np.float32, order='C')
我正在将音频保存到磁盘上,以便使用SoundFile听取它:
soundfile.write('out.wav', audio, sample_rate)
如果我直接将音频写入磁盘而不进行astype
操作,则不会发生失真(即);
# no distortion
audio = np.fromstring(raw_data, dtype=np.int16)
soundfile.write('out.wav', audio, sample_rate)
# distortion
audio = np.fromstring(raw_data, dtype=np.int16)
audio = audio.astype(np.float32, order='C')
soundfile.write('out.wav', audio, sample_rate)
这里应该采用什么正确的方法来转换数据类型呢?
s->scale = 1. / (1 << (avctx->bits_per_coded_sample - 1));
2)声音文件测试:numpy.array_equal(soundfile.read('file.wav', dtype='int16')[0] / 32768, soundfile.read('file.wav', dtype='float32')[0])
。 - bartolo-otrit