public int writeFrames(long[] sampleBuffer, int offset, int numFramesToWrite) throws IOException { if (ioState != IOState.WRITING) { throw new IOException("Cannot write to WavFile instance"); } for (int f = 0; f < numFramesToWrite; f++) { if (frameCounter == numFrames) { return f; } for (int c = 0; c < numChannels; c++) { writeSample(sampleBuffer[offset]); offset++; } frameCounter++; } return numFramesToWrite; }