playmus.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 17:41:55 +0000
changeset 241 503416fca921
parent 214 ab571e095c22
child 245 63b3650714de
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
slouken@0
     1
/*
slouken@0
     2
    PLAYMUS:  A test application for the SDL mixer library.
slouken@241
     3
    Copyright (C) 1997-2004 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@0
    28
#include <signal.h>
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@0
    36
slouken@0
    37
static int audio_open = 0;
slouken@0
    38
static Mix_Music *music = NULL;
slouken@75
    39
static int next_track = 0;
slouken@0
    40
slouken@0
    41
void CleanUp(void)
slouken@0
    42
{
megastep@4
    43
	if( Mix_PlayingMusic() ) {
megastep@4
    44
		Mix_FadeOutMusic(1500);
megastep@4
    45
		SDL_Delay(1500);
megastep@4
    46
	}
megastep@10
    47
	if ( music ) {
megastep@10
    48
		Mix_FreeMusic(music);
megastep@10
    49
		music = NULL;
megastep@10
    50
	}
slouken@0
    51
	if ( audio_open ) {
slouken@0
    52
		Mix_CloseAudio();
slouken@0
    53
		audio_open = 0;
slouken@0
    54
	}
slouken@0
    55
	SDL_Quit();
slouken@0
    56
}
slouken@0
    57
slouken@0
    58
void Usage(char *argv0)
slouken@0
    59
{
slouken@214
    60
	fprintf(stderr, "Usage: %s [-i] [-l] [-8] [-r rate] [-b buffers] [-v N] <musicfile>\n", argv0);
slouken@0
    61
}
megastep@13
    62
megastep@13
    63
void Menu(void)
megastep@13
    64
{
megastep@13
    65
	char buf[10];
megastep@13
    66
megastep@13
    67
	printf("Available commands: (p)ause (r)esume (h)alt > ");
megastep@13
    68
	fflush(stdin);
megastep@13
    69
	scanf("%s",buf);
megastep@13
    70
	switch(buf[0]){
megastep@13
    71
	case 'p': case 'P':
megastep@13
    72
		Mix_PauseMusic();
megastep@13
    73
		break;
megastep@13
    74
	case 'r': case 'R':
megastep@13
    75
		Mix_ResumeMusic();
megastep@13
    76
		break;
megastep@13
    77
	case 'h': case 'H':
megastep@13
    78
		Mix_HaltMusic();
megastep@13
    79
		break;
megastep@13
    80
	}
megastep@13
    81
	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
megastep@13
    82
		   Mix_PausedMusic() ? "yes" : "no");
megastep@13
    83
}
megastep@13
    84
slouken@75
    85
void IntHandler(int sig)
slouken@75
    86
{
slouken@75
    87
	switch (sig) {
slouken@75
    88
	        case SIGINT:
slouken@75
    89
			next_track++;
slouken@75
    90
			break;
slouken@75
    91
	}
slouken@75
    92
}
slouken@75
    93
slouken@150
    94
int main(int argc, char *argv[])
slouken@0
    95
{
slouken@47
    96
	int audio_rate;
slouken@0
    97
	Uint16 audio_format;
slouken@0
    98
	int audio_channels;
megastep@23
    99
	int audio_buffers;
slouken@214
   100
	int audio_volume = MIX_MAX_VOLUME;
megastep@10
   101
	int looping = 0;
megastep@13
   102
	int interactive = 0;
slouken@0
   103
	int i;
slouken@0
   104
slouken@0
   105
	/* Initialize variables */
slouken@0
   106
	audio_rate = 22050;
slouken@0
   107
	audio_format = AUDIO_S16;
slouken@0
   108
	audio_channels = 2;
megastep@23
   109
	audio_buffers = 4096;
slouken@0
   110
slouken@0
   111
	/* Check command line usage */
slouken@0
   112
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@0
   113
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@0
   114
			++i;
slouken@0
   115
			audio_rate = atoi(argv[i]);
slouken@0
   116
		} else
megastep@23
   117
		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
megastep@23
   118
			++i;
