playmus.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 10:27:48 -0500
changeset 518 8bc9b5fd2aae
parent 503 0c77794aaef2
child 521 565549e046b0
permissions -rw-r--r--
Happy New Year!
     1 /*
     2   PLAYMUS:  A test application for the SDL mixer library.
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 /* $Id$ */
    23 
    24 #include <stdlib.h>
    25 #include <stdio.h>
    26 #include <string.h>
    27 
    28 #ifdef unix
    29 #include <unistd.h>
    30 #endif
    31 
    32 #include "SDL.h"
    33 #include "SDL_mixer.h"
    34 
    35 #ifdef HAVE_SIGNAL_H
    36 #include <signal.h>
    37 #endif
    38 
    39 
    40 static int audio_open = 0;
    41 static Mix_Music *music = NULL;
    42 static int next_track = 0;
    43 
    44 void CleanUp(int exitcode)
    45 {
    46 	if( Mix_PlayingMusic() ) {
    47 		Mix_FadeOutMusic(1500);
    48 		SDL_Delay(1500);
    49 	}
    50 	if ( music ) {
    51 		Mix_FreeMusic(music);
    52 		music = NULL;
    53 	}
    54 	if ( audio_open ) {
    55 		Mix_CloseAudio();
    56 		audio_open = 0;
    57 	}
    58 	SDL_Quit();
    59 	exit(exitcode);
    60 }
    61 
    62 void Usage(char *argv0)
    63 {
    64 	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
    65 }
    66 
    67 void Menu(void)
    68 {
    69 	char buf[10];
    70 
    71 	printf("Available commands: (p)ause (r)esume (h)alt volume(v#) > ");
    72 	fflush(stdin);
    73 	if (scanf("%s",buf) == 1) {
    74 		switch(buf[0]){
    75 		case 'p': case 'P':
    76 			Mix_PauseMusic();
    77 			break;
    78 		case 'r': case 'R':
    79 			Mix_ResumeMusic();
    80 			break;
    81 		case 'h': case 'H':
    82 			Mix_HaltMusic();
    83 			break;
    84 		case 'v': case 'V':
    85 			Mix_VolumeMusic(atoi(buf+1));
    86 			break;
    87 		}
    88 	}
    89 	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
    90 		   Mix_PausedMusic() ? "yes" : "no");
    91 }
    92 
    93 #ifdef HAVE_SIGNAL_H
    94 
    95 void IntHandler(int sig)
    96 {
    97 	switch (sig) {
    98 	        case SIGINT:
    99 			next_track++;
   100 			break;
   101 	}
   102 }
   103 
   104 #endif
   105 
   106 int main(int argc, char *argv[])
   107 {
   108 	SDL_RWops *rwfp = NULL;
   109 	int audio_rate;
   110 	Uint16 audio_format;
   111 	int audio_channels;
   112 	int audio_buffers;
   113 	int audio_volume = MIX_MAX_VOLUME;
   114 	int looping = 0;
   115 	int interactive = 0;
   116 	int rwops = 0;
   117 	int i;
   118 
   119 	/* Initialize variables */
   120 	audio_rate = 22050;
   121 	audio_format = AUDIO_S16;
   122 	audio_channels = 2;
   123 	audio_buffers = 4096;
   124 
   125 	/* Check command line usage */
   126 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   127 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   128 			++i;
   129 			audio_rate = atoi(argv[i]);
   130 		} else
   131 		if ( strcmp(argv[i], "-m") == 0 ) {
   132 			audio_channels = 1;
   133 		} else
   134 		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
   135 			++i;
   136 			audio_channels = atoi(argv[i]);
   137 		} else
   138 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   139 			++i;
   140 			audio_buffers = atoi(argv[i]);
   141 		} else
   142 		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
   143 			++i;
   144 			audio_volume = atoi(argv[i]);
   145 		} else
   146 		if ( strcmp(argv[i], "-l") == 0 ) {
   147 			looping = -1;
   148 		} else
   149 		if ( strcmp(argv[i], "-i") == 0 ) {
   150 			interactive = 1;
   151 		} else
   152 		if ( strcmp(argv[i], "-8") == 0 ) {
   153 			audio_format = AUDIO_U8;
   154 		} else
   155 		if ( strcmp(argv[i], "-rwops") == 0 ) {
   156 			rwops = 1;
   157 		} else {
   158 			Usage(argv[0]);
   159 			return(1);
   160 		}
   161 	}
   162 	if ( ! argv[i] ) {
   163 		Usage(argv[0]);
   164 		return(1);
   165 	}
   166 
   167 	/* Initialize the SDL library */
   168 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   169 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   170 		return(255);
   171 	}
   172 
   173 #ifdef HAVE_SIGNAL_H
   174 	signal(SIGINT, IntHandler);
   175 	signal(SIGTERM, CleanUp);
   176 #endif
   177 
   178 	/* Open the audio device */
   179 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   180 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   181 		return(2);
   182 	} else {
   183 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   184 		printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate,
   185 			(audio_format&0xFF),
   186 			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
   187 			(audio_format&0x1000) ? "BE" : "LE",
   188 			audio_buffers );
   189 	}
   190 	audio_open = 1;
   191 
   192 	/* Set the music volume */
   193 	Mix_VolumeMusic(audio_volume);
   194 
   195 	/* Set the external music player, if any */
   196 	Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
   197 
   198 	while (argv[i]) {
   199 		next_track = 0;
   200 		
   201 		/* Load the requested music file */
   202 		if ( rwops ) {
   203 			rwfp = SDL_RWFromFile(argv[i], "rb");
   204 			music = Mix_LoadMUS_RW(rwfp);
   205 		} else {
   206 			music = Mix_LoadMUS(argv[i]);
   207 		}
   208 		if ( music == NULL ) {
   209 			fprintf(stderr, "Couldn't load %s: %s\n",
   210 				argv[i], SDL_GetError());
   211 			CleanUp(2);
   212 		}
   213 		
   214 		/* Play and then exit */
   215 		printf("Playing %s\n", argv[i]);
   216 		Mix_FadeInMusic(music,looping,2000);
   217 		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
   218 			if(interactive)
   219 				Menu();
   220 			else
   221 				SDL_Delay(100);
   222 		}
   223 		Mix_FreeMusic(music);
   224 		if ( rwops ) {
   225 			SDL_FreeRW(rwfp);
   226 		}
   227 		music = NULL;
   228 
   229 		/* If the user presses Ctrl-C more than once, exit. */
   230 		SDL_Delay(500);
   231 		if ( next_track > 1 ) break;
   232 		
   233 		i++;
   234 	}
   235 	CleanUp(0);
   236 
   237 	/* Not reached, but fixes compiler warnings */
   238 	return 0;
   239 }