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