使用scipy读取wav时如何修复'不完整的wav块'错误 [英] How to fix 'Incomplete wav chunk' error when reading wav with scipy

查看:1282
本文介绍了使用scipy读取wav时如何修复'不完整的wav块'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试采用一个(机器学习)模型,该模型吸收一个音频文件(.wav)并从中预测情绪(多标签分类).
我正在尝试从文件中读取采样率和信号,但是当从scipy.io.wavfile调用read(filename)时,我得到的是ValueError: Incomplete wav chunk.

I'm trying to fit a (machine-learning) model that takes in an audiofile (.wav) and predicts the emotion from it (multi-label classification).
I'm trying to read the sample rate and signal from the file, but when calling read(filename) from scipy.io.wavfile, I'm getting ValueError: Incomplete wav chunk.

  1. 我已经尝试从scipy.read()切换到librosa.read().
    它们同时输出信号和采样率,但是由于某些原因,librosascipy花费的时间要长得多,因此对于我的任务来说是不切实际的.

  1. I've tried switching from scipy.read() to librosa.read().
    They both output the signal and sample rate, but for some reason librosa takes exponentially longer time than scipy, and is impractical for my task.

我已按照建议sr, y = scipi.io.wavfile.read(open(filename, 'r')) comment19901173_14321627>此处,无济于事.

I've tried sr, y = scipi.io.wavfile.read(open(filename, 'r')) as suggested here, to no avail.

我尝试查看我的文件并检查可能导致它的原因:
在所有2084个wav文件中,有1057个很好(scipy设法读取了它们),并且 1027不好(提出错误).
我似乎找不到任何指向文件通过或失败的原因,但这仍然是一个奇怪的结果,因为所有文件均来自同一

I've tried looking into my files and checking what might cause it:
Out of all 2084 wav files, 1057 were good (=scipy managed to read them), and 1027 were bad (=raised the error).
I couldn't seem to find any thing pointing as to what makes a file pass or fail, but nonetheless it's a weird result, as all files are taken from the same dataset from the same origin.

我听说有人说我可以使用某些软件将文件重新导出为wav,并且应该可以.
我之所以没有这样做,是因为a)我没有任何音频处理软件,似乎有些矫kill过正; b)我想了解实际的问题,而不是在上面施加创可贴.

I've heard people saying I could just re-export the files as wav using some software, and it should work.
I didn't try this because a) I don't have any audio-processing software and it seems like an overkill, and b) I want to understand the actual problem rather than put a bandaid on it.

最小的,可复制的示例

假定filenames是我所有音频文件的子集,包含 fn_good fn_bad ,其中fn_good是要处理的实际文件,而fn_bad是实际的文件引发错误的文件.

Minimal, reproducible example

Assume filenames is a subset of all my audio files, containing fn_good and fn_bad, where fn_good is an actual file that gets processed, and fn_bad is an actual file that raises an error.

def extract_features(filenames):
    for fn in filenames:
        sr, y = scipy.io.wavfile.read(fn)
        print('Signal is: ', y)
        print('Sample rate is: ', sr)

其他信息

使用VLC,似乎scipy.io.wavfile支持编解码器,但是在两种情况下,两个文件都具有相同的编解码器,因此很奇怪,它们没有相同的效果... GOOD文件的编解码器:

Additional info

Using VLC, it seems that the codecs are supported by scipy.io.wavfile, but in either case, both files have the same codec, so it's weird they don't have the same effect... Codec of the GOOD file:

编解码器:

推荐答案

我不知道为什么scipy.io.wavfile无法读取文件-那里可能有一个无效的块,其他读者只是忽略了该块.请注意,即使我使用scipy.io.wavfile读取好"文件,也会生成警告(WavFileWarning: Chunk (non-data) not understood, skipping it.):

I don't know why scipy.io.wavfile can't read the file--there might be an invalid chunk in there that other readers simply ignore. Note that even when I read a "good" file with scipy.io.wavfile, a warning (WavFileWarning: Chunk (non-data) not understood, skipping it.) is generated:

In [22]: rate, data = wavfile.read('fearful_song_strong_dogs_act10_f_1.wav')                              
/Users/warren/mc37/lib/python3.7/site-packages/scipy/io/wavfile.py:273: WavFileWarning: Chunk (non-data) not understood, skipping it.
  WavFileWarning)

我可以使用 wavio (在github上的源代码: wavio ),这是我创建的一个程序包,其中包装了Python的标准wave库,并带有可理解NumPy数组的函数:

I can read 'fearful_song_strong_dogs_act06_f_0.wav' using wavio (source code on github: wavio), a package I created that wraps Python's standard wave library with functions that understand NumPy arrays:

In [13]: import wavio                                                                                     

In [14]: wav = wavio.read('fearful_song_strong_dogs_act06_f_0.wav')                                       

In [15]: wav                                                                                              
Out[15]: Wav(data.shape=(198598, 1), data.dtype=int16, rate=48000, sampwidth=2)

In [16]: plot(np.arange(wav.data.shape[0])/wav.rate, wav.data[:,0])                                       
Out[16]: [<matplotlib.lines.Line2D at 0x117cd9390>]

这篇关于使用scipy读取wav时如何修复'不完整的wav块'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