game.sound
Class CSoundPlayer

java.lang.Object
  extended by game.sound.CSoundPlayer

public class CSoundPlayer
extends java.lang.Object

Sound player. There should be only one CSoundPlayer by game. This is enforced by static methods and private constructors.

Author:
sroccaserra

Field Summary
static javax.sound.sampled.AudioFormat AUDIO_FORMAT
           
static int BITS_PER_SAMPLE
           
static int BYTES_PER_FRAME
           
static int BYTES_PER_SAMPLE
           
static javax.sound.sampled.DataLine.Info DATA_LINE_INFO
           
static boolean IS_BIG_ENDIAN
           
static boolean IS_SIGNED
           
private  byte[] m_aCriticalSoundData
           
private  byte[] m_aMissSoundData
           
private  byte[] m_aMultipleHitSoundData
           
private  byte[] m_aSingleHitSoundData
           
private  javax.sound.sampled.SourceDataLine[] m_aSourceDataLines
           
private  byte[] m_aStartSoundData
           
private  javax.sound.sampled.Mixer m_mixer
           
private static CSoundPlayer m_soundPlayer
          The only CSoundPlayer object.
static int MAX_AMPLITUDE
           
static int NB_CHANNELS
           
static int NB_SIMULTANEOUS_SOUNDS
           
static float SAMPLE_RATE
           
 
Constructor Summary
private CSoundPlayer()
          Private constructor, forbids external instanciation.
 
Method Summary
static void clear()
          Clears the SourceDataLines.
private  void closeSourceDataLines()
          Cleanup function.
private static byte[] generateModulation(float freq1, float freq2, float sLength)
          Generates a sine modulation between two frequencies and store it in a byte array.
private  javax.sound.sampled.SourceDataLine getSourceDataLine()
          Returns the first available SourceDataLine, or null if they are all busy.
static void init()
          Use this function to init java audio system, avoids lag at first sound played.
static void playCriticalSound()
           
static void playMissSound()
           
static void playMultipleHitSound()
           
static void playSingleHitSound()
           
private  void playSoundData(byte[] soundData)
          Sound threads creation.
static void playStartSound()
           
private static float sin(float f, float t)
          Usefull sine function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SAMPLE_RATE

public static final float SAMPLE_RATE
See Also:
Constant Field Values

BITS_PER_SAMPLE

public static final int BITS_PER_SAMPLE
See Also:
Constant Field Values

NB_CHANNELS

public static final int NB_CHANNELS
See Also:
Constant Field Values

BYTES_PER_SAMPLE

public static final int BYTES_PER_SAMPLE
See Also:
Constant Field Values

BYTES_PER_FRAME

public static final int BYTES_PER_FRAME
See Also:
Constant Field Values

NB_SIMULTANEOUS_SOUNDS

public static final int NB_SIMULTANEOUS_SOUNDS
See Also:
Constant Field Values

IS_SIGNED

public static final boolean IS_SIGNED
See Also:
Constant Field Values

IS_BIG_ENDIAN

public static final boolean IS_BIG_ENDIAN
See Also:
Constant Field Values

AUDIO_FORMAT

public static final javax.sound.sampled.AudioFormat AUDIO_FORMAT

DATA_LINE_INFO

public static final javax.sound.sampled.DataLine.Info DATA_LINE_INFO

MAX_AMPLITUDE

public static final int MAX_AMPLITUDE

m_soundPlayer

private static CSoundPlayer m_soundPlayer
The only CSoundPlayer object.


m_aStartSoundData

private final byte[] m_aStartSoundData

m_aSingleHitSoundData

private final byte[] m_aSingleHitSoundData

m_aMultipleHitSoundData

private final byte[] m_aMultipleHitSoundData

m_aMissSoundData

private final byte[] m_aMissSoundData

m_aCriticalSoundData

private final byte[] m_aCriticalSoundData

m_aSourceDataLines

private javax.sound.sampled.SourceDataLine[] m_aSourceDataLines

m_mixer

private javax.sound.sampled.Mixer m_mixer
Constructor Detail

CSoundPlayer

private CSoundPlayer()
Private constructor, forbids external instanciation.

Method Detail

init

public static void init()
Use this function to init java audio system, avoids lag at first sound played.


getSourceDataLine

private final javax.sound.sampled.SourceDataLine getSourceDataLine()
Returns the first available SourceDataLine, or null if they are all busy. The number of simultaneous SourceDataLines is limited by CSoundPlayer.

Returns:
an available SourceDataLine or null.

closeSourceDataLines

private final void closeSourceDataLines()
Cleanup function.


clear

public static void clear()
Clears the SourceDataLines. Gives the oportunity to close lines and start anew since Java simultaneous sounds under heavy strain can leave some hanged threads and unfinished business.


playSoundData

private final void playSoundData(byte[] soundData)
Sound threads creation.

Parameters:
soundData - : the sound data to be played by the thread.

playStartSound

public static final void playStartSound()

playSingleHitSound

public static final void playSingleHitSound()

playMultipleHitSound

public static final void playMultipleHitSound()

playMissSound

public static final void playMissSound()

playCriticalSound

public static final void playCriticalSound()

generateModulation

private static final byte[] generateModulation(float freq1,
                                               float freq2,
                                               float sLength)
Generates a sine modulation between two frequencies and store it in a byte array.

Parameters:
freq1 -
freq2 - : frequencies in Hz
sLength - : length of the sound in seconds
Returns:
the byte array containing generated sound data.

sin

private static final float sin(float f,
                               float t)
Usefull sine function.

Parameters:
f - : frequency
t - : time
Returns:
sin(2*PI*f*t)