playmus.c
author Stephane Peter <megastep@lokigames.com>
Mon, 01 Nov 1999 00:40:43 +0000
changeset 13 1e489d8b97d8
parent 10 2fbd0414a1fc
child 23 0578063e33d8
permissions -rw-r--r--
- Massive bugfixes for pausing/resuming music (handled in a generic way)
- New functions to query the paused state of either music or channels
- Added interactive mode (-i) to playmus, to aid debugging
slouken@0
     1
/*
slouken@0
     2
    PLAYMUS:  A test application for the SDL mixer library.
slouken@0
     3
    Copyright (C) 1997-1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This program is free software; you can redistribute it and/or modify
slouken@0
     6
    it under the terms of the GNU General Public License as published by
slouken@0
     7
    the Free Software Foundation; either version 2 of the License, or
slouken@0
     8
    (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This program is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
slouken@0
    13
    GNU General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU General Public License
slouken@0
    16
    along with this program; if not, write to the Free Software
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
*/
slouken@0
    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@0
    29
#ifndef WIN32
slouken@0
    30
#include <unistd.h>
slouken@0
    31
#endif
slouken@0
    32
slouken@0
    33
#include <SDL/SDL.h>
slouken@0
    34
#include "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@0
    39
slouken@0
    40
void CleanUp(void)
slouken@0
    41
{
megastep@4
    42
	if( Mix_PlayingMusic() ) {
megastep@4
    43
		Mix_FadeOutMusic(1500);
megastep@4
    44
		SDL_Delay(1500);
megastep@4
    45
	}
megastep@10
    46
	if ( music ) {
megastep@10
    47
		Mix_FreeMusic(music);
megastep@10
    48
		music = NULL;
megastep@10
    49
	}
slouken@0
    50
	if ( audio_open ) {
slouken@0
    51
		Mix_CloseAudio();
slouken@0
    52
		audio_open = 0;
slouken@0
    53
	}
slouken@0
    54
	SDL_Quit();
slouken@0
    55
}
slouken@0
    56
slouken@0
    57
void Usage(char *argv0)
slouken@0
    58
{
megastep@13
    59
	fprintf(stderr, "Usage: %s [-i] |-l] [-8] [-r rate] [-s] <musicfile>\n", argv0);
slouken@0
    60
}
megastep@13
    61
megastep@13
    62
void Menu(void)
megastep@13
    63
{
megastep@13
    64
	char buf[10];
megastep@13
    65
megastep@13
    66
	printf("Available commands: (p)ause (r)esume (h)alt > ");
megastep@13
    67
	fflush(stdin);
megastep@13
    68
	scanf("%s",buf);
megastep@13
    69
	switch(buf[0]){
megastep@13
    70
	case 'p': case 'P':
megastep@13
    71
		Mix_PauseMusic();
megastep@13
    72
		break;
megastep@13
    73
	case 'r': case 'R':
megastep@13
    74
		Mix_ResumeMusic();
megastep@13
    75
		break;
megastep@13
    76
	case 'h': case 'H':
megastep@13
    77
		Mix_HaltMusic();
megastep@13
    78
		break;
megastep@13
    79
	}
megastep@13
    80
	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
megastep@13
    81
		   Mix_PausedMusic() ? "yes" : "no");
megastep@13
    82
}
megastep@13
    83
slouken@0
    84
main(int argc, char *argv[])
slouken@0
    85
{
slouken@0
    86
	Uint32 audio_rate;
slouken@0
    87
	Uint16 audio_format;
slouken@0
    88
	int audio_channels;
megastep@10
    89
	int looping = 0;
megastep@13
    90
	int interactive = 0;
slouken@0
    91
	int i;
slouken@0
    92
slouken@0
    93
	/* Initialize variables */
slouken@0
    94
	audio_rate = 22050;
slouken@0
    95
	audio_format = AUDIO_S16;
slouken@0
    96
	audio_channels = 2;
slouken@0
    97
slouken@0
    98
	/* Check command line usage */
slouken@0
    99
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@0
   100
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@0
   101
			++i;
slouken@0
   102
			audio_rate = atoi(argv[i]);
slouken@0
   103
		} else
slouken@0
   104
		if ( strcmp(argv[i], "-m") == 0 ) {
slouken@0
   105
			audio_channels = 1;
slouken@0
   106
		} else
megastep@10
   107
		if ( strcmp(argv[i], "-l") == 0 ) {
megastep@10
   108
			looping = -1;
megastep@10
   109
		} else
megastep@13
   110
		if ( strcmp(argv[i], "-i") == 0 ) {
megastep@13
   111
			interactive = 1;
megastep@13
   112
		} else
slouken@0
   113
		if ( strcmp(argv[i], "-8") == 0 ) {
slouken@0
   114
			audio_format = AUDIO_U8;
slouken@0
   115
		} else {
slouken@0
   116
			Usage(argv[0]);
slouken@0
   117
			exit(1);
slouken@0
   118
		}
slouken@0
   119
	}
slouken@0
   120
	if ( ! argv[i] ) {
slouken@0
   121
		Usage(argv[0]);
slouken@0
   122
		exit(1);
slouken@0
   123
	}
slouken@0
   124
slouken@0
   125
	/* Initialize the SDL library */
slouken@0
   126
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
   127
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
   128
		exit(255);
slouken@0
   129
	}
slouken@0
   130
	atexit(CleanUp);
slouken@0
   131
	signal(SIGINT, exit);
slouken@0
   132
	signal(SIGTERM, exit);
slouken@0
   133
slouken@0
   134
	/* Open the audio device */
slouken@0
   135
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
slouken@0
   136
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
   137
		exit(2);
slouken@0
   138
	} else {
slouken@0
   139
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@0
   140
		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
slouken@0
   141
			(audio_format&0xFF),
slouken@0
   142
			(audio_channels > 1) ? "stereo" : "mono");
slouken@0
   143
	}
slouken@0
   144
	audio_open = 1;
slouken@0
   145
slouken@0
   146
	/* Set the external music player, if any */
slouken@0
   147
	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
slouken@0
   148
slouken@0
   149
	/* Load the requested music file */
slouken@0
   150
	music = Mix_LoadMUS(argv[i]);
slouken@0
   151
	if ( music == NULL ) {
slouken@0
   152
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
   153
						argv[i], SDL_GetError());
slouken@0
   154
		exit(2);
slouken@0
   155
	}
slouken@0
   156
slouken@0
   157
	/* Play and then exit */
megastep@10
   158
	Mix_FadeInMusic(music,looping,2000);
megastep@13
   159
	while ( Mix_PlayingMusic() || Mix_PausedMusic() ) {
megastep@13
   160
		if(interactive)
megastep@13
   161
			Menu();
megastep@13
   162
		else
megastep@13
   163
			SDL_Delay(100);
slouken@0
   164
	}
slouken@0
   165
	exit(0);
slouken@0
   166
}