1

I use that API to read wav file at Java: Reading and Writing Wav Files in Java

I read a wav file with Java and want to put all the values into 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 at debugger it says:

74700

However at the code it whe I use:

wavFile.display();

the output of it:

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

My allbuffer is bigger than frames as displayed by that API. How it comes?

PS: My previous questions about this:

Reading wav file in Java

How to divide a wav file into 1 second pieces with Java?

PS2: I fixed the bugs however when I run the program I get that error sometimes: What may 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)

Exception while removing reference: java.lang.InterruptedException

Community
  • 1
  • 1
kamaci
  • 72,915
  • 69
  • 228
  • 366
  • 1
    Maybe this will help some: http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html – MusiGenesis May 13 '11 at 00:32
  • Nice document and marking as a great comment. One thing more, that article says that reason is Thread.sleep() related. However there is no sleep() method at code. Is it calling from any other methods inside anything?(i.e. FileInputStream's read method or anything else?) – kamaci May 13 '11 at 07:12
  • it's probably something that the WavFile API is doing internally, unless *you're* making these calls in a multi-threaded manner. – MusiGenesis May 13 '11 at 17:41

2 Answers2

2

I think you want 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 of 1-second double[] arrays of samples (actually, the last array will be somewhat less than a full second). You can then iterate through this collection of arrays and turn each one into a separate WAV file.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
1

you need to account for framesRead when copying from the buffer into your list. on the last call, you are copying more doubles than you actually read from the file. (i.e. exactly like the example code in your first link).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • You are right. I changed wavFile.readFrames(buffer, 50); into wavFile.readFrames(buffer, 1); (and double[] buffer = new double[1];). However there is still problem new size of allBuffer is 74614 instead of 74613 and the last two element of arrayList is same? – kamaci May 12 '11 at 20:01
  • @kamaci, read my answer again. you don't need to change your buffer size, you just need to _use_ the framesRead result value when copying from buffer to allBuffer. look at the ReadExample.java code in your first link, notice how framesRead is being used when reading from buffer. – jtahlborn May 12 '11 at 20:05