com.threerings.media.sound
Class JavaSoundPlayer

java.lang.Object
  extended by com.threerings.media.sound.SoundPlayer
      extended by com.threerings.media.sound.JavaSoundPlayer
Direct Known Subclasses:
SoundManager

public class JavaSoundPlayer
extends SoundPlayer

Manages the playing of audio files via the Java Sound APIs.


Nested Class Summary
protected static class JavaSoundPlayer.SoundKey
          A key for tracking sounds.
 
Nested classes/interfaces inherited from class com.threerings.media.sound.SoundPlayer
SoundPlayer.Frob, SoundPlayer.SoundType
 
Field Summary
protected  RunQueue _callbackQueue
          The queue where callbacks for keys being processed are dispatched.
protected  LRUHashMap<JavaSoundPlayer.SoundKey,byte[][]> _clipCache
          The cache of recent audio clips .
protected  int _freeSpoolers
          The number of currently active LineSpoolers.
protected  SoundLoader _loader
          Does our package based sound loading.
protected  HashMap<JavaSoundPlayer.SoundKey,byte[][]> _lockedClips
          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  Queue<JavaSoundPlayer.SoundKey> _queue
          The queue of sound clips to be played.
protected  boolean _soundSeemsToWork
          If we every play a sound successfully, this is set to true.
protected  int _spoolerCount
          The number of currently active LineSpoolers.
protected static RuntimeAdjust.FileAdjust _testDir
          A pref that specifies a directory for us to get test sounds from.
protected static RuntimeAdjust.BooleanAdjust _verbose
           
static int DEFAULT_CACHE_SIZE
          The default clip cache holds 4 megs.
protected static byte DIE
           
protected static int LINEBUF_SIZE
          The size of the line's buffer.
protected static byte LOCK
           
protected static byte LOOP
           
protected static int MAX_QUEUE_SIZE
          The queue size at which we start to ignore requests to play sounds.
protected static long MAX_SOUND_DELAY
          The maximum time after which we throw away a sound rather than play it.
protected static int MAX_SPOOLERS
          The maximum number of spoolers we'll allow.
protected static long MAX_WAIT_TIME
          The maximum time a spooler will wait for a stream before deciding to shut down.
protected static byte PLAY
          Soundkey command constants.
protected static byte UNLOCK
           
 
Fields inherited from class com.threerings.media.sound.SoundPlayer
_clipVol, _disabledTypes, _enabledObservers, DEFAULT, PAN_CENTER, PAN_LEFT, PAN_RIGHT
 
Constructor Summary
JavaSoundPlayer(ResourceManager rmgr)
          Constructs a sound manager.
JavaSoundPlayer(ResourceManager rmgr, String defaultClipBundle, String defaultClipPath)
          Constructs a sound manager with the default clip cache size.
JavaSoundPlayer(ResourceManager rmgr, String defaultClipBundle, String defaultClipPath, int cacheSize)
          Constructs a sound manager.
JavaSoundPlayer(SoundLoader loader, int cacheSize)
           
 
Method Summary
protected  void addToPlayQueue(JavaSoundPlayer.SoundKey skey)
          Add the sound clip key to the queue to be played.
protected static void adjustPan(Line line, float pan)
          Set the pan value for the specified line.
protected static void adjustVolume(Line line, float vol)
          Use the gain control to implement volume.
protected  boolean enqueue(JavaSoundPlayer.SoundKey key, boolean okToStartNew)
          Enqueue a new SoundKey.
protected  byte[] getClipData(JavaSoundPlayer.SoundKey key)
          Called by spooling threads, loads clip data from the resource manager or the cache.
 RunQueue getSoundQueue()
          Gets the run queue on which sound should be played.
protected  InputStream getTestClip(JavaSoundPlayer.SoundKey key)
           
protected  boolean isTesting()
           
 void lock(String pkgPath, String... keys)
          Optionally lock each of these keys prior to playing, to guarantee that it will be quickly available for playing.
protected  SoundPlayer.Frob loop(String pkgPath, String key, float pan)
          Loop the specified sound, stopping as quickly as possible when stop is called.
