Pump up the size of our line buffer to get longer sounds to play less
skiptacularly and try going back to using line.drain since our sleep until we figure the sound will be done method wasn't working so well with the larger buffer. Hopefully drain has been fixed in the 4 years since it was commented out. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@590 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
@@ -31,7 +31,6 @@ import java.io.InputStream;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.sound.sampled.AudioFormat;
|
import javax.sound.sampled.AudioFormat;
|
||||||
@@ -84,7 +83,7 @@ public class SoundManager
|
|||||||
/**
|
/**
|
||||||
* Construct a new SoundType.
|
* Construct a new SoundType.
|
||||||
* Which should be a static variable stashed somewhere for the entire application to share.
|
* Which should be a static variable stashed somewhere for the entire application to share.
|
||||||
*
|
*
|
||||||
* @param strname a short string identifier, preferably without spaces.
|
* @param strname a short string identifier, preferably without spaces.
|
||||||
*/
|
*/
|
||||||
public SoundType (String strname)
|
public SoundType (String strname)
|
||||||
@@ -108,8 +107,8 @@ public class SoundManager
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Stop playing or looping the sound.
|
* Stop playing or looping the sound.
|
||||||
* At present, the granularity of this command is limited to the buffer size of the
|
* At present, the granularity of this command is limited to the buffer size of the
|
||||||
* line spooler, or about 8k of data. Thus, if playing an 11khz sample, it could take
|
* line spooler, or about 8k of data. Thus, if playing an 11khz sample, it could take
|
||||||
* 8/11ths of a second for the sound to actually stop playing.
|
* 8/11ths of a second for the sound to actually stop playing.
|
||||||
*/
|
*/
|
||||||
public void stop ();
|
public void stop ();
|
||||||
@@ -135,7 +134,7 @@ public class SoundManager
|
|||||||
*/
|
*/
|
||||||
public float getPan ();
|
public float getPan ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The default sound type. */
|
/** The default sound type. */
|
||||||
public static final SoundType DEFAULT = new SoundType("default");
|
public static final SoundType DEFAULT = new SoundType("default");
|
||||||
|
|
||||||
@@ -174,7 +173,7 @@ public class SoundManager
|
|||||||
_rmgr = rmgr;
|
_rmgr = rmgr;
|
||||||
_defaultClipBundle = defaultClipBundle;
|
_defaultClipBundle = defaultClipBundle;
|
||||||
_defaultClipPath = defaultClipPath;
|
_defaultClipPath = defaultClipPath;
|
||||||
_clipCache =
|
_clipCache =
|
||||||
new LRUHashMap<SoundKey, byte[][]>(cacheSize, new LRUHashMap.ItemSizer<byte[][]>() {
|
new LRUHashMap<SoundKey, byte[][]>(cacheSize, new LRUHashMap.ItemSizer<byte[][]>() {
|
||||||
public int computeSize (byte[][] value) {
|
public int computeSize (byte[][] value) {
|
||||||
int total = 0;
|
int total = 0;
|
||||||
@@ -213,11 +212,11 @@ public class SoundManager
|
|||||||
buf.append("clipVol=").append(_clipVol);
|
buf.append("clipVol=").append(_clipVol);
|
||||||
buf.append(", disabled=[");
|
buf.append(", disabled=[");
|
||||||
int ii = 0;
|
int ii = 0;
|
||||||
for (Iterator<SoundType> iter = _disabledTypes.iterator(); iter.hasNext(); ) {
|
for (SoundType soundType : _disabledTypes) {
|
||||||
if (ii++ > 0) {
|
if (ii++ > 0) {
|
||||||
buf.append(", ");
|
buf.append(", ");
|
||||||
}
|
}
|
||||||
buf.append(iter.next());
|
buf.append(soundType);
|
||||||
}
|
}
|
||||||
return buf.append("]").toString();
|
return buf.append("]").toString();
|
||||||
}
|
}
|
||||||
@@ -526,7 +525,7 @@ public class SoundManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On a spooling thread,
|
* On a spooling thread,
|
||||||
*/
|
*/
|
||||||
protected void playSound (SoundKey key)
|
protected void playSound (SoundKey key)
|
||||||
{
|
{
|
||||||
@@ -565,7 +564,6 @@ public class SoundManager
|
|||||||
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;
|
int totalRead = 0;
|
||||||
@@ -600,7 +598,7 @@ public class SoundManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (key.cmd == LOOP) {
|
if (key.cmd == LOOP) {
|
||||||
// if we're going to loop, reset the stream to the beginning if we can,
|
// if we're going to loop, reset the stream to the beginning if we can,
|
||||||
// otherwise just remake the stream
|
// otherwise just remake the stream
|
||||||
if (stream.markSupported()) {
|
if (stream.markSupported()) {
|
||||||
stream.reset();
|
stream.reset();
|
||||||
@@ -610,32 +608,7 @@ public class SoundManager
|
|||||||
}
|
}
|
||||||
} while (key.cmd == LOOP && key.running);
|
} while (key.cmd == LOOP && key.running);
|
||||||
|
|
||||||
// sleep the drain time. We never trust line.drain() because
|
line.drain();
|
||||||
// it is buggy and locks up on natively multithreaded systems
|
|
||||||
// (linux, winXP with HT).
|
|
||||||
float sampleRate = format.getSampleRate();
|
|
||||||
if (sampleRate == AudioSystem.NOT_SPECIFIED) {
|
|
||||||
sampleRate = 11025; // most of our sounds are
|
|
||||||
}
|
|
||||||
int sampleSize = format.getSampleSizeInBits();
|
|
||||||
if (sampleSize == AudioSystem.NOT_SPECIFIED) {
|
|
||||||
sampleSize = 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
int drainTime = (int) Math.ceil((totalRead * 8 * 1000) / (sampleRate * sampleSize));
|
|
||||||
|
|
||||||
// subtract out time we've already spent doing things.
|
|
||||||
drainTime -= System.currentTimeMillis() - startTime;
|
|
||||||
|
|
||||||
drainTime = Math.max(0, drainTime);
|
|
||||||
|
|
||||||
// add in a fudge factor of half a second
|
|
||||||
drainTime += 500;
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(drainTime);
|
|
||||||
} catch (InterruptedException ie) { }
|
|
||||||
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
log.warning("Error loading sound file [key=" + key + ", e=" + ioe + "].");
|
log.warning("Error loading sound file [key=" + key + ", e=" + ioe + "].");
|
||||||
|
|
||||||
@@ -647,7 +620,7 @@ public class SoundManager
|
|||||||
if (_soundSeemsToWork) {
|
if (_soundSeemsToWork) {
|
||||||
log.warning(err);
|
log.warning(err);
|
||||||
} else {
|
} else {
|
||||||
// this error comes every goddamned time we play a sound on someone with a
|
// this error comes every goddamned time we play a sound on someone with a
|
||||||
// misconfigured sound card, so let's just keep it to ourselves
|
// misconfigured sound card, so let's just keep it to ourselves
|
||||||
log.debug(err);
|
log.debug(err);
|
||||||
}
|
}
|
||||||
@@ -697,7 +670,7 @@ public class SoundManager
|
|||||||
data = new byte[1][];
|
data = new byte[1][];
|
||||||
data[0] = IOUtils.toByteArray(stream);
|
data[0] = IOUtils.toByteArray(stream);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// otherwise, randomize between all available sounds
|
// otherwise, randomize between all available sounds
|
||||||
Config c = getConfig(key);
|
Config c = getConfig(key);
|
||||||
String[] names = c.getValue(key.key, (String[])null);
|
String[] names = c.getValue(key.key, (String[])null);
|
||||||
@@ -787,7 +760,7 @@ public class SoundManager
|
|||||||
} catch (FileNotFoundException fnfe2) {
|
} catch (FileNotFoundException fnfe2) {
|
||||||
// only play the default sound if we have verbose sound debugging turned on.
|
// only play the default sound if we have verbose sound debugging turned on.
|
||||||
if (_verbose.getValue()) {
|
if (_verbose.getValue()) {
|
||||||
log.warning("Could not locate sound data [bundle=" + bundle +
|
log.warning("Could not locate sound data [bundle=" + bundle +
|
||||||
", path=" + path + "].");
|
", path=" + path + "].");
|
||||||
if (_defaultClipPath != null) {
|
if (_defaultClipPath != null) {
|
||||||
try {
|
try {
|
||||||
@@ -844,7 +817,7 @@ public class SoundManager
|
|||||||
// protected static void adjustVolumeIdeally (Line line, float volume)
|
// protected static void adjustVolumeIdeally (Line line, float volume)
|
||||||
// {
|
// {
|
||||||
// if (line.isControlSupported(FloatControl.Type.VOLUME)) {
|
// if (line.isControlSupported(FloatControl.Type.VOLUME)) {
|
||||||
// FloatControl vol = (FloatControl)
|
// FloatControl vol = (FloatControl)
|
||||||
// line.getControl(FloatControl.Type.VOLUME);
|
// line.getControl(FloatControl.Type.VOLUME);
|
||||||
//
|
//
|
||||||
// float min = vol.getMinimum();
|
// float min = vol.getMinimum();
|
||||||
@@ -1072,7 +1045,7 @@ public class SoundManager
|
|||||||
protected static final long MAX_SOUND_DELAY = 400L;
|
protected static final long MAX_SOUND_DELAY = 400L;
|
||||||
|
|
||||||
/** The size of the line's buffer. */
|
/** The size of the line's buffer. */
|
||||||
protected static final int LINEBUF_SIZE = 16 * 1024;
|
protected static final int LINEBUF_SIZE = 64 * 1024;
|
||||||
|
|
||||||
/** The maximum time a spooler will wait for a stream before deciding to shut down. */
|
/** The maximum time a spooler will wait for a stream before deciding to shut down. */
|
||||||
protected static final long MAX_WAIT_TIME = 30000L;
|
protected static final long MAX_WAIT_TIME = 30000L;
|
||||||
|
|||||||
Reference in New Issue
Block a user