playmus.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 03 Oct 2009 20:43:33 +0000
changeset 419 e27fe0bfe470
parent 386 695494546b3c
child 488 94b190330355
permissions -rw-r--r--
Sam Lantinga - Sat Oct 3 13:33:36 PDT 2009
* MOD support uses libmikmod and is dynamically loaded by default
     1 /*
     2     PLAYMUS:  A test application for the SDL mixer library.
     3     Copyright (C) 1997-2009 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 volume(v#) > ");
    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 	case 'v': case 'V':
    82 		Mix_VolumeMusic(atoi(buf+1));
    83 		break;
    84 	}
    85 	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
    86 		   Mix_PausedMusic() ? "yes" : "no");
    87 }
    88 
    89 void IntHandler(int sig)
    90 {
    91 	switch (sig) {
    92 	        case SIGINT:
    93 			next_track++;
    94 			break;
    95 	}
    96 }
    97 
    98 int main(int argc, char *argv[])
    99 {
   100 	SDL_RWops *rwfp;
   101 	int audio_rate;
   102 	Uint16 audio_format;
   103 	int audio_channels;
   104 	int audio_buffers;
   105 	int audio_volume = MIX_MAX_VOLUME;
   106 	int looping = 0;
   107 	int interactive = 0;
   108 	int rwops = 0;
   109 	int i;
   110 
   111 	/* Initialize variables */
   112 	audio_rate = 22050;
   113 	audio_format = AUDIO_S16;
   114 	audio_channels = 2;
   115 	audio_buffers = 4096;
   116 
   117 	/* Check command line usage */
   118 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   119 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   120 			++i;
   121 			audio_rate = atoi(argv[i]);
   122 		} else
   123 		if ( strcmp(argv[i], "-m") == 0 ) {
   124 			audio_channels = 1;
   125 		} else
   126 		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
   127 			++i;
   128 			audio_channels = atoi(argv[i]);
   129 		} else
   130 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   131 			++i;
   132 			audio_buffers = atoi(argv[i]);
   133 		} else
   134 		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
   135 			++i;
   136 			audio_volume = atoi(argv[i]);
   137 		} else
   138 		if ( strcmp(argv[i], "-l") == 0 ) {
   139 			looping = -1;
   140 		} else
   141 		if ( strcmp(argv[i], "-i") == 0 ) {
   142 			interactive = 1;
   143 		} else
   144 		if ( strcmp(argv[i], "-8") == 0 ) {
   145 			audio_format = AUDIO_U8;
   146 		} else
   147 		if ( strcmp(argv[i], "-rwops") == 0 ) {
   148 			rwops = 1;
   149 		} else {
   150 			Usage(argv[0]);
   151 			return(1);
   152 		}
   153 	}
   154 	if ( ! argv[i] ) {
   155 		Usage(argv[0]);
   156 		return(1);
   157 	}
   158 
   159 	/* Initialize the SDL library */
   160 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   161 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   162 		return(255);
   163 	}
   164 
   165 	signal(SIGINT, IntHandler);
   166 	signal(SIGTERM, CleanUp);
   167 
   168 	/* Open the audio device */
   169 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   170 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   171 		return(2);
   172 	} else {
   173 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   174 		printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate,
   175 			(audio_format&0xFF),
   176 			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
   177 			(audio_format&0x1000) ? "BE" : "LE",
   178 			audio_buffers );
   179 	}
   180 	audio_open = 1;
   181 
   182 	/* Set the music volume */
   183 	Mix_VolumeMusic(audio_volume);
   184 
   185 	/* Set the external music player, if any */
   186 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   187 
   188 	while (argv[i]) {
   189 		next_track = 0;
   190 		
   191 		/* Load the requested music file */
   192 		if ( rwops ) {
   193 			rwfp = SDL_RWFromFile(argv[i], "rb");
   194 			music = Mix_LoadMUS_RW(rwfp);
   195 		} else {
   196 			music = Mix_LoadMUS(argv[i]);
   197 		}
   198 		if ( music == NULL ) {
   199 			fprintf(stderr, "Couldn't load %s: %s\n",
   200 				argv[i], SDL_GetError());
   201 			CleanUp(2);
   202 		}
   203 		
   204 		/* Play and then exit */
   205 		printf("Playing %s\n", argv[i]);
   206 		Mix_FadeInMusic(music,looping,2000);
   207 		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
   208 			if(interactive)
   209 				Menu();
   210 			else
   211 				SDL_Delay(100);
   212 		}
   213 		Mix_FreeMusic(music);
   214 		if ( rwops ) {
   215 			SDL_FreeRW(rwfp);
   216 		}
   217 		music = NULL;
   218 
   219 		/* If the user presses Ctrl-C more than once, exit. */
   220 		SDL_Delay(500);
   221 		if ( next_track > 1 ) break;
   222 		
   223 		i++;
   224 	}
   225 	CleanUp(0);
   226 
   227 	/* Not reached, but fixes compiler warnings */
   228 	return 0;
   229 }