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