Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/paulscode/sound/Channel.java
8644 views
1
package paulscode.sound;
2
3
import java.util.LinkedList;
4
import javax.sound.sampled.AudioFormat;
5
6
/**
7
* The Channel class is the base class which can be extended for
8
* library-specific channels. It is also used in the "no-sound" library.
9
* A channel is a reserved sound-card voice through which sources are played
10
* back. Channels can be either streaming channels or normal (non-streaming)
11
* ones. For consistant naming conventions, each sub-class should have the
12
* name prefix "Channel".
13
*<br><br>
14
*<b><i> SoundSystem License:</b></i><br><b><br>
15
* You are free to use this library for any purpose, commercial or otherwise.
16
* You may modify this library or source code, and distribute it any way you
17
* like, provided the following conditions are met:
18
*<br>
19
* 1) You may not falsely claim to be the author of this library or any
20
* unmodified portion of it.
21
*<br>
22
* 2) You may not copyright this library or a modified version of it and then
23
* sue me for copyright infringement.
24
*<br>
25
* 3) If you modify the source code, you must clearly document the changes
26
* made before redistributing the modified source code, so other users know
27
* it is not the original code.
28
*<br>
29
* 4) You are not required to give me credit for this library in any derived
30
* work, but if you do, you must also mention my website:
31
* http://www.paulscode.com
32
*<br>
33
* 5) I the author will not be responsible for any damages (physical,
34
* financial, or otherwise) caused by the use if this library or any part
35
* of it.
36
*<br>
37
* 6) I the author do not guarantee, warrant, or make any representations,
38
* either expressed or implied, regarding the use of this library or any
39
* part of it.
40
* <br><br>
41
* Author: Paul Lamb
42
* <br>
43
* http://www.paulscode.com
44
* </b>
45
*/
46
public class Channel
47
{
48
/**
49
* The library class associated with this type of channel.
50
*/
51
protected Class libraryType = Library.class;
52
53
/**
54
* Global identifier for the type of channel (normal or streaming). Possible
55
* values for this varriable can be found in the
56
* {@link paulscode.sound.SoundSystemConfig SoundSystemConfig} class.
57
*/
58
public int channelType;
59
60
/**
61
* Processes status messages, warnings, and error messages.
62
*/
63
private SoundSystemLogger logger;
64
65
/**
66
* Whatever source is attached to this channel.
67
*/
68
public Source attachedSource = null;
69
70
/**
71
* Cumulative counter of the buffers played then unqued.
72
*/
73
public int buffersUnqueued = 0;
74
75
/**
76
* Constructor: Takes channelType identifier as a paramater. Possible values
77
* for channel type can be found in the
78
* {@link paulscode.sound.SoundSystemConfig SoundSystemConfig} class.
79
* @param type Type of channel (normal or streaming).
80
*/
81
public Channel( int type )
82
{
83
// grab a handle to the message logger:
84
logger = SoundSystemConfig.getLogger();
85
86
channelType = type;
87
}
88
89
/**
90
* Shuts the channel down and removes references to all instantiated objects.
91
*/
92
public void cleanup()
93
{
94
logger = null;
95
}
96
97
/**
98
* Queues up the initial byte[] buffers of data to be streamed.
99
* @param bufferList List of the first buffers to be played for a streaming source.
100
* @return False if an error occurred or if end of stream was reached.
101
*/
102
public boolean preLoadBuffers( LinkedList<byte[]> bufferList )
103
{
104
return true;
105
}
106
107
/**
108
* Queues up a byte[] buffer of data to be streamed.
109
* @param buffer The next buffer to be played for a streaming source.
110
* @return False if an error occurred or if the channel is shutting down.
111
*/
112
public boolean queueBuffer( byte[] buffer )
113
{
114
return false;
115
}
116
117
/**
118
* Feeds raw data to the stream.
119
* @param buffer Buffer containing raw audio data to stream.
120
* @return Number of prior buffers that have been processed.
121
*/
122
public int feedRawAudioData( byte[] buffer )
123
{
124
return 1;
125
}
126
127
/**
128
* Returns the number of queued byte[] buffers that have finished playing.
129
* @return Number of buffers processed.
130
*/
131
public int buffersProcessed()
132
{
133
return 0;
134
}
135
136
/**
137
* Calculates the number of milliseconds since the channel began playing.
138
* @return Milliseconds, or -1 if unable to calculate.
139
*/
140
public float millisecondsPlayed()
141
{
142
return -1;
143
}
144
/**
145
* Plays the next queued byte[] buffer. This method is run from the seperate
146
* {@link paulscode.sound.StreamThread StreamThread}.
147
* @return False when no more buffers are left to process.
148
*/
149
public boolean processBuffer()
150
{
151
return false;
152
}
153
154
/**
155
* Sets the channel up to receive the specified audio format.
156
*/
157
public void setAudioFormat( AudioFormat audioFormat )
158
{}
159
160
/**
161
* Dequeues all previously queued data.
162
*/
163
public void flush()
164
{}
165
166
/**
167
* Stops the channel, dequeues any queued data, and closes the channel.
168
*/
169
public void close()
170
{}
171
172
/**
173
* Plays the currently attached normal source, opens this channel up for
174
* streaming, or resumes playback if this channel was paused.
175
*/
176
public void play()
177
{}
178
179
/**
180
* Temporarily stops playback for this channel.
181
*/
182
public void pause()
183
{}
184
185
/**
186
* Stops playback for this channel and rewinds the attached source to the
187
* beginning.
188
*/
189
public void stop()
190
{}
191
192
/**
193
* Rewinds the attached source to the beginning. Stops the source if it was
194
* paused.
195
*/
196
public void rewind()
197
{}
198
199
/**
200
* Used to determine if a channel is actively playing a source. This method
201
* will return false if the channel is paused or stopped and when no data is
202
* queued to be streamed.
203
* @return True if this channel is playing a source.
204
*/
205
public boolean playing()
206
{
207
return false;
208
}
209
210
/**
211
* Returns the name of the class.
212
* @return "Channel" + library title.
213
*/
214
public String getClassName()
215
{
216
String libTitle = SoundSystemConfig.getLibraryTitle( libraryType );
217
218
if( libTitle.equals( "No Sound" ) )
219
return "Channel";
220
else
221
return "Channel" + libTitle;
222
}
223
224
/**
225
* Prints a message.
226
* @param message Message to print.
227
*/
228
protected void message( String message )
229
{
230
logger.message( message, 0 );
231
}
232
233
/**
234
* Prints an important message.
235
* @param message Message to print.
236
*/
237
protected void importantMessage( String message )
238
{
239
logger.importantMessage( message, 0 );
240
}
241
242
/**
243
* Prints the specified message if error is true.
244
* @param error True or False.
245
* @param message Message to print if error is true.
246
* @return True if error is true.
247
*/
248
protected boolean errorCheck( boolean error, String message )
249
{
250
return logger.errorCheck( error, getClassName(), message, 0 );
251
}
252
253
/**
254
* Prints an error message.
255
* @param message Message to print.
256
*/
257
protected void errorMessage( String message )
258
{
259
logger.errorMessage( getClassName(), message, 0 );
260
}
261
262
/**
263
* Prints an exception's error message followed by the stack trace.
264
* @param e Exception containing the information to print.
265
*/
266
protected void printStackTrace( Exception e )
267
{
268
logger.printStackTrace( e, 1 );
269
}
270
}
271
272