2 * $Source: /cvs/cvsroot/d2x/unused/bios/rbaudio.new,v $
5 * $Date: 2001-01-19 03:33:50 $
7 * Redbook Audio Library
9 * $Log: not supported by cvs2svn $
10 * Revision 1.18 1996/04/15 15:27:40 samir
11 * Play multiple tracks and PeekPlayStatus uses timer method!
13 * Revision 1.17 1996/04/12 20:08:42 samir
14 * Added debugging info and Redbook / HSG detection
16 * Revision 1.16 1996/04/12 19:13:29 matt
17 * Changed parameter name and added comments
19 * Revision 1.15 1996/04/12 18:08:02 samir
20 * We now pass a cd drive letter to RBAInit.
22 * Revision 1.14 1996/02/14 13:31:29 samir
23 * Fixed stupid bug that caused last track to never be played
25 * Revision 1.13 1996/02/13 17:16:04 samir
26 * Added Pause and Resume Caps.
28 * Revision 1.12 1996/01/11 10:08:01 matt
29 * RBARegisterCD wasn't checking SendRequest error code, so it was reporting
30 * CD tracks when there was no CD in the drive.
32 * Revision 1.11 1996/01/10 20:15:45 samir
33 * Changed behavior of RBACheckMediaChange to work
34 * correctly, as well as calling this function in critical
35 * RBA functions to take care of CD changes.
37 * Revision 1.10 1996/01/10 18:30:49 samir
38 * Changed property of RBCheckMediaChange which would
39 * not disable RBA if media had changed. GetNumberOfTracks
40 * returns num. of tracks or if media changed, re-registers
41 * CD and then returns number of tracks. If media has changed
42 * RBAPlayTrack will return an error.
44 * Revision 1.9 1995/08/01 19:30:11 samir
45 * Added ability to get number of tracks on CD via call.
47 * Revision 1.8 1995/07/20 16:13:56 samir
48 * Doesn't quit if CD isn't Redbook.
50 * Revision 1.7 1995/05/10 11:37:51 samir
51 * Checks whether CD-ROM in drive at start of game
53 * Revision 1.6 1995/05/09 18:00:08 samir
56 * Revision 1.5 1995/05/09 17:57:43 samir
57 * No CD in Drive, then diable RBA
59 * Revision 1.4 1995/05/09 17:12:24 samir
60 * Added user detection of Redbook Audio.
62 * Revision 1.3 1995/05/09 12:04:57 samir
63 * Added ability to known when a CD is playing
64 * and when it's stopped.
66 * Revision 1.2 1995/05/08 11:33:37 samir
72 char rbaudio_rcsid[] = "$Id: rbaudio.new,v 1.1.1.2 2001-01-19 03:33:50 bradleyb Exp $";
86 #define HSG_CD_PLAYBACK 0
87 #define MSF_CD_PLAYBACK 1
89 #define RBERR_WRITEPROTVIOL 0
90 #define RBERR_UNKNOWNUNIT 1
91 #define RBERR_DRIVENOTREADY 2
92 #define RBERR_UNKNOWNCOMMAND 3
93 #define RBERR_CRCERROR 4
94 #define RBERR_BADDRIVEREQ 5
95 #define RBERR_SEEKERROR 6
96 #define RBERR_UNKNOWNMEDIA 7
97 #define RBERR_SECTORNOTFOUND 8
98 #define RBERR_WRITEFAULT 10
99 #define RBERR_READFAULT 12
100 #define RBERR_GENERALFAILURE 13
101 #define RBERR_BADDISKCHANGE 15
103 #define RBSTAT_ERROR 0x8000
104 #define RBSTAT_BUSY 0x0200
105 #define RBSTAT_DONE 0x0100
110 // Request Header -----------------------------------------------------
112 typedef struct _REQHDR {
113 unsigned char length;
114 unsigned char subunit;
116 unsigned short status;
120 typedef struct _IOCTL {
122 unsigned short buffer_realoff;
123 unsigned short buffer_realseg;
124 unsigned short xferlen;
125 unsigned short sector;
130 // CD Information -----------------------------------------------------
133 char code; // Control Block Code
134 char track; // Track Number
135 long start; // Start of Track in MSF (Redbook)
136 unsigned char info; // Track Control Info
137 } CDTrackInfo = {11,2,0L,0};
142 } TrackInfoREQ = {{13,0,3,0,""}, {0,0,0,0,0}};
146 unsigned long devstat;
152 } DevStatREQ = {{13,0,3,0,""}, {0,0,0,0,0}};
162 } MedChgREQ = {{13,0,3,0,""}, {0,0,0,0,0}};
166 } StopREQ = {{13,0,133,0,""}};
170 } ResumeREQ = {{13,0,136,0,""}};
176 unsigned long lead_out;
177 } CDInfo = {10,0,0,0L};
182 } CDInfoREQ = {{13,0,3,0,""},{0,0,0,0,0}};
186 char mode; // Play in HSG or MSF(RedBook)
187 unsigned long playbeg;
188 unsigned long playlen; // HSG Format
189 char empty; // Empty byte used so that all requests have same size
190 } PlayREQ = {{13,0,132,0,""},0,0,0,0};
194 unsigned char out0in;
195 unsigned char out0vol;
196 unsigned char out1in;
197 unsigned char out1vol;
198 unsigned char out2in;
199 unsigned char out2vol;
200 unsigned char out3in;
201 unsigned char out3vol;
202 } AUDChannelCtl = {3,0,0,0,0,0,0,0,0};
207 } AUDChannelCtlREQ = {{13,0,12,0,""},{0,0,0,0,0}};
211 unsigned char out0in;
212 unsigned char out0vol;
213 unsigned char out1in;
214 unsigned char out1vol;
215 unsigned char out2in;
216 unsigned char out2vol;
217 unsigned char out3in;
218 unsigned char out3vol;
219 } AUDChannelInfo = {4,0,0,0,0,0,0,0,0};
224 } AUDChannelInfoREQ = {{13,0,3,0,""},{0,0,0,0,0}};
229 unsigned long headloc;
230 } HeadInfo = {1,MSF_CD_PLAYBACK,0L};
235 } HeadInfoREQ = {{13,0,3,0,""},{0,0,0,0,0}};
249 //@@} QChannelInfo = {12, MSF_CD_PLAYBACK, 2,0,0,0,0,0,0,0,0 };
254 //@@} QChannelInfoREQ = {{13, 0, 3, 0, ""}, {0,0,0,0,0}};
257 // Translation from RealMode->Protected mode buffer
259 typedef struct _REQ_xlatbuf { // Buffer for Real Mode Callback
266 // Other Data ---------------------------------------------------------
268 static unsigned char CDDriveLetter;
269 static unsigned long CDNumTracks,CDTrackStart[101];
270 static int RedBookEnabled = 0;
271 static int RedBookPlaybackType = MSF_CD_PLAYBACK;
272 static fix Playback_Start_Time = 0;
273 static fix Playback_Pause_Time = 0;
274 static fix Playback_Length = 0;
277 // Prototypes ---------------------------------------------------------
279 int RBSendRequest(char *request, char *xferptr, int xferlen);
280 unsigned long msf_to_lsn(unsigned long msf);
281 unsigned long lsn_to_msf(unsigned long lsn);
284 // --------------------------------------------------------------------
286 void RBAInit(ubyte cd_drive_num) //drive a == 0, drive b == 1
290 // Detect presence of CD-ROM
293 int386(0x2f, ®s, ®s);
296 RBADisable(); // Disable RedBook
297 // Error("RBA Error: MSCDEX.EXE compatible driver couldn't be found.");
299 CDDriveLetter = cd_drive_num;
300 // CDDriveLetter = (unsigned char)regs.x.ecx;
303 RBACheckMediaChange();
305 RBSendRequest((char*)&DevStatREQ, (char*)&DevStat, sizeof(DevStat));
307 // If Door drive open, or no disc in CD-ROM, then Redbook is Disabled.
308 if ((DevStat.devstat&2048) || (DevStat.devstat&1) || RBAEnabled() == 0)
311 //@@ if (DevStat.devstat&4)
312 //@@ mprintf((0, "supports cooked and raw reading.\n"));
314 //@@ mprintf((0, "supports only cooked reading.\n"));
316 //@@ if (DevStat.devstat&256)
317 //@@ mprintf((0, "audio channel manipulation.\n"));
319 //@@ mprintf((0, "no audio channel manipulation.\n"));
321 if (DevStat.devstat&512) {
322 // RedBookPlaybackType = MSF_CD_PLAYBACK;
323 mprintf((0, "supports HSG and RedBook addressing mode.\n"));
326 // RedBookPlaybackType = HSG_CD_PLAYBACK;
327 mprintf((0, "supports HSG addressing only.\n"));
330 RedBookPlaybackType = MSF_CD_PLAYBACK;
335 //find out how many tracks on the CD, and their starting locations
336 void RBARegisterCD(void)
341 // Get CD Information First
342 RBACheckMediaChange();
344 error = RBSendRequest((char*)&CDInfoREQ, (char*)&CDInfo, sizeof(CDInfo));
346 if (error & RBSTAT_ERROR) { //error!
351 CDNumTracks = (CDInfo.last-CDInfo.first)+1;
353 // Define Track Starts
354 for (i=CDInfo.first; i<=CDNumTracks; i++)
356 CDTrackInfo.track = i;
357 RBSendRequest((char *)&TrackInfoREQ, (char*)&CDTrackInfo, sizeof(CDTrackInfo));
358 CDTrackStart[i] = CDTrackInfo.start;
361 CDTrackStart[i] = CDInfo.lead_out;
365 long RBAGetDeviceStatus()
367 RBSendRequest((char*)&DevStatREQ, (char*)&DevStat, sizeof(DevStat));
368 return (long)DevStat.devstat;
372 int RBAGetNumberOfTracks(void)
374 // Get CD Information
375 if (RBACheckMediaChange())
382 int RBAPlayTrack(int track)
384 unsigned long playlen;
387 Playback_Start_Time = 0;
390 if (track < CDInfo.first || track > CDInfo.last) {
391 mprintf((0,"CD not Redbook Compatible. Will not play track.\n"));
393 // Error("RBA Error: Track %d doesn't exist on CD!!!", track);
395 if (RBACheckMediaChange()) {
396 mprintf((0, "Unable to play track due to CD change.\n"));
400 // Play the track now!!!
401 PlayREQ.mode = RedBookPlaybackType;
402 PlayREQ.playbeg = CDTrackStart[track];
404 if (RedBookPlaybackType == MSF_CD_PLAYBACK) {
406 msf_to_lsn(CDTrackStart[track+1]) - msf_to_lsn(CDTrackStart[track]);
409 mprintf((1, "RBERR: No! No! No! This shouldn't happen (HSG_CD_PLAYBACK).\n"));
411 PlayREQ.playlen = CDTrackStart[track+1] - CDTrackStart[track];
413 RBSendRequest((char *)&PlayREQ,NULL,0);
415 playlen = msf_to_lsn(CDTrackStart[track+1]) - msf_to_lsn(CDTrackStart[track]);
416 playlen = lsn_to_msf(playlen);
417 min = (int)((playlen >> 16) & 0xFF);
418 sec = (int)((playlen >> 8) & 0xFF);
420 Playback_Start_Time = timer_get_fixed_seconds();
421 Playback_Length = i2f(min*60+sec);
423 mprintf( (0,"Playing Track %d (len: %d secs)\n", track, min*60+sec) );
428 //plays tracks first through last, inclusive
429 int RBAPlayTracks(int first, int last)
431 unsigned long playlen;
434 Playback_Start_Time = 0;
437 if (first < CDInfo.first || last > CDInfo.last) {
438 mprintf((0,"Invalid start or end track.\n"));
440 // Error("RBA Error: Track %d doesn't exist on CD!!!", track);
442 if (RBACheckMediaChange()) {
443 mprintf((0, "Unable to play track due to CD change.\n"));
447 // Play the track now!!!
448 PlayREQ.mode = RedBookPlaybackType;
449 PlayREQ.playbeg = CDTrackStart[first];
451 if (RedBookPlaybackType == MSF_CD_PLAYBACK) {
453 msf_to_lsn(CDTrackStart[last+1]) - msf_to_lsn(CDTrackStart[first]);
456 mprintf((1, "RBERR: No! No! No! This shouldn't happen (HSG_CD_PLAYBACK).\n"));
458 PlayREQ.playlen = CDTrackStart[last+1] - CDTrackStart[first];
460 RBSendRequest((char *)&PlayREQ,NULL,0);
462 playlen = msf_to_lsn(CDTrackStart[last+1]) - msf_to_lsn(CDTrackStart[first]);
463 playlen = lsn_to_msf(playlen);
464 min = (int)((playlen >> 16) & 0xFF);
465 sec = (int)((playlen >> 8) & 0xFF);
467 Playback_Start_Time = timer_get_fixed_seconds();
468 Playback_Length = i2f(min*60+sec);
470 mprintf( (0,"Playing Tracks %d-%d (len: %d secs)\n", first,last,min*60+sec) );
478 RBSendRequest((char *)&StopREQ, NULL, 0);
479 Playback_Pause_Time = timer_get_fixed_seconds();
485 if (RBACheckMediaChange()) {
487 return RBA_MEDIA_CHANGED;
489 RBSendRequest((char *)&ResumeREQ, NULL, 0);
491 Playback_Start_Time += timer_get_fixed_seconds() - Playback_Pause_Time;
499 if (RBACheckMediaChange())
502 RBSendRequest((char *)&StopREQ,NULL,0);
506 void RBASetStereoAudio(RBACHANNELCTL *channels)
508 AUDChannelCtl.out0in = channels->out0in;
509 AUDChannelCtl.out0vol = channels->out0vol;
510 AUDChannelCtl.out1in = channels->out1in;
511 AUDChannelCtl.out1vol = channels->out1vol;
512 AUDChannelCtl.out2in = AUDChannelCtl.out2vol = 0;
513 AUDChannelCtl.out3in = AUDChannelCtl.out3vol = 0;
515 RBSendRequest((char*)&AUDChannelCtlREQ, (char*)&AUDChannelCtl, sizeof(AUDChannelCtl));
519 void RBASetQuadAudio(RBACHANNELCTL *channels)
521 AUDChannelCtl.out0in = (unsigned char)channels->out0in;
522 AUDChannelCtl.out0vol = (unsigned char)channels->out0vol;
523 AUDChannelCtl.out1in = (unsigned char)channels->out1in;
524 AUDChannelCtl.out1vol = (unsigned char)channels->out1vol;
525 AUDChannelCtl.out2in = (unsigned char)channels->out2in;
526 AUDChannelCtl.out2vol = (unsigned char)channels->out2vol;
527 AUDChannelCtl.out3in = (unsigned char)channels->out3in;
528 AUDChannelCtl.out3vol = (unsigned char)channels->out3vol;
530 RBSendRequest((char*)&AUDChannelCtlREQ, (char*)&AUDChannelCtl, sizeof(AUDChannelCtl));
534 void RBAGetAudioInfo(RBACHANNELCTL *channels)
536 RBSendRequest((char*)&AUDChannelInfoREQ, (char*)&AUDChannelInfo, sizeof(AUDChannelInfo));
538 channels->out0in = (int)AUDChannelInfo.out0in;
539 channels->out0vol = (int)AUDChannelInfo.out0vol;
540 channels->out1in = (int)AUDChannelInfo.out1in;
541 channels->out1vol = (int)AUDChannelInfo.out1vol;
542 channels->out2in = (int)AUDChannelInfo.out2in;
543 channels->out2vol = (int)AUDChannelInfo.out2vol;
544 channels->out3in = (int)AUDChannelInfo.out3in;
545 channels->out3vol = (int)AUDChannelInfo.out3vol;
550 void RBASetChannelVolume(int channel, int volume)
552 //RBACHANNELCTL channels;
554 RBSendRequest((char*)&AUDChannelInfoREQ, (char*)&AUDChannelInfo, sizeof(AUDChannelInfo));
556 AUDChannelCtl.out0in = AUDChannelInfo.out0in;
557 AUDChannelCtl.out0vol = AUDChannelInfo.out0vol;
558 AUDChannelCtl.out1in = AUDChannelInfo.out1in;
559 AUDChannelCtl.out1vol = AUDChannelInfo.out1vol;
560 AUDChannelCtl.out2in = AUDChannelInfo.out2in;
561 AUDChannelCtl.out2vol = AUDChannelInfo.out2vol;
562 AUDChannelCtl.out3in = AUDChannelInfo.out3in;
563 AUDChannelCtl.out3vol = AUDChannelInfo.out3vol;
565 if (channel == 0) AUDChannelCtl.out0vol = (unsigned char)volume;
566 if (channel == 1) AUDChannelCtl.out1vol = (unsigned char)volume;
567 if (channel == 2) AUDChannelCtl.out2vol = (unsigned char)volume;
568 if (channel == 3) AUDChannelCtl.out3vol = (unsigned char)volume;
570 RBSendRequest((char*)&AUDChannelCtlREQ,(char*)&AUDChannelCtl, sizeof(AUDChannelCtl));
575 void RBASetVolume(int volume)
577 RBASetChannelVolume(0,volume);
578 RBASetChannelVolume(1,volume);
582 long RBAGetHeadLoc(int *min, int *sec, int *frame)
586 if (RBACheckMediaChange())
587 return RBA_MEDIA_CHANGED;
589 HeadInfo.mode = RedBookPlaybackType;
590 RBSendRequest((char*)&HeadInfoREQ, (char*)&HeadInfo, sizeof(HeadInfo));
592 if (RedBookPlaybackType == MSF_CD_PLAYBACK)
593 loc = HeadInfo.headloc;
595 loc = lsn_to_msf(HeadInfo.headloc);
597 *min = (int)((loc >> 16) & 0xFF);
598 *sec = (int)((loc >> 8) & 0xFF);
599 *frame = (int)((loc >> 0) & 0xFF);
605 int RBAPeekPlayStatus()
607 if ((timer_get_fixed_seconds()-Playback_Start_Time) > Playback_Length) return 0;
614 if (RedBookEnabled < 1) return 0;
622 mprintf((0, "Enabling Redbook...\n"));
629 mprintf((0, "Disabling Redbook...\n"));
634 // RB functions: Internal to RBA library -----------------------------
636 //returns 1 if media has changed, else 0
637 int RBACheckMediaChange()
639 RBSendRequest((char *)&MedChgREQ, (char*)&MedChg, sizeof(MedChg));
641 if (MedChg.byte == 255 || MedChg.byte == 0) {
642 // New media in drive (or maybe no media in drive)
643 mprintf((0,"CD-ROM Media change detected\n"));
652 //returns 16-bit value. Bit 15 means error.
653 //WE NEED SYMBOLOIC CONSTANTS FOR ALL RETURN CODES & STATUS BITS
654 int RBSendRequest(char *request, char *xferptr, int xferlen)
657 dpmi_real_regs rregs;
660 REQ_xlatbuf *xlat_req; // Translated Buffer Request
661 char *xlat_xferptr; // Translated Buffer Transfer Buffer Ptr
663 unsigned short status;
665 if (!RedBookEnabled) return 0; // Don't send request if no RBA
667 memset(&rregs,0,sizeof(dpmi_real_regs));
669 // Get Temporary Real Mode Buffer for request translation
670 xlat_req = (REQ_xlatbuf *)dpmi_get_temp_low_buffer(128+xferlen);
671 memcpy(xlat_req, request, sizeof(REQHDR)+sizeof(IOCTL));
672 ioctl = (IOCTL *)(((char*)xlat_req)+sizeof(REQHDR));
674 // Set Transfer Buffer in IOCTL reqion of 'request'
675 if (xferlen && xferptr) {
676 xlat_xferptr = ((char*)xlat_req) + 128;
677 memcpy(xlat_xferptr, xferptr, xferlen);
679 ioctl->buffer_realoff = DPMI_real_offset(xlat_xferptr);
680 ioctl->buffer_realseg = DPMI_real_segment(xlat_xferptr);
681 ioctl->xferlen = xferlen;
684 // Setup and Send Request Packet
686 rregs.ecx = (unsigned)(CDDriveLetter);
687 rregs.es = DPMI_real_segment(xlat_req);
688 rregs.ebx = DPMI_real_offset(xlat_req);
689 dpmi_real_int386x(0x2f, &rregs);
691 // Return Translate Buffer to Protected mode 'xferptr'
692 if (xferlen && xferptr) {
693 memcpy(xferptr, xlat_xferptr, xferlen);
695 memcpy(request, xlat_req, sizeof(REQHDR)+sizeof(IOCTL));
698 status = ((REQHDR *)request)->status;
700 if (status & RBSTAT_ERROR) {
701 mprintf((0,"Error in SendRequest: %x.\n", status));
708 // Converts Logical Sector Number to Minutes Seconds Frame Format
710 unsigned long lsn_to_msf(unsigned long lsn)
712 unsigned long min,sec,frame;
721 return((min << 16) + (sec << 8) + (frame << 0));
724 // convert minutes seconds frame format to a logical sector number.
726 unsigned long msf_to_lsn(unsigned long msf)
728 unsigned long min,sec,frame;
730 min = (msf >> 16) & 0xFF;
731 sec = (msf >> 8) & 0xFF;
732 frame = (msf >> 0) & 0xFF;
734 return( (min * 60*75) + (sec * 75) + frame - 150 );