3

I've implemented the Reading wav files in Java in my project (android game) and it works fine. Now I need to break down the wav file reading into pieces and I don't know what am I doing wrong. The code runs, and it reads the first part (out of 5), but when trying to read the following parts, the FileInputStream returns -1 and I don't get the logic behind it.

Here's what I have so far:

private List<Float> extractBeats(FileHandle fileHandle) throws Wave.WavFileException, IOException {
    List<Float> peaksAppended;
    peaksAppended = new ArrayList<Float>();
    
    final FileHandle fileHandleFinal = fileHandle;
    for (int i = 0; i < GameSettings.numThreadsAnalyzing; i++) {
        final int I = i;

        AsyncTask partAnalyzing = new AsyncTask() {
            @Override
            public List<Float> call() throws Wave.WavFileException, IOException {
                final File file = fileHandleFinal.file();
                Wave.WavFile wavFile = Wave.WavFile.openWavFile(file);

                // Get the number of audio channels in the wav file
                final int NUM_CHANNELS = wavFile.getNumChannels();
                final int NUM_FRAMES = 1000;

                final long totalNumFrames = wavFile.getNumFrames();
                final long auxOffset = totalNumFrames / GameSettings.numThreadsAnalyzing;
                int offset = (int) auxOffset * I;

                if (offset>0){
                    wavFile.skipFrames(offset);
                }

                List<Float> peaks;
                double[] buffer = new double[NUM_FRAMES * NUM_CHANNELS];

                int framesToRead = NUM_FRAMES;
                double min = 10;
                double max = -10;

                // =========================================
                // Read file and find out MIN and MAX values
                // =========================================
                do {
                    // Read frames into buffer
                    framesToRead = wavFile.readFrames(buffer, offset, (int) auxOffset, framesToRead);

                    // Loop through frames and look for minimum and maximum value
                    for (int s = 0; s < framesToRead * NUM_CHANNELS; s++) {
                        if (buffer[s] > max) max = buffer[s];
                        if (buffer[s] < min) min = buffer[s];
                    }

                    System.out.println("Buffer_read : " + max + " and min " + min);
                }
                while (framesToRead != 0);

                // Close the wavFile
                wavFile.close();

                [. . .]//do some other beats extraction stuff

                return peaks;
            }
        };

        AsyncExecutor partAnalyzer = new AsyncExecutor(30);
        AsyncResult result = partAnalyzer.submit(partAnalyzing);
        
        [. . .]//do some more other beats extraction stuff
    }
}

This works fine for the first piece of the song. It reads the first 633000 frames. Now I want it to read the next 633000 frames, but it get stuck into the readSample method.

Here's the sequence running from readFrames method.

public int readFrames(double[] sampleBuffer, int offset, int partSize, int numFramesToRead) throws IOException, WavFileException {
        if (ioState != IOState.READING)
            throw new IOException("Cannot read from WavFile instance");

        for (int f = 0; f < numFramesToRead; f++) {
            if (frameCounter == offset + partSize)
                return f;

            for (int c = 0; c < numChannels; c++) {
                sampleBuffer[offset] = floatOffset + (double) readSample() / floatScale;
                offset++;
            }

            frameCounter++;
        }
        return numFramesToRead;
    }

private long readSample() throws IOException, WavFileException {
        long val = 0;

        for (int b = 0; b < bytesPerSample; b++) {
            if (bufferPointer == bytesRead) {
                int read = iStream.read(buffer, 0, BUFFER_SIZE);
                if (read == -1) throw new WavFileException("Not enough data available");
                bytesRead = read;
                bufferPointer = 0;
            }

            int v = buffer[bufferPointer];
            if (b < bytesPerSample - 1 || bytesPerSample == 1) v &= 0xFF;
            val += v << (b * 8);

            bufferPointer++;
        }

        return val;
    }

I tried to use the offset and pass it to the FileInputStream (iStream) but didn't work either. Then, I created the method skipFrames with the following code but also didn't help.

public void skipFrames(int offset){
    frameCounter = offset;
}

If solved, I can update the topic with a basic functionallity of the reading wav in pieces, great approach for sound analysis (which it's what I am doing). Any help would be greatly appreciated.

Community
  • 1
  • 1

0 Answers0