protected  SoundPlayer.Frob loop(String pkgPath, String key, float pan, byte cmd)
          Loop the specified sound.
protected  void play(String pkgPath, String key, float pan)
          Play the specified sound after the specified delay.
protected  void playSound(JavaSoundPlayer.SoundKey key)
          On a spooling thread,
protected  void processKey(JavaSoundPlayer.SoundKey key)
          Process the requested command in the specified SoundKey.
 void setSoundQueue(RunQueue queue)
          Sets the run queue on which sound should be played.
static AudioInputStream setupAudioStream(byte[] data)
          Sets up an audio stream from the given byte array, and gets it to convert itself to PCM data for writing to our output line (if it isn't already that)
static AudioInputStream setupAudioStream(InputStream in)
          Sets up an audio stream from the given byte array, and gets it to convert itself to PCM data for writing to our output line (if it isn't already that)
 void shutdown()
          Shut the damn thing off.
protected  void spoolerRun()
          This is the primary run method of the sound-playing threads.
 void unlock(String pkgPath, String... keys)
          Unlock the specified sounds so that its resources can be freed.
 
Methods inherited from class com.threerings.media.sound.SoundPlayer
addSoundEnabledObserver, getClipVolume, isEnabled, loop, loop, play, play, play, play, removeSoundEnabledObserver, setClipVolume, setEnabled, shouldPlay, summarizeState
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CACHE_SIZE

public static final int DEFAULT_CACHE_SIZE
The default clip cache holds 4 megs.

See Also:
Constant Field Values

_loader

protected SoundLoader _loader
Does our package based sound loading.


_callbackQueue

protected RunQueue _callbackQueue
The queue where callbacks for keys being processed are dispatched.


_queue

protected Queue<JavaSoundPlayer.SoundKey> _queue
The queue of sound clips to be played.


_spoolerCount

protected int _spoolerCount
The number of currently active LineSpoolers.


_freeSpoolers

protected int _freeSpoolers
The number of currently active LineSpoolers.


_soundSeemsToWork

protected boolean _soundSeemsToWork
If we every play a sound successfully, this is set to true.


_clipCache

protected LRUHashMap<JavaSoundPlayer.SoundKey,byte[][]> _clipCache
The cache of recent audio clips .


_lockedClips

protected HashMap<JavaSoundPlayer.SoundKey,byte[][]> _lockedClips
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.


PLAY

protected static final byte PLAY
Soundkey command constants.

See Also:
Constant Field Values

LOCK

protected static final byte LOCK
See Also:
Constant Field Values

UNLOCK

protected static final byte UNLOCK
See Also:
Constant Field Values

DIE

protected static final byte DIE
See Also:
Constant Field Values

LOOP

protected static final byte LOOP
See Also:
Constant Field Values

_testDir

protected static RuntimeAdjust.FileAdjust _testDir
A pref that specifies a directory for us to get test sounds from.


_verbose

protected static RuntimeAdjust.BooleanAdjust _verbose

MAX_QUEUE_SIZE

protected static final int MAX_QUEUE_SIZE
The queue size at which we start to ignore requests to play sounds.

See Also:
Constant Field Values

MAX_SOUND_DELAY

protected static final long MAX_SOUND_DELAY
The maximum time after which we throw away a sound rather than play it.

See Also:
Constant Field Values

LINEBUF_SIZE

protected static final int LINEBUF_SIZE
The size of the line's buffer.

See Also:
Constant Field Values

MAX_WAIT_TIME

protected static final long MAX_WAIT_TIME
The maximum time a spooler will wait for a stream before deciding to shut down.

See Also:
Constant Field Values

MAX_SPOOLERS

protected static final int MAX_SPOOLERS
The maximum number of spoolers we'll allow. This is a lot.

See Also:
Constant Field Values
Constructor Detail

JavaSoundPlayer

public JavaSoundPlayer(ResourceManager rmgr)
Constructs a sound manager.


JavaSoundPlayer

public JavaSoundPlayer(ResourceManager rmgr,
                       String defaultClipBundle,
                       String defaultClipPath)
Constructs a sound manager with the default clip cache size.

Parameters:
defaultClipPath - The pathname of a sound clip to use as a fallback if another sound clip cannot be located.

JavaSoundPlayer

public JavaSoundPlayer(ResourceManager rmgr,
                       String defaultClipBundle,
                       String defaultClipPath,
                       int cacheSize)
Constructs a sound manager.

Parameters:
defaultClipPath - The pathname of a sound clip to use as a fallback if another sound clip cannot be located.
cacheSize - the number of bytes of sound clips to cache.

JavaSoundPlayer

public JavaSoundPlayer(SoundLoader loader,
                       int cacheSize)
Method Detail

shutdown

public void shutdown()
Description copied from class: SoundPlayer
Shut the damn thing off.

Specified by:
shutdown in class SoundPlayer

setSoundQueue

public void setSoundQueue(RunQueue queue)
Sets the run queue on which sound should be played.


getSoundQueue

public RunQueue getSoundQueue()
Description copied from class: SoundPlayer
Gets the run queue on which sound should be played. It defaults to RunQueue.AWT.

Specified by:
getSoundQueue in class SoundPlayer

lock

public void lock(String pkgPath,
                 String... keys)
Description copied from class: SoundPlayer
Optionally lock each of these keys prior to playing, to guarantee that it will be quickly available for playing.

Specified by:
lock in class SoundPlayer

unlock

public void unlock(String pkgPath,
                   String... keys)
Description copied from class: SoundPlayer
Unlock the specified sounds so that its resources can be freed.

Specified by:
unlock in class SoundPlayer

play

protected void play(String pkgPath,
                    String key,
                    float pan)
Description copied from class: SoundPlayer
Play the specified sound after the specified delay.

Specified by:
play in class SoundPlayer
pan - a value from -1f (all left) to +1f (all right).

loop

protected SoundPlayer.Frob loop(String pkgPath,
                                String key,
                                float pan)
Description copied from class: SoundPlayer
Loop the specified sound, stopping as quickly as possible when stop is called.

Specified by:
loop in class SoundPlayer

loop

protected SoundPlayer.Frob loop(String pkgPath,
                                String key,
                                float pan,
                                byte cmd)
Loop the specified sound.


addToPlayQueue

protected void addToPlayQueue(JavaSoundPlayer.SoundKey skey)
Add the sound clip key to the queue to be played.


enqueue

protected boolean enqueue(JavaSoundPlayer.SoundKey key,
                          boolean okToStartNew)
Enqueue a new SoundKey.


spoolerRun

protected void spoolerRun()
This is the primary run method of the sound-playing threads.


processKey

protected void processKey(JavaSoundPlayer.SoundKey key)
                   throws Exception
Process the requested command in the specified SoundKey.

Throws:
Exception

setupAudioStream

public static AudioInputStream setupAudioStream(byte[] data)
                                         throws UnsupportedAudioFileException,
                                                IOException
Sets up an audio stream from the given byte array, and gets it to convert itself to PCM data for writing to our output line (if it isn't already that)

Throws:
UnsupportedAudioFileException
IOException

setupAudioStream

public static AudioInputStream setupAudioStream(InputStream in)
                                         throws UnsupportedAudioFileException,
                                                IOException
Sets up an audio stream from the given byte array, and gets it to convert itself to PCM data for writing to our output line (if it isn't already that)

Throws:
UnsupportedAudioFileException
IOException

playSound

protected void playSound(JavaSoundPlayer.SoundKey key)
On a spooling thread,


isTesting

protected boolean isTesting()
Returns:
true if we're using a test sound directory.

getClipData

protected byte[] getClipData(JavaSoundPlayer.SoundKey key)
                      throws IOException,
                             UnsupportedAudioFileException
Called by spooling threads, loads clip data from the resource manager or the cache.

Throws:
IOException
UnsupportedAudioFileException

getTestClip

protected InputStream getTestClip(JavaSoundPlayer.SoundKey key)

adjustVolume

protected static void adjustVolume(Line line,
                                   float vol)
Use the gain control to implement volume.


adjustPan

protected static void adjustPan(Line line,
                                float pan)
Set the pan value for the specified line.