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:
@@ -524,8 +524,10 @@ public class SoundManager
|
|||||||
float setPan = PAN_CENTER;
|
float setPan = PAN_CENTER;
|
||||||
line.start();
|
line.start();
|
||||||
_soundSeemsToWork = true;
|
_soundSeemsToWork = true;
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
byte[] buffer = new byte[LINEBUF_SIZE];
|
byte[] buffer = new byte[LINEBUF_SIZE];
|
||||||
|
int totalRead = 0;
|
||||||
do {
|
do {
|
||||||
// play the sound
|
// play the sound
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@@ -542,6 +544,8 @@ public class SoundManager
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
count = stream.read(buffer, 0, buffer.length);
|
count = stream.read(buffer, 0, buffer.length);
|
||||||
|
totalRead += count; // The final -1 will make us slightly off, but that's ok
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// this shouldn't ever ever happen because the stream
|
// this shouldn't ever ever happen because the stream
|
||||||
// we're given is from a reliable source
|
// we're given is from a reliable source
|
||||||
@@ -571,11 +575,15 @@ public class SoundManager
|
|||||||
if (sampleSize == AudioSystem.NOT_SPECIFIED) {
|
if (sampleSize == AudioSystem.NOT_SPECIFIED) {
|
||||||
sampleSize = 16;
|
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
|
// add in a fudge factor of half a second
|
||||||
drainTime += 500;
|
drainTime += 500;
|
||||||
|
|
||||||
|
// subtract out time we've already spent doing things.
|
||||||
|
drainTime -= (System.currentTimeMillis() - startTime) / 1000;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(drainTime);
|
Thread.sleep(drainTime);
|
||||||
} catch (InterruptedException ie) { }
|
} catch (InterruptedException ie) { }
|
||||||
|
|||||||
Reference in New Issue
Block a user