解决“ValueError:意外的文件结尾."来自 scipy/wavfile/read [英] Resolving 'ValueError: Unexpected end of file.' from scipy/wavfile/read

查看:37
本文介绍了解决“ValueError:意外的文件结尾."来自 scipy/wavfile/read的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java WavFile 类的迭代方法在 Java 中拆分立体声 wav 文件.当我使用 python 读取生成的单声道 wav 文件并使用 scipy.io.wavfile 中的读取方法输出采样率和数据时,出现错误:

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

我尝试使用其他单声道 wav 文件时没有遇到此错误,只有我使用 Java 创建的那些文件.查看其他论坛帖子,我知道这一定与我如何设置文件的标题信息有关,但是,在尝试争吵并阅读 API 文档以确保我的使用正确后,我不知所措.

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.

我的 Java 代码

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);
        }

从wav文件中提取数据的简单python代码

The simple python code that extracts data from the wav file

import scipy.io.wavfile as wav

fs, data = wav.read(file)

wav.close()

根据我的理解,无论来源如何,python 代码读取 wav 文件都应该没有问题.

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

推荐答案

您的 Java 代码从不调用 wavFiles[j]close() 方法.根据 http://www.labbookpages.co.uk/audio/javaWavFiles.htmlclose() 方法必须在使用 writeFrames 方法写入所有样本后调用.如果不调用,wav 文件可能不完整."

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

这篇关于解决“ValueError:意外的文件结尾."来自 scipy/wavfile/read的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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