paulscode.sound.libraries
Class ChannelJavaSound

java.lang.Object
  extended by paulscode.sound.Channel
      extended by paulscode.sound.libraries.ChannelJavaSound

public class ChannelJavaSound
extends Channel

The ChannelJavaSound class is used to reserve a sound-card voice using JavaSound. Channels can be either normal or streaming channels. For more information about the JavaSound API, visit http://java.sun.com/products/java-media/sound/

SoundSystem LibraryJavaSound License:

You are free to use this library for any purpose, commercial or otherwise. You may modify this library or source code, and distribute it any way you like, provided the following conditions are met:
1) You may not falsely claim to be the author of this library or any unmodified portion of it.
2) You may not copyright this library or a modified version of it and then sue me for copyright infringement.
3) If you modify the source code, you must clearly document the changes made before redistributing the modified source code, so other users know it is not the original code.
4) You are not required to give me credit for this library in any derived work, but if you do, you must also mention my website: http://www.paulscode.com
5) I the author will not be responsible for any damages (physical, financial, or otherwise) caused by the use if this library or any part of it.
6) I the author do not guarantee, warrant, or make any representations, either expressed or implied, regarding the use of this library or any part of it.

Author: Paul Lamb
http://www.paulscode.com


Field Summary
 javax.sound.sampled.Clip clip
          Used to play back a normal source.
 javax.sound.sampled.SourceDataLine sourceDataLine
          Used to play back a streaming source.
 
Fields inherited from class paulscode.sound.Channel
attachedSource, buffersUnqueued, channelType, libraryType
 
Constructor Summary
ChannelJavaSound(int type, javax.sound.sampled.Mixer mixer)
          Constructor: takes channelType identifier and a handle to the Mixer as paramaters.
 
Method Summary
 boolean attachBuffer(SoundBuffer buffer)
          Attaches the SoundBuffer to be played back for a normal source.
 int buffersProcessed()
          Returns the number of queued byte[] buffers that have finished playing.
 void cleanup()
          Empties the streamBuffers list, shuts the channel down and removes references to all instantiated objects.
 void close()
          Stops the channel, dequeues any queued data, and closes the channel.
 int feedRawAudioData(byte[] buffer)
          Feeds raw data to the stream.
 void flush()
          Dequeues all previously queued data.
 float millisecondsPlayed()
          Calculates the number of milliseconds since the channel began playing.
 void newMixer(javax.sound.sampled.Mixer m)
          Changes the current mixer
 void pause()
          Temporarily stops playback for this channel.
 void play()
          Plays the currently attached normal source, opens this channel up for streaming, or resumes playback if this channel was paused.
 boolean playing()
          Used to determine if a channel is actively playing a source.
 boolean preLoadBuffers(java.util.LinkedList<byte[]> bufferList)
          Queues up the initial byte[] buffers of data to be streamed.
 boolean processBuffer()
          Plays the next queued byte[] buffer.
 boolean queueBuffer(byte[] buffer)
          Queues up a byte[] buffer of data to be streamed.
 boolean resetStream(javax.sound.sampled.AudioFormat format)
          Sets the channel up to be streamed using the specified AudioFormat.
 void rewind()
          Rewinds the attached source to the beginning.
 void setAudioFormat(javax.sound.sampled.AudioFormat audioFormat)
          Sets the channel up to receive the specified audio format.
 void setGain(float g)
          Changes the volume.
 void setLooping(boolean value)
          Defines whether playback should loop or just play once.
 void setPan(float p)
          Changes the pan between left and right speaker to the specified value.
 void setPitch(float p)
          Changes the pitch to the specified value.
 void stop()
          Stops playback for this channel and rewinds the attached source to the beginning.
 
Methods inherited from class paulscode.sound.Channel
errorCheck, errorMessage, getClassName, importantMessage, message, printStackTrace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clip

public javax.sound.sampled.Clip clip
Used to play back a normal source.


sourceDataLine

public javax.sound.sampled.SourceDataLine sourceDataLine
Used to play back a streaming source.

Constructor Detail

ChannelJavaSound

public ChannelJavaSound(int type,
                        javax.sound.sampled.Mixer mixer)
Constructor: takes channelType identifier and a handle to the Mixer as paramaters. Possible values for channel type can be found in the SoundSystemConfig class.

