]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
fixed a stupid bug in PF_te_customflash that made it send lifetime as /256 (should...
[divverent/darkplaces.git] / cd_shared.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
21 // rights reserved.
22
23 #include "quakedef.h"
24 #include "cdaudio.h"
25 #include "snd_main.h"
26
27 #define MAXTRACKS       256
28
29 // Prototypes of the system dependent functions
30 extern void CDAudio_SysEject (void);
31 extern void CDAudio_SysCloseDoor (void);
32 extern int CDAudio_SysGetAudioDiskInfo (void);
33 extern float CDAudio_SysGetVolume (void);
34 extern void CDAudio_SysSetVolume (float volume);
35 extern int CDAudio_SysPlay (qbyte track);
36 extern int CDAudio_SysStop (void);
37 extern int CDAudio_SysPause (void);
38 extern int CDAudio_SysResume (void);
39 extern int CDAudio_SysUpdate (void);
40 extern void CDAudio_SysInit (void);
41 extern int CDAudio_SysStartup (void);
42 extern void CDAudio_SysShutdown (void);
43
44 // used by menu to ghost CD audio slider
45 cvar_t  cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0"};
46
47 static qboolean wasPlaying = false;
48 static qboolean initialized = false;
49 static qboolean enabled = false;
50 static float cdvolume;
51 static qbyte remap[MAXTRACKS];
52 static qbyte maxTrack;
53 static int faketrack = -1;
54
55 static float saved_vol = 1.0f;
56
57 // exported variables
58 qboolean cdValid = false;
59 qboolean cdPlaying = false;
60 qboolean cdPlayLooping = false;
61 qbyte cdPlayTrack;
62
63
64 static void CDAudio_Eject (void)
65 {
66         if (!enabled)
67                 return;
68
69         CDAudio_SysEject();
70 }
71
72
73 static void CDAudio_CloseDoor (void)
74 {
75         if (!enabled)
76                 return;
77
78         CDAudio_SysCloseDoor();
79 }
80
81 static int CDAudio_GetAudioDiskInfo (void)
82 {
83         int ret;
84
85         cdValid = false;
86
87         ret = CDAudio_SysGetAudioDiskInfo();
88         if (ret < 1)
89                 return -1;
90
91         cdValid = true;
92         maxTrack = ret;
93
94         return 0;
95 }
96
97
98 void CDAudio_Play (qbyte track, qboolean looping)
99 {
100         sfx_t* sfx;
101
102         if (!enabled)
103                 return;
104
105         track = remap[track];
106         if (track < 1)
107         {
108                 Con_Printf("CDAudio: Bad track number %u.\n", track);
109                 return;
110         }
111
112         if (cdPlaying && cdPlayTrack == track && faketrack == -1)
113                 return;
114         CDAudio_Stop ();
115
116         // Try playing a fake track (sound file) first
117         sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
118         if (sfx == NULL || sfx->fetcher == NULL)
119                 sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
120         if (sfx != NULL)
121         {
122                 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
123                 if (faketrack != -1)
124                 {
125                         if (looping)
126                                 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
127                         S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
128                         Con_Printf ("Fake CD track %u playing...\n", track);
129                 }
130         }
131
132         // If we can't play a fake CD track, try the real one
133         if (faketrack == -1)
134         {
135                 if (!cdValid)
136                 {
137                         CDAudio_GetAudioDiskInfo();
138                         if (!cdValid)
139                         {
140                                 Con_Print ("No CD in player.\n");
141                                 return;
142                         }
143                 }
144
145                 if (track > maxTrack)
146                 {
147                         Con_Printf("CDAudio: Bad track number %u.\n", track);
148                         return;
149                 }
150
151                 if (CDAudio_SysPlay(track) == -1)
152                         return;
153         }
154
155         cdPlayLooping = looping;
156         cdPlayTrack = track;
157         cdPlaying = true;
158
159         if (cdvolume == 0.0)
160                 CDAudio_Pause ();
161 }
162
163
164 void CDAudio_Stop (void)
165 {
166         if (!enabled || !cdPlaying)
167                 return;
168
169         if (faketrack != -1)
170         {
171                 S_StopChannel (faketrack);
172                 faketrack = -1;
173         }
174         else if (CDAudio_SysStop() == -1)
175                 return;
176
177         wasPlaying = false;
178         cdPlaying = false;
179 }
180
181 void CDAudio_Pause (void)
182 {
183         if (!enabled || !cdPlaying)
184                 return;
185
186         if (faketrack != -1)
187                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
188         else if (CDAudio_SysPause() == -1)
189                 return;
190
191         wasPlaying = cdPlaying;
192         cdPlaying = false;
193 }
194
195
196 void CDAudio_Resume (void)
197 {
198         if (!enabled || cdPlaying || !wasPlaying)
199                 return;
200
201         if (faketrack != -1)
202                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
203         else if (CDAudio_SysResume() == -1)
204                 return;
205         cdPlaying = true;
206 }
207
208 static void CD_f (void)
209 {
210         const char *command;
211         int ret;
212         int n;
213
214         if (Cmd_Argc() < 2)
215                 return;
216
217         command = Cmd_Argv (1);
218
219         if (strcasecmp(command, "on") == 0)
220         {
221                 enabled = true;
222                 return;
223         }
224
225         if (strcasecmp(command, "off") == 0)
226         {
227                 if (cdPlaying)
228                         CDAudio_Stop();
229                 enabled = false;
230                 return;
231         }
232
233         if (strcasecmp(command, "reset") == 0)
234         {
235                 enabled = true;
236                 if (cdPlaying)
237                         CDAudio_Stop();
238                 for (n = 0; n < MAXTRACKS; n++)
239                         remap[n] = n;
240                 CDAudio_GetAudioDiskInfo();
241                 return;
242         }
243
244         if (strcasecmp(command, "remap") == 0)
245         {
246                 ret = Cmd_Argc() - 2;
247                 if (ret <= 0)
248                 {
249                         for (n = 1; n < MAXTRACKS; n++)
250                                 if (remap[n] != n)
251                                         Con_Printf("  %u -> %u\n", n, remap[n]);
252                         return;
253                 }
254                 for (n = 1; n <= ret; n++)
255                         remap[n] = atoi(Cmd_Argv (n+1));
256                 return;
257         }
258
259         if (strcasecmp(command, "close") == 0)
260         {
261                 CDAudio_CloseDoor();
262                 return;
263         }
264
265         if (strcasecmp(command, "play") == 0)
266         {
267                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
268                 return;
269         }
270
271         if (strcasecmp(command, "loop") == 0)
272         {
273                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
274                 return;
275         }
276
277         if (strcasecmp(command, "stop") == 0)
278         {
279                 CDAudio_Stop();
280                 return;
281         }
282
283         if (strcasecmp(command, "pause") == 0)
284         {
285                 CDAudio_Pause();
286                 return;
287         }
288
289         if (strcasecmp(command, "resume") == 0)
290         {
291                 CDAudio_Resume();
292                 return;
293         }
294
295         if (strcasecmp(command, "eject") == 0)
296         {
297                 if (cdPlaying && faketrack == -1)
298                         CDAudio_Stop();
299                 CDAudio_Eject();
300                 cdValid = false;
301                 return;
302         }
303
304         if (strcasecmp(command, "info") == 0)
305         {
306                 CDAudio_GetAudioDiskInfo ();
307                 if (cdValid)
308                         Con_Printf("%u tracks on CD.\n", maxTrack);
309                 else
310                         Con_Print ("No CD in player.\n");
311                 if (cdPlaying)
312                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
313                 else if (wasPlaying)
314                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
315                 Con_Printf("Volume is %f\n", cdvolume);
316                 return;
317         }
318 }
319
320 void CDAudio_SetVolume (float newvol)
321 {
322         // If the volume hasn't changed
323         if (newvol == cdvolume)
324                 return;
325
326         // If the CD has been muted
327         if (newvol == 0.0f)
328                 CDAudio_Pause ();
329         else
330         {
331                 // If the CD has been unmuted
332                 if (cdvolume == 0.0f)
333                         CDAudio_Resume ();
334
335                 if (faketrack != -1)
336                         S_SetChannelVolume (faketrack, newvol);
337                 CDAudio_SysSetVolume (newvol);
338         }
339
340         cdvolume = newvol;
341 }
342
343 void CDAudio_Update (void)
344 {
345         if (!enabled)
346                 return;
347
348         CDAudio_SetVolume (bgmvolume.value);
349
350         if (faketrack == -1)
351                 CDAudio_SysUpdate();
352 }
353
354 int CDAudio_Init (void)
355 {
356         int i;
357
358         if (cls.state == ca_dedicated)
359                 return -1;
360
361 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
362         if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
363                 return -1;
364
365         CDAudio_SysInit();
366
367         for (i = 0; i < MAXTRACKS; i++)
368                 remap[i] = i;
369
370         Cvar_RegisterVariable(&cdaudioinitialized);
371         Cvar_SetValueQuick(&cdaudioinitialized, true);
372         enabled = true;
373
374         Cmd_AddCommand("cd", CD_f);
375
376         return 0;
377 }
378
379 int CDAudio_Startup (void)
380 {
381         CDAudio_SysStartup ();
382
383         if (CDAudio_GetAudioDiskInfo())
384         {
385                 Con_Print("CDAudio_Init: No CD in player.\n");
386                 cdValid = false;
387         }
388
389         saved_vol = CDAudio_SysGetVolume ();
390         if (saved_vol < 0.0f)
391         {
392                 Con_Print ("Can't get initial CD volume\n");
393                 saved_vol = 1.0f;
394         }
395         else
396                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
397
398         initialized = true;
399
400         Con_Print("CD Audio Initialized\n");
401
402         return 0;
403 }
404
405 void CDAudio_Shutdown (void)
406 {
407         if (!initialized)
408                 return;
409
410         CDAudio_SysSetVolume (saved_vol);
411
412         CDAudio_Stop();
413         CDAudio_SysShutdown();
414         initialized = false;
415 }