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