Little bit of widening, type safety, and flagging overrides.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@306 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Dave Hoover
2007-10-17 23:34:32 +00:00
parent 8e7997a061
commit 1ebd28a450
6 changed files with 87 additions and 127 deletions
@@ -39,7 +39,7 @@ import javax.sound.midi.Synthesizer;
public class MidiPlayer extends MusicPlayer
implements MetaEventListener
{
// documentation inherited
@Override // documentation inherited
public void init ()
throws Exception
{
@@ -50,13 +50,13 @@ public class MidiPlayer extends MusicPlayer
}
}
// documentation inherited
@Override // documentation inherited
public void shutdown ()
{
_sequencer.close();
}
// documentation inherited
@Override // documentation inherited
public void start (InputStream stream)
throws Exception
{
@@ -65,14 +65,14 @@ public class MidiPlayer extends MusicPlayer
_sequencer.addMetaEventListener(this);
}
// documentation inherited
@Override // documentation inherited
public void stop ()
{
_sequencer.removeMetaEventListener(this);
_sequencer.stop();
}
// documentation inherited
@Override // documentation inherited
public void setVolume (float volume)
{
if (_channels != null) {
@@ -37,7 +37,7 @@ import micromod.resamplers.LinearResampler;
*/
public class ModPlayer extends MusicPlayer
{
// documentation inherited
@Override // documentation inherited
public void init ()
throws Exception
{
@@ -45,20 +45,19 @@ public class ModPlayer extends MusicPlayer
_device.start();
}
// documentation inherited
@Override // documentation inherited
public void shutdown ()
{
_device.stop();
}
// documentation inherited
@Override // documentation inherited
public void start (InputStream stream)
throws Exception
{
Module module = ModuleLoader.read(new DataInputStream(stream));
final MicroMod mod = new MicroMod(
module, _device, new LinearResampler());
final MicroMod mod = new MicroMod(module, _device, new LinearResampler());
_player = new Thread("narya mod player") {
public void run () {
@@ -86,13 +85,13 @@ public class ModPlayer extends MusicPlayer
_player.start();
}
// documentation inherited
@Override // documentation inherited
public void stop ()
{
_player = null;
}
// documentation inherited
@Override // documentation inherited
public void setVolume (float vol)
{
_device.setVolume(vol);
@@ -110,17 +109,13 @@ public class ModPlayer extends MusicPlayer
super(new SS16LEAudioFormatConverter(), 44100, 1000);
}
/**
* Adjust the volume of the line that we're sending our mod data to.
*/
@Override // documentation inherited
public void setVolume (float vol)
{
SoundManager.adjustVolume(sourceDataLine, vol);
}
/**
* Access the drain method of the line.
*/
@Override // documentation inherited
public void drain ()
{
sourceDataLine.drain();
@@ -43,7 +43,7 @@ import com.threerings.media.Log;
*/
public class Mp3Player extends MusicPlayer
{
// documentation inherited
@Override // documentation inherited
public void init ()
{
// TODO: some stuff needs to move here, like setting up the line
@@ -51,12 +51,12 @@ public class Mp3Player extends MusicPlayer
// out (the format might always be known..).
}
// documentation inherited
@Override // documentation inherited
public void shutdown ()
{
}
// documentation inherited
@Override // documentation inherited
public void start (final InputStream stream)
throws Exception
{
@@ -74,15 +74,12 @@ public class Mp3Player extends MusicPlayer
}
AudioFormat sourceFormat = inStream.getFormat();
AudioFormat.Encoding targetEnc =
AudioFormat.Encoding.PCM_SIGNED;
AudioFormat.Encoding targetEnc = AudioFormat.Encoding.PCM_SIGNED;
inStream = AudioSystem.getAudioInputStream(
targetEnc, inStream);
inStream = AudioSystem.getAudioInputStream(targetEnc, inStream);
AudioFormat format = inStream.getFormat();
DataLine.Info info = new DataLine.Info(
SourceDataLine.class, format);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
_line = (SourceDataLine) AudioSystem.getLine(info);
@@ -122,13 +119,13 @@ public class Mp3Player extends MusicPlayer
_player.start();
}
// documentation inherited
@Override // documentation inherited
public void stop ()
{
_player = null;
}
// documentation inherited
@Override // documentation inherited
public void setVolume (float volume)
{
// TODO : line won't be null when we initialize it in the right place
@@ -61,8 +61,7 @@ public class MusicManager
}
/**
* Returns a string summarizing our volume settings and disabled sound
* types.
* Returns a string summarizing our volume settings and disabled sound types.
*/
public String summarizeState ()
{
@@ -138,7 +137,7 @@ public class MusicManager
MusicKey mkey = new MusicKey(pkgPath, key, -1);
if (!_musicStack.isEmpty()) {
MusicKey current = (MusicKey) _musicStack.getFirst();
MusicKey current = _musicStack.getFirst();
// if we're currently playing this song..
if (mkey.equals(current)) {
@@ -155,7 +154,7 @@ public class MusicManager
} else {
// we aren't currently playing this song. Simply remove.
for (Iterator iter=_musicStack.iterator(); iter.hasNext(); ) {
for (Iterator<MusicKey> iter=_musicStack.iterator(); iter.hasNext(); ) {
if (key.equals(iter.next())) {
iter.remove();
return;
@@ -165,8 +164,7 @@ public class MusicManager
}
}
Log.debug("Sequence stopped that wasn't in the stack anymore " +
"[key=" + mkey + "].");
Log.debug("Sequence stopped that wasn't in the stack anymore [key=" + mkey + "].");
}
/**
@@ -178,15 +176,14 @@ public class MusicManager
return;
}
// if the volume is off, we don't actually want to play anything
// but we want to at least decrement any loopers by one
// and keep them on the top of the queue
// if the volume is off, we don't actually want to play anything but we want to at least
// decrement any loopers by one and keep them on the top of the queue
if (_musicVol == 0f) {
handleMusicStopped();
return;
}
MusicKey info = (MusicKey) _musicStack.getFirst();
MusicKey info = _musicStack.getFirst();
Config c = _smgr.getConfig(info);
String[] names = c.getValue(info.key, (String[])null);
@@ -208,7 +205,7 @@ public class MusicManager
}
// shutdown the old player if we're switching music types
if (! playerClass.isInstance(_musicPlayer)) {
if (!playerClass.isInstance(_musicPlayer)) {
if (_musicPlayer != null) {
_musicPlayer.shutdown();
}
@@ -219,8 +216,8 @@ public class MusicManager
_musicPlayer.init(_playerListener);
} catch (Exception e) {
Log.warning("Unable to instantiate music player [class=" +
playerClass + ", e=" + e + "].");
Log.warning("Unable to instantiate music player [class=" + playerClass +
", e=" + e + "].");
// scrap it, try again with the next song
_musicPlayer = null;
@@ -271,8 +268,7 @@ public class MusicManager
}
/**
* Stop whatever song is currently playing and deal with the
* MusicKey associated with it.
* Stop whatever song is currently playing and deal with the MusicKey associated with it.
*/
protected void handleMusicStopped ()
{
@@ -281,7 +277,7 @@ public class MusicManager
}
// see what was playing
MusicKey current = (MusicKey) _musicStack.getFirst();
MusicKey current = _musicStack.getFirst();
// see how many times the song was to loop and act accordingly
switch (current.loops) {
@@ -333,7 +329,7 @@ public class MusicManager
protected float _musicVol = 1f;
/** The stack of songs that we're playing. */
protected LinkedList _musicStack = new LinkedList();
protected LinkedList<MusicKey> _musicStack = new LinkedList<MusicKey>();
/** The current music player, if any. */
protected MusicPlayer _musicPlayer;
@@ -29,14 +29,13 @@ import com.threerings.media.sound.SoundManager.SoundType;
public interface SoundCodes
{
/**
* Alert sounds are the type of sounds a player would hear when
* getting a puzzle challenge.
* Alert sounds are the type of sounds a player would hear when getting a puzzle challenge.
*/
public static final SoundType ALERT = new SoundType("alert");
/**
* Feedback sounds are the type of sounds a player would here when
* clicking on buttons or performing an action.
* Feedback sounds are the type of sounds a player would here when clicking on buttons or
* performing an action.
*/
public static final SoundType FEEDBACK = new SoundType("feedback");
@@ -82,8 +82,7 @@ public class SoundManager
{
/**
* 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.
*/
@@ -92,6 +91,7 @@ public class SoundManager
_strname = strname;
}
@Override // documentation inherited
public String toString ()
{
return _strname;
@@ -107,10 +107,9 @@ public class SoundManager
{
/**
* Stop playing or looping the sound.
* 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 8/11ths of a second
* for the sound to actually stop playing.
* 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
* 8/11ths of a second for the sound to actually stop playing.
*/
public void stop ();
@@ -154,8 +153,7 @@ public class SoundManager
* @param defaultClipPath The pathname of a sound clip to use as a
* fallback if another sound clip cannot be located.
*/
public SoundManager (
ResourceManager rmgr, String defaultClipBundle, String defaultClipPath)
public SoundManager (ResourceManager rmgr, String defaultClipBundle, String defaultClipPath)
{
// save things off
_rmgr = rmgr;
@@ -182,8 +180,7 @@ public class SoundManager
}
/**
* Returns a string summarizing our volume settings and disabled sound
* types.
* Returns a string summarizing our volume settings and disabled sound types.
*/
public String summarizeState ()
{
@@ -191,7 +188,7 @@ public class SoundManager
buf.append("clipVol=").append(_clipVol);
buf.append(", disabled=[");
int ii = 0;
for (Iterator iter = _disabledTypes.iterator(); iter.hasNext(); ) {
for (Iterator<SoundType> iter = _disabledTypes.iterator(); iter.hasNext(); ) {
if (ii++ > 0) {
buf.append(", ");
}
@@ -311,16 +308,14 @@ public class SoundManager
* @param delay the delay in milliseconds.
* @param pan a value from -1f (all left) to +1f (all right).
*/
public void play (
SoundType type, String pkgPath, String key, int delay, float pan)
public void play (SoundType type, String pkgPath, String key, int delay, float pan)
{
if (type == null) {
type = DEFAULT; // let the lazy kids play too
}
if ((_clipVol != 0f) && isEnabled(type)) {
final SoundKey skey = new SoundKey(PLAY, pkgPath, key, delay,
_clipVol, pan);
final SoundKey skey = new SoundKey(PLAY, pkgPath, key, delay, _clipVol, pan);
if (delay > 0) {
new Interval() {
public void expired () {
@@ -372,8 +367,8 @@ public class SoundManager
}
} else /* if (_verbose.getValue()) */ {
Log.warning("SoundManager not playing sound because " +
"too many sounds in queue [key=" + skey + "].");
Log.warning("SoundManager not playing sound because too many sounds in queue " +
"[key=" + skey + "].");
}
}
@@ -390,8 +385,7 @@ public class SoundManager
} else {
_queue.appendLoud(key);
queued = true;
add = okToStartNew && (_freeSpoolers == 0) &&
(_spoolerCount < MAX_SPOOLERS);
add = okToStartNew && (_freeSpoolers == 0) && (_spoolerCount < MAX_SPOOLERS);
if (add) {
_spoolerCount++;
}
@@ -422,12 +416,12 @@ public class SoundManager
SoundKey key;
synchronized (_queue) {
_freeSpoolers++;
key = (SoundKey) _queue.get(MAX_WAIT_TIME);
key = _queue.get(MAX_WAIT_TIME);
_freeSpoolers--;
if (key == null || key.cmd == DIE) {
_spoolerCount--;
// if dieing and there are others to kill, do so
// if dying and there are others to kill, do so
if (key != null && _spoolerCount > 0) {
_queue.appendLoud(key);
}
@@ -464,8 +458,7 @@ public class SoundManager
// copy cached to lock map
_lockedClips.put(key, _clipCache.get(key));
} catch (Exception e) {
// don't whine about LOCK failures unless
// we are verbosely logging
// don't whine about LOCK failures unless we are verbosely logging
if (_verbose.getValue()) {
throw e;
}
@@ -568,34 +561,28 @@ 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((LINEBUF_SIZE * 8 * 1000) / (sampleRate * sampleSize));
// add in a fudge factor of half a second
drainTime += 500;
try {
Thread.sleep(drainTime);
} catch (InterruptedException ie) {
}
} catch (InterruptedException ie) { }
} catch (IOException ioe) {
Log.warning("Error loading sound file [key=" + key +
", e=" + ioe + "].");
Log.warning("Error loading sound file [key=" + key + ", e=" + ioe + "].");
} catch (UnsupportedAudioFileException uafe) {
Log.warning("Unsupported sound format [key=" + key +
", e=" + uafe + "].");
Log.warning("Unsupported sound format [key=" + key + ", e=" + uafe + "].");
} catch (LineUnavailableException lue) {
String err = "Line not available to play sound [key=" + key.key +
", e=" + lue + "].";
String err = "Line not available to play sound [key=" + key.key + ", e=" + lue + "].";
if (_soundSeemsToWork) {
Log.warning(err);
} else {
// 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
// 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
Log.debug(err);
}
@@ -616,8 +603,7 @@ public class SoundManager
}
/**
* Called by spooling threads, loads clip data from the resource
* manager or the cache.
* Called by spooling threads, loads clip data from the resource manager or the cache.
*/
protected byte[] getClipData (SoundKey key)
throws IOException, UnsupportedAudioFileException
@@ -630,13 +616,13 @@ public class SoundManager
_clipCache.clear();
}
data = (byte[][]) _clipCache.get(key);
data = _clipCache.get(key);
// see if it's in the locked cache (we first look in the regular
// clip cache so that locked clips that are still cached continue
// to be moved to the head of the LRU queue)
if (data == null) {
data = (byte[][]) _lockedClips.get(key);
data = _lockedClips.get(key);
}
if (data == null) {
@@ -714,8 +700,7 @@ public class SoundManager
try {
return new FileInputStream(pick);
} catch (Exception e) {
Log.warning("Error reading test sound [e=" + e + ", file=" +
pick + "].");
Log.warning("Error reading test sound [e=" + e + ", file=" + pick + "].");
}
}
return null;
@@ -735,15 +720,13 @@ public class SoundManager
try {
clipin = _rmgr.getResource(path);
} catch (FileNotFoundException fnfe2) {
// only play the default sound if we have verbose sound
// debuggin turned on.
// only play the default sound if we have verbose sound debugging turned on.
if (_verbose.getValue()) {
Log.warning("Could not locate sound data [bundle=" +
bundle + ", path=" + path + "].");
Log.warning("Could not locate sound data [bundle=" + bundle +
", path=" + path + "].");
if (_defaultClipPath != null) {
try {
clipin = _rmgr.getResource(
_defaultClipBundle, _defaultClipPath);
clipin = _rmgr.getResource(_defaultClipBundle, _defaultClipPath);
} catch (FileNotFoundException fnfe3) {
try {
clipin = _rmgr.getResource(_defaultClipPath);
@@ -773,7 +756,7 @@ public class SoundManager
*/
protected Config getConfig (SoundKey key)
{
Config c = (Config) _configs.get(key.pkgPath);
Config c = _configs.get(key.pkgPath);
if (c == null) {
String propPath = key.pkgPath + Sounds.PROP_NAME;
Properties props = new Properties();
@@ -818,11 +801,9 @@ public class SoundManager
*/
protected static void adjustVolume (Line line, float vol)
{
FloatControl control = (FloatControl)
line.getControl(FloatControl.Type.MASTER_GAIN);
FloatControl control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
// the only problem is that gain is specified in decibals,
// which is a logarithmic scale.
// the only problem is that gain is specified in decibals, which is a logarithmic scale.
// Since we want max volume to leave the sample unchanged, our
// maximum volume translates into a 0db gain.
float gain;
@@ -842,8 +823,7 @@ public class SoundManager
protected static void adjustPan (Line line, float pan)
{
try {
FloatControl control =
(FloatControl) line.getControl(FloatControl.Type.PAN);
FloatControl control = (FloatControl) line.getControl(FloatControl.Type.PAN);
control.setValue(pan);
} catch (Exception e) {
Log.debug("Cannot set pan on line: " + e);
@@ -897,8 +877,7 @@ public class SoundManager
/**
* Constructor for a sound effect soundkey.
*/
public SoundKey (byte cmd, String pkgPath, String key, int delay,
float volume, float pan)
public SoundKey (byte cmd, String pkgPath, String key, int delay, float volume, float pan)
{
this(cmd, pkgPath, key);
@@ -950,26 +929,24 @@ public class SoundManager
return (stamp + MAX_SOUND_DELAY < System.currentTimeMillis());
}
// documentation inherited
@Override // documentation inherited
public String toString ()
{
return "SoundKey{cmd=" + cmd + ", pkgPath=" + pkgPath +
", key=" + key + "}";
return "SoundKey{cmd=" + cmd + ", pkgPath=" + pkgPath + ", key=" + key + "}";
}
// documentation inherited
@Override // documentation inherited
public int hashCode ()
{
return pkgPath.hashCode() ^ key.hashCode();
}
// documentation inherited
@Override // documentation inherited
public boolean equals (Object o)
{
if (o instanceof SoundKey) {
SoundKey that = (SoundKey) o;
return this.pkgPath.equals(that.pkgPath) &&
this.key.equals(that.key);
return this.pkgPath.equals(that.pkgPath) && this.key.equals(that.key);
}
return false;
}
@@ -982,7 +959,7 @@ public class SoundManager
protected ResourceManager _rmgr;
/** The queue of sound clips to be played. */
protected Queue _queue = new Queue();
protected Queue<SoundKey> _queue = new Queue<SoundKey>();
/** The number of currently active LineSpoolers. */
protected int _spoolerCount, _freeSpoolers;
@@ -994,18 +971,18 @@ public class SoundManager
protected float _clipVol = 1f;
/** The cache of recent audio clips . */
protected LRUHashMap _clipCache = new LRUHashMap(10);
protected LRUHashMap<SoundKey,byte[][]> _clipCache = new LRUHashMap<SoundKey,byte[][]>(10);
/** The set of locked audio clips; this is separate from the LRU so
* that locking clips doesn't booch up an otherwise normal caching
* agenda. */
protected HashMap _lockedClips = new HashMap();
protected HashMap<SoundKey,byte[][]> _lockedClips = new HashMap<SoundKey,byte[][]>();
/** A set of soundTypes for which sound is enabled. */
protected HashSet _disabledTypes = new HashSet();
protected HashSet<SoundType> _disabledTypes = new HashSet<SoundType>();
/** A cache of config objects we've created. */
protected LRUHashMap _configs = new LRUHashMap(5);
protected LRUHashMap<String,Config> _configs = new LRUHashMap<String,Config>(5);
/** Soundkey command constants. */
protected static final byte PLAY = 0;
@@ -1017,26 +994,22 @@ public class SoundManager
/** A pref that specifies a directory for us to get test sounds from. */
protected static RuntimeAdjust.FileAdjust _testDir =
new RuntimeAdjust.FileAdjust(
"Test sound directory", "narya.media.sound.test_dir",
MediaPrefs.config, true, "");
"Test sound directory", "narya.media.sound.test_dir", MediaPrefs.config, true, "");
protected static RuntimeAdjust.BooleanAdjust _verbose =
new RuntimeAdjust.BooleanAdjust(
"Verbose sound event logging", "narya.media.sound.verbose",
MediaPrefs.config, false);
"Verbose sound event logging", "narya.media.sound.verbose", MediaPrefs.config, false);
/** The queue size at which we start to ignore requests to play sounds. */
protected static final int MAX_QUEUE_SIZE = 25;
/** The maximum time after which we throw away a sound rather
* than play it. */
/** The maximum time after which we throw away a sound rather than play it. */
protected static final long MAX_SOUND_DELAY = 400L;
/** The size of the line's buffer. */
protected static final int LINEBUF_SIZE = 8 * 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;
/** The maximum number of spoolers we'll allow. This is a lot. */