playmus.c
changeset 23 0578063e33d8
parent 13 1e489d8b97d8
child 24 a25bb2d59ce8
equal deleted inserted replaced
22:5104018a0c45 23:0578063e33d8
    54 	SDL_Quit();
    54 	SDL_Quit();
    55 }
    55 }
    56 
    56 
    57 void Usage(char *argv0)
    57 void Usage(char *argv0)
    58 {
    58 {
    59 	fprintf(stderr, "Usage: %s [-i] |-l] [-8] [-r rate] [-s] <musicfile>\n", argv0);
    59 	fprintf(stderr, "Usage: %s [-i] |-l] [-8] [-r rate] [-b buffers] [-s] <musicfile>\n", argv0);
    60 }
    60 }
    61 
    61 
    62 void Menu(void)
    62 void Menu(void)
    63 {
    63 {
    64 	char buf[10];
    64 	char buf[10];
    84 main(int argc, char *argv[])
    84 main(int argc, char *argv[])
    85 {
    85 {
    86 	Uint32 audio_rate;
    86 	Uint32 audio_rate;
    87 	Uint16 audio_format;
    87 	Uint16 audio_format;
    88 	int audio_channels;
    88 	int audio_channels;
       
    89 	int audio_buffers;
    89 	int looping = 0;
    90 	int looping = 0;
    90 	int interactive = 0;
    91 	int interactive = 0;
    91 	int i;
    92 	int i;
    92 
    93 
    93 	/* Initialize variables */
    94 	/* Initialize variables */
    94 	audio_rate = 22050;
    95 	audio_rate = 22050;
    95 	audio_format = AUDIO_S16;
    96 	audio_format = AUDIO_S16;
    96 	audio_channels = 2;
    97 	audio_channels = 2;
       
    98 	audio_buffers = 4096;
    97 
    99 
    98 	/* Check command line usage */
   100 	/* Check command line usage */
    99 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   101 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   100 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   102 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   101 			++i;
   103 			++i;
   102 			audio_rate = atoi(argv[i]);
   104 			audio_rate = atoi(argv[i]);
       
   105 		} else
       
   106 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
       
   107 			++i;
       
   108 			audio_buffers = atoi(argv[i]);
   103 		} else
   109 		} else
   104 		if ( strcmp(argv[i], "-m") == 0 ) {
   110 		if ( strcmp(argv[i], "-m") == 0 ) {
   105 			audio_channels = 1;
   111 			audio_channels = 1;
   106 		} else
   112 		} else
   107 		if ( strcmp(argv[i], "-l") == 0 ) {
   113 		if ( strcmp(argv[i], "-l") == 0 ) {
   130 	atexit(CleanUp);
   136 	atexit(CleanUp);
   131 	signal(SIGINT, exit);
   137 	signal(SIGINT, exit);
   132 	signal(SIGTERM, exit);
   138 	signal(SIGTERM, exit);
   133 
   139 
   134 	/* Open the audio device */
   140 	/* Open the audio device */
   135 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
   141 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   136 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   142 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   137 		exit(2);
   143 		exit(2);
   138 	} else {
   144 	} else {
   139 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   145 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   140 		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
   146 		printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
   141 			(audio_format&0xFF),
   147 			(audio_format&0xFF),
   142 			(audio_channels > 1) ? "stereo" : "mono");
   148 			(audio_channels > 1) ? "stereo" : "mono", 
       
   149 			audio_buffers );
   143 	}
   150 	}
   144 	audio_open = 1;
   151 	audio_open = 1;
   145 
   152 
   146 	/* Set the external music player, if any */
   153 	/* Set the external music player, if any */
   147 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   154 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));