-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAudioClip.java
More file actions
408 lines (337 loc) · 7.99 KB
/
Copy pathAudioClip.java
File metadata and controls
408 lines (337 loc) · 7.99 KB
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
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.net.URL;
import java.net.URI;
import java.io.File;
import java.net.URISyntaxException;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.MediaException;
import javafx.stage.Stage;
import javafx.util.Duration;
import javax.swing.SwingUtilities;
import java.util.concurrent.CountDownLatch;
import javafx.embed.swing.JFXPanel;
import javafx.application.Platform;
/**
* This class makes Clips easier and more intuitive to use
*
* @author Ofek Gila
* @since May 19th, 2015
* @lastedited May 22nd, 2015
* @version 2.8
*/
public class AudioClip extends Application implements Runnable {
/**
* double length The length of the audioclip when initialized
*/
public double length;
private String soundLocation;
private File file;
private Media clip;
private MediaPlayer player;
private boolean loop;
private boolean running;
private double startat, stopat;
private double rate, volume;
private int numCycles, totalCycles, cycleOn;
final CountDownLatch latch = new CountDownLatch(1);
public AudioClip(AudioClip ac) {
load(ac);
}
public AudioClip(String soundLocation, double start, double stop) {
this(soundLocation, start, stop, false);
}
public AudioClip(String soundLocation, double start, double stop, boolean loop) {
load(soundLocation, start, stop, loop);
length = length();
}
public AudioClip(String soundLocation, boolean loop) {
this(soundLocation, 0, -1, loop);
}
public AudioClip(String soundLocation) {
this(soundLocation, false);
}
public AudioClip(File f) {
this(f, 0, -1, false);
}
public AudioClip(File f, boolean loop) {
this(f, 0, -1, loop);
}
public AudioClip(File f, double start, double stop) {
this(f, start, stop, false);
}
public AudioClip(File f, double start, double stop, boolean loop) {
load(f, start, stop, loop);
length = length();
}
public String getLocation() {
return soundLocation;
}
public String getName() {
return soundLocation.substring(soundLocation.lastIndexOf("\\")+1, soundLocation.lastIndexOf("."));
}
public String getExtension() {
try {
return soundLocation.substring(soundLocation.lastIndexOf(".")).substring(1);
}
catch (StringIndexOutOfBoundsException e) {
System.err.println("Cannot find extension in " + soundLocation);
return "";
}
}
public String toString() {
return getName();
}
public void reload() {
dispose();
load();
}
public void dispose() {
player.dispose();
}
public void load() {
player = new MediaPlayer(clip);
player.setOnEndOfMedia(this);
setStart(startat);
setStop(stopat);
setRate(rate);
setVolume(volume);
}
public void replay() {
reload();
play();
}
public void reInit(String soundLocation) {
reInit(soundLocation, 0, -1, false);
}
public void reInit(String soundLocation, boolean loop) {
reInit(soundLocation, loop);
}
public void reInit(File f) {
reInit(f, 0, -1, false);
}
public void reInit(File f, boolean loop) {
reInit(f, 0, -1, loop);
}
public void reInit(File f, double start, double stop) {
reInit(f, start, stop, false);
}
public void reInit(File f, double start, double stop, boolean loop) {
dispose();
load(f, start, stop, loop);
length = length();
}
public void reInit(String soundLocation, double start, double stop) {
reInit(soundLocation, start, stop, false);
}
public void reInit(String soundLocation, double start, double stop, boolean loop) {
dispose();
load(soundLocation, start, stop, loop);
length = length();
}
public void load(String soundLocation) {
load(soundLocation, 0, -1, false);
}
public void load(AudioClip ac) {
noException();
clip = ac.getClip();
player = new MediaPlayer(clip);
player.setOnEndOfMedia(this);
play(); stop(); while (Double.isNaN(length()));
length = ac.length();
setStart(ac.getStart());
setStop(ac.getStop());
setVolume(ac.getVolume());
setRate(ac.getRate());
startat = getStart(); stopat = getStop();
setLoop(ac.getLoop());
numCycles = ac.getCycleCount();
totalCycles = 0;
cycleOn = 0;
running = false;
}
public void load (File f, double start, double stop, boolean loop) {
noException();
this.file = f;
this.soundLocation = f.getName();
URL resource;
try {
resource = f.toURI().toURL();
try {
clip = new Media(resource.toString());
} catch (NullPointerException e) {
System.err.println("Cannot find file " + soundLocation);
System.exit(1);
} catch (MediaException e) {
System.err.println("Unsupported file format: " + soundLocation);
System.exit(1);
}
} catch (MalformedURLException e) {
System.err.println("Bad URL");
System.exit(1);
}
player = new MediaPlayer(clip);
player.setOnEndOfMedia(this);
play();
stop();
setStart(start);
if (stop >= 0)
setStop(stop);
startat = getStart(); stopat = getStop();
volume = rate = 1;
totalCycles = 0;
cycleOn = 0;
numCycles = 1;
running = false;
this.loop = loop;
while (Double.isNaN(length()));
}
public void load(String soundLocation, double start, double stop, boolean loop) {
load(new File(soundLocation), start, stop, loop);
}
public void loadNPlay() {
load();
play();
}
public double length() {
return (getStop() - getStart()) / getRate();
}
public Media getClip() {
return clip;
}
public MediaPlayer getPlayer() {
return player;
}
public void play() {
if (isRunning())
stop();
player.play();
running = true;
}
public void play(boolean fromstart) {
if (isRunning())
if (fromstart)
stop();
else return;
player.play();
running = true;
}
public void play(double position) {
double start = getStart();
setStart(position);
play();
setStart(start);
}
public void stop() {
player.stop();
running = false;
cycleOn = 0;
}
public void pause() {
player.pause();
running = false;
}
public void setStart(double time) {
player.setStartTime(new Duration(time));
startat = getStart();
}
public double getStart() {
return player.getStartTime().toMillis();
}
public void setStop(double time) {
player.setStopTime(new Duration(time));
stopat = getStop();
}
public double getStop() {
return player.getStopTime().toMillis();
}
public void setVolume(double volume) {
player.setVolume(volume);
this.volume = getVolume();
}
public double getVolume() {
return player.getVolume();
}
public double getRate() {
return player.getRate();
}
public void setRate(double rate) {
player.setRate(rate);
this.rate = getRate();
}
public void setPosition(double position) {
if (isPlaying())
player.seek(new Duration(position));
else {
play();
while(!isPlaying());
player.seek(new Duration(position));
pause();
}
}
public double getPosition() {
return player.getCurrentTime().toMillis();
}
public double lengthSeconds() {
return length / 1E3;
}
public void setLoop(boolean loop) {
this.loop = loop;
}
public boolean getLoop() {
return loop;
}
public void setCycleCount(int count) {
numCycles = count;
}
public int getCycleCount() {
return numCycles;
}
public int getCycleOn() {
return cycleOn;
}
public int getTotalCycles() {
return totalCycles;
}
public boolean isRunning() {
return running;
}
public boolean isPlaying() {
return player.getStatus().equals(Status.PLAYING);
}
@Override // runs when song stops
public void run() {
player.stop();
running = false;
totalCycles++;
if (loop)
replay();
else {
cycleOn++;
if (cycleOn < numCycles)
replay();
else {
cycleOn = 0;
reload();
}
}
}
@Override
public void start(Stage primaryStage) {}
public void noException() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFXPanel(); // initializes JavaFX environment
latch.countDown();
}
});
try {
latch.await();
} catch (Exception e) {System.err.println("ahh");}
}
}