Debian Codegraph @

code/client/snd_main.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 Copyright (C) 2005 Stuart Dalton ([email protected])
5
6 This file is part of Quake III Arena source code.
7
8 Quake III Arena source code is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 Quake III Arena source code is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Quake III Arena source code; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ===========================================================================
22 */
23
24 #include "client.h"
25 #include "snd_codec.h"
26 #include "snd_local.h"
27 #include "snd_public.h"
28
29 cvar_t *s_volume;
30 cvar_t *s_muted;
31 cvar_t *s_musicVolume;
32 cvar_t *s_doppler;
33 cvar_t *s_backend;
34 cvar_t *s_muteWhenMinimized;
35 cvar_t *s_muteWhenUnfocused;
36
37 static soundInterface_t si;
38
39 /*
40 =================
41 S_ValidateInterface
42 =================
43 */
44 static qboolean S_ValidSoundInterface( soundInterface_t *pSi )
45 {
46 if( !pSi->Shutdown ) return qfalse;
47 if( !pSi->StartSound ) return qfalse;
48 if( !pSi->StartLocalSound ) return qfalse;
49 if( !pSi->StartBackgroundTrack ) return qfalse;
50 if( !pSi->StopBackgroundTrack ) return qfalse;
51 if( !pSi->RawSamples ) return qfalse;
52 if( !pSi->StopAllSounds ) return qfalse;
53 if( !pSi->ClearLoopingSounds ) return qfalse;
54 if( !pSi->AddLoopingSound ) return qfalse;
55 if( !pSi->AddRealLoopingSound ) return qfalse;
56 if( !pSi->StopLoopingSound ) return qfalse;
57 if( !pSi->Respatialize ) return qfalse;
58 if( !pSi->UpdateEntityPosition ) return qfalse;
59 if( !pSi->Update ) return qfalse;
60 if( !pSi->DisableSounds ) return qfalse;
61 if( !pSi->BeginRegistration ) return qfalse;
62 if( !pSi->RegisterSound ) return qfalse;
63 if( !pSi->ClearSoundBuffer ) return qfalse;
64 if( !pSi->SoundInfo ) return qfalse;
65 if( !pSi->SoundList ) return qfalse;
66
67 #ifdef USE_VOIP
68 if( !pSi->StartCapture ) return qfalse;
69 if( !pSi->AvailableCaptureSamples ) return qfalse;
70 if( !pSi->Capture ) return qfalse;
71 if( !pSi->StopCapture ) return qfalse;
72 if( !pSi->MasterGain ) return qfalse;
73 #endif
74
75 return qtrue;
76 }
77
78 /*
79 =================
80 S_StartSound
81 =================
82 */
83 void S_StartSound( vec3_t origin, int entnum, int entchannel, sfxHandle_t sfx )
84 {
85 if( si.StartSound ) {
86 si.StartSound( origin, entnum, entchannel, sfx );
87 }
88 }
89
90 /*
91 =================
92 S_StartLocalSound
93 =================
94 */
95 void S_StartLocalSound( sfxHandle_t sfx, int channelNum )
96 {
97 if( si.StartLocalSound ) {
98 si.StartLocalSound( sfx, channelNum );
99 }
100 }
101
102 /*
103 =================
104 S_StartBackgroundTrack
105 =================
106 */
107 void S_StartBackgroundTrack( const char *intro, const char *loop )
108 {
109 if( si.StartBackgroundTrack ) {
110 si.StartBackgroundTrack( intro, loop );
111 }
112 }
113
114 /*
115 =================
116 S_StopBackgroundTrack
117 =================
118 */
119 void S_StopBackgroundTrack( void )
120 {
121 if( si.StopBackgroundTrack ) {
122 si.StopBackgroundTrack( );
123 }
124 }
125
126 /*
127 =================
128 S_RawSamples
129 =================
130 */
131 void S_RawSamples (int stream, int samples, int rate, int width, int channels,
132 const byte *data, float volume, int entityNum)
133 {
134 if(si.RawSamples)
135 si.RawSamples(stream, samples, rate, width, channels, data, volume, entityNum);
136 }
137
138 /*
139 =================
140 S_StopAllSounds
141 =================
142 */
143 void S_StopAllSounds( void )
144 {
145 if( si.StopAllSounds ) {
146 si.StopAllSounds( );
147 }
148 }
149
150 /*
151 =================
152 S_ClearLoopingSounds
153 =================
154 */
155 void S_ClearLoopingSounds( qboolean killall )
156 {
157 if( si.ClearLoopingSounds ) {
158 si.ClearLoopingSounds( killall );
159 }
160 }
161
162 /*
163 =================
164 S_AddLoopingSound
165 =================
166 */
167 void S_AddLoopingSound( int entityNum, const vec3_t origin,
168 const vec3_t velocity, sfxHandle_t sfx )
169 {
170 if( si.AddLoopingSound ) {
171 si.AddLoopingSound( entityNum, origin, velocity, sfx );
172 }
173 }
174
175 /*
176 =================
177 S_AddRealLoopingSound
178 =================
179 */
180 void S_AddRealLoopingSound( int entityNum, const vec3_t origin,
181 const vec3_t velocity, sfxHandle_t sfx )
182 {
183 if( si.AddRealLoopingSound ) {
184 si.AddRealLoopingSound( entityNum, origin, velocity, sfx );
185 }
186 }
187
188 /*
189 =================
190 S_StopLoopingSound
191 =================
192 */
193 void S_StopLoopingSound( int entityNum )
194 {
195 if( si.StopLoopingSound ) {
196 si.StopLoopingSound( entityNum );
197 }
198 }
199
200 /*
201 =================
202 S_Respatialize
203 =================
204 */
205 void S_Respatialize( int entityNum, const vec3_t origin,
206 vec3_t axis[3], int inwater )
207 {
208 if( si.Respatialize ) {
209 si.Respatialize( entityNum, origin, axis, inwater );
210 }
211 }
212
213 /*
214 =================
215 S_UpdateEntityPosition
216 =================
217 */
218 void S_UpdateEntityPosition( int entityNum, const vec3_t origin )
219 {
220 if( si.UpdateEntityPosition ) {
221 si.UpdateEntityPosition( entityNum, origin );
222 }
223 }
224
225 /*
226 =================
227 S_Update
228 =================
229 */
230 void S_Update( void )
231 {
232 if(s_muted->integer)
233 {
234 if(!(s_muteWhenMinimized->integer && com_minimized->integer) &&
235 !(s_muteWhenUnfocused->integer && com_unfocused->integer))
236 {
237 s_muted->integer = qfalse;
238 s_muted->modified = qtrue;
239 }
240 }
241 else
242 {
243 if((s_muteWhenMinimized->integer && com_minimized->integer) ||
244 (s_muteWhenUnfocused->integer && com_unfocused->integer))
245 {
246 s_muted->integer = qtrue;
247 s_muted->modified = qtrue;
248 }
249 }
250
251 if( si.Update ) {
252 si.Update( );
253 }
254 }
255
256 /*
257 =================
258 S_DisableSounds
259 =================
260 */
261 void S_DisableSounds( void )
262 {
263 if( si.DisableSounds ) {
264 si.DisableSounds( );
265 }
266 }
267
268 /*
269 =================
270 S_BeginRegistration
271 =================
272 */
273 void S_BeginRegistration( void )
274 {
275 if( si.BeginRegistration ) {
276 si.BeginRegistration( );
277 }
278 }
279
280 /*
281 =================
282 S_RegisterSound
283 =================
284 */
285 sfxHandle_t S_RegisterSound( const char *sample, qboolean compressed )
286 {
287 if( si.RegisterSound ) {
288 return si.RegisterSound( sample, compressed );
289 } else {
290 return 0;
291 }
292 }
293
294 /*
295 =================
296 S_ClearSoundBuffer
297 =================
298 */
299 void S_ClearSoundBuffer( void )
300 {
301 if( si.ClearSoundBuffer ) {
302 si.ClearSoundBuffer( );
303 }
304 }
305
306 /*
307 =================
308 S_SoundInfo
309 =================
310 */
311 void S_SoundInfo( void )
312 {
313 if( si.SoundInfo ) {
314 si.SoundInfo( );
315 }
316 }
317
318 /*
319 =================
320 S_SoundList
321 =================
322 */
323 void S_SoundList( void )
324 {
325 if( si.SoundList ) {
326 si.SoundList( );
327 }
328 }
329
330
331 #ifdef USE_VOIP
332 /*
333 =================
334 S_StartCapture
335 =================
336 */
337 void S_StartCapture( void )
338 {
339 if( si.StartCapture ) {
340 si.StartCapture( );
341 }
342 }
343
344 /*
345 =================
346 S_AvailableCaptureSamples
347 =================
348 */
349 int S_AvailableCaptureSamples( void )
350 {
351 if( si.AvailableCaptureSamples ) {
352 return si.AvailableCaptureSamples( );
353 }
354 return 0;
355 }
356
357 /*
358 =================
359 S_Capture
360 =================
361 */
362 void S_Capture( int samples, byte *data )
363 {
364 if( si.Capture ) {
365 si.Capture( samples, data );
366 }
367 }
368
369 /*
370 =================
371 S_StopCapture
372 =================
373 */
374 void S_StopCapture( void )
375 {
376 if( si.StopCapture ) {
377 si.StopCapture( );
378 }
379 }
380
381 /*
382 =================
383 S_MasterGain
384 =================
385 */
386 void S_MasterGain( float gain )
387 {
388 if( si.MasterGain ) {
389 si.MasterGain( gain );
390 }
391 }
392 #endif
393
394 //=============================================================================
395
396 /*
397 =================
398 S_Play_f
399 =================
400 */
401 void S_Play_f( void ) {
402 int i;
403 int c;
404 sfxHandle_t h;
405
406 if( !si.RegisterSound || !si.StartLocalSound ) {
407 return;
408 }
409
410 c = Cmd_Argc();
411
412 if( c < 2 ) {
413 Com_Printf ("Usage: play <sound filename> [sound filename] [sound filename] ...\n");
414 return;
415 }
416
417 for( i = 1; i < c; i++ ) {
418 h = si.RegisterSound( Cmd_Argv(i), qfalse );
419
420 if( h ) {
421 si.StartLocalSound( h, CHAN_LOCAL_SOUND );
422 }
423 }
424 }
425
426 /*
427 =================
428 S_Music_f
429 =================
430 */
431 void S_Music_f( void ) {
432 int c;
433
434 if( !si.StartBackgroundTrack ) {
435 return;
436 }
437
438 c = Cmd_Argc();
439
440 if ( c == 2 ) {
441 si.StartBackgroundTrack( Cmd_Argv(1), NULL );
442 } else if ( c == 3 ) {
443 si.StartBackgroundTrack( Cmd_Argv(1), Cmd_Argv(2) );
444 } else {
445 Com_Printf ("Usage: music <musicfile> [loopfile]\n");
446 return;
447 }
448
449 }
450
451 /*
452 =================
453 S_Music_f
454 =================
455 */
456 void S_StopMusic_f( void )
457 {
458 if(!si.StopBackgroundTrack)
459 return;
460
461 si.StopBackgroundTrack();
462 }
463
464
465 //=============================================================================
466
467 /*
468 =================
469 S_Init
470 =================
471 */
472 void S_Init( void )
473 {
474 cvar_t *cv;
475 qboolean started = qfalse;
476
477 Com_Printf( "------ Initializing Sound ------\n" );
478
479 s_volume = Cvar_Get( "s_volume", "0.8", CVAR_ARCHIVE );
480 s_musicVolume = Cvar_Get( "s_musicvolume", "0.25", CVAR_ARCHIVE );
481 s_muted = Cvar_Get("s_muted", "0", CVAR_ROM);
482 s_doppler = Cvar_Get( "s_doppler", "1", CVAR_ARCHIVE );
483 s_backend = Cvar_Get( "s_backend", "", CVAR_ROM );
484 s_muteWhenMinimized = Cvar_Get( "s_muteWhenMinimized", "0", CVAR_ARCHIVE );
485 s_muteWhenUnfocused = Cvar_Get( "s_muteWhenUnfocused", "0", CVAR_ARCHIVE );
486
487 cv = Cvar_Get( "s_initsound", "1", 0 );
488 if( !cv->integer ) {
489 Com_Printf( "Sound disabled.\n" );
490 } else {
491
492 S_CodecInit( );
493
494 Cmd_AddCommand( "play", S_Play_f );
495 Cmd_AddCommand( "music", S_Music_f );
496 Cmd_AddCommand( "stopmusic", S_StopMusic_f );
497 Cmd_AddCommand( "s_list", S_SoundList );
498 Cmd_AddCommand( "s_stop", S_StopAllSounds );
499 Cmd_AddCommand( "s_info", S_SoundInfo );
500
501 cv = Cvar_Get( "s_useOpenAL", "1", CVAR_ARCHIVE | CVAR_LATCH );
502 if( cv->integer ) {
503 //OpenAL
504 started = S_AL_Init( &si );
505 Cvar_Set( "s_backend", "OpenAL" );
506 }
507
508 if( !started ) {
509 started = S_Base_Init( &si );
510 Cvar_Set( "s_backend", "base" );
511 }
512
513 if( started ) {
514 if( !S_ValidSoundInterface( &si ) ) {
515 Com_Error( ERR_FATAL, "Sound interface invalid" );
516 }
517
518 S_SoundInfo( );
519 Com_Printf( "Sound initialization successful.\n" );
520 } else {
521 Com_Printf( "Sound initialization failed.\n" );
522 }
523 }
524
525 Com_Printf( "--------------------------------\n");
526 }
527
528 /*
529 =================
530 S_Shutdown
531 =================
532 */
533 void S_Shutdown( void )
534 {
535 if( si.Shutdown ) {
536 si.Shutdown( );
537 }
538
539 Com_Memset( &si, 0, sizeof( soundInterface_t ) );
540
541 Cmd_RemoveCommand( "play" );
542 Cmd_RemoveCommand( "music");
543 Cmd_RemoveCommand( "stopmusic");
544 Cmd_RemoveCommand( "s_list" );
545 Cmd_RemoveCommand( "s_stop" );
546 Cmd_RemoveCommand( "s_info" );
547
548 S_CodecShutdown( );
549 }
550