Parameters:
type - Type of channel (normal or streaming).
mixer - Handle to the JavaSound Mixer.
Method Detail

cleanup

public void cleanup()
Empties the streamBuffers list, shuts the channel down and removes references to all instantiated objects.

Overrides:
cleanup in class Channel

newMixer

public void newMixer(javax.sound.sampled.Mixer m)
Changes the current mixer

Parameters:
m - New mixer to use.

attachBuffer

public boolean attachBuffer(SoundBuffer buffer)
Attaches the SoundBuffer to be played back for a normal source.

Parameters:
buffer - SoundBuffer containing the wave data and format to attach
Returns:
False if an error occurred.

setAudioFormat

public void setAudioFormat(javax.sound.sampled.AudioFormat audioFormat)
Sets the channel up to receive the specified audio format.

Overrides:
setAudioFormat in class Channel
Parameters:
audioFormat - Format to use when playing the stream data.

resetStream

public boolean resetStream(javax.sound.sampled.AudioFormat format)
Sets the channel up to be streamed using the specified AudioFormat.

Parameters:
format - Format to use when playing the stream data.
Returns:
False if an error occurred.

setLooping

public void setLooping(boolean value)
Defines whether playback should loop or just play once.

Parameters:
value - Loop or not.

setPan

public void setPan(float p)
Changes the pan between left and right speaker to the specified value. -1 = left speaker only. 0 = middle, both speakers. 1 = right speaker only.

Parameters:
p - Pan value to use.

setGain

public void setGain(float g)
Changes the volume. 0 = no volume. 1 = maximum volume (initial gain)

Parameters:
g - Gain value to use.

setPitch

public void setPitch(float p)
Changes the pitch to the specified value.

Parameters:
p - Float value between 0.5f and 2.0f.

preLoadBuffers

public boolean preLoadBuffers(java.util.LinkedList<byte[]> bufferList)
Queues up the initial byte[] buffers of data to be streamed.

Overrides:
preLoadBuffers in class Channel
Parameters:
bufferList - List of the first buffers to be played for a streaming source.
Returns:
False if problem occurred or end of stream was reached.

queueBuffer

public boolean queueBuffer(byte[] buffer)
Queues up a byte[] buffer of data to be streamed.

Overrides:
queueBuffer in class Channel
Parameters:
buffer - The next buffer to be played for a streaming source.
Returns:
False if an error occurred or if the channel is shutting down.

processBuffer

public boolean processBuffer()
Plays the next queued byte[] buffer. This method is run from the seperate StreamThread.

Overrides:
processBuffer in class Channel
Returns:
False when no more buffers are left to process.

feedRawAudioData

public int feedRawAudioData(byte[] buffer)
Feeds raw data to the stream.

Overrides:
feedRawAudioData in class Channel
Parameters:
buffer - Buffer containing raw audio data to stream.
Returns:
Number of prior buffers that have been processed, or -1 if error.

buffersProcessed

public int buffersProcessed()
Returns the number of queued byte[] buffers that have finished playing.

Overrides:
buffersProcessed in class Channel
Returns:
Number of buffers processed.

flush

public void flush()
Dequeues all previously queued data.

Overrides:
flush in class Channel

close

public void close()
Stops the channel, dequeues any queued data, and closes the channel.

Overrides:
close in class Channel

play

public void play()
Plays the currently attached normal source, opens this channel up for streaming, or resumes playback if this channel was paused.

Overrides:
play in class Channel

pause

public void pause()
Temporarily stops playback for this channel.

Overrides:
pause in class Channel

stop

public void stop()
Stops playback for this channel and rewinds the attached source to the beginning.

Overrides:
stop in class Channel

rewind

public void rewind()
Rewinds the attached source to the beginning. Stops the source if it was paused.

Overrides:
rewind in class Channel

millisecondsPlayed

public float millisecondsPlayed()
Calculates the number of milliseconds since the channel began playing.

Overrides:
millisecondsPlayed in class Channel
Returns:
Milliseconds, or -1 if unable to calculate.

playing

public boolean playing()
Used to determine if a channel is actively playing a source. This method will return false if the channel is paused or stopped and when no data is queued to be streamed.

Overrides:
playing in class Channel
Returns:
True if this channel is playing a source.