Resolving 'ValueError: Unexpected end of file.' from scipy/wavfile/read

爱⌒轻易说出口 提交于 2019-12-11 07:27:52

问题


I am splitting a stereo wav file in Java using iterative methods with the Java WavFile class. When I use a python to read the resulting mono wav file and out put the sample rate and data using the read method from scipy.io.wavfile I get an error:

'Value Error: Unexpected end of File.'

I do not get this error with other mono wav files I try, only those I have made with Java. Looking at other forum posts I know that it must have to do with how I am setting up the header information for the files, however, after trying altercations and reading API docs to ensure that my usage is correct, I am at a loss.

My Java Code

try {
            WavFile srcFile = WavFile.openWavFile(this.audioFile);

            long numFrames = srcFile.getNumFrames();
            long sampleRate = srcFile.getSampleRate(); 
            int channels = 2;
            int bitRate = 16;


            File[] files = new File[channels];
            WavFile[] wavFiles = new WavFile[channels];

            for (int i = 1; i < channels + 1; i++) {
                //Populates File Array
                files[i - 1] = new File("output" + i + ".WAV");
                wavFiles[i - 1] = WavFile.newWavFile(files[i - 1], 1, numFrames, bitRate, sampleRate);
            }

            int bufferLength = Math.toIntExact(numFrames);

            int[][] buffer = new int[channels][bufferLength];

            long frameCounter = 0;

            while (frameCounter < numFrames){
                srcFile.readFrames(buffer,bufferLength);

                for (int j = 0; j<channels;j++){
                    wavFiles[j].writeFrames(buffer[j],bufferLength);
                }

                frameCounter += bufferLength;
            }

        } catch (Exception ex) {
            System.out.println("WavFile failed to open source file: " + ex);
        }

The simple python code that extracts data from the wav file

import scipy.io.wavfile as wav

fs, data = wav.read(file)

wav.close()

From my understanding the python code should have no problem reading the wav file no matter its source.


回答1:


Your Java code never calls the close() method for wavFiles[j]. According to http://www.labbookpages.co.uk/audio/javaWavFiles.html, the close() method "must be called once all samples have been written using the writeFrames methods. If not called, the wav file may be incomplete."




回答2:


It seems that you got incorrect data in the head of wav file. I'm pretty sure that the number of frames in the file is smaller than the actual one.

You can either try to find the source of the error in the Java code, or use a Python library that is more resilient to header errors, such as the built-in library wave:

>>> import wave
>>> track = wave.open('output1.WAV', 'r')
>>> track.getnframes()
3417927
>>> track.readframes(10)
b'\x00\x00\x00\x00\xff\xff\x01\x00\x01\x00\xfe\xff\x01\x00\x00\x00\xff\xff\x01\x00'

And don't forget to do track.close() or to use a with statement to avoid memory leaks.



来源:https://stackoverflow.com/questions/55387983/resolving-valueerror-unexpected-end-of-file-from-scipy-wavfile-read

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!