megastep@23
   119
			audio_buffers = atoi(argv[i]);
megastep@23
   120
		} else
slouken@214
   121
		if ( (strcmp(argv[i], "-v") == 0) && argv[i+1] ) {
slouken@214
   122
			++i;
slouken@214
   123
			audio_volume = atoi(argv[i]);
slouken@214
   124
		} else
slouken@0
   125
		if ( strcmp(argv[i], "-m") == 0 ) {
slouken@0
   126
			audio_channels = 1;
slouken@0
   127
		} else
megastep@10
   128
		if ( strcmp(argv[i], "-l") == 0 ) {
megastep@10
   129
			looping = -1;
megastep@10
   130
		} else
megastep@13
   131
		if ( strcmp(argv[i], "-i") == 0 ) {
megastep@13
   132
			interactive = 1;
megastep@13
   133
		} else
slouken@0
   134
		if ( strcmp(argv[i], "-8") == 0 ) {
slouken@0
   135
			audio_format = AUDIO_U8;
slouken@0
   136
		} else {
slouken@0
   137
			Usage(argv[0]);
slouken@83
   138
			return(1);
slouken@0
   139
		}
slouken@0
   140
	}
slouken@0
   141
	if ( ! argv[i] ) {
slouken@0
   142
		Usage(argv[0]);
slouken@83
   143
		return(1);
slouken@0
   144
	}
slouken@0
   145
slouken@0
   146
	/* Initialize the SDL library */
slouken@0
   147
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
   148
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@83
   149
		return(255);
slouken@0
   150
	}
slouken@0
   151
	atexit(CleanUp);
slouken@75
   152
	signal(SIGINT, IntHandler);
slouken@0
   153
	signal(SIGTERM, exit);
slouken@0
   154
slouken@0
   155
	/* Open the audio device */
megastep@23
   156
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
slouken@0
   157
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@83
   158
		return(2);
slouken@0
   159
	} else {
slouken@0
   160
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
megastep@23
   161
		printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
slouken@0
   162
			(audio_format&0xFF),
megastep@23
   163
			(audio_channels > 1) ? "stereo" : "mono", 
megastep@23
   164
			audio_buffers );
slouken@0
   165
	}
slouken@0
   166
	audio_open = 1;
slouken@0
   167
slouken@214
   168
	/* Set the music volume */
slouken@214
   169
	Mix_VolumeMusic(audio_volume);
slouken@214
   170
slouken@0
   171
	/* Set the external music player, if any */
slouken@0
   172
	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
slouken@0
   173
slouken@75
   174
	while (argv[i]) {
slouken@75
   175
		next_track = 0;
slouken@75
   176
		
slouken@75
   177
		/* Load the requested music file */
slouken@75
   178
		music = Mix_LoadMUS(argv[i]);
slouken@75
   179
		if ( music == NULL ) {
slouken@75
   180
			fprintf(stderr, "Couldn't load %s: %s\n",
slouken@75
   181
				argv[i], SDL_GetError());
slouken@83
   182
			return(2);
slouken@75
   183
		}
slouken@75
   184
		
slouken@75
   185
		/* Play and then exit */
slouken@75
   186
		printf("Playing %s\n", argv[i]);
slouken@75
   187
		Mix_FadeInMusic(music,looping,2000);
slouken@155
   188
		while ( !next_track && (Mix_PlayingMusic() || Mix_PausedMusic()) ) {
slouken@75
   189
			if(interactive)
slouken@75
   190
				Menu();
slouken@75
   191
			else
slouken@75
   192
				SDL_Delay(100);
slouken@75
   193
		}
slouken@75
   194
		Mix_FreeMusic(music);
slouken@149
   195
		music = NULL;
slouken@0
   196
slouken@75
   197
		/* If the user presses Ctrl-C more than once, exit. */
slouken@75
   198
		SDL_Delay(500);
slouken@75
   199
		if ( next_track > 1 ) break;
slouken@75
   200
		
slouken@75
   201
		i++;
slouken@0
   202
	}
slouken@83
   203
	return(0);
slouken@0
   204
}