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.
slouken@0
     1
/*
slouken@0
     2
    PLAYMUS:  A test application for the SDL mixer library.
slouken@386
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@138
     5
    This library is free software; you can redistribute it and/or
slouken@138
     6
    modify it under the terms of the GNU Library General Public
slouken@138
     7
    License as published by the Free Software Foundation; either
slouken@138
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@138
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@138
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@138
    13
    Library General Public License for more details.
slouken@0
    14
slouken@138
    15
    You should have received a copy of the GNU Library General Public
slouken@138
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@138
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@140
    23
/* $Id$ */
slouken@138
    24
slouken@0
    25
#include <stdlib.h>
slouken@0
    26
#include <stdio.h>
slouken@0
    27
#include <string.h>
slouken@488
    28
slouken@488
    29
#ifdef HAVE_SIGNAL_H
slouken@0
    30
#include <signal.h>
slouken@488
    31
#endif
slouken@47
    32
#ifdef unix
slouken@0
    33
#include <unistd.h>
slouken@0
    34
#endif
slouken@0
    35
slouken@24
    36
#include "SDL.h"
slouken@34
    37
#include "SDL_mixer.h"
slouken@0
    38
slouken@0
    39
slouken@0
    40
static int audio_open = 0;
slouken@0
    41
static Mix_Music *music = NULL;
slouken@75
    42
static int next_track = 0;
slouken@0
    43
slouken@321
    44
void CleanUp(int exitcode)
slouken@0
    45
{
megastep@4
    46
	if( Mix_PlayingMusic() ) {
megastep@4
    47
		Mix_FadeOutMusic(1500);
megastep@4
    48
		SDL_Delay(1500);
megastep@4
    49
	}
megastep@10
    50
	if ( music ) {
megastep@10
    51
		Mix_FreeMusic(music);
megastep@10
    52
		music = NULL;
megastep@10
    53
	}
slouken@0
    54
	if ( audio_open ) {
slouken@0
    55
		Mix_CloseAudio();
slouken@0
    56
		audio_open = 0;
slouken@0
    57
	}
slouken@0
    58
	SDL_Quit();
slouken@321
    59
	exit(exitcode);
slouken@0
    60
}
slouken@0
    61
slouken@0
    62
void Usage(char *argv0)
slouken@0
    63
{
slouken@256
    64
	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] [-b buffers] [-v N] [-rwops] <musicfile>\n", argv0);
slouken@0
    65
}
megastep@13
    66
megastep@13
    67
void Menu(void)
megastep@13
    68
{
megastep@13
    69
	char buf[10];
megastep@13
    70
slouken@333
    71
	printf("Available commands: (p)ause (r)esume (h)alt volume(v#) > ");
megastep@13
    72
	fflush(stdin);
slouken@488
    73
	if (scanf("%s",buf) == 1) {
slouken@488
    74
		switch(buf[0]){
slouken@488
    75
		case 'p': case 'P':
slouken@488
    76
			Mix_PauseMusic();
slouken@488
    77
			break;
slouken@488
    78
		case 'r': case 'R':
slouken@488
    79
			Mix_ResumeMusic();
slouken@488
    80
			break;
slouken@488
    81
		case 'h': case 'H':
slouken@488
    82
			Mix_HaltMusic();
slouken@488
    83
			break;
slouken@488
    84
		case 'v': case 'V':
slouken@488
    85
			Mix_VolumeMusic(atoi(buf+1));
slouken@488
    86
			break;
slouken@488
    87
		}
megastep@13
    88
	}
megastep@13
    89
	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
megastep@13
    90
		   Mix_PausedMusic() ? "yes" : "no");
megastep@13
    91
}
megastep@13
    92
slouken@488
    93
#ifdef HAVE_SIGNAL_H
slouken@488
    94
slouken@75
    95
void IntHandler(int sig)
slouken@75
    96
{
slouken@75
    97
	switch (sig) {
slouken@75
    98
	        case SIGINT:
slouken@75
    99
			next_track++;
slouken@75
   100
			break;
slouken@75
   101
	}
slouken@75
   102
}
slouken@75
   103
slouken@488
   104
#endif
slouken@488
   105
slouken@150
   106
int main(int argc, char *argv[])
slouken@0
   107
{
slouken@488
   108
	SDL_RWops *rwfp = NULL;
slouken@47
   109
	int audio_rate;
slouken@0
   110
	Uint16 audio_format;
slouken@0
   111
	int audio_channels;
megastep@23
   112
	int audio_buffers;
slouken@214
   113
	int audio_volume = MIX_MAX_VOLUME;
megastep@10
   114
	int looping = 0;
megastep@13
   115
	int interactive = 0;
slouken@256
   116
	int rwops = 0;
slouken@0
   117
	int i;
slouken@0
   118
slouken@0
   119
	/* Initialize variables */
slouken@0
   120
	audio_rate = 22050;
slouken@0
   121
	audio_format = AUDIO_S16;
slouken@0
   122
	audio_channels = 2;
megastep@23
   123
	audio_buffers = 4096;
slouken@0
   124
slouken@0
   125
	/* Check command line usage */
slouken@0
   126
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@0
   127
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@0
   128
			++i;
slouken@0
   129
			audio_rate = atoi(argv[i]);
slouken@0
   130
		} else
