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