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