slouken@245
   131
		if ( strcmp(argv[i], "-m") == 0 ) {
slouken@245
   132
			audio_channels = 1;
slouken@245
   133
		} else
slouken@245
   134
		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
slouken@245
   135
			++i;
slouken@245
   136
			audio_channels = atoi(argv[i]);
slouken@245
   137
		} else
megastep@23
   138
		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
megastep@23
   139
			++i;
megastep@23
   140
			audio_buffers = atoi(argv[i]);
megastep@23
   141
		} else
slouken@214
   142
		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
slouken@214
   143
			++i;
slouken@214
   144
			audio_volume = atoi(argv[i]);
slouken@214
   145
		} else
megastep@10
   146
		if ( strcmp(argv[i], "-l") == 0 ) {
megastep@10
   147
			looping = -1;
megastep@10
   148
		} else
megastep@13
   149
		if ( strcmp(argv[i], "-i") == 0 ) {
megastep@13
   150
			interactive = 1;
megastep@13
   151
		} else
slouken@0
   152
		if ( strcmp(argv[i], "-8") == 0 ) {
slouken@0
   153
			audio_format = AUDIO_U8;
slouken@256
   154
		} else
slouken@256
   155
		if ( strcmp(argv[i], "-rwops") == 0 ) {
slouken@256
   156
			rwops = 1;
slouken@0
   157
		} else {
slouken@0
   158
			Usage(argv[0]);
slouken@83
   159
			return(1);
slouken@0
   160
		}
slouken@0
   161
	}
slouken@0
   162
	if ( ! argv[i] ) {
slouken@0
   163
		Usage(argv[0]);
slouken@83
   164
		return(1);
slouken@0
   165
	}
slouken@0
   166
slouken@0
   167
	/* Initialize the SDL library */
slouken@0
   168
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
   169
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@83
   170
		return(255);
slouken@0
   171
	}
slouken@245
   172
slouken@488
   173
#ifdef HAVE_SIGNAL_H
slouken@75
   174
	signal(SIGINT, IntHandler);
slouken@321
   175
	signal(SIGTERM, CleanUp);
slouken@488
   176
#endif
slouken@0
   177
slouken@0
   178
	/* Open the audio device */
megastep@23
   179
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
slouken@0
   180
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@83
   181
		return(2);
slouken@0
   182
	} else {
slouken@0
   183
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@297
   184
		printf("Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n", audio_rate,
slouken@0
   185
			(audio_format&0xFF),
slouken@245
   186
			(audio_channels > 2) ? "surround" : (audio_channels > 1) ? "stereo" : "mono", 
slouken@297
   187
			(audio_format&0x1000) ? "BE" : "LE",
megastep@23
   188
			audio_buffers );
slouken@0
   189
	}
slouken@0
   190
	audio_open = 1;
slouken@0
   191
slouken@214
   192
	/* Set the music volume */
slouken@214
   193
	Mix_VolumeMusic(audio_volume);
slouken@214
   194
slouken@0
   195
	/* Set the external music player, if any */
slouken@488
   196
	Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD"));
slouken@0
   197
slouken@75
   198
	while (argv[i]) {
slouken@75
   199
		next_track = 0;
slouken@75
   200
		
slouken@75
   201
		/* Load the requested music file */
slouken@256
   202
		if ( rwops ) {
slouken@256
   203
			rwfp = SDL_RWFromFile(argv[i], "rb");
slouken@257
   204
			music = Mix_LoadMUS_RW(rwfp);
slouken@256
   205
		} else {
slouken@256
   206
			music = Mix_LoadMUS(argv[i]);
slouken@256
   207
		}
slouken@75
   208
		if ( music == NULL ) {
slouken@75
   209
			fprintf(stderr, "Couldn't load %s: %s\n",
slouken@75
   210
				argv[i], SDL_GetError());
slouken@321
   211
			CleanUp(2);
slouken@75
   212
		}
slouken@75
   213
		
slouken@75
   214
		/* Play and then exit */
slouken@75
   215
		printf("Playing %s\n", argv[i]);
slouken@75
   216
		Mix_FadeInMusic(music,looping,2000);
slouken@155
   217
		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
slouken@75
   218
			if(interactive)
slouken@75
   219
				Menu();
slouken@75
   220
			else
slouken@75
   221
				SDL_Delay(100);
slouken@75
   222
		}
slouken@75
   223
		Mix_FreeMusic(music);
slouken@256
   224
		if ( rwops ) {
slouken@256
   225
			SDL_FreeRW(rwfp);
slouken@256
   226
		}
slouken@149
   227
		music = NULL;
slouken@0
   228
slouken@75
   229
		/* If the user presses Ctrl-C more than once, exit. */
slouken@75
   230
		SDL_Delay(500);
slouken@75
   231
		if ( next_track > 1 ) break;
slouken@75
   232
		
slouken@75
   233
		i++;
slouken@0
   234
	}
slouken@321
   235
	CleanUp(0);
slouken@321
   236
slouken@321
   237
	/* Not reached, but fixes compiler warnings */
slouken@321
   238
	return 0;
slouken@0
   239
}