Mercurial > hg > ristipolku
changeset 107:dd896bc7352b
Fix sound streaming code to cope with partial buffer reads.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Mar 2011 22:29:38 +0200 |
parents | 41c6cca69d60 |
children | ae66d21d4747 |
files | game/SoundManager.java |
diffstat | 1 files changed, 15 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- 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); } }