playmus.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Feb 2011 13:33:16 -0800
changeset 503 0c77794aaef2
parent 488 94b190330355
child 518 8bc9b5fd2aae
permissions -rw-r--r--
Fixed bug 1021 (misplaced HAVE_SIGNAL_H condition in playmus.c and playwave.c)

Ozkan Sezer 2010-10-08 01:45:34 PDT

The HAVE_SIGNAL_H dependent signal.h includes in playmus.c and playwave.c are
misplaced, because SDL_config.h is not included at that point and this causes
signal.h to be never included. Moving it after the SDL.h and SDL_mixer.h
includes fixes the issue, like in the following.
     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 
    29 #ifdef unix
    30 #include <unistd.h>
    31 #endif
    32 
    33 #include "SDL.h"
    34 #include "SDL_mixer.h"
    35 
    36 #ifdef HAVE_SIGNAL_H
    37 #include <signal.h>
    38 #endif
    39 
    40 
    41 static int audio_open = 0;
    42 static Mix_Music *music = NULL;
    43 static int next_track = 0;
    44 
    45 void CleanUp(int exitcode)
    46 {
    47 	if( Mix_PlayingMusic() ) {
    48 		Mix_FadeOutMusic(1500);
    49 		SDL_Delay(1500);
    50 	}
    51 	if ( music ) {
    52 		Mix_FreeMusic(music);
    53 		music = NULL;
    54 	}
    55 	if ( audio_open ) {
    56 		Mix_CloseAudio();
    57 		audio_open = 0;
    58 	}
    59 	SDL_Quit();
    60 	exit(exitcode);
    61 }
    62 
    63 void Usage(char *argv0)
    64 {
    65 	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
    66 }
    67 
    68 void Menu(void)
    69 {
    70 	char buf[10];
    71 
    72 	printf("Available commands: (p)ause (r)esume (h)alt volume(v#) > ");
    73 	fflush(stdin);
    74 	if (scanf("%s",buf) == 1) {
    75 		switch(buf[0]){
    76 		case 'p': case 'P':
    77 			Mix_PauseMusic();
    78 			break;
    79 		case 'r': case 'R':
    80 			Mix_ResumeMusic();
    81 			break;
    82 		case 'h': case 'H':
    83 			Mix_HaltMusic();
    84 			break;
    85 		case 'v': case 'V':
    86 			Mix_VolumeMusic(atoi(buf+1));
    87 			break;
    88 		}
    89 	}
    90 	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
    91 		   Mix_PausedMusic() ? "yes" : "no");
    92 }
    93 
    94 #ifdef HAVE_SIGNAL_H
    95 
    96 void IntHandler(int sig)
    97 {
    98 	switch (sig) {
    99 	        case SIGINT:
   100 			next_track++;
   101 			break;
   102 	}
   103 }
   104 
   105 #endif
   106 
   107 int main(int argc, char *argv[])
   108 {
   109 	SDL_RWops *rwfp = NULL;
   110 	int audio_rate;
   111 	Uint16 audio_format;
   112 	int audio_channels;
   113 	int audio_buffers;
   114 	int audio_volume = MIX_MAX_VOLUME;
   115 	int looping = 0;
   116 	int interactive = 0;
   117 	int rwops = 0;
   118 	int i;
   119 
   120 	/* Initialize variables */
   121 	audio_rate = 22050;
   122 	audio_format = AUDIO_S16;
   123 	audio_channels = 2;
   124 	audio_buffers = 4096;
   125 
   126 	/* Check command line usage */
   127 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   128 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   129 			++i;
   130 			audio_rate = atoi(argv[i]);
   131 		} else
   132 		if ( strcmp(argv[i], "-m") == 0 ) {
   133 			audio_channels = 1;
   134 		} else
   135 		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
   136 			++i;
   137 			audio_channels = atoi(argv[i]);
   138 		} else
   139 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   140 			++i;
   141 			audio_buffers = atoi(argv[i]);
   142 		} else
   143 		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
   144 			++i;
   145 			audio_volume = atoi(argv[i]);
   146 		} else
   147 		if ( strcmp(argv[i], "-l") == 0 ) {
   148 			looping = -1;
   149 		} else
   150 		if ( strcmp(argv[i], "-i") == 0 ) {
   151 			interactive = 1;
   152 		} else
   153 		if ( strcmp(argv[i], "-8") == 0 ) {
   154 			audio_format = AUDIO_U8;
   155 		} else
   156 		if ( strcmp(argv[i], "-rwops") == 0 ) {
   157 			rwops = 1;
   158 		} else {
   159 			Usage(argv[0]);
   160 			return(1);
   161 		}
   162 	}
   163 	if ( ! argv[i] ) {
   164 		Usage(argv[0]);
   165 		return(1);
   166 	}
   167 
   168 	/* Initialize the SDL library */
   169 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   170 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   171 		return(255);
   172 	}
   173 
   174 #ifdef HAVE_SIGNAL_H
   175 	signal(SIGINT, IntHandler);
   176 	signal(SIGTERM, CleanUp);
   177 #endif
   178 
   179 	/* Open the audio device */
   180 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   181 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   182 		return(2);
   183 	} else {
   184 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   185 		printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate,
   186 			(audio_format&0xFF),
   187 			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
   188 			(audio_format&0x1000) ? "BE" : "LE",
   189 			audio_buffers );
   190 	}
   191 	audio_open = 1;
   192 
   193 	/* Set the music volume */
   194 	Mix_VolumeMusic(audio_volume);
   195 
   196 	/* Set the external music player, if any */
   197 	Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
   198 
   199 	while (argv[i]) {
   200 		next_track = 0;
   201 		
   202 		/* Load the requested music file */
   203 		if ( rwops ) {
   204 			rwfp = SDL_RWFromFile(argv[i], "rb");
   205 			music = Mix_LoadMUS_RW(rwfp);
   206 		} else {
   207 			music = Mix_LoadMUS(argv[i]);
   208 		}
   209 		if ( music == NULL ) {
   210 			fprintf(stderr, "Couldn't load %s: %s\n",
   211 				argv[i], SDL_GetError());
   212 			CleanUp(2);
   213 		}
   214 		
   215 		/* Play and then exit */
   216 		printf("Playing %s\n", argv[i]);
   217 		Mix_FadeInMusic(music,looping,2000);
   218 		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
   219 			if(interactive)
   220 				Menu();
   221 			else
   222 				SDL_Delay(100);
   223 		}
   224 		Mix_FreeMusic(music);
   225 		if ( rwops ) {
   226 			SDL_FreeRW(rwfp);
   227 		}
   228 		music = NULL;
   229 
   230 		/* If the user presses Ctrl-C more than once, exit. */
   231 		SDL_Delay(500);
   232 		if ( next_track > 1 ) break;
   233 		
   234 		i++;
   235 	}
   236 	CleanUp(0);
   237 
   238 	/* Not reached, but fixes compiler warnings */
   239 	return 0;
   240 }