playmus.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 20 Aug 2006 23:25:46 +0000
changeset 333 b5fd2f76ed9c
parent 321 a72a7f07c8f1
child 386 695494546b3c
permissions -rw-r--r--
Date: Sat, 19 Aug 2006 00:13:54 +0200
From: Hans de Goede
Subject: PATCH: add volume control to playmus interactive mode

Hi,

First a short intro I'm a Linux enthousiast and developer, mainly
working on Linux gaming related stuff.

I've been working on getting d1x (descent 3d-shooter linux port) to play
the music from the original game. Now, 2 full days further I'm getting
there (as in nearly finished).

Because of <insert long story here if you really want to know ask> I
cannot use SDL_mixer directly from d1x, so I'm using popen("playmus -i
xxxx", "w") to start playmus todo the playback and I control it (-i is
interactive mode) by writing the nescesarry commands to the stdin FILE *
returned by popen.

However by default playmus' interactive mode doesn't allow changing the
volume, which I need to be able todo. The attached patch adds this
capability to playmus, and is otherwise harmless. I hope you can
incorperate this patch into playmus, so that I don't have to ship a
modified version as part of the d1x package.

Thanks & Regards,

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