Package com.ardor3d.audio
Class Library
java.lang.Object
com.ardor3d.audio.Library
- Direct Known Subclasses:
LibraryJOAL
The Library class is the class from which all library types are extended.
It provides generic methods for interfacing with the audio libraries
supported by the SoundSystem. Specific libraries should extend this class
and override the necessary methods. For consistant naming conventions, each
sub-class should have the name prefix "Library".
This class may also be used as the "No Sound Library" (i.e. silent mode) if
no other audio libraries are supported by the host machine, or to mute all
sound.
SoundSystem 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 Julien Gouesse: I removed Midi support in order to stop depending on javax.sound more easily
SoundSystem 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 Julien Gouesse: I removed Midi support in order to stop depending on javax.sound more easily
-
Field Summary
Modifier and TypeFieldDescriptionprotected HashMap
<String, SoundBuffer> Map containing sound file data for easy lookup by filename / identifier.protected ListenerData
Position and orientation of the listener.Array containing maximum number of non-streaming audio channels.protected boolean
Whether or not the library requires reversal of audio data byte order.Map containing all created sources for easy look-up by name.Array containing maximum number of non-streaming audio channels.protected StreamThread
Handles processing for all streaming sources. -
Constructor Summary
ConstructorDescriptionLibrary()
Constructor: Instantiates the source map and listener information. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Activates a previously culled source, so it can be played again.void
Makes sure the current volume levels of streaming sources and MIDI are correct.void
cleanup()
Stops all sources, shuts down sound library, and removes references to all instantiated objects.void
copySources
(HashMap<String, Source> srcMap) Creates sources based on the source map provided.protected Channel
createChannel
(int type) Creates a new channel of the specified type (normal or streaming).void
Culls the specified source.void
dequeueSound
(String sourcename, String filename) Removes the first occurrence of the specified filename from the specified source's list of sounds to play when previous playback ends.void
Notifies the underlying library that the Doppler parameters have changed.protected boolean
errorCheck
(boolean error, String message) Prints the specified message if error is true.protected void
errorMessage
(String message) Prints an error message.void
fadeOut
(String sourcename, FilenameURL filenameURL, long milis) Fades out the volume of whatever the specified source is currently playing, then begins playing the specified file at the source's previously assigned volume level.void
fadeOutIn
(String sourcename, FilenameURL filenameURL, long milisOut, long milisIn) Fades out the volume of whatever the specified source is currently playing, then fades the volume back in playing the specified file.int
feedRawAudioData
(Source source, byte[] buffer) Feeds raw data through the specified source.int
feedRawAudioData
(String sourcename, byte[] buffer) Feeds raw data through the specified source.void
Clears all previously queued data from a stream.Returns the filenames of all previously loaded sounds.Returns the sourcenames of all sources.Returns the name of the class.static String
Returns a longer description of this library type.Returns information about the listener.float
Returns the pitch of the specified source.Returns the Source object identified by the specified name.Returns the sources map.static String
getTitle()
Returns the short title of this library type.float
Returns the current volume of the specified source, or zero if the specified source was not found.protected void
importantMessage
(String message) Prints an important message.void
init()
Initializes the sound library.static boolean
Checks if the no-sound library type is compatible.void
Tells all the sources that the listener has moved.void
loadMidi
(boolean toLoop, String sourcename, FilenameURL filenameURL) Loads the specified MIDI file, and saves the source information about it.boolean
loadSound
(FilenameURL filenameURL) Pre-loads a sound into memory.boolean
loadSound
(SoundBuffer buffer, String identifier) Saves the specified sample data, under the specified identifier.protected void
Prints a message.boolean
midiSourcename
(String sourcename) Checks if the sourcename matches the midi source.float
millisecondsPlayed
(String sourcename) Returns the number of miliseconds since the specified source began playing.void
moveListener
(float x, float y, float z) Moves the listener relative to the current position.void
newSource
(boolean priority, boolean toStream, boolean toLoop, String sourcename, FilenameURL filenameURL, float posX, float posY, float posZ, int attModel, float distOrRoll) Creates a new source using the specified information.void
Pauses the specified source.void
Plays the specified source.void
Looks up the specified source and plays it.protected void
Prints an exception's error message followed by the stack trace.void
queueSound
(String sourcename, FilenameURL filenameURL) If the specified source is a streaming source or MIDI source, this method queues up the next sound to play when the previous playback ends.void
quickPlay
(boolean priority, boolean toStream, boolean toLoop, String sourcename, FilenameURL filenameURL, float posX, float posY, float posZ, int attModel, float distOrRoll, boolean tmp) Creates and immediately plays a new source that will be removed when it finishes playing.void
rawDataStream
(AudioFormat audioFormat, boolean priority, String sourcename, float posX, float posY, float posZ, int attModel, float distOrRoll) Opens a direct line for streaming audio data.void
removeSource
(String sourcename) Stops and deletes the specified source.void
Searches for and removes all temporary sources that have finished playing.void
Plays all sources whose 'toPlay' varriable is true but are not currently playing (such as sources which were culled while looping and then reactivated).boolean
Indicates whether or not this library requires some codecs to reverse-order the audio data they generate.void
Rewinds the specified source.void
setAttenuation
(String sourcename, int model) Sets the specified source's attenuation model.void
setDistOrRoll
(String sourcename, float dr) Sets the specified source's fade distance or rolloff factor.void
setListenerAngle
(float angle) Changes the listeners orientation to the specified 'angle' radians counterclockwise around the y-Axis.void
Changes the listeners position and orientation using the specified listener data.void
setListenerOrientation
(float lookX, float lookY, float lookZ, float upX, float upY, float upZ) Changes the listeners orientation using the specified coordinates.void
setListenerPosition
(float x, float y, float z) Changes the listener's position.void
setListenerVelocity
(float x, float y, float z) Sets the listener's velocity, for use in Doppler effect.void
setLooping
(String sourcename, boolean lp) Sets the specified source's looping parameter.void
setMasterVolume
(float value) Sets the overall volume to the specified value, affecting all sources.void
Manually sets the specified source's pitch.void
setPosition
(String sourcename, float x, float y, float z) Changes the specified source's position.void
setPriority
(String sourcename, boolean pri) Sets the specified source's priority factor.void
setTemporary
(String sourcename, boolean temporary) Defines whether or not the source should be removed after it finishes playing.void
setVelocity
(String sourcename, float x, float y, float z) Sets the specified source's velocity, for use in Doppler effect.void
Manually sets the specified source's volume.void
Stops the specified source.void
turnListener
(float angle) Turn the listener 'angle' radians counterclockwise around the y-Axis, relative to the current angle.void
Unloads the current Midi file.void
unloadSound
(String filename) Removes a pre-loaded sound from memory.
-
Field Details
-
listener
Position and orientation of the listener. -
bufferMap
Map containing sound file data for easy lookup by filename / identifier. -
sourceMap
Map containing all created sources for easy look-up by name. -
streamingChannels
Array containing maximum number of non-streaming audio channels. -
normalChannels
Array containing maximum number of non-streaming audio channels. -
streamThread
Handles processing for all streaming sources. -
reverseByteOrder
protected boolean reverseByteOrderWhether or not the library requires reversal of audio data byte order.
-
-
Constructor Details
-
Library
Constructor: Instantiates the source map and listener information. NOTES: The 'super()' method should be at the top of constructors for all extended classes. The varriable 'libraryType' should be given a new value in the constructors for all extended classes.- Throws:
SoundSystemException
-
-
Method Details
-
cleanup
public void cleanup()Stops all sources, shuts down sound library, and removes references to all instantiated objects. -
init
Initializes the sound library.- Throws:
SoundSystemException
-
libraryCompatible
public static boolean libraryCompatible()Checks if the no-sound library type is compatible.- Returns:
- True or false.
-
createChannel
Creates a new channel of the specified type (normal or streaming). Possible values for channel type can be found in theSoundSystemConfig
class.- Parameters:
type
- Type of channel.- Returns:
- The new channel.
-
loadSound
Pre-loads a sound into memory.- Parameters:
filenameURL
- Filename/URL of the sound file to load.- Returns:
- True if the sound loaded properly.
-
loadSound
Saves the specified sample data, under the specified identifier. This identifier can be later used in place of 'filename' parameters to reference the sample data.- Parameters:
buffer
- the sample data and audio format to save.identifier
- What to call the sample.- Returns:
- True if there weren't any problems.
-
getAllLoadedFilenames
Returns the filenames of all previously loaded sounds.- Returns:
- LinkedList of String filenames.
-
getAllSourcenames
Returns the sourcenames of all sources.- Returns:
- LinkedList of String sourcenames.
-
unloadSound
Removes a pre-loaded sound from memory. This is a good method to use for freeing up memory after a large sound file is no longer needed. NOTE: the source will remain in memory after this method has been called, for as long as the sound is attached to an existing source.- Parameters:
filename
- Filename/identifier of the sound file to unload.
-
rawDataStream
public void rawDataStream(AudioFormat audioFormat, boolean priority, String sourcename, float posX, float posY, float posZ, int attModel, float distOrRoll) Opens a direct line for streaming audio data.- Parameters:
audioFormat
- Format that the data will be in.sourcename
- A unique identifier for this source. Two sources may not use the same sourcename.posX
- X position for this source.posY
- Y position for this source.posZ
- Z position for this source.attModel
- Attenuation model to use.distOrRoll
- Either the fading distance or rolloff factor, depending on the value of "attmodel".
-
newSource
public void newSource(boolean priority, boolean toStream, boolean toLoop, String sourcename, FilenameURL filenameURL, float posX, float posY, float posZ, int attModel, float distOrRoll) Creates a new source using the specified information.- Parameters:
priority
- Setting this to true will prevent other sounds from overriding this one.toStream
- Setting this to true will load the sound in pieces rather than all at once.toLoop
- Should this source loop, or play only once.sourcename
- A unique identifier for this source. Two sources may not use the same sourcename.filenameURL
- Filename/URL of the sound file to play at this source.posX
- X position for this source.posY
- Y position for this source.posZ
- Z position for this source.attModel
- Attenuation model to use.distOrRoll
- Either the fading distance or rolloff factor, depending on the value of "attmodel".
-
quickPlay
public void quickPlay(boolean priority, boolean toStream, boolean toLoop, String sourcename, FilenameURL filenameURL, float posX, float posY, float posZ, int attModel, float distOrRoll, boolean tmp) Creates and immediately plays a new source that will be removed when it finishes playing.- Parameters:
priority
- Setting this to true will prevent other sounds from overriding this one.toStream
- Setting this to true will load the sound in pieces rather than all at once.toLoop
- Should this source loop, or play only once.sourcename
- A unique identifier for this source. Two sources may not use the same sourcename.filenameURL
- The filename/URL of the sound file to play at this source.posX
- X position for this source.posY
- Y position for this source.posZ
- Z position for this source.attModel
- Attenuation model to use.distOrRoll
- Either the fading distance or rolloff factor, depending on the value of "attmodel".
-
setTemporary
Defines whether or not the source should be removed after it finishes playing.- Parameters:
sourcename
- The source's name.temporary
- True or False.
-
setPosition
Changes the specified source's position.- Parameters:
sourcename
- The source's name.x
- Destination X coordinate.y
- Destination Y coordinate.z
- Destination Z coordinate.
-
setPriority
Sets the specified source's priority factor. A priority source will not be overriden if there are too many sources playing at once.- Parameters:
sourcename
- The source's name.pri
- True or False.
-
setLooping
Sets the specified source's looping parameter. If parameter lp is false, the source will play once and stop.- Parameters:
sourcename
- The source's name.lp
- True or False.
-
setAttenuation
Sets the specified source's attenuation model.- Parameters:
sourcename
- The source's name.model
- Attenuation model to use.
-
setDistOrRoll
Sets the specified source's fade distance or rolloff factor.- Parameters:
sourcename
- The source's name.dr
- Fade distance or rolloff factor.
-
setVelocity
Sets the specified source's velocity, for use in Doppler effect.- Parameters:
sourcename
- The source's name.x
- Velocity along world x-axis.y
- Velocity along world y-axis.z
- Velocity along world z-axis.
-
setListenerVelocity
public void setListenerVelocity(float x, float y, float z) Sets the listener's velocity, for use in Doppler effect.- Parameters:
x
- Velocity along world x-axis.y
- Velocity along world y-axis.z
- Velocity along world z-axis.
-
dopplerChanged
public void dopplerChanged()Notifies the underlying library that the Doppler parameters have changed. -
millisecondsPlayed
Returns the number of miliseconds since the specified source began playing.- Returns:
- miliseconds, or -1 if not playing or unable to calculate
-
feedRawAudioData
Feeds raw data through the specified source. The source must be a streaming source and it can not be already associated with a file or URL to stream from.- Parameters:
sourcename
- Name of the streaming source to play from.buffer
- Byte buffer containing raw audio data to stream.- Returns:
- Number of prior buffers that have been processed, or -1 if unable to queue the buffer (if the source was culled, for example).
-
feedRawAudioData
Feeds raw data through the specified source. The source must be a streaming source and it can not be already associated with a file or URL to stream from.- Parameters:
source
- Streaming source to play from.buffer
- Byte buffer containing raw audio data to stream.- Returns:
- Number of prior buffers that have been processed, or -1 if unable to queue the buffer (if the source was culled, for example).
-
play
Looks up the specified source and plays it.- Parameters:
sourcename
- Name of the source to play.
-
play
Plays the specified source.- Parameters:
source
- The source to play.
-
stop
Stops the specified source.- Parameters:
sourcename
- The source's name.
-
pause
Pauses the specified source.- Parameters:
sourcename
- The source's name.
-
rewind
Rewinds the specified source.- Parameters:
sourcename
- The source's name.
-
flush
Clears all previously queued data from a stream.- Parameters:
sourcename
- The source's name.
-
cull
Culls the specified source. A culled source will not play until it has been activated again.- Parameters:
sourcename
- The source's name.
-
activate
Activates a previously culled source, so it can be played again.- Parameters:
sourcename
- The source's name.
-
setMasterVolume
public void setMasterVolume(float value) Sets the overall volume to the specified value, affecting all sources.- Parameters:
value
- New volume, float value ( 0.0f - 1.0f ).
-
setVolume
Manually sets the specified source's volume.- Parameters:
sourcename
- The source's name.value
- A float value ( 0.0f - 1.0f ).
-
getVolume
Returns the current volume of the specified source, or zero if the specified source was not found.- Parameters:
sourcename
- Source to read volume from.- Returns:
- Float value representing the source volume (0.0f - 1.0f).
-
setPitch
Manually sets the specified source's pitch.- Parameters:
sourcename
- The source's name.value
- A float value ( 0.5f - 2.0f ).
-
getPitch
Returns the pitch of the specified source.- Parameters:
sourcename
- The source's name.- Returns:
- Float value representing the source pitch (0.5f - 2.0f).
-
moveListener
public void moveListener(float x, float y, float z) Moves the listener relative to the current position.- Parameters:
x
- X offset.y
- Y offset.z
- Z offset.
-
setListenerPosition
public void setListenerPosition(float x, float y, float z) Changes the listener's position.- Parameters:
x
- Destination X coordinate.y
- Destination Y coordinate.z
- Destination Z coordinate.
-
turnListener
public void turnListener(float angle) Turn the listener 'angle' radians counterclockwise around the y-Axis, relative to the current angle.- Parameters:
angle
- Angle in radians.
-
setListenerAngle
public void setListenerAngle(float angle) Changes the listeners orientation to the specified 'angle' radians counterclockwise around the y-Axis.- Parameters:
angle
- Angle in radians.
-
setListenerOrientation
public void setListenerOrientation(float lookX, float lookY, float lookZ, float upX, float upY, float upZ) Changes the listeners orientation using the specified coordinates.- Parameters:
lookX
- X element of the look-at direction.lookY
- Y element of the look-at direction.lookZ
- Z element of the look-at direction.upX
- X element of the up direction.upY
- Y element of the up direction.upZ
- Z element of the up direction.
-
setListenerData
Changes the listeners position and orientation using the specified listener data.- Parameters:
l
- Listener data to use.
-
copySources
Creates sources based on the source map provided.- Parameters:
srcMap
- Sources to copy.
-
removeSource
Stops and deletes the specified source.- Parameters:
sourcename
- The source's name.
-
removeTemporarySources
public void removeTemporarySources()Searches for and removes all temporary sources that have finished playing. -
replaySources
public void replaySources()Plays all sources whose 'toPlay' varriable is true but are not currently playing (such as sources which were culled while looping and then reactivated). -
queueSound
If the specified source is a streaming source or MIDI source, this method queues up the next sound to play when the previous playback ends. This method has no effect on non-streaming sources.- Parameters:
sourcename
- Source identifier.filenameURL
- Filename/URL of the sound file to play next.
-
dequeueSound
Removes the first occurrence of the specified filename from the specified source's list of sounds to play when previous playback ends. This method has no effect on non-streaming sources.- Parameters:
sourcename
- Source identifier.filename
- Filename/identifier of the sound file to remove from the queue.
-
fadeOut
Fades out the volume of whatever the specified source is currently playing, then begins playing the specified file at the source's previously assigned volume level. If the filenameURL parameter is null or empty, the specified source will simply fade out and stop. The miliseconds parameter must be non-negative or zero. This method will remove anything that is currently in the specified source's list of queued sounds that would have played next when the current sound finished playing. This method may only be used for streaming and MIDI sources.- Parameters:
sourcename
- Name of the source to fade out.filenameURL
- Filename/URL of the sound file to play next, or null for none.milis
- Number of miliseconds the fadeout should take.
-
fadeOutIn
Fades out the volume of whatever the specified source is currently playing, then fades the volume back in playing the specified file. Final volume after fade-in completes will be equal to the source's previously assigned volume level. The filenameURL parameter may not be null or empty. The miliseconds parameters must be non-negative or zero. This method will remove anything that is currently in the specified source's list of queued sounds that would have played next when the current sound finished playing. This method may only be used for streaming and MIDI sources.- Parameters:
sourcename
- Name of the source to fade out/in.filenameURL
- Filename/URL of the sound file to play next, or null for none.milisOut
- Number of miliseconds the fadeout should take.milisIn
- Number of miliseconds the fadein should take.
-
checkFadeVolumes
public void checkFadeVolumes()Makes sure the current volume levels of streaming sources and MIDI are correct. This method is designed to help reduce the "jerky" fading behavior that happens when using some library and codec pluggins (such as LibraryJavaSound and CodecJOrbis). This method has no effect on normal "non-streaming" sources. It would normally be called somewhere in the main "game loop". IMPORTANT: To optimize frame-rates, do not call this method for every frame. It is better to just call this method at some acceptable "granularity" (play around with different granularities to find what sounds acceptable for a particular situation). -
loadMidi
Loads the specified MIDI file, and saves the source information about it.- Parameters:
toLoop
- Midi file should loop or play once.sourcename
- Source identifier.filenameURL
- Filename/URL of the MIDI file to load.
-
unloadMidi
public void unloadMidi()Unloads the current Midi file. -
midiSourcename
Checks if the sourcename matches the midi source.- Parameters:
sourcename
- Source identifier.- Returns:
- True if sourcename and midi sourcename match.
-
getSource
Returns the Source object identified by the specified name.- Parameters:
sourcename
- The source's name.- Returns:
- The source, or null if not found.
-
listenerMoved
public void listenerMoved()Tells all the sources that the listener has moved. -
getSources
Returns the sources map.- Returns:
- Map of all sources.
-
getListenerData
Returns information about the listener.- Returns:
- A ListenerData object.
-
reverseByteOrder
public boolean reverseByteOrder()Indicates whether or not this library requires some codecs to reverse-order the audio data they generate.- Returns:
- True if audio data should be reverse-ordered.
-
getTitle
Returns the short title of this library type.- Returns:
- A short title.
-
getDescription
Returns a longer description of this library type.- Returns:
- A longer description.
-
getClassName
Returns the name of the class.- Returns:
- "Library" + library title.
-
message
Prints a message.- Parameters:
message
- Message to print.
-
importantMessage
Prints an important message.- Parameters:
message
- Message to print.
-
errorCheck
Prints the specified message if error is true.- Parameters:
error
- True or False.message
- Message to print if error is true.- Returns:
- True if error is true.
-
errorMessage
Prints an error message.- Parameters:
message
- Message to print.
-
printStackTrace
Prints an exception's error message followed by the stack trace.- Parameters:
e
- Exception containing the information to print.
-