playmus.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Dec 2004 20:08:57 +0000
changeset 256 6d7e102500c9
parent 245 63b3650714de
child 257 8a32c0a3c609
permissions -rw-r--r--
Added SDL_RWops support for reading MP3 files
     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(void)
    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 }
    57 
    58 void Usage(char *argv0)
    59 {
    60 	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
    61 }
    62 
    63 void Menu(void)
    64 {
    65 	char buf[10];
    66 
    67 	printf("Available commands: (p)ause (r)esume (h)alt > ");
    68 	fflush(stdin);
    69 	scanf("%s",buf);
    70 	switch(buf[0]){
    71 	case 'p': case 'P':
    72 		Mix_PauseMusic();
    73 		break;
    74 	case 'r': case 'R':
    75 		Mix_ResumeMusic();
    76 		break;
    77 	case 'h': case 'H':
    78 		Mix_HaltMusic();
    79 		break;
    80 	}
    81 	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
    82 		   Mix_PausedMusic() ? "yes" : "no");
    83 }
    84 
    85 void IntHandler(int sig)
    86 {
    87 	switch (sig) {
    88 	        case SIGINT:
    89 			next_track++;
    90 			break;
    91 	}
    92 }
    93 
    94 int main(int argc, char *argv[])
    95 {
    96 	SDL_RWops *rwfp;
    97 	int audio_rate;
    98 	Uint16 audio_format;
    99 	int audio_channels;
   100 	int audio_buffers;
   101 	int audio_volume = MIX_MAX_VOLUME;
   102 	int looping = 0;
   103 	int interactive = 0;
   104 	int rwops = 0;
   105 	int i;
   106 
   107 	/* Initialize variables */
   108 	audio_rate = 22050;
   109 	audio_format = AUDIO_S16;
   110 	audio_channels = 2;
   111 	audio_buffers = 4096;
   112 
   113 	/* Check command line usage */
   114 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   115 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   116 			++i;
   117 			audio_rate = atoi(argv[i]);
   118 		} else
   119 		if ( strcmp(argv[i], "-m") == 0 ) {
   120 			audio_channels = 1;
   121 		} else
   122 		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
   123 			++i;
   124 			audio_channels = atoi(argv[i]);
   125 		} else
   126 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   127 			++i;
   128 			audio_buffers = atoi(argv[i]);
   129 		} else
   130 		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
   131 			++i;
   132 			audio_volume = atoi(argv[i]);
   133 		} else
   134 		if ( strcmp(argv[i], "-l") == 0 ) {
   135 			looping = -1;
   136 		} else
   137 		if ( strcmp(argv[i], "-i") == 0 ) {
   138 			interactive = 1;
   139 		} else
   140 		if ( strcmp(argv[i], "-8") == 0 ) {
   141 			audio_format = AUDIO_U8;
   142 		} else
   143 		if ( strcmp(argv[i], "-rwops") == 0 ) {
   144 			rwops = 1;
   145 		} else {
   146 			Usage(argv[0]);
   147 			return(1);
   148 		}
   149 	}
   150 	if ( ! argv[i] ) {
   151 		Usage(argv[0]);
   152 		return(1);
   153 	}
   154 
   155 	/* Initialize the SDL library */
   156 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   157 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   158 		return(255);
   159 	}
   160 
   161 	atexit(CleanUp);
   162 	signal(SIGINT, IntHandler);
   163 	signal(SIGTERM, exit);
   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, %d bytes audio buffer\n", audio_rate,
   172 			(audio_format&0xFF),
   173 			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
   174 			audio_buffers );
   175 	}
   176 	audio_open = 1;
   177 
   178 	/* Set the music volume */
   179 	Mix_VolumeMusic(audio_volume);
   180 
   181 	/* Set the external music player, if any */
   182 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   183 
   184 	while (argv[i]) {
   185 		next_track = 0;
   186 		
   187 		/* Load the requested music file */
   188 		if ( rwops ) {
   189 			rwfp = SDL_RWFromFile(argv[i], "rb");
   190 			music = Mix_LoadMUS(argv[i]);
   191 		} else {
   192 			music = Mix_LoadMUS(argv[i]);
   193 		}
   194 		if ( music == NULL ) {
   195 			fprintf(stderr, "Couldn't load %s: %s\n",
   196 				argv[i], SDL_GetError());
   197 			return(2);
   198 		}
   199 		
   200 		/* Play and then exit */
   201 		printf("Playing %s\n", argv[i]);
   202 		Mix_FadeInMusic(music,looping,2000);
   203 		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
   204 			if(interactive)
   205 				Menu();
   206 			else
   207 				SDL_Delay(100);
   208 		}
   209 		Mix_FreeMusic(music);
   210 		if ( rwops ) {
   211 			SDL_FreeRW(rwfp);
   212 		}
   213 		music = NULL;
   214 
   215 		/* If the user presses Ctrl-C more than once, exit. */
   216 		SDL_Delay(500);
   217 		if ( next_track > 1 ) break;
   218 		
   219 		i++;
   220 	}
   221 	return(0);
   222 }