Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/Sound.java
1456 views
1
package org.newdawn.slick;
2
3
import java.net.URL;
4
5
import org.newdawn.slick.openal.Audio;
6
import org.newdawn.slick.openal.SoundStore;
7
import org.newdawn.slick.util.Log;
8
9
/**
10
* A single sound effect loaded from either OGG or XM/MOD file. Sounds are allocated to
11
* channels dynamically - if not channel is available the sound will not play.
12
*
13
* @author kevin
14
*/
15
public class Sound {
16
/** The internal sound effect represent this sound */
17
private Audio sound;
18
19
/**
20
* Create a new Sound
21
*
22
* @param url The location of the OGG or MOD/XM to load
23
* @throws SlickException Indicates a failure to load the sound effect
24
*/
25
public Sound(URL url) throws SlickException {
26
SoundStore.get().init();
27
String ref = url.getFile();
28
29
try {
30
if (ref.toLowerCase().endsWith(".ogg")) {
31
sound = SoundStore.get().getOgg(url.openStream());
32
} else if (ref.toLowerCase().endsWith(".wav")) {
33
sound = SoundStore.get().getWAV(url.openStream());
34
} else if (ref.toLowerCase().endsWith(".aif")) {
35
sound = SoundStore.get().getAIF(url.openStream());
36
} else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {
37
sound = SoundStore.get().getMOD(url.openStream());
38
} else {
39
throw new SlickException("Only .xm, .mod, .aif, .wav and .ogg are currently supported.");
40
}
41
} catch (Exception e) {
42
Log.error(e);
43
throw new SlickException("Failed to load sound: "+ref);
44
}
45
}
46
47
/**
48
* Create a new Sound
49
*
50
* @param ref The location of the OGG or MOD/XM to load
51
* @throws SlickException Indicates a failure to load the sound effect
52
*/
53
public Sound(String ref) throws SlickException {
54
SoundStore.get().init();
55
56
try {
57
if (ref.toLowerCase().endsWith(".ogg")) {
58
sound = SoundStore.get().getOgg(ref);
59
} else if (ref.toLowerCase().endsWith(".wav")) {
60
sound = SoundStore.get().getWAV(ref);
61
} else if (ref.toLowerCase().endsWith(".aif")) {
62
sound = SoundStore.get().getAIF(ref);
63
} else if (ref.toLowerCase().endsWith(".xm") || ref.toLowerCase().endsWith(".mod")) {
64
sound = SoundStore.get().getMOD(ref);
65
} else {
66
throw new SlickException("Only .xm, .mod, .aif, .wav and .ogg are currently supported.");
67
}
68
} catch (Exception e) {
69
Log.error(e);
70
throw new SlickException("Failed to load sound: "+ref);
71
}
72
}
73
74
/**
75
* Play this sound effect at default volume and pitch
76
*/
77
public void play() {
78
play(1.0f, 1.0f);
79
}
80
81
/**
82
* Play this sound effect at a given volume and pitch
83
*
84
* @param pitch The pitch to play the sound effect at
85
* @param volume The volumen to play the sound effect at
86
*/
87
public void play(float pitch, float volume) {
88
sound.playAsSoundEffect(pitch, volume * SoundStore.get().getSoundVolume(), false);
89
}
90
91
/**
92
* Play a sound effect from a particular location
93
*
94
* @param x The x position of the source of the effect
95
* @param y The y position of the source of the effect
96
* @param z The z position of the source of the effect
97
*/
98
public void playAt(float x, float y, float z) {
99
playAt(1.0f, 1.0f, x,y,z);
100
}
101
102
/**
103
* Play a sound effect from a particular location
104
*
105
* @param pitch The pitch to play the sound effect at
106
* @param volume The volumen to play the sound effect at
107
* @param x The x position of the source of the effect
108
* @param y The y position of the source of the effect
109
* @param z The z position of the source of the effect
110
*/
111
public void playAt(float pitch, float volume, float x, float y, float z) {
112
sound.playAsSoundEffect(pitch, volume * SoundStore.get().getSoundVolume(), false, x,y,z);
113
}
114
/**
115
* Loop this sound effect at default volume and pitch
116
*/
117
public void loop() {
118
loop(1.0f, 1.0f);
119
}
120
121
/**
122
* Loop this sound effect at a given volume and pitch
123
*
124
* @param pitch The pitch to play the sound effect at
125
* @param volume The volumen to play the sound effect at
126
*/
127
public void loop(float pitch, float volume) {
128
sound.playAsSoundEffect(pitch, volume * SoundStore.get().getSoundVolume(), true);
129
}
130
131
/**
132
* Check if the sound is currently playing
133
*
134
* @return True if the sound is playing
135
*/
136
public boolean playing() {
137
return sound.isPlaying();
138
}
139
140
/**
141
* Stop the sound being played
142
*/
143
public void stop() {
144
sound.stop();
145
}
146
}
147
148