playmus.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 Apr 2013 01:27:39 -0700
changeset 611 595186c9c0ec
parent 601 05123263dab3
child 617 87116a42526e
permissions -rw-r--r--
Build music support into the framework
slouken@0
     1
/*
slouken@518
     2
  PLAYMUS:  A test application for the SDL mixer library.
slouken@601
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@0
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@140
    22
/* $Id$ */
slouken@138
    23
slouken@0
    24
#include <stdlib.h>
slouken@0
    25
#include <stdio.h>
slouken@0
    26
#include <string.h>
slouken@488
    27
slouken@47
    28
#ifdef unix
slouken@0
    29
#include <unistd.h>
slouken@0
    30
#endif
slouken@0
    31
slouken@24
    32
#include "SDL.h"
slouken@34
    33
#include "SDL_mixer.h"
slouken@0
    34
slouken@503
    35
#ifdef HAVE_SIGNAL_H
slouken@503
    36
#include <signal.h>
slouken@503
    37
#endif
slouken@503
    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@521
   225
			SDL_RWclose(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
}