playmus.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 21 Oct 1999 18:02:08 +0000
changeset 0 4ce2db4db959
child 4 5c4485704b30
permissions -rw-r--r--
Initial revision
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
{
slouken@0
    42
	if ( audio_open ) {
slouken@0
    43
		Mix_CloseAudio();
slouken@0
    44
		audio_open = 0;
slouken@0
    45
	}
slouken@0
    46
	if ( music ) {
slouken@0
    47
		Mix_FreeMusic(music);
slouken@0
    48
		music = NULL;
slouken@0
    49
	}
slouken@0
    50
	SDL_Quit();
slouken@0
    51
}
slouken@0
    52
slouken@0
    53
void Usage(char *argv0)
slouken@0
    54
{
slouken@0
    55
	fprintf(stderr, "Usage: %s [-8] [-r rate] [-s] <musicfile>\n", argv0);
slouken@0
    56
}
slouken@0
    57
	
slouken@0
    58
main(int argc, char *argv[])
slouken@0
    59
{
slouken@0
    60
	Uint32 audio_rate;
slouken@0
    61
	Uint16 audio_format;
slouken@0
    62
	int audio_channels;
slouken@0
    63
	int i;
slouken@0
    64
slouken@0
    65
	/* Initialize variables */
slouken@0
    66
	audio_rate = 22050;
slouken@0
    67
	audio_format = AUDIO_S16;
slouken@0
    68
	audio_channels = 2;
slouken@0
    69
slouken@0
    70
	/* Check command line usage */
slouken@0
    71
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@0
    72
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@0
    73
			++i;
slouken@0
    74
			audio_rate = atoi(argv[i]);
slouken@0
    75
		} else
slouken@0
    76
		if ( strcmp(argv[i], "-m") == 0 ) {
slouken@0
    77
			audio_channels = 1;
slouken@0
    78
		} else
slouken@0
    79
		if ( strcmp(argv[i], "-8") == 0 ) {
slouken@0
    80
			audio_format = AUDIO_U8;
slouken@0
    81
		} else {
slouken@0
    82
			Usage(argv[0]);
slouken@0
    83
			exit(1);
slouken@0
    84
		}
slouken@0
    85
	}
slouken@0
    86
	if ( ! argv[i] ) {
slouken@0
    87
		Usage(argv[0]);
slouken@0
    88
		exit(1);
slouken@0
    89
	}
slouken@0
    90
slouken@0
    91
	/* Initialize the SDL library */
slouken@0
    92
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
    93
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
    94
		exit(255);
slouken@0
    95
	}
slouken@0
    96
	atexit(CleanUp);
slouken@0
    97
	signal(SIGINT, exit);
slouken@0
    98
	signal(SIGTERM, exit);
slouken@0
    99
slouken@0
   100
	/* Open the audio device */
slouken@0
   101
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
slouken@0
   102
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
   103
		exit(2);
slouken@0
   104
	} else {
slouken@0
   105
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
slouken@0
   106
		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
slouken@0
   107
			(audio_format&0xFF),
slouken@0
   108
			(audio_channels > 1) ? "stereo" : "mono");
slouken@0
   109
	}
slouken@0
   110
	audio_open = 1;
slouken@0
   111
slouken@0
   112
	/* Set the external music player, if any */
slouken@0
   113
	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
slouken@0
   114
slouken@0
   115
	/* Load the requested music file */
slouken@0
   116
	music = Mix_LoadMUS(argv[i]);
slouken@0
   117
	if ( music == NULL ) {
slouken@0
   118
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
   119
						argv[i], SDL_GetError());
slouken@0
   120
		exit(2);
slouken@0
   121
	}
slouken@0
   122
slouken@0
   123
	/* Play and then exit */
slouken@0
   124
	Mix_PlayMusic(music,0);
slouken@0
   125
	while ( Mix_PlayingMusic() ) {
slouken@0
   126
		SDL_Delay(100);
slouken@0
   127
	}
slouken@0
   128
	exit(0);
slouken@0
   129
}