forked from cravxx/unfm2jg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesktopSoundClip.java
134 lines (125 loc) · 3.87 KB
/
DesktopSoundClip.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
import fallk.logmaster.HLogger;
import javax.sound.sampled.*;
import java.applet.AudioClip;
import java.io.ByteArrayInputStream;
/**
* An implementation of AudioClip, optimized for desktop apps. The Sun-provided AudioClip for Applets is a bit buggy.
*
* @author DragShot
*/
class DesktopSoundClip implements AudioClip {
private Clip clip = null;
private AudioInputStream sound;
private boolean loaded = false;
private int lfrpo = -1;
private int cntcheck = 0;
/**
* Creates an unloaded, empty SoundClip.
*/
public DesktopSoundClip() {
}
/**
* Creates a SoundClip from an array of bytes.
*
* @param is An array of bytes with the audio file data.
*/
public DesktopSoundClip(byte[] is) {
try {
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(is);
sound = AudioSystem.getAudioInputStream(bytearrayinputstream);
sound.mark(is.length);
AudioFormat format = sound.getFormat();
// ULAW format is not directly supported, it needs to be wrapped.
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(),
format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2,
format.getFrameRate(), true); // big endian
sound = AudioSystem.getAudioInputStream(format, sound);
sound.mark(is.length * 2);
}
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
loaded = true;
} catch (Exception exception) {
HLogger.error(new StringBuilder().append("Loading Clip error: ").append(exception).toString());
loaded = false;
}
}
@Override
/**
* @inheritdoc
*/
public void play() {
if (loaded) {
try {
if (!clip.isOpen()) {
try {
clip.open(sound);
} catch (Exception exception) {
}
clip.loop(0);
} else {
clip.loop(1);
}
lfrpo = -1;
cntcheck = 5;
} catch (Exception exception) {
}
}
}
@Override
/**
* @inheritdoc
*/
public void loop() {
if (loaded) {
try {
if (!clip.isOpen()) {
try {
clip.open(sound);
} catch (Exception exception) {
}
}
clip.loop(70);
lfrpo = -2;
cntcheck = 0;
} catch (Exception exception) {
}
}
}
@Override
/**
* @inheritdoc
*/
public void stop() {
if (loaded) {
try {
clip.stop();
lfrpo = -1;
} catch (Exception exception) {
}
}
}
/**
* Checks the line state and closes it if it's not in use anymore. This helps to save memory and CPU resources in general, overall in cases where a lot of sounds are played.
*/
public void checkopen() {
if (loaded && clip.isOpen() && lfrpo != -2) {
if (cntcheck == 0) {
int i = clip.getFramePosition();
if (lfrpo == i && !clip.isRunning()) {
try {
clip.close();
sound.reset();
} catch (Exception exception) {
}
lfrpo = -1;
} else {
lfrpo = i;
}
} else {
cntcheck--;
}
}
}
}