playmus.c
author Sam Lantinga <slouken@lokigames.com>
Fri, 14 Jan 2000 18:59:44 +0000
changeset 34 25e00090b15c
parent 24 a25bb2d59ce8
child 47 103ac6380537
permissions -rw-r--r--
*** empty log message ***
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@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@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@23
    59
	fprintf(stderr, "Usage: %s [-i] |-l] [-8] [-r rate] [-b buffers] [-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@23
    89
	int audio_buffers;
megastep@10
    90
	int looping = 0;
megastep@13
    91
	int interactive = 0;
slouken@0
    92
	int i;
slouken@0
    93
slouken@0
    94
	/* Initialize variables */
slouken@0
    95
	audio_rate = 22050;
slouken@0
    96
	audio_format = AUDIO_S16;
slouken@0
    97
	audio_channels = 2;
megastep@23
    98
	audio_buffers = 4096;
slouken@0
    99
slouken@0
   100
	/* Check command line usage */
slouken@0
   101
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@0
   102
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@0
   103
			++i;
slouken@0
   104
			audio_rate = atoi(argv[i]);
slouken@0
   105
		} else
megastep@23
   106
		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
megastep@23
   107
			++i;
megastep@23
   108
			audio_buffers = atoi(argv[i]);
megastep@23
   109
		} else
slouken@0
   110
		if ( strcmp(argv[i], "-m") == 0 ) {
slouken@0
   111
			audio_channels = 1;
slouken@0
   112
		} else
megastep@10
   113
		if ( strcmp(argv[i], "-l") == 0 ) {
megastep@10
   114
			looping = -1;
megastep@10
   115
		} else
megastep@13
   116
		if ( strcmp(argv[i], "-i") == 0 ) {
megastep@13
   117
			interactive = 1;
megastep@13
   118
		} else
slouken@0
   119
		if ( strcmp(argv[i], "-8") == 0 ) {
slouken@0
   120
			audio_format = AUDIO_U8;
slouken@0
   121
		} else {
slouken@0
   122
			Usage(argv[0]);
slouken@0
   123
			exit(1);
slouken@0
   124
		}
slouken@0
   125
	}
slouken@0
   126
	if ( ! argv[i] ) {
slouken@0
   127
		Usage(argv[0]);
slouken@0
   128
		exit(1);
slouken@0
   129
	}
slouken@0
   130
slouken@0
   131
	/* Initialize the SDL library */
slouken@0
   132
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
   133
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
   134
		exit(255);
slouken@0
   135
	}
slouken@0
   136
	atexit(CleanUp);
slouken@0
   137
	signal(SIGINT, exit);
slouken@0
   138
	signal(SIGTERM, exit);
slouken@0
   139
slouken@0
   140
	/* Open the audio device */
megastep@23
   141
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
slouken@0
   142
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
   143
		exit(2);
slouken@0
   144
	} else {
slouken@0
   145
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
megastep@23
   146
		printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
slouken@0
   147
			(audio_format&0xFF),
megastep@23
   148
			(audio_channels > 1) ? "stereo" : "mono", 
megastep@23
   149
			audio_buffers );
slouken@0
   150
	}
slouken@0
   151
	audio_open = 1;
slouken@0
   152
slouken@0
   153
	/* Set the external music player, if any */
slouken@0
   154
	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
slouken@0
   155
slouken@0
   156
	/* Load the requested music file */
slouken@0
   157
	music = Mix_LoadMUS(argv[i]);
slouken@0
   158
	if ( music == NULL ) {
slouken@0
   159
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
   160
						argv[i], SDL_GetError());
slouken@0
   161
		exit(2);
slouken@0
   162
	}
slouken@0
   163
slouken@0
   164
	/* Play and then exit */
megastep@10
   165
	Mix_FadeInMusic(music,looping,2000);
megastep@13
   166
	while ( Mix_PlayingMusic() || Mix_PausedMusic() ) {
megastep@13
   167
		if(interactive)
megastep@13
   168
			Menu();
megastep@13
   169
		else
megastep@13
   170
			SDL_Delay(100);
slouken@0
   171
	}
slouken@0
   172
	exit(0);
slouken@0
   173
}