# HG changeset patch # User Matti Hamalainen # Date 1299184178 -7200 # Node ID dd896bc7352b2563c46fd3d439943e6542854b5a # Parent 41c6cca69d6023aa79bba7ee596ca5908621273b Fix sound streaming code to cope with partial buffer reads. diff -r 41c6cca69d60 -r dd896bc7352b game/SoundManager.java --- a/game/SoundManager.java Thu Mar 03 21:21:35 2011 +0200 +++ b/game/SoundManager.java Thu Mar 03 22:29:38 2011 +0200 @@ -245,7 +245,7 @@ // use a short, 100ms (1/10th sec) buffer for filters that // change in real-time - int bufferSize = playbackFormat.getFrameSize() * Math.round(playbackFormat.getSampleRate() / 10); + int bufferSize = playbackFormat.getFrameSize() * Math.round(playbackFormat.getSampleRate() / 20); // create, open, and start the line SourceDataLine line; @@ -310,17 +310,14 @@ // get line and buffer from ThreadLocals SourceDataLine line = (SourceDataLine) localLine.get(); byte[] buffer = (byte[])localBuffer.get(); - + if (line == null || buffer == null) return; - - System.out.print("SMGR.SoundPlayer.run()\n"); // copy data to the line try { - int numBytesRead = 0; - - while (numBytesRead != -1) { + boolean playing = true; + while (playing) { // if paused, wait until unpaused synchronized (pausedLock) { @@ -335,9 +332,17 @@ } // copy data - numBytesRead = source.read(buffer, 0, buffer.length); - if (numBytesRead != -1) - line.write(buffer, 0, numBytesRead); + int bufPos = 0; + while (bufPos < buffer.length && playing) + { + int res = source.read(buffer, bufPos, buffer.length - bufPos); + if (res != -1) + bufPos += res; + else + playing = false; + } + if (playing) + line.write(buffer, 0, bufPos); } }