Problem reading wav file using Java

I use this API to read a wav file in Java: Read and write Wav files in Java

I read a wav file with Java and want to put all the values ​​in an arraylist. I wrote:

// Open the wav file specified as the first argument WavFile wavFile = WavFile.openWavFile(fileToRemoveSilence); List<Double> allBuffer = new ArrayList<Double>(); .... do { // Read frames into buffer framesRead = wavFile.readFrames(buffer, 50); for (double aBuffer : buffer) { allBuffer.add(aBuffer); } } while (framesRead != 0); 

When I check the size of allBuffer on the debugger, it says:

 74700 

However, in the code I'm using:

 wavFile.display(); 

its conclusion:

 .... ... Frames: 74613 .... 

My allbuffer is larger than the frames displayed by this API. How does this happen?

PS: My previous questions:

Reading a wav file in Java

How to split wav file for 1 second with Java?

PS2: I fixed the errors, but when I run the program, I sometimes get this error: what could be the problem?

 java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134) at sun.java2d.Disposer.run(Disposer.java:127) at java.lang.Thread.run(Thread.java:662) 

Link removal exception: java.lang.InterruptedException

+1
source share
2 answers

you need to consider frames. When copying from a buffer to your list. on the last call you copy more doubles than you actually read from the file. (i.e. just like the code example in your first link).

+1
source

I think you need something like this:

 // Open the wav file specified as the first argument WavFile wavFile = WavFile.openWavFile(fileToRemoveSilence); List<double[]> allBuffer = new List<double[]>(); int bufferSize = 44100; // 1 second, probably double[] buffer; .... do { // Read frames into buffer buffer = new double[bufferSize]; framesRead = wavFile.readFrames(buffer, bufferSize); if (framesRead == bufferSize) { allBuffer.add(buffer); } else { double[] crippleBuffer = new double[framesRead]; Array.Copy(buffer, crippleBuffer, framesRead); allBuffer.add(crippleBuffer); break; } } 

This will leave you with a List 1-second double[] pattern arrays (in fact, the last array will be slightly less than a full second). You can then iterate over this collection of arrays and turn them into a separate WAV file.

+2
source

All Articles