aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/sound/S.java
blob: 87cda5230553e80b060e027dddb7760035c6fabc (plain)
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
/*
 * S.java
 * Copyright (C) 2003
 * 
 * $Id: S.java,v 1.8 2005-01-12 01:21:34 cawe Exp $
 */
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
package jake2.sound;

import jake2.Defines;
import jake2.game.cvar_t;
import jake2.qcommon.Com;
import jake2.qcommon.Cvar;

import java.util.Vector;

/**
 * S
 */
public class S {
	
	static Sound impl;
	static cvar_t s_impl;
	
	static Vector drivers = new Vector(3);
	 
	static {
		try {
			// dummy driver (no sound)
			Class.forName("jake2.sound.DummyDriver");
			try {
				Class.forName("org.lwjgl.openal.AL");
				Class.forName("jake2.sound.lwjgl.LWJGLSoundImpl");
			} catch (ClassNotFoundException e) {
				// ignore the lwjgl driver if runtime not in classpath
			}
			// prefered driver
			try {
				Class.forName("net.java.games.joal.AL");
				Class.forName("jake2.sound.joal.JOALSoundImpl");
			} catch (ClassNotFoundException e) {
				// ignore the joal driver if runtime not in classpath
			}
		}
		catch (Throwable e) {
		}
	};
	
	public static void register(Sound driver) {
		if (driver == null) {
			throw new IllegalArgumentException("Sound implementation can't be null");
		}
		if (!drivers.contains(driver)) {
			drivers.add(driver);
		}
	}
	
	public static void useDriver(String driverName) {
		Sound driver = null;
		int count = drivers.size();
		for (int i = 0; i < count; i++) {
			driver = (Sound) drivers.get(i);
			if (driver.getName().equals(driverName)) {
				impl = driver;
				return;
			}
		}
		// if driver not found use dummy
		impl = (Sound)drivers.firstElement();
	}
	
	public static void Init() {
		
		Com.Printf("\n------- sound initialization -------\n");

		cvar_t cv = Cvar.Get("s_initsound", "1", 0);
		if (cv.value == 0.0f) {
			Com.Printf("not initializing.\n");
			useDriver("dummy");
			return;			
		}

		// set the last registered driver as default
		String defaultDriver = "dummy";
		if (drivers.size() > 1){
			defaultDriver = ((Sound)drivers.lastElement()).getName();
		}
		
		s_impl = Cvar.Get("s_impl", defaultDriver, Defines.CVAR_ARCHIVE);
		useDriver(s_impl.string);

		if (impl.Init()) {
			// driver ok
			Cvar.Set("s_impl", impl.getName());
		} else {
			// fallback
			useDriver("dummy");
		}
		
		Com.Printf("\n------- use sound driver \"" + impl.getName() + "\" -------\n");
		StopAllSounds();
	}
	
	public static void Shutdown() {
		impl.Shutdown();
	}
	
	/*
	=====================
	S_BeginRegistration
	=====================
	*/
	public static void BeginRegistration() {
		impl.BeginRegistration();		
	}
	
	/*
	=====================
	S_RegisterSound
	=====================
	*/
	public static sfx_t RegisterSound(String sample) {
		return impl.RegisterSound(sample);
	}
	
	/*
	=====================
	S_EndRegistration
	=====================
	*/
	public static void EndRegistration() {
		impl.EndRegistration();
	}
	
	/*
	==================
	S_StartLocalSound
	==================
	*/
	public static void StartLocalSound(String sound) {
		impl.StartLocalSound(sound);		
	}
	
	/*
	====================
	S_StartSound

	Validates the parms and ques the sound up
	if pos is NULL, the sound will be dynamically sourced from the entity
	Entchannel 0 will never override a playing sound
	====================
	*/
	public static void StartSound(float[] origin, int entnum, int entchannel, sfx_t sfx, float fvol, float attenuation, float timeofs) {
		impl.StartSound(origin, entnum, entchannel, sfx, fvol, attenuation, timeofs);
	}

	/*
	============
	S_Update

	Called once each time through the main loop
	============
	*/
	public static void Update(float[] origin, float[] forward, float[] right, float[] up) {
		impl.Update(origin, forward, right, up);
	}

	/*
	============
	S_RawSamples
	 
	Cinematic streaming and voice over network
	============
	*/
	public static void RawSamples(int samples, int rate, int width, int channels, byte[] data) {
		impl.RawSamples(samples, rate, width, channels, data);
	}

	/*
	==================
	S_StopAllSounds
	==================
	*/
	public static void StopAllSounds() {
		impl.StopAllSounds();
	}
	
	public static String getDriverName() {
		return impl.getName();
	}
	
	public static String[] getDriverNames() {
		String[] names = new String[drivers.size()];
		for (int i = 0; i < names.length; i++) {
			names[i] = ((Sound)drivers.get(i)).getName();
		}
		return names;
	}
}