playmus.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 17:41:55 +0000
changeset 241 503416fca921
parent 214 ab571e095c22
child 245 63b3650714de
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
     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] [-b buffers] [-v N] <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 	int audio_rate;
    97 	Uint16 audio_format;
    98 	int audio_channels;
    99 	int audio_buffers;
   100 	int audio_volume = MIX_MAX_VOLUME;
   101 	int looping = 0;
   102 	int interactive = 0;
   103 	int i;
   104 
   105 	/* Initialize variables */
   106 	audio_rate = 22050;
   107 	audio_format = AUDIO_S16;
   108 	audio_channels = 2;
   109 	audio_buffers = 4096;
   110 
   111 	/* Check command line usage */
   112 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   113 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   114 			++i;
   115 			audio_rate = atoi(argv[i]);
   116 		} else
   117 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   118 			++i;
   119 			audio_buffers = atoi(argv[i]);
   120 		} else
   121 		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
   122 			++i;
   123 			audio_volume = atoi(argv[i]);
   124 		} else
   125 		if ( strcmp(argv[i], "-m") == 0 ) {
   126 			audio_channels = 1;
   127 		} else
   128 		if ( strcmp(argv[i], "-l") == 0 ) {
   129 			looping = -1;
   130 		} else
   131 		if ( strcmp(argv[i], "-i") == 0 ) {
   132 			interactive = 1;
   133 		} else
   134 		if ( strcmp(argv[i], "-8") == 0 ) {
   135 			audio_format = AUDIO_U8;
   136 		} else {
   137 			Usage(argv[0]);
   138 			return(1);
   139 		}
   140 	}
   141 	if ( ! argv[i] ) {
   142 		Usage(argv[0]);
   143 		return(1);
   144 	}
   145 
   146 	/* Initialize the SDL library */
   147 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   148 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   149 		return(255);
   150 	}
   151 	atexit(CleanUp);
   152 	signal(SIGINT, IntHandler);
   153 	signal(SIGTERM, exit);
   154 
   155 	/* Open the audio device */
   156 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   157 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   158 		return(2);
   159 	} else {
   160 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   161 		printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
   162 			(audio_format&0xFF),
   163 			(audio_channels > 1) ? "stereo" : "mono", 
   164 			audio_buffers );
   165 	}
   166 	audio_open = 1;
   167 
   168 	/* Set the music volume */
   169 	Mix_VolumeMusic(audio_volume);
   170 
   171 	/* Set the external music player, if any */
   172 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   173 
   174 	while (argv[i]) {
   175 		next_track = 0;
   176 		
   177 		/* Load the requested music file */
   178 		music = Mix_LoadMUS(argv[i]);
   179 		if ( music == NULL ) {
   180 			fprintf(stderr, "Couldn't load %s: %s\n",
   181 				argv[i], SDL_GetError());
   182 			return(2);
   183 		}
   184 		
   185 		/* Play and then exit */
   186 		printf("Playing %s\n", argv[i]);
   187 		Mix_FadeInMusic(music,looping,2000);
   188 		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
   189 			if(interactive)
   190 				Menu();
   191 			else
   192 				SDL_Delay(100);
   193 		}
   194 		Mix_FreeMusic(music);
   195 		music = NULL;
   196 
   197 		/* If the user presses Ctrl-C more than once, exit. */
   198 		SDL_Delay(500);
   199 		if ( next_track > 1 ) break;
   200 		
   201 		i++;
   202 	}
   203 	return(0);
   204 }