test/testcdrom.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Apr 2003 16:33:56 +0000
changeset 613 9c6717a1c66f
parent 568 0cd6b268193b
child 1151 be9c9c8f6d53
permissions -rw-r--r--
Added MacOS X CD-ROM audio support (thanks Max and Darrell)
     1 
     2 /* Test the SDL CD-ROM audio functions */
     3 
     4 #include <stdlib.h>
     5 #include <stdio.h>
     6 #include <ctype.h>
     7 
     8 #include "SDL.h"
     9 
    10 
    11 static void PrintStatus(int driveindex, SDL_CD *cdrom)
    12 {
    13 	CDstatus status;
    14 	char *status_str;
    15 
    16 	status = SDL_CDStatus(cdrom);
    17 	switch (status) {
    18 		case CD_TRAYEMPTY:
    19 			status_str = "tray empty";
    20 			break;
    21 		case CD_STOPPED:
    22 			status_str = "stopped";
    23 			break;
    24 		case CD_PLAYING:
    25 			status_str = "playing";
    26 			break;
    27 		case CD_PAUSED:
    28 			status_str = "paused";
    29 			break;
    30 		case CD_ERROR:
    31 			status_str = "error state";
    32 			break;
    33 	}
    34 	printf("Drive %d status: %s\n", driveindex, status_str);
    35 	if ( status >= CD_PLAYING ) {
    36 		int m, s, f;
    37 		FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
    38 		printf("Currently playing track %d, %d:%2.2d\n",
    39 			cdrom->track[cdrom->cur_track].id, m, s);
    40 	}
    41 }
    42 
    43 static void ListTracks(SDL_CD *cdrom)
    44 {
    45 	int i;
    46 	int m, s, f;
    47 	char* trtype;
    48 
    49 	SDL_CDStatus(cdrom);
    50 	printf("Drive tracks: %d\n", cdrom->numtracks);
    51 	for ( i=0; i<cdrom->numtracks; ++i ) {
    52 		FRAMES_TO_MSF(cdrom->track[i].length, &m, &s, &f);
    53 		if ( f > 0 )
    54 			++s;
    55 		switch(cdrom->track[i].type)
    56 		{
    57 		    case SDL_AUDIO_TRACK:
    58 			trtype="audio";
    59 			break;
    60 		    case SDL_DATA_TRACK:
    61 			trtype="data";
    62 			break;
    63 		    default:
    64 			trtype="unknown";
    65 			break;
    66 		}
    67 		printf("\tTrack (index %d) %d: %d:%2.2d / %d [%s track]\n", i,
    68 					cdrom->track[i].id, m, s, cdrom->track[i].length, trtype);
    69 	}
    70 }
    71 
    72 static void PrintUsage(char *argv0)
    73 {
    74 	fprintf(stderr, "Usage: %s [drive#] [command] [command] ...\n", argv0);
    75 	fprintf(stderr, "Where 'command' is one of:\n");
    76 	fprintf(stderr, "	-status\n");
    77 	fprintf(stderr, "	-list\n");
    78 	fprintf(stderr, "	-play [first_track] [first_frame] [num_tracks] [num_frames]\n");
    79 	fprintf(stderr, "	-pause\n");
    80 	fprintf(stderr, "	-resume\n");
    81 	fprintf(stderr, "	-stop\n");
    82 	fprintf(stderr, "	-eject\n");
    83 	fprintf(stderr, "	-sleep <milliseconds>\n");
    84 }
    85 
    86 int main(int argc, char *argv[])
    87 {
    88 	int drive;
    89 	int i;
    90 	SDL_CD *cdrom;
    91 
    92 	/* Initialize SDL first */
    93 	if ( SDL_Init(SDL_INIT_CDROM) < 0 ) {
    94 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    95 		exit(1);
    96 	}
    97 	atexit(SDL_Quit);
    98 
    99 	/* Find out how many CD-ROM drives are connected to the system */
   100 	if ( SDL_CDNumDrives() == 0 ) {
   101 		printf("No CD-ROM devices detected\n");
   102 		exit(0);
   103 	}
   104 	printf("Drives available: %d\n", SDL_CDNumDrives());
   105 	for ( i=0; i<SDL_CDNumDrives(); ++i ) {
   106 		printf("Drive %d:  \"%s\"\n", i, SDL_CDName(i));
   107 	}
   108 
   109 	/* Open the CD-ROM */
   110 	drive = 0;
   111 	i=1;
   112 	if ( argv[i] && isdigit(argv[i][0]) ) {
   113 		drive = atoi(argv[i++]);
   114 	}
   115 	cdrom = SDL_CDOpen(drive);
   116 	if ( cdrom == NULL ) {
   117 		fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
   118 							SDL_GetError());
   119 		exit(2);
   120 	}
   121 #ifdef TEST_NULLCD
   122 	cdrom = NULL;
   123 #endif
   124 	
   125 	/* Find out which function to perform */
   126 	for ( ; argv[i]; ++i ) {
   127 		if ( strcmp(argv[i], "-status") == 0 ) {
   128 			/* PrintStatus(drive, cdrom); */
   129 		} else
   130 		if ( strcmp(argv[i], "-list") == 0 ) {
   131 			ListTracks(cdrom);
   132 		} else
   133 		if ( strcmp(argv[i], "-play") == 0 ) {
   134 			int strack, sframe;
   135 			int ntrack, nframe;
   136 
   137 			strack = 0;
   138 			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   139 				strack = atoi(argv[++i]);
   140 			}
   141 			sframe = 0;
   142 			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   143 				sframe = atoi(argv[++i]);
   144 			}
   145 			ntrack = 0;
   146 			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   147 				ntrack = atoi(argv[++i]);
   148 			}
   149 			nframe = 0;
   150 			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   151 				nframe = atoi(argv[++i]);
   152 			}
   153 			if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) {
   154 				if ( SDL_CDPlayTracks(cdrom, strack, sframe,
   155 							ntrack, nframe) < 0 ) {
   156 					fprintf(stderr,
   157 			"Couldn't play tracks %d/%d for %d/%d: %s\n",
   158 				strack, sframe, ntrack, nframe, SDL_GetError());
   159 				}
   160 			} else {
   161 				fprintf(stderr, "No CD in drive!\n");
   162 			}
   163 		} else
   164 		if ( strcmp(argv[i], "-pause") == 0 ) {
   165 			if ( SDL_CDPause(cdrom) < 0 ) {
   166 				fprintf(stderr, "Couldn't pause CD: %s\n",
   167 								SDL_GetError());
   168 			}
   169 		} else
   170 		if ( strcmp(argv[i], "-resume") == 0 ) {
   171 			if ( SDL_CDResume(cdrom) < 0 ) {
   172 				fprintf(stderr, "Couldn't resume CD: %s\n",
   173 								SDL_GetError());
   174 			}
   175 		} else
   176 		if ( strcmp(argv[i], "-stop") == 0 ) {
   177 			if ( SDL_CDStop(cdrom) < 0 ) {
   178 				fprintf(stderr, "Couldn't eject CD: %s\n",
   179 								SDL_GetError());
   180 			}
   181 		} else
   182 		if ( strcmp(argv[i], "-eject") == 0 ) {
   183 			if ( SDL_CDEject(cdrom) < 0 ) {
   184 				fprintf(stderr, "Couldn't eject CD: %s\n",
   185 								SDL_GetError());
   186 			}
   187 		} else
   188 		if ( (strcmp(argv[i], "-sleep") == 0) &&
   189 				(argv[i+1] && isdigit(argv[i+1][0])) ) {
   190 			SDL_Delay(atoi(argv[++i]));
   191 			printf("Delayed %d milliseconds\n", atoi(argv[i]));
   192 		} else {
   193 			PrintUsage(argv[0]);
   194 			SDL_CDClose(cdrom);
   195 			exit(1);
   196 		}
   197 	}
   198 	PrintStatus(drive, cdrom);
   199 	SDL_CDClose(cdrom);
   200 
   201 	return(0);
   202 }