-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWaveEngine.java
More file actions
78 lines (67 loc) · 2.48 KB
/
Copy pathWaveEngine.java
File metadata and controls
78 lines (67 loc) · 2.48 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
import java.io.*;
import java.util.*;
import javax.sound.sampled.*;
public class WaveEngine implements LineListener {
// Sound name -> Sound clip
private HashMap<String, Clip> clipMap;
private int maxClips;
private int counter = 0;
public WaveEngine() {
this(256);
}
public WaveEngine(int maxClips) {
this.maxClips = maxClips;
clipMap = new HashMap<String, Clip>(maxClips);
}
public void load(String name, String filename) {
if (counter == maxClips) {
System.out.println("ERROR: cannot load a sound clip any more.");
return;
}
try {
AudioInputStream stream = AudioSystem.getAudioInputStream(
getClass().getResource(filename));
AudioFormat format = stream.getFormat();
// transform ulaw/alaw format into pcm format
if ((format.getEncoding() == AudioFormat.Encoding.ULAW)
|| (format.getEncoding() == AudioFormat.Encoding.ALAW)) {
AudioFormat newFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits() * 2, format.getChannels(),
format.getFrameSize() * 2, format.getFrameRate(), true);
stream = AudioSystem.getAudioInputStream(newFormat, stream);
format = newFormat;
}
DataLine.Info info = new DataLine.Info(Clip.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("ERROR: not supported format");
System.exit(0);
}
Clip clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(this);
clip.open(stream);
clipMap.put(name, clip);
stream.close();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public void play(String name) {
Clip clip = (Clip)clipMap.get(name);
if (clip != null) {
clip.start();
}
}
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
Clip clip = (Clip) event.getSource();
clip.stop();
clip.setFramePosition(0);
}
}
}