playmus.c
changeset 0 4ce2db4db959
child 4 5c4485704b30
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/playmus.c	Thu Oct 21 18:02:08 1999 +0000
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 +    PLAYMUS:  A test application for the SDL mixer library.
     1.6 +    Copyright (C) 1997-1999  Sam Lantinga
     1.7 +
     1.8 +    This program is free software; you can redistribute it and/or modify
     1.9 +    it under the terms of the GNU General Public License as published by
    1.10 +    the Free Software Foundation; either version 2 of the License, or
    1.11 +    (at your option) any later version.
    1.12 +
    1.13 +    This program is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +    GNU General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU General Public License
    1.19 +    along with this program; if not, write to the Free Software
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    5635-34 Springhouse Dr.
    1.24 +    Pleasanton, CA 94588 (USA)
    1.25 +    slouken@devolution.com
    1.26 +*/
    1.27 +
    1.28 +#include <stdlib.h>
    1.29 +#include <stdio.h>
    1.30 +#include <string.h>
    1.31 +#include <signal.h>
    1.32 +#ifndef WIN32
    1.33 +#include <unistd.h>
    1.34 +#endif
    1.35 +
    1.36 +#include <SDL/SDL.h>
    1.37 +#include "mixer.h"
    1.38 +
    1.39 +
    1.40 +static int audio_open = 0;
    1.41 +static Mix_Music *music = NULL;
    1.42 +
    1.43 +void CleanUp(void)
    1.44 +{
    1.45 +	if ( audio_open ) {
    1.46 +		Mix_CloseAudio();
    1.47 +		audio_open = 0;
    1.48 +	}
    1.49 +	if ( music ) {
    1.50 +		Mix_FreeMusic(music);
    1.51 +		music = NULL;
    1.52 +	}
    1.53 +	SDL_Quit();
    1.54 +}
    1.55 +
    1.56 +void Usage(char *argv0)
    1.57 +{
    1.58 +	fprintf(stderr, "Usage: %s [-8] [-r rate] [-s] <musicfile>\n", argv0);
    1.59 +}
    1.60 +	
    1.61 +main(int argc, char *argv[])
    1.62 +{
    1.63 +	Uint32 audio_rate;
    1.64 +	Uint16 audio_format;
    1.65 +	int audio_channels;
    1.66 +	int i;
    1.67 +
    1.68 +	/* Initialize variables */
    1.69 +	audio_rate = 22050;
    1.70 +	audio_format = AUDIO_S16;
    1.71 +	audio_channels = 2;
    1.72 +
    1.73 +	/* Check command line usage */
    1.74 +	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
    1.75 +		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
    1.76 +			++i;
    1.77 +			audio_rate = atoi(argv[i]);
    1.78 +		} else
    1.79 +		if ( strcmp(argv[i], "-m") == 0 ) {
    1.80 +			audio_channels = 1;
    1.81 +		} else
    1.82 +		if ( strcmp(argv[i], "-8") == 0 ) {
    1.83 +			audio_format = AUDIO_U8;
    1.84 +		} else {
    1.85 +			Usage(argv[0]);
    1.86 +			exit(1);
    1.87 +		}
    1.88 +	}
    1.89 +	if ( ! argv[i] ) {
    1.90 +		Usage(argv[0]);
    1.91 +		exit(1);
    1.92 +	}
    1.93 +
    1.94 +	/* Initialize the SDL library */
    1.95 +	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
    1.96 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.97 +		exit(255);
    1.98 +	}
    1.99 +	atexit(CleanUp);
   1.100 +	signal(SIGINT, exit);
   1.101 +	signal(SIGTERM, exit);
   1.102 +
   1.103 +	/* Open the audio device */
   1.104 +	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
   1.105 +		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   1.106 +		exit(2);
   1.107 +	} else {
   1.108 +		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   1.109 +		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
   1.110 +			(audio_format&0xFF),
   1.111 +			(audio_channels > 1) ? "stereo" : "mono");
   1.112 +	}
   1.113 +	audio_open = 1;
   1.114 +
   1.115 +	/* Set the external music player, if any */
   1.116 +	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   1.117 +
   1.118 +	/* Load the requested music file */
   1.119 +	music = Mix_LoadMUS(argv[i]);
   1.120 +	if ( music == NULL ) {
   1.121 +		fprintf(stderr, "Couldn't load %s: %s\n",
   1.122 +						argv[i], SDL_GetError());
   1.123 +		exit(2);
   1.124 +	}
   1.125 +
   1.126 +	/* Play and then exit */
   1.127 +	Mix_PlayMusic(music,0);
   1.128 +	while ( Mix_PlayingMusic() ) {
   1.129 +		SDL_Delay(100);
   1.130 +	}
   1.131 +	exit(0);
   1.132 +}