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