playmus.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 13:02:16 +0000
changeset 140 efa15d2a5403
parent 138 4d0dc6b4985d
child 149 e0fd2710ef72
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     PLAYMUS:  A test application for the SDL mixer library.
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  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] [-s] <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 main(int argc, char *argv[])
    95 {
    96 	int audio_rate;
    97 	Uint16 audio_format;
    98 	int audio_channels;
    99 	int audio_buffers;
   100 	int looping = 0;
   101 	int interactive = 0;
   102 	int i;
   103 
   104 	/* Initialize variables */
   105 	audio_rate = 22050;
   106 	audio_format = AUDIO_S16;
   107 	audio_channels = 2;
   108 	audio_buffers = 4096;
   109 
   110 	/* Check command line usage */
   111 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   112 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   113 			++i;
   114 			audio_rate = atoi(argv[i]);
   115 		} else
   116 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   117 			++i;
   118 			audio_buffers = atoi(argv[i]);
   119 		} else
   120 		if ( strcmp(argv[i], "-m") == 0 ) {
   121 			audio_channels = 1;
   122 		} else
   123 		if ( strcmp(argv[i], "-l") == 0 ) {
   124 			looping = -1;
   125 		} else
   126 		if ( strcmp(argv[i], "-i") == 0 ) {
   127 			interactive = 1;
   128 		} else
   129 		if ( strcmp(argv[i], "-8") == 0 ) {
   130 			audio_format = AUDIO_U8;
   131 		} else {
   132 			Usage(argv[0]);
   133 			return(1);
   134 		}
   135 	}
   136 	if ( ! argv[i] ) {
   137 		Usage(argv[0]);
   138 		return(1);
   139 	}
   140 
   141 	/* Initialize the SDL library */
   142 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   143 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   144 		return(255);
   145 	}
   146 	atexit(CleanUp);
   147 	signal(SIGINT, IntHandler);
   148 	signal(SIGTERM, exit);
   149 
   150 	/* Open the audio device */
   151 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   152 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   153 		return(2);
   154 	} else {
   155 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   156 		printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
   157 			(audio_format&0xFF),
   158 			(audio_channels > 1) ? "stereo" : "mono", 
   159 			audio_buffers );
   160 	}
   161 	audio_open = 1;
   162 
   163 	/* Set the external music player, if any */
   164 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   165 
   166 	while (argv[i]) {
   167 		next_track = 0;
   168 		
   169 		/* Load the requested music file */
   170 		music = Mix_LoadMUS(argv[i]);
   171 		if ( music == NULL ) {
   172 			fprintf(stderr, "Couldn't load %s: %s\n",
   173 				argv[i], SDL_GetError());
   174 			return(2);
   175 		}
   176 		
   177 		/* Play and then exit */
   178 		printf("Playing %s\n", argv[i]);
   179 		Mix_FadeInMusic(music,looping,2000);
   180 		while ( !next_track && Mix_PlayingMusic() || Mix_PausedMusic() ) {
   181 			if(interactive)
   182 				Menu();
   183 			else
   184 				SDL_Delay(100);
   185 		}
   186 		Mix_FreeMusic(music);
   187 
   188 		/* If the user presses Ctrl-C more than once, exit. */
   189 		SDL_Delay(500);
   190 		if ( next_track > 1 ) break;
   191 		
   192 		i++;
   193 	}
   194 	return(0);
   195 }