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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
package NET.worlds.scape;
import NET.worlds.network.Cache;
import NET.worlds.network.URL;
class SoundState implements State {
public static final int IDLE = 0;
public static final int start = 1;
public static final int intel0 = 1000;
public static final int intel10 = 1010;
public static final int intel11 = 1011;
public static final int intel20 = 1020;
public static final int intel21 = 1021;
public static final int intel30 = 1030;
public static final int intel31 = 1031;
public static final int intel32 = 1032;
public static final int intel33 = 1033;
public static final int realAudio0 = 2000;
public static final int realAudio10 = 2010;
public static final int realAudio11 = 2011;
private static SoundState instance = new SoundState();
private static URL dummy = URL.make("home:dummy.wav");
private SoundState() {
}
private void debugOut(int n, String s) {
if (Sound.debugLevel > n) {
System.out.println(s);
}
}
public static State instance() {
return instance;
}
@Override
public Object doState(StateContext sc, Object obj) {
Sound sound = (Sound)sc;
Object rv;
switch (sound.state) {
case 1:
rv = this.start(sound, obj);
break;
case 1000:
rv = this.intel0(sound, obj);
break;
case 1010:
rv = this.intel10(sound, obj);
break;
case 1011:
rv = this.intel11(sound, obj);
break;
case 1020:
rv = this.intel20(sound, obj);
break;
case 1021:
rv = this.intel21(sound, obj);
break;
case 1030:
rv = this.intel30(sound, obj);
break;
case 1031:
rv = this.intel31(sound, obj);
break;
case 1032:
rv = this.intel32(sound, obj);
break;
case 1033:
rv = this.intel33(sound, obj);
break;
case 2000:
rv = this.realAudio0(sound, obj);
break;
case 2010:
rv = this.realAudio10(sound, obj);
break;
case 2011:
rv = this.realAudio11(sound, obj);
break;
default:
rv = this.error(sound, obj);
}
return rv;
}
private Object error(Sound sound, Object obj) {
this.debugOut(2, "Unknown state in SoundState " + sound.state);
return null;
}
private Object start(Sound sound, Object obj) {
this.debugOut(6, "Entering SoundState ");
assert sound.backgroundState == 1;
if (sound.isRealAudio(sound.getURL())) {
sound.changeState(2000);
} else {
sound.changeState(1000);
}
sound.doState(null);
return null;
}
private Object intel0(Sound sound, Object obj) {
this.debugOut(6, "Entering intel0 ");
assert sound.backgroundState == 1;
if (!SoundResource.instance().syncLock(1)) {
return null;
} else if (!sound.getURL().isRemote()) {
sound.changeState(1010);
BackgroundLoader.get(sound, sound.getURL());
return null;
} else {
sound.cachedFile = Cache.getFile(sound.getURL());
if (sound.cachedFile.done()) {
sound.ensureClosure();
sound.changeState(1020);
BackgroundLoader.get(sound, sound.getURL());
return null;
} else {
sound.cachedFile.finalize();
sound.cachedFile = null;
sound.changeState(1030);
BackgroundLoader.get(sound, dummy);
return null;
}
}
}
private Object intel10(Sound sound, Object localName) {
assert sound.backgroundState == 2;
assert localName != null;
SoundResource.instance().asyncLock();
sound.changeState(1011);
if (sound.openPlayer((URL)localName)) {
return localName;
} else {
this.debugOut(2, "intel10: couldn't open sound " + sound.getURL());
return null;
}
}
private Object intel11(Sound sound, Object obj) {
this.debugOut(6, "Entering intel11 ");
assert sound.backgroundState == 1;
if (obj != null) {
if (sound.updateSound()) {
sound.player.start(sound.getRepeat());
} else {
sound.closePlayer();
}
}
sound.changeState(0);
return null;
}
private Object intel20(Sound sound, Object obj) {
this.debugOut(6, "Entering intel20 ");
assert sound.backgroundState == 2;
SoundResource.instance().asyncLock();
if (sound.openPlayer((URL)obj)) {
sound.changeState(1021);
return obj;
} else {
this.debugOut(2, "intel20: couldn't open player on sound " + sound.getURL());
sound.changeState(1021);
return null;
}
}
private Object intel21(Sound sound, Object obj) {
this.debugOut(6, "Entering intel21 ");
assert sound.backgroundState == 1;
if (obj == null) {
sound.unlockCache();
} else if (sound.updateSound()) {
this.debugOut(6, "intel21 starting sound");
sound.player.start(sound.getRepeat());
} else {
this.debugOut(6, "intel21 closing player");
sound.closePlayer();
}
sound.changeState(0);
return null;
}
private Object intel30(Sound sound, Object obj) {
this.debugOut(6, "Entering intel30 ");
assert sound.backgroundState == 2;
SoundResource.instance().asyncLock();
if (sound.openPlayer(dummy)) {
sound.changeState(1031);
return obj;
} else {
this.debugOut(2, "intel30: couldn't open player on file " + sound.getURL());
sound.changeState(1031);
return null;
}
}
private Object intel31(Sound sound, Object obj) {
this.debugOut(6, "Entering intel31 ");
assert sound.backgroundState == 1;
if (obj == null) {
sound.changeState(0);
} else if (sound.updateSound()) {
sound.player.start(1);
sound.changeState(1032);
BackgroundLoader.get(sound, sound.getURL());
} else {
sound.closePlayer();
sound.changeState(0);
}
return null;
}
private Object intel32(Sound sound, Object obj) {
this.debugOut(6, "Entering intel32 ");
assert sound.backgroundState == 2;
sound.changeState(1033);
return obj;
}
private Object intel33(Sound sound, Object obj) {
this.debugOut(6, "Entering intel33 ");
assert sound.backgroundState == 1;
if (obj != null && sound.playAfterLoading) {
sound.closePlayer();
sound.changeState(1000);
sound.doState(obj);
} else {
sound.changeState(0);
}
return null;
}
private Object realAudio0(Sound sound, Object obj) {
this.debugOut(6, "Entering RealAudio0 ");
assert sound.backgroundState == 1;
if (!SoundResource.instance().syncLock(2)) {
return null;
} else {
sound.changeState(2010);
BackgroundLoader.get(sound, dummy);
return null;
}
}
private Object realAudio10(Sound sound, Object obj) {
this.debugOut(6, "Entering realAudio10 ");
assert sound.backgroundState == 2;
SoundResource.instance().asyncLock();
if (sound.openPlayer(sound.getURL())) {
sound.changeState(2011);
return sound;
} else {
return null;
}
}
private Object realAudio11(Sound sound, Object obj) {
this.debugOut(6, "Entering realAudio11 ");
assert sound.backgroundState == 1;
if (obj != null) {
if (sound.updateSound()) {
sound.player.start(sound.getRepeat());
} else {
sound.closePlayer();
}
}
sound.changeState(0);
return null;
}
}
|