playmus.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 14 May 2006 21:07:27 +0000
changeset 321 a72a7f07c8f1
parent 297 c901cf1335d4
child 333 b5fd2f76ed9c
permissions -rw-r--r--
Removed use of atexit()
     1 /*
     2     PLAYMUS:  A test application for the SDL mixer library.
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 #include <stdlib.h>
    26 #include <stdio.h>
    27 #include <string.h>
    28 #include <signal.h>
    29 #ifdef unix
    30 #include <unistd.h>
    31 #endif
    32 
    33 #include "SDL.h"
    34 #include "SDL_mixer.h"
    35 
    36 
    37 static int audio_open = 0;
    38 static Mix_Music *music = NULL;
    39 static int next_track = 0;
    40 
    41 void CleanUp(int exitcode)
    42 {
    43 	if( Mix_PlayingMusic() ) {
    44 		Mix_FadeOutMusic(1500);
    45 		SDL_Delay(1500);
    46 	}
    47 	if ( music ) {
    48 		Mix_FreeMusic(music);
    49 		music = NULL;
    50 	}
    51 	if ( audio_open ) {
    52 		Mix_CloseAudio();
    53 		audio_open = 0;
    54 	}
    55 	SDL_Quit();
    56 	exit(exitcode);
    57 }
    58 
    59 void Usage(char *argv0)
    60 {
    61 	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
    62 }
    63 
    64 void Menu(void)
    65 {
    66 	char buf[10];
    67 
    68 	printf("Available commands: (p)ause (r)esume (h)alt > ");
    69 	fflush(stdin);
    70 	scanf("%s",buf);
    71 	switch(buf[0]){
    72 	case 'p': case 'P':
    73 		Mix_PauseMusic();
    74 		break;
    75 	case 'r': case 'R':
    76 		Mix_ResumeMusic();
    77 		break;
    78 	case 'h': case 'H':
    79 		Mix_HaltMusic();
    80 		break;
    81 	}
    82 	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
    83 		   Mix_PausedMusic() ? "yes" : "no");
    84 }
    85 
    86 void IntHandler(int sig)
    87 {
    88 	switch (sig) {
    89 	        case SIGINT:
    90 			next_track++;
    91 			break;
    92 	}
    93 }
    94 
    95 int main(int argc, char *argv[])
    96 {
    97 	SDL_RWops *rwfp;
    98 	int audio_rate;
    99 	Uint16 audio_format;
   100 	int audio_channels;
   101 	int audio_buffers;
   102 	int audio_volume = MIX_MAX_VOLUME;
   103 	int looping = 0;
   104 	int interactive = 0;
   105 	int rwops = 0;
   106 	int i;
   107 
   108 	/* Initialize variables */
   109 	audio_rate = 22050;
   110 	audio_format = AUDIO_S16;
   111 	audio_channels = 2;
   112 	audio_buffers = 4096;
   113 
   114 	/* Check command line usage */
   115 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   116 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   117 			++i;
   118 			audio_rate = atoi(argv[i]);
   119 		} else
   120 		if ( strcmp(argv[i], "-m") == 0 ) {
   121 			audio_channels = 1;
   122 		} else
   123 		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
   124 			++i;
   125 			audio_channels = atoi(argv[i]);
   126 		} else
   127 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   128 			++i;
   129 			audio_buffers = atoi(argv[i]);
   130 		} else
   131 		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
   132 			++i;
   133 			audio_volume = atoi(argv[i]);
   134 		} else
   135 		if ( strcmp(argv[i], "-l") == 0 ) {
   136 			looping = -1;
   137 		} else
   138 		if ( strcmp(argv[i], "-i") == 0 ) {
   139 			interactive = 1;
   140 		} else
   141 		if ( strcmp(argv[i], "-8") == 0 ) {
   142 			audio_format = AUDIO_U8;
   143 		} else
   144 		if ( strcmp(argv[i], "-rwops") == 0 ) {
   145 			rwops = 1;
   146 		} else {
   147 			Usage(argv[0]);
   148 			return(1);
   149 		}
   150 	}
   151 	if ( ! argv[i] ) {
   152 		Usage(argv[0]);
   153 		return(1);
   154 	}
   155 
   156 	/* Initialize the SDL library */
   157 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   158 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   159 		return(255);
   160 	}
   161 
   162 	signal(SIGINT, IntHandler);
   163 	signal(SIGTERM, CleanUp);
   164 
   165 	/* Open the audio device */
   166 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   167 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   168 		return(2);
   169 	} else {
   170 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   171 		printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate,
   172 			(audio_format&0xFF),
   173 			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
   174 			(audio_format&0x1000) ? "BE" : "LE",
   175 			audio_buffers );
   176 	}
   177 	audio_open = 1;
   178 
   179 	/* Set the music volume */
   180 	Mix_VolumeMusic(audio_volume);
   181 
   182 	/* Set the external music player, if any */
   183 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   184 
   185 	while (argv[i]) {
   186 		next_track = 0;
   187 		
   188 		/* Load the requested music file */
   189 		if ( rwops ) {
   190 			rwfp = SDL_RWFromFile(argv[i], "rb");
   191 			music = Mix_LoadMUS_RW(rwfp);
   192 		} else {
   193 			music = Mix_LoadMUS(argv[i]);
   194 		}
   195 		if ( music == NULL ) {
   196 			fprintf(stderr, "Couldn't load %s: %s\n",
   197 				argv[i], SDL_GetError());
   198 			CleanUp(2);
   199 		}
   200 		
   201 		/* Play and then exit */
   202 		printf("Playing %s\n", argv[i]);
   203 		Mix_FadeInMusic(music,looping,2000);
   204 		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
   205 			if(interactive)
   206 				Menu();
   207 			else
   208 				SDL_Delay(100);
   209 		}
   210 		Mix_FreeMusic(music);
   211 		if ( rwops ) {
   212 			SDL_FreeRW(rwfp);
   213 		}
   214 		music = NULL;
   215 
   216 		/* If the user presses Ctrl-C more than once, exit. */
   217 		SDL_Delay(500);
   218 		if ( next_track > 1 ) break;
   219 		
   220 		i++;
   221 	}
   222 	CleanUp(0);
   223 
   224 	/* Not reached, but fixes compiler warnings */
   225 	return 0;
   226 }