Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/openal/DeferredSound.java
1461 views
1
package org.newdawn.slick.openal;
2
3
import java.io.IOException;
4
import java.io.InputStream;
5
6
import org.newdawn.slick.loading.DeferredResource;
7
import org.newdawn.slick.loading.LoadingList;
8
import org.newdawn.slick.util.Log;
9
10
/**
11
* A sound implementation that can load the actual sound file at a later
12
* point.
13
*
14
* @author kevin
15
*/
16
public class DeferredSound extends AudioImpl implements DeferredResource {
17
/** Indicate a OGG to be loaded */
18
public static final int OGG = 1;
19
/** Indicate a WAV to be loaded */
20
public static final int WAV = 2;
21
/** Indicate a MOD/XM to be loaded */
22
public static final int MOD = 3;
23
/** Indicate a AIF to be loaded */
24
public static final int AIF = 4;
25
26
/** The type of sound to be loader */
27
private int type;
28
/** The location of the sound this proxy wraps */
29
private String ref;
30
/** The loaded sound if it's already been brought up */
31
private Audio target;
32
/** The input stream to load the sound this proxy wraps from (can be null) */
33
private InputStream in;
34
35
/**
36
* Create a new sound on request to load
37
*
38
* @param ref The location of the sound to load
39
* @param type The type of sound to load
40
* @param in The input stream to load from
41
*/
42
public DeferredSound(String ref, InputStream in, int type) {
43
this.ref = ref;
44
this.type = type;
45
46
// nasty hack to detect when we're loading from a stream
47
if (ref.equals(in.toString())) {
48
this.in = in;
49
}
50
51
LoadingList.get().add(this);
52
}
53
54
/**
55
* Check if the target has already been loaded
56
*/
57
private void checkTarget() {
58
if (target == null) {
59
throw new RuntimeException("Attempt to use deferred sound before loading");
60
}
61
}
62
63
/**
64
* @see org.newdawn.slick.loading.DeferredResource#load()
65
*/
66
public void load() throws IOException {
67
boolean before = SoundStore.get().isDeferredLoading();
68
SoundStore.get().setDeferredLoading(false);
69
if (in != null) {
70
switch (type) {
71
case OGG:
72
target = SoundStore.get().getOgg(in);
73
break;
74
case WAV:
75
target = SoundStore.get().getWAV(in);
76
break;
77
case MOD:
78
target = SoundStore.get().getMOD(in);
79
break;
80
case AIF:
81
target = SoundStore.get().getAIF(in);
82
break;
83
default:
84
Log.error("Unrecognised sound type: "+type);
85
break;
86
}
87
} else {
88
switch (type) {
89
case OGG:
90
target = SoundStore.get().getOgg(ref);
91
break;
92
case WAV:
93
target = SoundStore.get().getWAV(ref);
94
break;
95
case MOD:
96
target = SoundStore.get().getMOD(ref);
97
break;
98
case AIF:
99
target = SoundStore.get().getAIF(ref);
100
break;
101
default:
102
Log.error("Unrecognised sound type: "+type);
103
break;
104
}
105
}
106
SoundStore.get().setDeferredLoading(before);
107
}
108
109
/**
110
* @see org.newdawn.slick.openal.AudioImpl#isPlaying()
111
*/
112
public boolean isPlaying() {
113
checkTarget();
114
115
return target.isPlaying();
116
}
117
118
/**
119
* @see org.newdawn.slick.openal.AudioImpl#playAsMusic(float, float, boolean)
120
*/
121
public int playAsMusic(float pitch, float gain, boolean loop) {
122
checkTarget();
123
return target.playAsMusic(pitch, gain, loop);
124
}
125
126
/**
127
* @see org.newdawn.slick.openal.AudioImpl#playAsSoundEffect(float, float, boolean)
128
*/
129
public int playAsSoundEffect(float pitch, float gain, boolean loop) {
130
checkTarget();
131
return target.playAsSoundEffect(pitch, gain, loop);
132
}
133
134
/**
135
* Play this sound as a sound effect
136
*
137
* @param pitch The pitch of the play back
138
* @param gain The gain of the play back
139
* @param loop True if we should loop
140
* @param x The x position of the sound
141
* @param y The y position of the sound
142
* @param z The z position of the sound
143
*/
144
public int playAsSoundEffect(float pitch, float gain, boolean loop, float x, float y, float z) {
145
checkTarget();
146
return target.playAsSoundEffect(pitch, gain, loop, x, y, z);
147
}
148
149
/**
150
* @see org.newdawn.slick.openal.AudioImpl#stop()
151
*/
152
public void stop() {
153
checkTarget();
154
target.stop();
155
}
156
157
/**
158
* @see org.newdawn.slick.loading.DeferredResource#getDescription()
159
*/
160
public String getDescription() {
161
return ref;
162
}
163
164
}
165
166