]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
MCBSP version 2 loading in the engine.. Some very ugly code (will be rewritten)....
[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 "snd_main.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 (qbyte 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"};
46
47 static qboolean wasPlaying = false;
48 static qboolean initialized = false;
49 static qboolean enabled = false;
50 static float cdvolume;
51 static qbyte remap[MAXTRACKS];
52 static qbyte 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 qbyte 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 (qbyte 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 || sfx->fetcher == NULL)
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((qbyte)atoi(Cmd_Argv (2)), false);
272                 return;
273         }
274
275         if (strcasecmp(command, "loop") == 0)
276         {
277                 CDAudio_Play((qbyte)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
324 void CDAudio_SetVolume (float newvol)
325 {
326         // If the volume hasn't changed
327         if (newvol == cdvolume)
328                 return;
329
330         // If the CD has been muted
331         if (newvol == 0.0f)
332                 CDAudio_Pause ();
333         else
334         {
335                 // If the CD has been unmuted
336                 if (cdvolume == 0.0f)
337                         CDAudio_Resume ();
338
339                 if (faketrack != -1)
340                         S_SetChannelVolume (faketrack, newvol);
341                 CDAudio_SysSetVolume (newvol);
342         }
343
344         cdvolume = newvol;
345 }
346
347 void CDAudio_Update (void)
348 {
349         if (!enabled)
350                 return;
351
352         CDAudio_SetVolume (bgmvolume.value);
353
354         if (faketrack == -1)
355                 CDAudio_SysUpdate();
356 }
357
358 int CDAudio_Init (void)
359 {
360         int i;
361
362         if (cls.state == ca_dedicated)
363                 return -1;
364
365 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
366         if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
367                 return -1;
368
369         CDAudio_SysInit();
370
371         for (i = 0; i < MAXTRACKS; i++)
372                 remap[i] = i;
373
374         Cvar_RegisterVariable(&cdaudioinitialized);
375         Cvar_SetValueQuick(&cdaudioinitialized, true);
376         enabled = true;
377
378         Cmd_AddCommand("cd", CD_f);
379
380         return 0;
381 }
382
383 int CDAudio_Startup (void)
384 {
385         CDAudio_SysStartup ();
386
387         if (CDAudio_GetAudioDiskInfo())
388         {
389                 Con_Print("CDAudio_Init: No CD in player.\n");
390                 cdValid = false;
391         }
392
393         saved_vol = CDAudio_SysGetVolume ();
394         if (saved_vol < 0.0f)
395         {
396                 Con_Print ("Can't get initial CD volume\n");
397                 saved_vol = 1.0f;
398         }
399         else
400                 Con_Printf ("Initial CD volume: %g\n", saved_vol);
401
402         initialized = true;
403
404         Con_Print("CD Audio Initialized\n");
405
406         return 0;
407 }
408
409 void CDAudio_Shutdown (void)
410 {
411         if (!initialized)
412                 return;
413
414         CDAudio_SysSetVolume (saved_vol);
415
416         CDAudio_Stop();
417         CDAudio_SysShutdown();
418         initialized = false;
419 }