-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioPlayer.java
More file actions
152 lines (133 loc) · 4.08 KB
/
Copy pathAudioPlayer.java
File metadata and controls
152 lines (133 loc) · 4.08 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
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
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.LineListener;
import javax.sound.sampled.SourceDataLine;
import marytts.util.data.audio.MonoAudioInputStream;
import marytts.util.data.audio.StereoAudioInputStream;
/**
* A single Thread Audio Player Once used it has to be initialized again
*
* @author GOXR3PLUS
* @version modified by JonnyJack (me)
*/
public class AudioPlayer extends Thread {
public static final int MONO = 0;
public static final int STEREO = 3;
public static final int LEFT_ONLY = 1;
public static final int RIGHT_ONLY = 2;
private AudioInputStream ais;
private LineListener lineListener;
private SourceDataLine line;
private int outputMode;
private Status status = Status.WAITING;
private boolean exitRequested = false;
private float gain = 1.0f;
public enum Status {
WAITING,
PLAYING;
}
public AudioPlayer() {
}
public void setAudio(AudioInputStream audio) {
if (status == Status.PLAYING) {
throw new IllegalStateException("Cannot set audio while playing");
}
this.ais = audio;
}
/**
* Cancel the AudioPlayer which will cause the Thread to exit
*/
public void cancel() {
if (line != null) {
line.stop();
}
exitRequested = true;
}
/**
* Returns the GainValue
*/
public float getGainValue() {
return gain;
}
/**
* Sets Gain value. Line should be opened before calling this method. Linear
* scale 0.0 <--> 1.0 Threshold Coed. : 1/2 to avoid saturation.
*/
public void setGain(float fGain) {
// Set the value
gain = fGain;
// Better type
if (line != null && line.isControlSupported(FloatControl.Type.MASTER_GAIN))
((FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN))
.setValue((float) (20 * Math.log10(fGain <= 0.0 ? 0.0000 : fGain)));
}
@Override
public void run() {
status = Status.PLAYING;
AudioFormat audioFormat = ais.getFormat();
if (audioFormat.getChannels() == 1) {
if (outputMode != 0) {
ais = new StereoAudioInputStream(ais, outputMode);
audioFormat = ais.getFormat();
}
} else {
assert audioFormat.getChannels() == 2 : "Unexpected number of channels: " + audioFormat.getChannels();
if (outputMode == 0) {
ais = new MonoAudioInputStream(ais);
} else if (outputMode == 1 || outputMode == 2) {
ais = new StereoAudioInputStream(ais, outputMode);
} else {
assert outputMode == 3 : "Unexpected output mode: " + outputMode;
}
}
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
if (line == null) {
boolean bIsSupportedDirectly = AudioSystem.isLineSupported(info);
if (!bIsSupportedDirectly) {
AudioFormat sourceFormat = audioFormat;
AudioFormat targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
sourceFormat.getSampleRate(), sourceFormat.getSampleSizeInBits(),
sourceFormat.getChannels(),
sourceFormat.getChannels() * (sourceFormat.getSampleSizeInBits() / 8),
sourceFormat.getSampleRate(), sourceFormat.isBigEndian());
ais = AudioSystem.getAudioInputStream(targetFormat, ais);
audioFormat = ais.getFormat();
}
info = new DataLine.Info(SourceDataLine.class, audioFormat);
line = (SourceDataLine) AudioSystem.getLine(info);
}
if (lineListener != null) {
line.addLineListener(lineListener);
}
line.open(audioFormat);
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, null, ex);
return;
}
line.start();
setGain(getGainValue());
int nRead = 0;
byte[] abData = new byte[65532];
while ((nRead != -1) && (!exitRequested)) {
try {
nRead = ais.read(abData, 0, abData.length);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.WARNING, null, ex);
}
if (nRead >= 0) {
line.write(abData, 0, nRead);
}
}
if (!exitRequested) {
line.drain();
}
line.close();
}
}