/
StreamMusic.java
427 lines (387 loc) · 11.3 KB
/
StreamMusic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* Copyright (c) 2012, Finn Kuusisto
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package kuusisto.tinysound.internal;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import kuusisto.tinysound.Music;
import kuusisto.tinysound.TinySound;
public class StreamMusic implements Music {
private URL dataURL;
private Mixer mixer;
private MusicReference reference;
public StreamMusic(URL dataURL, Mixer mixer) {
this.dataURL = dataURL;
this.mixer = mixer;
try {
//TODO should this be thrown from here?
this.reference = new StreamMusicReference(this.dataURL, false,
false, 0, 0, 1.0);
this.mixer.registerMusicReference(this.reference);
} catch (IOException e) {
System.err.println("Failed to open stream for Music");
}
}
/**
* Play this Music and loop if specified.
* @param loop if this Music should loop
*/
public void play(boolean loop) {
this.reference.setPlaying(true);
this.reference.setLoop(loop);
}
/**
* Play this Music at the specified volume and loop if specified.
* @param loop if this Music should loop
* @param volume the volume to play the this Music
*/
public void play(boolean loop, double volume) {
this.reference.setPlaying(true);
this.setLoop(loop);
this.setVolume(volume);
}
/**
* Stop playing this Music and set its position to the beginning.
*/
public void stop() {
this.reference.setPlaying(false);
this.rewind();
}
/**
* Stop playing this Music and keep its current position.
*/
public void pause() {
this.reference.setPlaying(false);
}
/**
* Play this Music from its current position.
*/
public void resume() {
this.reference.setPlaying(true);
}
/**
* Set this Music's position to the beginning.
*/
public void rewind() {
this.reference.setPosition(0);
}
/**
* Set this Music's position to the loop position.
*/
public void rewindToLoopPosition() {
long byteIndex = this.reference.getLoopPosition();
this.reference.setPosition(byteIndex);
}
/**
* Determine if this Music is playing.
* @return true if this Music is playing
*/
public boolean playing() {
return this.reference.getPlaying();
}
/**
* Determine if this Music will loop.
* @return true if this Music will loop
*/
public boolean loop() {
return this.reference.getLoop();
}
/**
* Set whether this Music will loop.
* @param loop whether this Music will loop
*/
public void setLoop(boolean loop) {
this.reference.setLoop(loop);
}
@Override
public int getLoopPositionByFrame() {
int bytesPerChannelForFrame = TinySound.FORMAT.getFrameSize() /
TinySound.FORMAT.getChannels();
long byteIndex = this.reference.getLoopPosition();
return (int)(byteIndex / bytesPerChannelForFrame);
}
@Override
public double getLoopPositionBySeconds() {
int bytesPerChannelForFrame = TinySound.FORMAT.getFrameSize() /
TinySound.FORMAT.getChannels();
long byteIndex = this.reference.getLoopPosition();
return (byteIndex / (TinySound.FORMAT.getFrameRate() *
bytesPerChannelForFrame));
}
@Override
public void setLoopPositionByFrame(int frameIndex) {
//get the byte index for a channel
int bytesPerChannelForFrame = TinySound.FORMAT.getFrameSize() /
TinySound.FORMAT.getChannels();
long byteIndex = (long)(frameIndex * bytesPerChannelForFrame);
this.reference.setLoopPosition(byteIndex);
}
@Override
public void setLoopPositionBySeconds(double seconds) {
//get the byte index for a channel
int bytesPerChannelForFrame = TinySound.FORMAT.getFrameSize() /
TinySound.FORMAT.getChannels();
long byteIndex = (long)(seconds * TinySound.FORMAT.getFrameRate() *
bytesPerChannelForFrame);
this.reference.setLoopPosition(byteIndex);
}
/**
* Get the volume of this Music.
* @return volume of this Music
*/
public double getVolume() {
return this.reference.getVolume();
}
/**
* Set the volume of this Music.
* @param volume the desired volume of this Music
*/
public void setVolume(double volume) {
if (volume >= 0.0) {
this.reference.setVolume(volume);
}
}
@Override
public void unload() {
//unregister the reference
this.mixer.unRegisterMusicReference(this.reference);
this.reference.dispose();
this.mixer = null;
this.dataURL = null;
this.reference = null;
}
/////////////
//Reference//
/////////////
private static class StreamMusicReference implements MusicReference {
private URL url;
private InputStream data;
private long numBytesPerChannel; //not per frame, but the whole sound
private byte[] buf;
private byte[] skipBuf;
private boolean playing;
private boolean loop;
private long loopPosition;
private long position;
private double volume;
public StreamMusicReference(URL dataURL, boolean playing, boolean loop,
int loopPosition, int position, double volume)
throws IOException {
this.url = dataURL;
this.playing = playing;
this.loop = loop;
this.loopPosition = loopPosition;
this.position = position;
this.volume = volume;
this.buf = new byte[4];
this.skipBuf = new byte[50];
//now get the data stream
this.data = this.url.openStream();
}
@Override
public synchronized boolean getPlaying() {
return this.playing;
}
@Override
public synchronized boolean getLoop() {
return this.loop;
}
@Override
public synchronized long getPosition() {
return this.position;
}
@Override
public synchronized long getLoopPosition() {
return this.loopPosition;
}
/**
* Get the number of bytes remaining for each channel until the end of
* this Music.
* @return number of bytes remaining for each channel
*/
@Override
public synchronized long bytesAvailable() {
return this.numBytesPerChannel - this.position;
}
@Override
public synchronized double getVolume() {
return this.volume;
}
@Override
public synchronized void setPlaying(boolean playing) {
this.playing = playing;
}
@Override
public synchronized void setLoop(boolean loop) {
this.loop = loop;
}
@Override
public synchronized void setPosition(long position) {
if (position >= 0 && position < this.numBytesPerChannel) {
//if it's later, skip
if (position >= this.position) {
this.skipBytes(position - this.position);
}
else { //otherwise skip from the beginning
//first close our current stream
try {
this.data.close();
} catch (IOException e) {
//whatever...
}
//open a new stream
try {
this.data = this.url.openStream();
this.position = 0;
this.skipBytes(position);
} catch (IOException e) {
System.err.println("Failed to open stream for Music");
this.playing = false;
}
}
}
}
@Override
public synchronized void setLoopPosition(long loopPosition) {
if (loopPosition >= 0 && loopPosition < this.numBytesPerChannel) {
this.loopPosition = loopPosition;
}
}
@Override
public synchronized void setVolume(double volume) {
this.volume = volume;
}
@Override
public synchronized void nextTwoBytes(int[] data, boolean bigEndian) {
//try to read audio data
int tmpRead = 0;
int numRead = 0;
try {
while (numRead < this.buf.length && tmpRead != -1) {
tmpRead = this.data.read(this.buf, numRead,
this.buf.length - numRead);
numRead += tmpRead;
}
} catch (IOException e) {
//this shouldn't happen if the bytes were written correctly to
//the temp file, but this sound should now be invalid at least
this.position = this.numBytesPerChannel;
System.err.println("Failed reading bytes for stream sound");
}
//copy the values into the caller buffer
if (bigEndian) {
//left
data[0] = ((this.buf[0] << 8) |
(this.buf[1] & 0xFF));
//right
data[1] = ((this.buf[2] << 8) |
(this.buf[3] & 0xFF));
}
else {
//left
data[0] = ((this.buf[1] << 8) |
(this.buf[0] & 0xFF));
//right
data[1] = ((this.buf[3] << 8) |
(this.buf[2] & 0xFF));
}
//increment the position appropriately
if (tmpRead == -1) { //reached end of file in the middle of reading
//this should never happen
this.position = this.numBytesPerChannel;
}
else {
this.position += 2;
}
//wrap if looping
if (this.loop && this.position >= this.numBytesPerChannel) {
this.setPosition(this.loopPosition);
}
}
@Override
public synchronized void skipBytes(long num) {
//couple of shortcuts if we are going to complete the stream
if ((this.position + num) >= this.numBytesPerChannel) {
//if we're not looping, nothing special needs to happen
if (!this.loop) {
this.position += num;
return;
}
else {
//compute the next position
long loopLength = this.numBytesPerChannel -
this.loopPosition;
long bytesOver = (this.position + num) -
this.numBytesPerChannel;
long nextPosition = this.loopPosition +
(bytesOver % loopLength);
//and set us there
this.setPosition(nextPosition);
return;
}
}
//this is the number of bytes to skip per channel, so double it
long numSkip = num * 2;
//spin read since skip is not always supported apparently and won't
//guarantee a correct skip amount
int tmpRead = 0;
int numRead = 0;
try {
while (numRead < numSkip && tmpRead != -1) {
tmpRead = this.data.read(this.skipBuf, numRead,
this.skipBuf.length - numRead);
numRead += tmpRead;
}
} catch (IOException e) {
//hmm... I guess invalidate this reference
this.position = this.numBytesPerChannel;
}
//increment the position appropriately
if (tmpRead == -1) { //reached end of file in the middle of reading
this.position = this.numBytesPerChannel;
}
else {
this.position += num;
}
}
/**
* Does any cleanup necessary to dispose of resources in use by this
* MusicReference.
*/
@Override
public synchronized void dispose() {
this.playing = false;
this.position = this.numBytesPerChannel;
this.url = null;
try {
this.data.close();
} catch (IOException e) {
//whatever... this should never happen
}
}
}
}