Path: blob/main/src/lwjgl/java/paulscode/sound/Channel.java
8644 views
package paulscode.sound;12import java.util.LinkedList;3import javax.sound.sampled.AudioFormat;45/**6* The Channel class is the base class which can be extended for7* library-specific channels. It is also used in the "no-sound" library.8* A channel is a reserved sound-card voice through which sources are played9* back. Channels can be either streaming channels or normal (non-streaming)10* ones. For consistant naming conventions, each sub-class should have the11* name prefix "Channel".12*<br><br>13*<b><i> SoundSystem License:</b></i><br><b><br>14* You are free to use this library for any purpose, commercial or otherwise.15* You may modify this library or source code, and distribute it any way you16* like, provided the following conditions are met:17*<br>18* 1) You may not falsely claim to be the author of this library or any19* unmodified portion of it.20*<br>21* 2) You may not copyright this library or a modified version of it and then22* sue me for copyright infringement.23*<br>24* 3) If you modify the source code, you must clearly document the changes25* made before redistributing the modified source code, so other users know26* it is not the original code.27*<br>28* 4) You are not required to give me credit for this library in any derived29* work, but if you do, you must also mention my website:30* http://www.paulscode.com31*<br>32* 5) I the author will not be responsible for any damages (physical,33* financial, or otherwise) caused by the use if this library or any part34* of it.35*<br>36* 6) I the author do not guarantee, warrant, or make any representations,37* either expressed or implied, regarding the use of this library or any38* part of it.39* <br><br>40* Author: Paul Lamb41* <br>42* http://www.paulscode.com43* </b>44*/45public class Channel46{47/**48* The library class associated with this type of channel.49*/50protected Class libraryType = Library.class;5152/**53* Global identifier for the type of channel (normal or streaming). Possible54* values for this varriable can be found in the55* {@link paulscode.sound.SoundSystemConfig SoundSystemConfig} class.56*/57public int channelType;5859/**60* Processes status messages, warnings, and error messages.61*/62private SoundSystemLogger logger;6364/**65* Whatever source is attached to this channel.66*/67public Source attachedSource = null;6869/**70* Cumulative counter of the buffers played then unqued.71*/72public int buffersUnqueued = 0;7374/**75* Constructor: Takes channelType identifier as a paramater. Possible values76* for channel type can be found in the77* {@link paulscode.sound.SoundSystemConfig SoundSystemConfig} class.78* @param type Type of channel (normal or streaming).79*/80public Channel( int type )81{82// grab a handle to the message logger:83logger = SoundSystemConfig.getLogger();8485channelType = type;86}8788/**89* Shuts the channel down and removes references to all instantiated objects.90*/91public void cleanup()92{93logger = null;94}9596/**97* Queues up the initial byte[] buffers of data to be streamed.98* @param bufferList List of the first buffers to be played for a streaming source.99* @return False if an error occurred or if end of stream was reached.100*/101public boolean preLoadBuffers( LinkedList<byte[]> bufferList )102{103return true;104}105106/**107* Queues up a byte[] buffer of data to be streamed.108* @param buffer The next buffer to be played for a streaming source.109* @return False if an error occurred or if the channel is shutting down.110*/111public boolean queueBuffer( byte[] buffer )112{113return false;114}115116/**117* Feeds raw data to the stream.118* @param buffer Buffer containing raw audio data to stream.119* @return Number of prior buffers that have been processed.120*/121public int feedRawAudioData( byte[] buffer )122{123return 1;124}125126/**127* Returns the number of queued byte[] buffers that have finished playing.128* @return Number of buffers processed.129*/130public int buffersProcessed()131{132return 0;133}134135/**136* Calculates the number of milliseconds since the channel began playing.137* @return Milliseconds, or -1 if unable to calculate.138*/139public float millisecondsPlayed()140{141return -1;142}143/**144* Plays the next queued byte[] buffer. This method is run from the seperate145* {@link paulscode.sound.StreamThread StreamThread}.146* @return False when no more buffers are left to process.147*/148public boolean processBuffer()149{150return false;151}152153/**154* Sets the channel up to receive the specified audio format.155*/156public void setAudioFormat( AudioFormat audioFormat )157{}158159/**160* Dequeues all previously queued data.161*/162public void flush()163{}164165/**166* Stops the channel, dequeues any queued data, and closes the channel.167*/168public void close()169{}170171/**172* Plays the currently attached normal source, opens this channel up for173* streaming, or resumes playback if this channel was paused.174*/175public void play()176{}177178/**179* Temporarily stops playback for this channel.180*/181public void pause()182{}183184/**185* Stops playback for this channel and rewinds the attached source to the186* beginning.187*/188public void stop()189{}190191/**192* Rewinds the attached source to the beginning. Stops the source if it was193* paused.194*/195public void rewind()196{}197198/**199* Used to determine if a channel is actively playing a source. This method200* will return false if the channel is paused or stopped and when no data is201* queued to be streamed.202* @return True if this channel is playing a source.203*/204public boolean playing()205{206return false;207}208209/**210* Returns the name of the class.211* @return "Channel" + library title.212*/213public String getClassName()214{215String libTitle = SoundSystemConfig.getLibraryTitle( libraryType );216217if( libTitle.equals( "No Sound" ) )218return "Channel";219else220return "Channel" + libTitle;221}222223/**224* Prints a message.225* @param message Message to print.226*/227protected void message( String message )228{229logger.message( message, 0 );230}231232/**233* Prints an important message.234* @param message Message to print.235*/236protected void importantMessage( String message )237{238logger.importantMessage( message, 0 );239}240241/**242* Prints the specified message if error is true.243* @param error True or False.244* @param message Message to print if error is true.245* @return True if error is true.246*/247protected boolean errorCheck( boolean error, String message )248{249return logger.errorCheck( error, getClassName(), message, 0 );250}251252/**253* Prints an error message.254* @param message Message to print.255*/256protected void errorMessage( String message )257{258logger.errorMessage( getClassName(), message, 0 );259}260261/**262* Prints an exception's error message followed by the stack trace.263* @param e Exception containing the information to print.264*/265protected void printStackTrace( Exception e )266{267logger.printStackTrace( e, 1 );268}269}270271272