scipy.io:不能写wavfile

问题描述:

我有一个问题写一个2D numpy的数组作为波形文件(音频)scipy.io:不能写wavfile

按照DOC我应该写一个2D INT16 numpy的阵列

https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.io.wavfile.write.html

16-bit PCM -32768 +32767 int16 

正如我在FLOAT32格式范围(-1,1)numpy的阵列I首先将它转换为16位诠释

stereoAudio = ((stereoAudio * bits16max)).astype('int16') 

print "argmax : " + str(np.amax(stereoAudio)) 
print "argmin : " + str(np.amin(stereoAudio)) 

outWaveFileName = "out/file.wav" 
print "writing " + outWaveFileName 
wavfile.write(outWaveFileName,44100,stereoAudio) 

我得到以下输出:

argmax : 4389 
argmin : -4381 
writing out/file.wav 
Traceback (most recent call last): 
    File "/Users/me/file.py", line 132, in <module> 
wavfile.write(outWaveFileName,44100,stereoAudio) 
    File "//anaconda/lib/python2.7/site-packages/scipy/io/wavfile.py", line 353, in write 
    bytes_per_second, block_align, bit_depth) 
error: ushort format requires 0 <= number <= USHRT_MAX 

我的值是16位和-4391之间4389格式化应该没问题。但我的数据看起来解释为ushort

+0

您问题中的链接已死亡 –

函数scipy.io.wavfile预计输入数组的形状为(num_samples, num_channels)。我怀疑你的阵列有形状(num_channels, num_samples)。然后write会尝试将num_samples置于结构中的16位字段中,该结构会写入WAV文件,但num_samples的值对于16位值而言太大。 (请注意,如果num_samples是足够小,你不会得到一个错误,但文件不会有正确的格式。)

速战速决是写你的数组的转置:

wavfile.write(outWaveFileName, 44100, stereoAudio.T) 

例如,下面是一些演示错误的代码; xy具有形状(2,40000):

In [12]: x = (2*np.random.rand(2, 40000) - 1).astype(np.float32) 

In [13]: y = (x*32767).astype('int16') 

In [14]: from scipy.io import wavfile 

In [15]: wavfile.write('foo.wav', 44100, y) 
--------------------------------------------------------------------------- 
error          Traceback (most recent call last) 
<ipython-input-15-36b8cd0e729c> in <module>() 
----> 1 wavfile.write('foo.wav', 44100, y) 

/Users/warren/anaconda/lib/python2.7/site-packages/scipy/io/wavfile.pyc in write(filename, rate, data) 
    351 
    352   fmt_chunk_data = struct.pack('<HHIIHH', format_tag, channels, fs, 
--> 353          bytes_per_second, block_align, bit_depth) 
    354   if not (dkind == 'i' or dkind == 'u'): 
    355    # add cbSize field for non-PCM files 

error: ushort format requires 0 <= number <= USHRT_MAX 

移调阵列所以输入到wavfile.write具有预期的形状:

In [16]: wavfile.write('foo.wav', 44100, y.T) 

回读数据,以验证它的工作如预期:

In [22]: fs, z = wavfile.read('foo.wav') 

In [23]: np.allclose(z, y.T) 
Out[23]: True 
+0

谢谢,您是对的,我的数组是(num_channels,num_samples) –