playmus.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 13 Apr 2010 22:30:19 -0700
changeset 488 94b190330355
parent 386 695494546b3c
child 503 0c77794aaef2
permissions -rw-r--r--
Fixed bug 948

Simon Howard 2010-02-13 13:57:20 PST

Created an attachment (id=490) [details]
Patch against SDL_mixer 1.2.11 to add MingW32CE support.

Following bug 947, this is a patch against SDL 1.2.11 to make SDL_mixer build
with MingW32CE.

Some notes:

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