new cvar sys_useclockgettime (default 0) that makes DP use clock_gettime as time...
[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 "sound.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 (unsigned char 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","indicates if CD Audio system is active"};
46
47 static qboolean wasPlaying = false;
48 static qboolean initialized = false;
49 static qboolean enabled = false;
50 static float cdvolume;
51 typedef char filename_t[MAX_QPATH];
52 static filename_t remap[MAXTRACKS];
53 static unsigned char maxTrack;
54 static int faketrack = -1;
55
56 static float saved_vol = 1.0f;
57
58 // exported variables
59 qboolean cdValid = false;
60 qboolean cdPlaying = false;
61 qboolean cdPlayLooping = false;
62 unsigned char cdPlayTrack;
63
64 cl_cdstate_t cd;
65
66 static void CDAudio_Eject (void)
67 {
68         if (!enabled)
69                 return;
70
71         CDAudio_SysEject();
72 }
73
74
75 static void CDAudio_CloseDoor (void)
76 {
77         if (!enabled)
78                 return;
79
80         CDAudio_SysCloseDoor();
81 }
82
83 static int CDAudio_GetAudioDiskInfo (void)
84 {
85         int ret;
86
87         cdValid = false;
88
89         ret = CDAudio_SysGetAudioDiskInfo();
90         if (ret < 1)
91                 return -1;
92
93         cdValid = true;
94         maxTrack = ret;
95
96         return 0;
97 }
98
99
100 void CDAudio_Play_byName (const char *trackname, qboolean looping)
101 {
102         unsigned int track;
103         sfx_t* sfx;
104
105         Host_StartVideo();
106
107         if (!enabled)
108                 return;
109
110         if(strspn(trackname, "0123456789") == strlen(trackname))
111         {
112                 track = (unsigned char) atoi(trackname);
113                 if(track > 0 && track < MAXTRACKS)
114                                 if(*remap[track])
115                                         trackname = remap[track];
116         }
117
118         if(strspn(trackname, "0123456789") == strlen(trackname))
119         {
120                 track = (unsigned char) atoi(trackname);
121                 if (track < 1)
122                 {
123                         Con_Printf("CDAudio: Bad track number %u.\n", track);
124                         return;
125                 }
126         }
127         else
128                 track = 0;
129
130         if (cdPlaying && cdPlayTrack == track && faketrack == -1)
131                 return;
132         CDAudio_Stop ();
133
134         // Try playing a fake track (sound file) first
135         if(track >= 1)
136         {
137                 sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, false);
138                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
139                         sfx = S_PrecacheSound (va ("cdtracks/track%03u.wav", track), false, false);
140                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
141                         sfx = S_PrecacheSound (va ("cdtracks/track%02u", track), false, false);
142                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
143                         sfx = S_PrecacheSound (va ("cdtracks/track%03u", track), false, false);
144         }
145         else
146         {
147                 sfx = S_PrecacheSound (va("cdtracks/%s.wav", trackname), false, false);
148                 if (sfx == NULL || !S_IsSoundPrecached (sfx))
149                         sfx = S_PrecacheSound (va("cdtracks/%s", trackname), false, false);
150         }
151         if (sfx != NULL)
152         {
153                 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
154                 if (faketrack != -1)
155                 {
156                         if (looping)
157                                 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
158                         S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
159                         if(track >= 1)
160                                 Con_Printf ("Fake CD track %u playing...\n", track);
161                         else
162                                 Con_Printf ("BGM track %s playing...\n", trackname);
163                 }
164         }
165
166         // If we can't play a fake CD track, try the real one
167         if (faketrack == -1)
168         {
169                 if(track < 1)
170                 {
171                         Con_Print("Could not load BGM track.\n");
172                         return;
173                 }
174
175                 if (!cdValid)
176                 {
177                         CDAudio_GetAudioDiskInfo();
178                         if (!cdValid)
179                         {
180                                 Con_Print ("No CD in player.\n");
181                                 return;
182                         }
183                 }
184
185                 if (track > maxTrack)
186                 {
187                         Con_Printf("CDAudio: Bad track number %u.\n", track);
188                         return;
189                 }
190
191                 if (CDAudio_SysPlay(track) == -1)
192                         return;
193         }
194
195         cdPlayLooping = looping;
196         cdPlayTrack = track;
197         cdPlaying = true;
198
199         if (cdvolume == 0.0)
200                 CDAudio_Pause ();
201 }
202
203 void CDAudio_Play (unsigned char track, qboolean looping)
204 {
205         char buf[20];
206         dpsnprintf(buf, sizeof(buf), "%d", (int) track);
207         CDAudio_Play_byName(buf, looping);
208 }
209
210 void CDAudio_Stop (void)
211 {
212         if (!enabled)
213                 return;
214
215         if (faketrack != -1)
216         {
217                 S_StopChannel (faketrack, true);
218                 faketrack = -1;
219         }
220         else if (cdPlaying && (CDAudio_SysStop() == -1))
221                 return;
222
223         wasPlaying = false;
224         cdPlaying = false;
225 }
226
227 void CDAudio_Pause (void)
228 {
229         if (!enabled || !cdPlaying)
230                 return;
231
232         if (faketrack != -1)
233                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
234         else if (CDAudio_SysPause() == -1)
235                 return;
236
237         wasPlaying = cdPlaying;
238         cdPlaying = false;
239 }
240
241
242 void CDAudio_Resume (void)
243 {
244         if (!enabled || cdPlaying || !wasPlaying)
245                 return;
246
247         if (faketrack != -1)
248                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
249         else if (CDAudio_SysResume() == -1)
250                 return;
251         cdPlaying = true;
252 }
253
254 static void CD_f (void)
255 {
256         const char *command;
257         int ret;
258         int n;
259
260         if (Cmd_Argc() < 2)
261                 return;
262
263         command = Cmd_Argv (1);
264
265         if (strcasecmp(command, "remap") != 0)
266                 Host_StartVideo();
267
268         if (strcasecmp(command, "on") == 0)
269         {
270                 enabled = true;
271                 return;
272         }
273
274         if (strcasecmp(command, "off") == 0)
275         {
276                 if (cdPlaying)
277                         CDAudio_Stop();
278                 enabled = false;
279                 return;
280         }
281
282         if (strcasecmp(command, "reset") == 0)
283         {
284                 enabled = true;
285                 if (cdPlaying)
286                         CDAudio_Stop();
287                 for (n = 0; n < MAXTRACKS; n++)
288                         *remap[n] = 0; // empty string, that is, unremapped
289                 CDAudio_GetAudioDiskInfo();
290                 return;
291         }
292
293         if (strcasecmp(command, "remap") == 0)
294         {
295                 ret = Cmd_Argc() - 2;
296                 if (ret <= 0)
297                 {
298                         for (n = 1; n < MAXTRACKS; n++)
299                                 if (*remap[n])
300                                         Con_Printf("  %u -> %s\n", n, remap[n]);
301                         return;
302                 }
303                 for (n = 1; n <= ret; n++)
304                         strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
305                 return;
306         }
307
308         if (strcasecmp(command, "close") == 0)
309         {
310                 CDAudio_CloseDoor();
311                 return;
312         }
313
314         if (strcasecmp(command, "play") == 0)
315         {
316                 CDAudio_Play_byName(Cmd_Argv (2), false);
317                 return;
318         }
319
320         if (strcasecmp(command, "loop") == 0)
321         {
322                 CDAudio_Play_byName(Cmd_Argv (2), true);
323                 return;
324         }
325
326         if (strcasecmp(command, "stop") == 0)
327         {
328                 CDAudio_Stop();
329                 return;
330         }
331
332         if (strcasecmp(command, "pause") == 0)
333         {
334                 CDAudio_Pause();
335                 return;
336         }
337
338         if (strcasecmp(command, "resume") == 0)
339         {
340                 CDAudio_Resume();
341                 return;
342         }
343
344         if (strcasecmp(command, "eject") == 0)
345         {
346                 if (cdPlaying && faketrack == -1)
347                         CDAudio_Stop();
348                 CDAudio_Eject();
349                 cdValid = false;
350                 return;
351         }
352
353         if (strcasecmp(command, "info") == 0)
354         {
355                 CDAudio_GetAudioDiskInfo ();
356                 if (cdValid)
357                         Con_Printf("%u tracks on CD.\n", maxTrack);
358                 else
359                         Con_Print ("No CD in player.\n");
360                 if (cdPlaying)
361                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
362                 else if (wasPlaying)
363                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
364                 Con_Printf("Volume is %f\n", cdvolume);
365                 return;
366         }
367
368         Con_Printf("CD commands:\n");
369         Con_Printf("cd on - enables CD audio system\n");
370         Con_Printf("cd off - stops and disables CD audio system\n");
371         Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
372         Con_Printf("cd remap <remap1> [remap2] [remap3] [...] - chooses (possibly emulated) CD tracks to play when a map asks for a particular track, this has many uses\n");
373         Con_Printf("cd close - closes CD tray\n");
374         Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
375         Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
376         Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
377         Con_Printf("cd stop - stops playing current CD track\n");
378         Con_Printf("cd pause - pauses CD playback\n");
379         Con_Printf("cd resume - unpauses CD playback\n");
380         Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
381 }
382
383 void CDAudio_SetVolume (float newvol)
384 {
385         // If the volume hasn't changed
386         if (newvol == cdvolume)
387                 return;
388
389         // If the CD has been muted
390         if (newvol == 0.0f)
391                 CDAudio_Pause ();
392         else
393         {
394                 // If the CD has been unmuted
395                 if (cdvolume == 0.0f)
396                         CDAudio_Resume ();
397
398                 if (faketrack != -1)
399                         S_SetChannelVolume (faketrack, newvol);
400                 CDAudio_SysSetVolume (newvol);
401         }
402
403         cdvolume = newvol;
404 }
405
406 void CDAudio_Update (void)
407 {
408         if (!enabled)
409                 return;
410
411         CDAudio_SetVolume (bgmvolume.value);
412
413         if (faketrack == -1)
414                 CDAudio_SysUpdate();
415 }
416
417 int CDAudio_Init (void)
418 {
419         int i;
420
421         if (cls.state == ca_dedicated)
422                 return -1;
423
424 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
425         if (COM_CheckParm("-nocdaudio"))
426                 return -1;
427
428         CDAudio_SysInit();
429
430         for (i = 0; i < MAXTRACKS; i++)
431                 *remap[i] = 0;
432
433         Cvar_RegisterVariable(&cdaudioinitialized);
434         Cvar_SetValueQuick(&cdaudioinitialized, true);
435         enabled = true;
436
437         Cmd_AddCommand("cd", CD_f, "execute a CD drive command (cd on/off/reset/remap/close/play/loop/stop/pause/resume/eject/info) - use cd by itself for usage");
438
439         return 0;
440 }
441
442 int CDAudio_Startup (void)
443 {
444         if (COM_CheckParm("-nocdaudio"))
445                 return -1;
446
447         CDAudio_SysStartup ();
448
449         if (CDAudio_GetAudioDiskInfo())
450         {
451                 Con_Print("CDAudio_Init: No CD in player.\n");
452                 cdValid = false;
453         }
454
455         saved_vol = CDAudio_SysGetVolume ();
456         if (saved_vol < 0.0f)
457         {
458                 Con_Print ("Can't get initial CD volume\n");
459                 saved_vol = 1.0f;
460         }
461         else
462                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
463
464         initialized = true;
465
466         Con_Print("CD Audio Initialized\n");
467
468         return 0;
469 }
470
471 void CDAudio_Shutdown (void)
472 {
473         if (!initialized)
474                 return;
475
476         CDAudio_SysSetVolume (saved_vol);
477
478         CDAudio_Stop();
479         CDAudio_SysShutdown();
480         initialized = false;
481 }