Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - How to fix 'Incomplete wav chunk' error when reading wav with scipy

My problem

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.

What I've tried

  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.

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

  3. 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.

  4. 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.

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)

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: enter image description here

Codec of the BAD file: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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>]

plot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...