From bf50fef84c03ee2252959416ca3b1bdb4c5e6245 Mon Sep 17 00:00:00 2001 From: Dave Hoover Date: Tue, 30 Oct 2007 22:54:46 +0000 Subject: [PATCH] When playing sounds, sleep for our estimated line reading time based on how long we think the sound will really take to play, not how long the last chunk of our buffer will take. With bumped up LINEBUF_SIZE, we'd sleep longer, but would actually START sleeping much sooner, so we would truncate sounds. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@321 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/java/com/threerings/media/sound/SoundManager.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/java/com/threerings/media/sound/SoundManager.java b/src/java/com/threerings/media/sound/SoundManager.java index d7d4b90e..747b8ee7 100644 --- a/src/java/com/threerings/media/sound/SoundManager.java +++ b/src/java/com/threerings/media/sound/SoundManager.java @@ -524,8 +524,10 @@ public class SoundManager float setPan = PAN_CENTER; line.start(); _soundSeemsToWork = true; + long startTime = System.currentTimeMillis(); byte[] buffer = new byte[LINEBUF_SIZE]; + int totalRead = 0; do { // play the sound int count = 0; @@ -542,6 +544,8 @@ public class SoundManager } try { count = stream.read(buffer, 0, buffer.length); + totalRead += count; // The final -1 will make us slightly off, but that's ok + } catch (IOException e) { // this shouldn't ever ever happen because the stream // we're given is from a reliable source @@ -571,11 +575,15 @@ public class SoundManager if (sampleSize == AudioSystem.NOT_SPECIFIED) { sampleSize = 16; } - int drainTime = (int) Math.ceil((LINEBUF_SIZE * 8 * 1000) / (sampleRate * sampleSize)); + + int drainTime = (int) Math.ceil((totalRead * 8 * 1000) / (sampleRate * sampleSize)); // add in a fudge factor of half a second drainTime += 500; + // subtract out time we've already spent doing things. + drainTime -= (System.currentTimeMillis() - startTime) / 1000; + try { Thread.sleep(drainTime); } catch (InterruptedException ie) { }