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
This commit is contained in:
Dave Hoover
2007-10-30 22:54:46 +00:00
parent bce057c307
commit bf50fef84c
@@ -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) { }