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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
/*
* JOALSoundImpl.java
* Copyright (C) 2004
*
* $Id: JOALSoundImpl.java,v 1.12 2005-04-26 22:39:34 cawe Exp $
*/
package jake2.sound.joal;
import jake2.Defines;
import jake2.Globals;
import jake2.game.*;
import jake2.qcommon.*;
import jake2.sound.*;
import jake2.util.Lib;
import jake2.util.Vargs;
import java.awt.image.SampleModel;
import java.io.*;
import java.nio.IntBuffer;
import net.java.games.joal.*;
import net.java.games.joal.eax.EAX;
import net.java.games.joal.eax.EAXFactory;
/**
* JOALSoundImpl
*/
public final class JOALSoundImpl implements Sound {
static {
S.register(new JOALSoundImpl());
};
static AL al;
static ALC alc;
static EAX eax;
cvar_t s_volume;
private static final int MAX_SFX = Defines.MAX_SOUNDS * 2;
private int[] buffers = new int[MAX_SFX];
// singleton
private JOALSoundImpl() {
}
/**
* unpack OpenAL shared library on Linux
*/
private void unpack() {
String path = System.getProperty("user.home") + "/.jake2/libopenal.so";
File f = new File(path);
if (!f.exists()) {
try {
f.createNewFile();
InputStream in = getClass().getResourceAsStream("/libopenal.so");
OutputStream out = new FileOutputStream(f);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (Exception e) {
f.delete();
}
}
}
/* (non-Javadoc)
* @see jake2.sound.SoundImpl#Init()
*/
public boolean Init() {
// preload OpenAL native library
String os = System.getProperty("os.name");
if (os.startsWith("Linux")) {
unpack();
} else if (os.startsWith("Windows")) {
try {
System.loadLibrary("OpenAL32");
} catch (Throwable e) {}
}
try {
initOpenAL();
al = ALFactory.getAL();
checkError();
initOpenALExtensions();
} catch (OpenALException e) {
Com.Printf(e.getMessage() + '\n');
return false;
} catch (Throwable e) {
Com.DPrintf(e.getMessage() + '\n');
return false;
}
al.alGenBuffers(MAX_SFX, buffers);
s_volume = Cvar.Get("s_volume", "0.7", Defines.CVAR_ARCHIVE);
int count = Channel.init(al, buffers, s_volume.value);
Com.Printf("... using " + count + " channels\n");
al.alDistanceModel(AL.AL_INVERSE_DISTANCE_CLAMPED);
Cmd.AddCommand("play", new xcommand_t() {
public void execute() {
Play();
}
});
Cmd.AddCommand("stopsound", new xcommand_t() {
public void execute() {
StopAllSounds();
}
});
Cmd.AddCommand("soundlist", new xcommand_t() {
public void execute() {
SoundList();
}
});
Cmd.AddCommand("soundinfo", new xcommand_t() {
public void execute() {
SoundInfo_f();
}
});
num_sfx = 0;
Com.Printf("sound sampling rate: 44100Hz\n");
StopAllSounds();
Com.Printf("------------------------------------\n");
return true;
}
private void initOpenAL() throws OpenALException {
ALFactory.initialize();
alc = ALFactory.getALC();
String deviceName = null;
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
deviceName = "DirectSound3D";
}
ALC.Device device = alc.alcOpenDevice(deviceName);
String deviceSpecifier = alc.alcGetString(device, ALC.ALC_DEVICE_SPECIFIER);
String defaultSpecifier = alc.alcGetString(device, ALC.ALC_DEFAULT_DEVICE_SPECIFIER);
Com.Printf(os + " using " + ((deviceName == null) ? defaultSpecifier : deviceName) + '\n');
ALC.Context context = alc.alcCreateContext(device, new int[] {0});
alc.alcMakeContextCurrent(context);
// Check for an error.
if (alc.alcGetError(device) != ALC.ALC_NO_ERROR) {
Com.DPrintf("Error with SoundDevice");
}
}
private void initOpenALExtensions() throws OpenALException {
if (al.alIsExtensionPresent("EAX2.0")) {
Com.Printf("... using EAX2.0\n");
eax = EAXFactory.getEAX();
} else {
Com.Printf("... EAX2.0 not found\n");
eax = null;
}
}
void exitOpenAL() {
// Get the current context.
ALC.Context curContext = alc.alcGetCurrentContext();
// Get the device used by that context.
ALC.Device curDevice = alc.alcGetContextsDevice(curContext);
// Reset the current context to NULL.
alc.alcMakeContextCurrent(null);
// Release the context and the device.
alc.alcDestroyContext(curContext);
alc.alcCloseDevice(curDevice);
}
/* (non-Javadoc)
* @see jake2.sound.SoundImpl#RegisterSound(jake2.sound.sfx_t)
*/
private void initBuffer(byte[] samples, int bufferId, int freq) {
al.alBufferData(buffers[bufferId], AL.AL_FORMAT_MONO16, samples,
samples.length, freq);
}
private void checkError() {
Com.DPrintf("AL Error: " + alErrorString() +'\n');
}
private String alErrorString(){
int error;
String message = "";
if ((error = al.alGetError()) != AL.AL_NO_ERROR) {
switch(error) {
case AL.AL_INVALID_OPERATION: message = "invalid operation"; break;
case AL.AL_INVALID_VALUE: message = "invalid value"; break;
case AL.AL_INVALID_ENUM: message = "invalid enum"; break;
case AL.AL_INVALID_NAME: message = "invalid name"; break;
default: message = "" + error;
}
}
return message;
}
/* (non-Javadoc)
* @see jake2.sound.SoundImpl#Shutdown()
*/
public void Shutdown() {
StopAllSounds();
Channel.shutdown();
al.alDeleteBuffers(buffers.length, buffers);
exitOpenAL();
Cmd.RemoveCommand("play");
Cmd.RemoveCommand("stopsound");
Cmd.RemoveCommand("soundlist");
Cmd.RemoveCommand("soundinfo");
// free all sounds
for (int i = 0; i < num_sfx; i++) {
if (known_sfx[i].name == null)
continue;
known_sfx[i].clear();
}
num_sfx = 0;
}
/* (non-Javadoc)
* @see jake2.sound.SoundImpl#StartSound(float[], int, int, jake2.sound.sfx_t, float, float, float)
*/
public void StartSound(float[] origin, int entnum, int entchannel, sfx_t sfx, float fvol, float attenuation, float timeofs) {
if (sfx == null)
return;
if (sfx.name.charAt(0) == '*')
sfx = RegisterSexedSound(Globals.cl_entities[entnum].current, sfx.name);
if (LoadSound(sfx) == null)
return; // can't load sound
if (attenuation != Defines.ATTN_STATIC)
attenuation *= 0.5f;
PlaySound.allocate(origin, entnum, entchannel, buffers[sfx.bufferId], fvol, attenuation, timeofs);
}
private float[] listenerOrigin = {0, 0, 0};
private float[] listenerOrientation = {0, 0, 0, 0, 0, 0};
private IntBuffer eaxEnv = Lib.newIntBuffer(1);
private int currentEnv = -1;
private boolean changeEnv = true;
// TODO workaround for JOAL-bug
// should be EAX.LISTENER
private final static int EAX_LISTENER = 0;
// should be EAX.SOURCE
private final static int EAX_SOURCE = 1;
/* (non-Javadoc)
* @see jake2.sound.SoundImpl#Update(float[], float[], float[], float[])
*/
public void Update(float[] origin, float[] forward, float[] right, float[] up) {
Channel.convertVector(origin, listenerOrigin);
al.alListenerfv(AL.AL_POSITION, listenerOrigin);
Channel.convertOrientation(forward, up, listenerOrientation);
al.alListenerfv(AL.AL_ORIENTATION, listenerOrientation);
if (eax != null) {
// workaround for environment initialisation
if (currentEnv == -1) {
eaxEnv.put(0, EAX.EAX_ENVIRONMENT_UNDERWATER);
eax.EAXSet(EAX_LISTENER, EAX.DSPROPERTY_EAXLISTENER_ENVIRONMENT | EAX.DSPROPERTY_EAXLISTENER_DEFERRED, 0, eaxEnv, 4);
changeEnv = true;
}
if ((GameBase.gi.pointcontents.pointcontents(origin)& Defines.MASK_WATER)!= 0) {
changeEnv = currentEnv != EAX.EAX_ENVIRONMENT_UNDERWATER;
currentEnv = EAX.EAX_ENVIRONMENT_UNDERWATER;
} else {
changeEnv = currentEnv != EAX.EAX_ENVIRONMENT_GENERIC;
currentEnv = EAX.EAX_ENVIRONMENT_GENERIC;
}
if (changeEnv) {
eaxEnv.put(0, currentEnv);
eax.EAXSet(EAX_LISTENER, EAX.DSPROPERTY_EAXLISTENER_ENVIRONMENT | EAX.DSPROPERTY_EAXLISTENER_DEFERRED, 0, eaxEnv, 4);
}
}
Channel.addLoopSounds();
Channel.addPlaySounds();
Channel.playAllSounds(listenerOrigin, s_volume.value);
}
/* (non-Javadoc)
* @see jake2.sound.SoundImpl#StopAllSounds()
*/
public void StopAllSounds() {
PlaySound.reset();
Channel.reset();
}
/* (non-Javadoc)
* @see jake2.sound.Sound#getName()
*/
public String getName() {
return "joal";
}
int s_registration_sequence;
boolean s_registering;
/* (non-Javadoc)
* @see jake2.sound.Sound#BeginRegistration()
*/
public void BeginRegistration() {
s_registration_sequence++;
s_registering = true;
}
/* (non-Javadoc)
* @see jake2.sound.Sound#RegisterSound(java.lang.String)
*/
public sfx_t RegisterSound(String name) {
sfx_t sfx = FindName(name, true);
sfx.registration_sequence = s_registration_sequence;
if (!s_registering)
LoadSound(sfx);
return sfx;
}
/* (non-Javadoc)
* @see jake2.sound.Sound#EndRegistration()
*/
public void EndRegistration() {
int i;
sfx_t sfx;
int size;
// free any sounds not from this registration sequence
for (i = 0; i < num_sfx; i++) {
sfx = known_sfx[i];
if (sfx.name == null)
continue;
if (sfx.registration_sequence != s_registration_sequence) {
// don't need this sound
sfx.clear();
}
}
// load everything in
for (i = 0; i < num_sfx; i++) {
sfx = known_sfx[i];
if (sfx.name == null)
continue;
LoadSound(sfx);
}
s_registering = false;
}
sfx_t RegisterSexedSound(entity_state_t ent, String base) {
sfx_t sfx = null;
// determine what model the client is using
String model = null;
int n = Globals.CS_PLAYERSKINS + ent.number - 1;
if (Globals.cl.configstrings[n] != null) {
int p = Globals.cl.configstrings[n].indexOf('\\');
if (p >= 0) {
p++;
model = Globals.cl.configstrings[n].substring(p);
//strcpy(model, p);
p = model.indexOf('/');
if (p > 0)
model = model.substring(0, p);
}
}
// if we can't figure it out, they're male
if (model == null || model.length() == 0)
model = "male";
// see if we already know of the model specific sound
String sexedFilename = "#players/" + model + "/" + base.substring(1);
//Com_sprintf (sexedFilename, sizeof(sexedFilename), "#players/%s/%s", model, base+1);
sfx = FindName(sexedFilename, false);
if (sfx != null) return sfx;
//
// fall back strategies
//
// not found , so see if it exists
if (FS.FileLength(sexedFilename.substring(1)) > 0) {
// yes, register it
return RegisterSound(sexedFilename);
}
// try it with the female sound in the pak0.pak
if (model.equalsIgnoreCase("female")) {
String femaleFilename = "player/female/" + base.substring(1);
if (FS.FileLength("sound/" + femaleFilename) > 0)
return AliasName(sexedFilename, femaleFilename);
}
// no chance, revert to the male sound in the pak0.pak
String maleFilename = "player/male/" + base.substring(1);
return AliasName(sexedFilename, maleFilename);
}
static sfx_t[] known_sfx = new sfx_t[MAX_SFX];
static {
for (int i = 0; i< known_sfx.length; i++)
known_sfx[i] = new sfx_t();
}
static int num_sfx;
sfx_t FindName(String name, boolean create) {
int i;
sfx_t sfx = null;
if (name == null)
Com.Error(Defines.ERR_FATAL, "S_FindName: NULL\n");
if (name.length() == 0)
Com.Error(Defines.ERR_FATAL, "S_FindName: empty name\n");
if (name.length() >= Defines.MAX_QPATH)
Com.Error(Defines.ERR_FATAL, "Sound name too long: " + name);
// see if already loaded
for (i = 0; i < num_sfx; i++)
if (name.equals(known_sfx[i].name)) {
return known_sfx[i];
}
if (!create)
return null;
// find a free sfx
for (i = 0; i < num_sfx; i++)
if (known_sfx[i].name == null)
// registration_sequence < s_registration_sequence)
break;
if (i == num_sfx) {
if (num_sfx == MAX_SFX)
Com.Error(Defines.ERR_FATAL, "S_FindName: out of sfx_t");
num_sfx++;
}
sfx = known_sfx[i];
sfx.clear();
sfx.name = name;
sfx.registration_sequence = s_registration_sequence;
sfx.bufferId = i;
return sfx;
}
/*
==================
S_AliasName
==================
*/
sfx_t AliasName(String aliasname, String truename)
{
sfx_t sfx = null;
String s;
int i;
s = new String(truename);
// find a free sfx
for (i=0 ; i < num_sfx ; i++)
if (known_sfx[i].name == null)
break;
if (i == num_sfx)
{
if (num_sfx == MAX_SFX)
Com.Error(Defines.ERR_FATAL, "S_FindName: out of sfx_t");
num_sfx++;
}
sfx = known_sfx[i];
sfx.clear();
sfx.name = new String(aliasname);
sfx.registration_sequence = s_registration_sequence;
sfx.truename = s;
// set the AL bufferId
sfx.bufferId = i;
return sfx;
}
/*
==============
S_LoadSound
==============
*/
public sfxcache_t LoadSound(sfx_t s) {
if (s.isCached) return s.cache;
sfxcache_t sc = WaveLoader.LoadSound(s);
if (sc != null) {
initBuffer(sc.data, s.bufferId, sc.speed);
s.isCached = true;
// free samples for GC
s.cache.data = null;
}
return sc;
}
/* (non-Javadoc)
* @see jake2.sound.Sound#StartLocalSound(java.lang.String)
*/
public void StartLocalSound(String sound) {
sfx_t sfx = RegisterSound(sound);
if (sfx == null) {
Com.Printf("S_StartLocalSound: can't cache " + sound + "\n");
return;
}
StartSound(null, Globals.cl.playernum + 1, 0, sfx, 1, 1, 0.0f);
}
/* (non-Javadoc)
* @see jake2.sound.Sound#RawSamples(int, int, int, int, byte[])
*/
public void RawSamples(int samples, int rate, int width, int channels, byte[] data) {
// TODO implement RawSamples
}
/*
===============================================================================
console functions
===============================================================================
*/
void Play() {
int i;
String name;
sfx_t sfx;
i = 1;
while (i < Cmd.Argc()) {
name = new String(Cmd.Argv(i));
if (name.indexOf('.') == -1)
name += ".wav";
sfx = RegisterSound(name);
StartLocalSound(name);
i++;
}
}
void SoundList() {
int i;
sfx_t sfx;
sfxcache_t sc;
int size, total;
total = 0;
for (i = 0; i < num_sfx; i++) {
sfx = known_sfx[i];
if (sfx.registration_sequence == 0)
continue;
sc = sfx.cache;
if (sc != null) {
size = sc.length * sc.width * (sc.stereo + 1);
total += size;
if (sc.loopstart >= 0)
Com.Printf("L");
else
Com.Printf(" ");
Com.Printf("(%2db) %6i : %s\n", new Vargs(3).add(sc.width * 8).add(size).add(sfx.name));
} else {
if (sfx.name.charAt(0) == '*')
Com.Printf(" placeholder : " + sfx.name + "\n");
else
Com.Printf(" not loaded : " + sfx.name + "\n");
}
}
Com.Printf("Total resident: " + total + "\n");
}
void SoundInfo_f() {
Com.Printf("%5d stereo\n", new Vargs(1).add(1));
Com.Printf("%5d samples\n", new Vargs(1).add(22050));
Com.Printf("%5d samplebits\n", new Vargs(1).add(16));
Com.Printf("%5d speed\n", new Vargs(1).add(44100));
}
}
|