summaryrefslogtreecommitdiffstats
path: root/src/jake2/sound/S.java
blob: 90b178cc757692acb6fbf016d10f356633f983e1 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * S.java
 * Copyright (C) 2003
 * 
 * $Id: S.java,v 1.13 2005-12-13 00:00:25 salomo 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.nio.ByteBuffer;
import java.util.Vector;

/**
 * S
 */
public class S {
	
	static Sound impl;
	static cvar_t s_impl;
	
	static Vector drivers = new Vector(3);
	
	/** 
	 * Searches for and initializes all known sound drivers.
	 */
	static {	    
			// dummy driver (no sound)
			try {	    
			    Class.forName("jake2.sound.DummyDriver");
			    // initialize impl with the default value
			    // this is  necessary for dedicated mode
			    useDriver("dummy");
			} catch (Throwable e) {
			    Com.DPrintf("could not init dummy sound driver class.");
			}
			
			try {
				Class.forName("org.lwjgl.openal.AL");
				Class.forName("jake2.sound.lwjgl.LWJGLSoundImpl");
			} catch (Throwable e) {
				// ignore the lwjgl driver if runtime not in classpath
			    Com.DPrintf("could not init lwjgl sound driver class.");
			}
			
			// prefered driver
			try {
				Class.forName("com.jogamp.openal.AL");
				Class.forName("jake2.sound.joal.JOALSoundImpl");
			} catch (Throwable e) {
				// ignore the joal driver if runtime not in classpath
			    Com.DPrintf("could not init joal sound driver class.");
			}
		
	};
	
	/**
	 * Registers a new Sound Implementor.
	 */
	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);
		}
	}
	
	/**
	 * Switches to the specific sound 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.lastElement();
	}
	
	/**
	 * Initializes the sound module.
	 */
	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();
	}
	
	/**
	 * Called before the sounds are to be loaded and registered.
	 */
	public static void BeginRegistration() {
		impl.BeginRegistration();		
	}
	
	/**
	 * Registers and loads a sound.
	 */
	public static sfx_t RegisterSound(String sample) {
		return impl.RegisterSound(sample);
	}
	
	/**
	 * Called after all sounds are registered and loaded.
	 */
	public static void EndRegistration() {
		impl.EndRegistration();
	}
	
	/**
	 * Starts a local sound.
	 */
	public static void StartLocalSound(String sound) {
		impl.StartLocalSound(sound);		
	}
	
	/** 
	 * 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);
	}

	/**
	 * Updates the sound renderer according to the changes in the environment,
	 * 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);
	}

	/**
	 * Cinematic streaming and voice over network.
	 */
	public static void RawSamples(int samples, int rate, int width, int channels, ByteBuffer data) {
		impl.RawSamples(samples, rate, width, channels, data);
	}
    
	/**
	 * Switches off the sound streaming.
	 */ 
    public static void disableStreaming() {
        impl.disableStreaming();
    }

	/**
	 * Stops all sounds. 
	 */
	public static void StopAllSounds() {
		impl.StopAllSounds();
	}
	
	public static String getDriverName() {
		return impl.getName();
	}
	
	/**
	 * Returns a string array containing all sound driver names.
	 */
	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;
	}
	
	/**
	 * This is used, when resampling to this default sampling rate is activated 
	 * in the wavloader. It is placed here that sound implementors can override 
	 * this one day.
	 */
	public static int getDefaultSampleRate()
	{
		return 44100;
	}
}