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