-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAePlayWave.java
120 lines (105 loc) · 3.88 KB
/
AePlayWave.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
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
/*
* Adapted from:
* http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml
*/
public class AePlayWave extends Thread {
//TODO quit() often does not stop the music immediately but takes several seconds
public static final int DEFAULT_BUFFER_SIZE = 524288; // 128Kb
public static final int PRIME_CUP_BUFFER_SIZE = 11534336;
public static final int PETIT_CUP_BUFFER_SIZE = 12478054;
public static final String BATTLE_MUSIC_PRIME_CUP = "music/prime-cup1-3.wav";
public static final String BATTLE_MUSIC_PETIT_CUP = "music/petit-cup1-3.wav";
public static final String SUPER_EFFECTIVE = "sfx/super_effective.wav";
public static final String NOT_EFFECTIVE = "sfx/not_effective.wav";
public static final String NORMAL_EFFECTIVE = "sfx/normal_effective.wav";
private String filename;
private Position curPosition;
private final int EXTERNAL_BUFFER_SIZE;
private volatile boolean stopped = false;
enum Position {
LEFT, RIGHT, NORMAL
};
public AePlayWave(String wavfile) {
filename = wavfile;
curPosition = Position.NORMAL;
EXTERNAL_BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
}
public AePlayWave(String wavfile, Position p) {
filename = wavfile;
curPosition = p;
EXTERNAL_BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
}
public AePlayWave(String wavfile, int size) {
filename = wavfile;
curPosition = Position.NORMAL;
EXTERNAL_BUFFER_SIZE = size;
}
public void run() {
File soundFile = new File(filename);
if (!soundFile.exists()) {
return;
}
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
if (auline.isControlSupported(FloatControl.Type.PAN)) {
FloatControl pan = (FloatControl) auline
.getControl(FloatControl.Type.PAN);
if (curPosition == Position.RIGHT)
pan.setValue(1.0f);
else if (curPosition == Position.LEFT)
pan.setValue(-1.0f);
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
try {
int startRead = 0;
while (nBytesRead != -1 && !stopped) {
nBytesRead = audioInputStream.read(abData, startRead, startRead + 2048);
if (nBytesRead >= 0)
auline.write(abData, startRead, nBytesRead);
startRead += nBytesRead;
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
public void quit() {
stopped = true;
}
}