playmus.c
author Stephane Peter <megastep@lokigames.com>
Sat, 30 Oct 1999 00:21:34 +0000
changeset 10 2fbd0414a1fc
parent 4 5c4485704b30
child 13 1e489d8b97d8
permissions -rw-r--r--
- Fixed Mix_PlayingMusic()
- Added real generic music looping, just like samples
- Added Mix_FadeOutInMusic() to fade out the current music and fade in
a new one in the background
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@10
    59
	fprintf(stderr, "Usage: %s |-l] [-8] [-r rate] [-s] <musicfile>\n", argv0);
slouken@0
    60
}
slouken@0
    61
	
slouken@0
    62
main(int argc, char *argv[])
slouken@0
    63
{
slouken@0
    64
	Uint32 audio_rate;
slouken@0
    65
	Uint16 audio_format;
slouken@0
    66
	int audio_channels;
megastep@10
    67
	int looping = 0;
slouken@0
    68
	int i;
slouken@0
    69
slouken@0
    70
	/* Initialize variables */
slouken@0
    71
	audio_rate = 22050;
slouken@0
    72
	audio_format = AUDIO_S16;
slouken@0
    73
	audio_channels = 2;
slouken@0
    74
slouken@0
    75
	/* Check command line usage */
slouken@0
    76
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@0
    77
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@0
    78
			++i;
slouken@0
    79
			audio_rate = atoi(argv[i]);
slouken@0
    80
		} else
slouken@0
    81
		if ( strcmp(argv[i], "-m") == 0 ) {
slouken@0
    82
			audio_channels = 1;
slouken@0
    83
		} else
megastep@10
    84
		if ( strcmp(argv[i], "-l") == 0 ) {
megastep@10
    85
			looping = -1;
megastep@10
    86
		} else
slouken@0
    87
		if ( strcmp(argv[i], "-8") == 0 ) {
slouken@0
    88
			audio_format = AUDIO_U8;
slouken@0
    89
		} else {
slouken@0
    90
			Usage(argv[0]);
slouken@0
    91
			exit(1);
slouken@0
    92
		}
slouken@0
    93
	}
slouken@0
    94
	if ( ! argv[i] ) {
slouken@0
    95
		Usage(argv[0]);
slouken@0
    96
		exit(1);
slouken@0
    97
	}
slouken@0
    98
slouken@0
    99
	/* Initialize the SDL library */
slouken@0
   100
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
   101
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
   102
		exit(255);
slouken@0
   103
	}
slouken@0
   104
	atexit(CleanUp);
slouken@0
   105
	signal(SIGINT, exit);
slouken@0
   106
	signal(SIGTERM, exit);
slouken@0
   107
slouken@0
   108
	/* Open the audio device */
slouken@0
   109
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
slouken@0
   110
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
   111
		exit(2);
slouken@0
   112
	} else {
slouken@0
   113
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@0
   114
		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
slouken@0
   115
			(audio_format&0xFF),
slouken@0
   116
			(audio_channels > 1) ? "stereo" : "mono");
slouken@0
   117
	}
slouken@0
   118
	audio_open = 1;
slouken@0
   119
slouken@0
   120
	/* Set the external music player, if any */
slouken@0
   121
	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
slouken@0
   122
slouken@0
   123
	/* Load the requested music file */
slouken@0
   124
	music = Mix_LoadMUS(argv[i]);
slouken@0
   125
	if ( music == NULL ) {
slouken@0
   126
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
   127
						argv[i], SDL_GetError());
slouken@0
   128
		exit(2);
slouken@0
   129
	}
slouken@0
   130
slouken@0
   131
	/* Play and then exit */
megastep@10
   132
	Mix_FadeInMusic(music,looping,2000);
slouken@0
   133
	while ( Mix_PlayingMusic() ) {
slouken@0
   134
		SDL_Delay(100);
slouken@0
   135
	}
slouken@0
   136
	exit(0);
slouken@0
   137
}