]> icculus.org git repositories - btb/d2x.git/blob - main/songs.c
comments
[btb/d2x.git] / main / songs.c
1 /* $Id: songs.c,v 1.14 2004-08-29 17:57:23 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Routines to manage the songs in Descent.
18  *
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30 #if !defined(_MSC_VER) && !defined(macintosh)
31 #include <unistd.h>
32 #endif
33
34 #include "inferno.h"
35 #include "error.h"
36 #include "pstypes.h"
37 #include "args.h"
38 #include "songs.h"
39 #include "mono.h"
40 #include "cfile.h"
41 #include "digi.h"
42 #include "rbaudio.h"
43 #include "kconfig.h"
44 #include "timer.h"
45
46 song_info Songs[MAX_NUM_SONGS];
47 int Songs_initialized = 0;
48
49 #ifndef MACINTOSH
50 int Num_songs;
51 #endif
52
53 extern void digi_stop_current_song();
54
55 int Redbook_enabled = 1;
56
57 //0 if redbook is no playing, else the track number
58 int Redbook_playing = 0;
59
60 #define NumLevelSongs (Num_songs - SONG_FIRST_LEVEL_SONG)
61
62 extern int CD_blast_mixer();
63
64 #ifndef MACINTOSH
65 #define REDBOOK_VOLUME_SCALE  (255/3)           //255 is MAX
66 #else
67 #define REDBOOK_VOLUME_SCALE    (255)
68 #endif
69
70 //takes volume in range 0..8
71 void set_redbook_volume(int volume)
72 {
73         #ifndef MACINTOSH
74         RBASetVolume(0);                // makes the macs sound really funny
75         #endif
76         RBASetVolume(volume*REDBOOK_VOLUME_SCALE/8);
77 }
78
79 extern char CDROM_dir[];
80
81 void songs_init()
82 {
83         int i;
84         char inputline[80+1];
85         CFILE * fp;
86
87         if ( Songs_initialized ) return;
88
89
90         #if !defined(MACINTOSH) && !defined(WINDOWS)    // don't crank it if on a macintosh!!!!!
91                 if (!FindArg("-nomixer"))
92                         CD_blast_mixer();   // Crank it!
93         #endif
94
95
96         if (cfexist("descent.sng")) {   // mac (demo?) datafiles don't have the .sng file
97                 fp = cfopen( "descent.sng", "rb" );
98                 if ( fp == NULL )
99                 {
100                         Error( "Couldn't open descent.sng" );
101                 }
102                 i = 0;
103                 while (cfgets(inputline, 80, fp ))
104                 {
105                         if ( strlen( inputline ) )
106                         {
107                                 Assert( i < MAX_NUM_SONGS );
108                                 sscanf( inputline, "%s %s %s",
109                                                 Songs[i].filename,
110                                                 Songs[i].melodic_bank_file,
111                                                 Songs[i].drum_bank_file );
112                                 //printf( "%d. '%s' '%s' '%s'\n",i,Songs[i].filename,Songs[i].melodic_bank_file,Songs[i].drum_bank_file );
113                                 i++;
114                         }
115                 }
116                 Num_songs = i;
117                 if (Num_songs <= SONG_FIRST_LEVEL_SONG)
118                         Error("Must have at least %d songs",SONG_FIRST_LEVEL_SONG+1);
119                 cfclose(fp);
120         }
121
122         Songs_initialized = 1;
123
124         //      RBA Hook
125         #if !defined(SHAREWARE) || ( defined(SHAREWARE) && defined(APPLE_DEMO) )
126                 if (FindArg("-noredbook"))
127                 {
128                         Redbook_enabled = 0;
129                 }
130                 else    // use redbook
131                 {
132                         #ifndef __MSDOS__ // defined(WINDOWS) || defined(MACINTOSH)
133                                 RBAInit();
134                         #else
135                                 RBAInit(toupper(CDROM_dir[0]) - 'A');
136                         #endif
137
138                                 if (RBAEnabled())
139                         {
140                                 set_redbook_volume(Config_redbook_volume);
141                                 RBARegisterCD();
142                         }
143                 }
144                 atexit(RBAStop);    // stop song on exit
145         #endif  // endof ifndef SHAREWARE, ie ifdef SHAREWARE
146 }
147
148 #define FADE_TIME (f1_0/2)
149
150 //stop the redbook, so we can read off the CD
151 void songs_stop_redbook(void)
152 {
153         int old_volume = Config_redbook_volume*REDBOOK_VOLUME_SCALE/8;
154         fix old_time = timer_get_fixed_seconds();
155
156         if (Redbook_playing) {          //fade out volume
157                 int new_volume;
158                 do {
159                         fix t = timer_get_fixed_seconds();
160
161                         new_volume = fixmuldiv(old_volume,(FADE_TIME - (t-old_time)),FADE_TIME);
162
163                         if (new_volume < 0)
164                                 new_volume = 0;
165
166                         RBASetVolume(new_volume);
167
168                 } while (new_volume > 0);
169         }
170
171         RBAStop();                      // Stop CD, if playing
172
173         RBASetVolume(old_volume);       //restore volume
174
175         Redbook_playing = 0;            
176
177 }
178
179 //stop any songs - midi or redbook - that are currently playing
180 void songs_stop_all(void)
181 {
182         digi_stop_current_song();       // Stop midi song, if playing
183
184         songs_stop_redbook();                   // Stop CD, if playing
185 }
186
187 int force_rb_register=0;
188
189 void reinit_redbook()
190 {
191         #ifndef __MSDOS__ // defined(WINDOWS) || defined(MACINTOSH)
192                 RBAInit();
193         #else
194                 RBAInit(toupper(CDROM_dir[0]) - 'A');
195         #endif
196
197         if (RBAEnabled())
198         {
199                 set_redbook_volume(Config_redbook_volume);
200                 RBARegisterCD();
201                 force_rb_register=0;
202         }
203 }
204
205
206 //returns 1 if track started sucessfully
207 //start at tracknum.  if keep_playing set, play to end of disc.  else
208 //play only specified track
209 int play_redbook_track(int tracknum,int keep_playing)
210 {
211         Redbook_playing = 0;
212
213         if (!RBAEnabled() && Redbook_enabled && !FindArg("-noredbook"))
214                 reinit_redbook();
215
216         if (force_rb_register) {
217                 RBARegisterCD();                        //get new track list for new CD
218                 force_rb_register = 0;
219         }
220
221         if (Redbook_enabled && RBAEnabled()) {
222                 int num_tracks = RBAGetNumberOfTracks();
223                 if (tracknum <= num_tracks)
224                         if (RBAPlayTracks(tracknum,keep_playing?num_tracks:tracknum))  {
225                                 Redbook_playing = tracknum;
226                         }
227         }
228
229         return (Redbook_playing != 0);
230 }
231
232
233 #if 0
234
235 #define REDBOOK_TITLE_TRACK         2
236 #define REDBOOK_CREDITS_TRACK       3
237 #define REDBOOK_FIRST_LEVEL_TRACK   (songs_haved2_cd()?4:1)
238
239 // songs_haved2_cd returns 1 if the descent 2 CD is in the drive and
240 // 0 otherwise
241 int songs_haved2_cd()
242 {
243         char temp[128],cwd[128];
244         
245         getcwd(cwd, 128);
246
247         strcpy(temp,CDROM_dir);
248
249         #ifndef MACINTOSH               //for PC, strip of trailing slash
250         if (temp[strlen(temp)-1] == '\\')
251                 temp[strlen(temp)-1] = 0;
252         #endif
253
254         if ( !chdir(temp) ) {
255                 chdir(cwd);
256                 return 1;
257         }
258
259         return 0;
260 }
261
262 #else
263
264
265 /* Redbook versions of 13 songs from Descent 1 as found on the Macintosh version.
266    All the same tracklist, but some versions have tracks mixed to different lengths
267  1:  Data
268  2:  Primitive Rage
269  3:  Outer Limits
270  4:  The Escape (aka Close Call)
271  5:  Ether in the Air (aka The Darkness of Space)
272  6:  Robotic Menace (aka Get It On)
273  7:  Virtual Tension (aka Fight)
274  8:  Time for the Big Guns (aka Death Lurks Beneath)
275  9:  Mystery Metal (aka C-4 Home Recipe)
276  10: Hydraulic Pressure (aka Escape)
277  11: Not That Button! (aka Backwards Time)
278  12: Industrial Accident (aka Crazyfactory)
279  13: Overdrive (aka Machine Gun)
280  14: A Big Problem (aka Insanity)
281  */
282 #define D1_DISCID_1         0xb60d990e
283 #define D1_DISCID_2         0xde0feb0e
284 #define D1_DISCID_3         0xb70ee40e
285
286 #define D1_RB_TITLE             2
287 #define D1_RB_BRIEFING          3
288 #define D1_RB_ENDLEVEL          4
289 #define D1_RB_ENDGAME           3
290 #define D1_RB_CREDITS           5
291 #define D1_RB_FIRST_LEVEL_SONG  6
292
293 /* Descent II
294  1:  Data
295  2:  Title
296  3:  Crawl
297  4:  Glut
298  5:  Gunner Down
299  6:  Cold Reality
300  7:  Ratzez
301  8:  Crush
302  9:  Untitled
303  10: Haunted (Instrumental Remix)
304  11: Are You Descent?
305  12: Techno Industry
306  13: Robot Jungle
307  */
308 #define D2_DISCID_1         0x22115710 // Mac version, has some extended versions and 3 bonus tracks
309 #define D2_DISCID_2         0xac0bc30d
310 #define D2_DISCID_3         0xc40c0a0d
311 #define D2_DISCID_4         0xc610080d
312 #define D2_DISCID_5         0xcc101b0d
313 #define D2_DISCID_6         0xd00bf30d
314 #define D2_DISCID_7         0xd2101d0d
315 #define D2_DISCID_8         0xd410070d
316 #define D2_DISCID_9         0xda10370d
317
318 #define D2_RB_TITLE            2
319 #define D2_RB_CREDITS          3
320 #define D2_RB_FIRST_LEVEL_SONG 4
321
322 /* Same as above, but all tracks shifted by one
323  1:  Data
324  2:  Data
325  3:  Title
326  4:  Crawl
327  5:  Glut
328  6:  Gunner Down
329  7:  Cold Reality
330  8:  Ratzez
331  9:  Crush
332  10: Untitled
333  11: Haunted (Instrumental Remix)
334  12: Are You Descent?
335  13: Techno Industry
336  14: Robot Jungle
337  */
338 #define D2_2_DISCID_1       0xe010a30e
339
340 #define D2_2_RB_TITLE               3
341 #define D2_2_RB_CREDITS             4
342 #define D2_2_RB_FIRST_LEVEL_SONG    5
343
344 /* Descent II: The Infinite Abyss
345  1:  Data
346  2:  Title
347  3:  Cold Reality - Extended Remix
348  4:  Crawl - Extended Remix
349  5:  Gunner Down - Extended Remix
350  6:  Ratzez - Extended Remix
351  7:  Techno Industry - Extended Remix
352  8:  Are You Descent? - Extended Remix
353  9:  Robot Jungle - Extended Remix
354  */
355 #define D2_IA_DISCID_1      0x7d0ff809
356 #define D2_IA_DISCID_2      0x8110ec09
357 #define D2_IA_DISCID_3      0x82104909
358 #define D2_IA_DISCID_4      0x85101d09
359 #define D2_IA_DISCID_5      0x87102209
360
361 #define D2_IA_RB_TITLE              2
362 #define D2_IA_RB_CREDITS            3
363 #define D2_IA_RB_FIRST_LEVEL_SONG   4
364
365 /* Descent II: Vertigo Series
366  1:  Data
367  2:  Crush - Extended Remix
368  3:  Glut - Extended Remix
369  4:  Haunted - Instrumental Re-Remix
370  5:  New Song #1
371  6:  Untitled - Extended Remix
372  7:  New Song #2
373  8:  New Song #3
374  */
375 #define D2_X_DISCID_1       0x53078208
376 #define D2_X_DISCID_2       0x64071408
377
378
379 int songs_redbook_track(int songnum)
380 {
381         uint32_t discid;
382
383         if (!Redbook_enabled)
384                 return 0;
385
386         discid = RBAGetDiscID();
387
388         switch (discid) {
389                 case D1_DISCID_1:
390                 case D1_DISCID_2:
391                 case D1_DISCID_3:
392                         switch (songnum) {
393                                 case SONG_TITLE:            return D1_RB_TITLE;
394                                 case SONG_BRIEFING:         return D1_RB_BRIEFING;
395                                 case SONG_ENDLEVEL:         return D1_RB_ENDLEVEL;
396                                 case SONG_ENDGAME:          return D1_RB_ENDGAME;
397                                 case SONG_CREDITS:          return D1_RB_CREDITS;
398                                 case SONG_FIRST_LEVEL_SONG: return D1_RB_FIRST_LEVEL_SONG;
399                                 default: Int3();
400                         }
401                 case D2_DISCID_1:
402                 case D2_DISCID_2:
403                 case D2_DISCID_3:
404                 case D2_DISCID_4:
405                 case D2_DISCID_5:
406                 case D2_DISCID_6:
407                 case D2_DISCID_7:
408                 case D2_DISCID_8:
409                 case D2_DISCID_9:
410                         switch (songnum) {
411                                 case SONG_TITLE:            return D2_RB_TITLE;
412                                 case SONG_CREDITS:          return D2_RB_CREDITS;
413                                 case SONG_FIRST_LEVEL_SONG: return D2_RB_FIRST_LEVEL_SONG;
414                                 default: Int3();
415                         }
416                 case D2_2_DISCID_1:
417                         switch (songnum) {
418                                 case SONG_TITLE:            return D2_2_RB_TITLE;
419                                 case SONG_CREDITS:          return D2_2_RB_CREDITS;
420                                 case SONG_FIRST_LEVEL_SONG: return D2_2_RB_FIRST_LEVEL_SONG;
421                                 default: Int3();
422                         }
423                 case D2_IA_DISCID_1:
424                 case D2_IA_DISCID_2:
425                 case D2_IA_DISCID_3:
426                 case D2_IA_DISCID_4:
427                 case D2_IA_DISCID_5:
428                         switch (songnum) {
429                                 case SONG_TITLE:            return D2_IA_RB_TITLE;
430                                 case SONG_CREDITS:          return D2_IA_RB_CREDITS;
431                                 case SONG_FIRST_LEVEL_SONG: return D2_IA_RB_FIRST_LEVEL_SONG;
432                                 default: Int3();
433                         }
434                 case D2_X_DISCID_1:
435                 case D2_X_DISCID_2:
436                         return 2;
437
438                 default:
439                         con_printf(CON_DEBUG, "Unknown CD. discid: %x\n", discid);
440                         return 1;
441         }
442 }
443
444 #define REDBOOK_TITLE_TRACK         (songs_redbook_track(SONG_TITLE))
445 #define REDBOOK_CREDITS_TRACK       (songs_redbook_track(SONG_CREDITS))
446 #define REDBOOK_FIRST_LEVEL_TRACK   (songs_redbook_track(SONG_FIRST_LEVEL_SONG))
447
448 #endif
449
450
451 void songs_play_song( int songnum, int repeat )
452 {
453         #ifndef SHAREWARE
454         //Assert(songnum != SONG_ENDLEVEL && songnum != SONG_ENDGAME);  //not in full version
455         #endif
456
457         if ( !Songs_initialized )
458                 songs_init();
459
460         //stop any music already playing
461
462         songs_stop_all();
463
464         //do we want any of these to be redbook songs?
465
466         if (force_rb_register) {
467                 RBARegisterCD();                        //get new track list for new CD
468                 force_rb_register = 0;
469         }
470
471         if (songnum == SONG_TITLE)
472                 play_redbook_track(REDBOOK_TITLE_TRACK,0);
473         else if (songnum == SONG_CREDITS)
474                 play_redbook_track(REDBOOK_CREDITS_TRACK,0);
475
476         if (!Redbook_playing) {         //not playing redbook, so play midi
477
478                 #ifndef MACINTOSH
479                         digi_play_midi_song( Songs[songnum].filename, Songs[songnum].melodic_bank_file, Songs[songnum].drum_bank_file, repeat );
480                 #else
481                         digi_play_midi_song(songnum, repeat);
482                 #endif
483         }
484 }
485
486 int current_song_level;
487
488 void songs_play_level_song( int levelnum )
489 {
490         int songnum;
491         int n_tracks;
492
493         Assert( levelnum != 0 );
494
495         if ( !Songs_initialized )
496                 songs_init();
497
498         songs_stop_all();
499
500         current_song_level = levelnum;
501
502         songnum = (levelnum>0)?(levelnum-1):(-levelnum);
503
504         if (!RBAEnabled() && Redbook_enabled && !FindArg("-noredbook"))
505                 reinit_redbook();
506
507         if (force_rb_register) {
508                 RBARegisterCD();                        //get new track list for new CD
509                 force_rb_register = 0;
510         }
511
512         if (Redbook_enabled && RBAEnabled() && (n_tracks = RBAGetNumberOfTracks()) > 1) {
513
514                 //try to play redbook
515
516                 mprintf((0,"n_tracks = %d\n",n_tracks));
517
518                 play_redbook_track(REDBOOK_FIRST_LEVEL_TRACK + (songnum % (n_tracks-REDBOOK_FIRST_LEVEL_TRACK+1)),1);
519         }
520
521         if (! Redbook_playing) {                        //not playing redbook, so play midi
522
523                 songnum = SONG_FIRST_LEVEL_SONG + (songnum % NumLevelSongs);
524
525                 #ifndef MACINTOSH
526                         digi_play_midi_song( Songs[songnum].filename, Songs[songnum].melodic_bank_file, Songs[songnum].drum_bank_file, 1 );
527                 #else
528                         digi_play_midi_song( songnum, 1 );
529                 #endif
530
531         }
532 }
533
534 //this should be called regularly to check for redbook restart
535 void songs_check_redbook_repeat()
536 {
537         static fix last_check_time;
538         fix current_time;
539
540         if (!Redbook_playing || Config_redbook_volume==0) return;
541
542         current_time = timer_get_fixed_seconds();
543         if (current_time < last_check_time || (current_time - last_check_time) >= F2_0) {
544                 if (!RBAPeekPlayStatus()) {
545                         stop_time();
546                         // if title ends, start credit music
547                         // if credits music ends, restart it
548                         if (Redbook_playing == REDBOOK_TITLE_TRACK || Redbook_playing == REDBOOK_CREDITS_TRACK)
549                                 play_redbook_track(REDBOOK_CREDITS_TRACK,0);
550                         else {
551                                 //songs_goto_next_song();
552         
553                                 //new code plays all tracks to end of disk, so if disk has
554                                 //stopped we must be at end.  So start again with level 1 song.
555         
556                                 songs_play_level_song(1);
557                         }
558                         start_time();
559                 }
560                 last_check_time = current_time;
561         }
562 }
563
564 //goto the next level song
565 void songs_goto_next_song()
566 {
567         if (Redbook_playing)            //get correct track
568                 current_song_level = RBAGetTrackNum() - REDBOOK_FIRST_LEVEL_TRACK + 1;
569
570         songs_play_level_song(current_song_level+1);
571
572 }
573
574 //goto the previous level song
575 void songs_goto_prev_song()
576 {
577         if (Redbook_playing)            //get correct track
578                 current_song_level = RBAGetTrackNum() - REDBOOK_FIRST_LEVEL_TRACK + 1;
579
580         if (current_song_level > 1)
581                 songs_play_level_song(current_song_level-1);
582
583 }
584