edu.uiuc.cs397rhc.sound
Class SoundFilePlayer

java.lang.Object
  extended by edu.uiuc.cs397rhc.sound.SoundFilePlayer
All Implemented Interfaces:
Equalizable, Fadeable
Direct Known Subclasses:
MidiPlayer, MP3Player

public abstract class SoundFilePlayer
extends java.lang.Object
implements Equalizable, Fadeable

SoundFilePlayer loops over a given sound file while providing fading functionality.

Author:
Pedro DeRose

Field Summary
static int DEFAULT_LOOP
          Default looping mode for playback.
 
Fields inherited from interface edu.uiuc.cs397rhc.sound.Fadeable
DEFAULT_FADE_DURATION, DEFAULT_SLEEP_INTERVAL
 
Constructor Summary
SoundFilePlayer(java.lang.String filename)
          Creates a SoundFilePlayer for the file with the given file name.
SoundFilePlayer(java.lang.String filename, int loop)
          Creates a SoundFilePlayer for the file with the given file name.
 
Method Summary
 void fade(double targetPercent)
          Fade from the current multiplier to the target multiplier, over the default duration and with the default interval between volume changes.
 void fade(double targetPercent, long duration)
          Fade from the current multiplier to the target mulitplier, over the given duration and with the default interval between volume changes.
 void fade(double targetPercent, long duration, int steps)
          Fade from the current multiplier to the target multiplier, over the given duration and in the given number of steps.
 double getFadeMultiplier()
          Returns the current fade multiplier.
 java.io.File getFile()
          The File being played.
 int getLoopCount()
          The number of times the file should be looped, or -1 for infinite.
protected  double getVolumeMultiplier()
          Convenience method that returns a single multiplier by which the volume should be multiplied to account for both fading and equalizing.
 double getVolumePercentage()
          Gets the independent volume percentage of the object.
 boolean isFading()
          Whether a fade is in progress
 boolean isPlaying()
          Whether the file should be currently playing.
protected abstract  void play()
          Plays the file in a loop until isPlaying() returns false or until the file is done looping, in which case stop() should be called.
 void setFadeMultiplier(double multiplier)
          Sets the current fade multiplier, thereby also stopping any fading.
 void setVolumePercentage(double percent)
          Sets the volume percentage of the object independently of other volume manipulations, such as fading.
 void start()
          Starts playing the file in a new thread.
 void stop()
          Stops playing the file.
abstract  void updateVolume()
          Update the volume to reflect the current fade multiplier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LOOP

public static final int DEFAULT_LOOP
Default looping mode for playback.

See Also:
Constant Field Values
Constructor Detail

SoundFilePlayer

public SoundFilePlayer(java.lang.String filename,
                       int loop)
Creates a SoundFilePlayer for the file with the given file name.

Parameters:
filename - the name of the MP3 file
loop - the number of times to loop, or -1 for infinite

SoundFilePlayer

public SoundFilePlayer(java.lang.String filename)
Creates a SoundFilePlayer for the file with the given file name.

Parameters:
filename - the name of the MP3 file
Method Detail

isPlaying

public boolean isPlaying()
Whether the file should be currently playing. Subclasses are responsible for respecting this value in their play() methods.

Returns:
true if playing, false otherwise

getLoopCount

public int getLoopCount()
The number of times the file should be looped, or -1 for infinite.

Returns:
the number of times the file should be looped, or -1 for infinite

getFile

public java.io.File getFile()
The File being played.

Returns:
the file being played

getVolumePercentage

public double getVolumePercentage()
Description copied from interface: Equalizable
Gets the independent volume percentage of the object.

Specified by:
getVolumePercentage in interface Equalizable
Returns:
the volume percentage

setVolumePercentage

public void setVolumePercentage(double percent)
Description copied from interface: Equalizable
Sets the volume percentage of the object independently of other volume manipulations, such as fading.

Specified by:
setVolumePercentage in interface Equalizable
Parameters:
percent - the new volume percentage

getFadeMultiplier

public double getFadeMultiplier()
Description copied from interface: Fadeable
Returns the current fade multiplier.

Specified by:
getFadeMultiplier in interface Fadeable
Returns:
the current fade multiplier

setFadeMultiplier

public void setFadeMultiplier(double multiplier)
Description copied from interface: Fadeable
Sets the current fade multiplier, thereby also stopping any fading.

Specified by:
setFadeMultiplier in interface Fadeable
Parameters:
multiplier - the new fade multiplier

isFading

public boolean isFading()
Description copied from interface: Fadeable
Whether a fade is in progress

Specified by:
isFading in interface Fadeable
Returns:
true if a fade is in progress, false otherwise

fade

public void fade(double targetPercent)
Description copied from interface: Fadeable
Fade from the current multiplier to the target multiplier, over the default duration and with the default interval between volume changes.

Specified by:
fade in interface Fadeable
Parameters:
targetPercent - the target volume percentage after the fade

fade

public void fade(double targetPercent,
                 long duration)
Description copied from interface: Fadeable
Fade from the current multiplier to the target mulitplier, over the given duration and with the default interval between volume changes.

Specified by:
fade in interface Fadeable
Parameters:
targetPercent - the target volume percentage after the fade
duration - the duration of the fade in milliseconds

fade

public void fade(double targetPercent,
                 long duration,
                 int steps)
Description copied from interface: Fadeable
Fade from the current multiplier to the target multiplier, over the given duration and in the given number of steps.

Specified by:
fade in interface Fadeable
Parameters:
targetPercent - the target volume percentage after the fade
duration - the duration of the fade in milliseconds
steps - the number of volume changes during the fade

getVolumeMultiplier

protected double getVolumeMultiplier()
Convenience method that returns a single multiplier by which the volume should be multiplied to account for both fading and equalizing.

Returns:
a value by which the volume should be multiplied to account for both fading and equalizing

updateVolume

public abstract void updateVolume()
Description copied from interface: Fadeable
Update the volume to reflect the current fade multiplier. This should generally not have to be called by a client class, and is used primarily by the FadeHelper to notify a volume change during a fade.

Specified by:
updateVolume in interface Fadeable

play

protected abstract void play()
Plays the file in a loop until isPlaying() returns false or until the file is done looping, in which case stop() should be called.


start

public void start()
Starts playing the file in a new thread.


stop

public void stop()
Stops playing the file.