]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
download system:
[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         Host_StartVideo();
261
262         if (Cmd_Argc() < 2)
263                 return;
264
265         command = Cmd_Argv (1);
266
267         if (strcasecmp(command, "on") == 0)
268         {
269                 enabled = true;
270                 return;
271         }
272
273         if (strcasecmp(command, "off") == 0)
274         {
275                 if (cdPlaying)
276                         CDAudio_Stop();
277                 enabled = false;
278                 return;
279         }
280
281         if (strcasecmp(command, "reset") == 0)
282         {
283                 enabled = true;
284                 if (cdPlaying)
285                         CDAudio_Stop();
286                 for (n = 0; n < MAXTRACKS; n++)
287                         *remap[n] = 0; // empty string, that is, unremapped
288                 CDAudio_GetAudioDiskInfo();
289                 return;
290         }
291
292         if (strcasecmp(command, "remap") == 0)
293         {
294                 ret = Cmd_Argc() - 2;
295                 if (ret <= 0)
296                 {
297                         for (n = 1; n < MAXTRACKS; n++)
298                                 if (*remap[n])
299                                         Con_Printf("  %u -> %s\n", n, remap[n]);
300                         return;
301                 }
302                 for (n = 1; n <= ret; n++)
303                         strlcpy(remap[n], Cmd_Argv (n+1), sizeof(*remap));
304                 return;
305         }
306
307         if (strcasecmp(command, "close") == 0)
308         {
309                 CDAudio_CloseDoor();
310                 return;
311         }
312
313         if (strcasecmp(command, "play") == 0)
314         {
315                 CDAudio_Play_byName(Cmd_Argv (2), false);
316                 return;
317         }
318
319         if (strcasecmp(command, "loop") == 0)
320         {
321                 CDAudio_Play_byName(Cmd_Argv (2), true);
322                 return;
323         }
324
325         if (strcasecmp(command, "stop") == 0)
326         {
327                 CDAudio_Stop();
328                 return;
329         }
330
331         if (strcasecmp(command, "pause") == 0)
332         {
333                 CDAudio_Pause();
334                 return;
335         }
336
337         if (strcasecmp(command, "resume") == 0)
338         {
339                 CDAudio_Resume();
340                 return;
341         }
342
343         if (strcasecmp(command, "eject") == 0)
344         {
345                 if (cdPlaying && faketrack == -1)
346                         CDAudio_Stop();
347                 CDAudio_Eject();
348                 cdValid = false;
349                 return;
350         }
351
352         if (strcasecmp(command, "info") == 0)
353         {
354                 CDAudio_GetAudioDiskInfo ();
355                 if (cdValid)
356                         Con_Printf("%u tracks on CD.\n", maxTrack);
357                 else
358                         Con_Print ("No CD in player.\n");
359                 if (cdPlaying)
360                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
361                 else if (wasPlaying)
362                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
363                 Con_Printf("Volume is %f\n", cdvolume);
364                 return;
365         }
366
367         Con_Printf("CD commands:\n");
368         Con_Printf("cd on - enables CD audio system\n");
369         Con_Printf("cd off - stops and disables CD audio system\n");
370         Con_Printf("cd reset - resets CD audio system (clears track remapping and re-reads disc information)");
371         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");
372         Con_Printf("cd close - closes CD tray\n");
373         Con_Printf("cd eject - stops playing music and opens CD tray to allow you to change disc\n");
374         Con_Printf("cd play <tracknumber> - plays selected track in remapping table\n");
375         Con_Printf("cd loop <tracknumber> - plays and repeats selected track in remapping table\n");
376         Con_Printf("cd stop - stops playing current CD track\n");
377         Con_Printf("cd pause - pauses CD playback\n");
378         Con_Printf("cd resume - unpauses CD playback\n");
379         Con_Printf("cd info - prints basic disc information (number of tracks, currently playing track, volume level)\n");
380 }
381
382 void CDAudio_SetVolume (float newvol)
383 {
384         // If the volume hasn't changed
385         if (newvol == cdvolume)
386                 return;
387
388         // If the CD has been muted
389         if (newvol == 0.0f)
390                 CDAudio_Pause ();
391         else
392         {
393                 // If the CD has been unmuted
394                 if (cdvolume == 0.0f)
395                         CDAudio_Resume ();
396
397                 if (faketrack != -1)
398                         S_SetChannelVolume (faketrack, newvol);
399                 CDAudio_SysSetVolume (newvol);
400         }
401
402         cdvolume = newvol;
403 }
404
405 void CDAudio_Update (void)
406 {
407         if (!enabled)
408                 return;
409
410         CDAudio_SetVolume (bgmvolume.value);
411
412         if (faketrack == -1)
413                 CDAudio_SysUpdate();
414 }
415
416 int CDAudio_Init (void)
417 {
418         int i;
419
420         if (cls.state == ca_dedicated)
421                 return -1;
422
423 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
424         if (COM_CheckParm("-nocdaudio"))
425                 return -1;
426
427         CDAudio_SysInit();
428
429         for (i = 0; i < MAXTRACKS; i++)
430                 *remap[i] = 0;
431
432         Cvar_RegisterVariable(&cdaudioinitialized);
433         Cvar_SetValueQuick(&cdaudioinitialized, true);
434         enabled = true;
435
436         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");
437
438         return 0;
439 }
440
441 int CDAudio_Startup (void)
442 {
443         if (COM_CheckParm("-nocdaudio"))
444                 return -1;
445
446         CDAudio_SysStartup ();
447
448         if (CDAudio_GetAudioDiskInfo())
449         {
450                 Con_Print("CDAudio_Init: No CD in player.\n");
451                 cdValid = false;
452         }
453
454         saved_vol = CDAudio_SysGetVolume ();
455         if (saved_vol < 0.0f)
456         {
457                 Con_Print ("Can't get initial CD volume\n");
458                 saved_vol = 1.0f;
459         }
460         else
461                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
462
463         initialized = true;
464
465         Con_Print("CD Audio Initialized\n");
466
467         return 0;
468 }
469
470 void CDAudio_Shutdown (void)
471 {
472         if (!initialized)
473                 return;
474
475         CDAudio_SysSetVolume (saved_vol);
476
477         CDAudio_Stop();
478         CDAudio_SysShutdown();
479         initialized = false;
480 }