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
     1 /*
     2     PLAYMUS:  A test application for the SDL mixer library.
     3     Copyright (C) 1997-1999  Sam Lantinga
     4 
     5     This program is free software; you can redistribute it and/or modify
     6     it under the terms of the GNU General Public License as published by
     7     the Free Software Foundation; either version 2 of the License, or
     8     (at your option) any later version.
     9 
    10     This program is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13     GNU General Public License for more details.
    14 
    15     You should have received a copy of the GNU General Public License
    16     along with this program; if not, write to the Free Software
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     5635-34 Springhouse Dr.
    21     Pleasanton, CA 94588 (USA)
    22     slouken@devolution.com
    23 */
    24 
    25 #include <stdlib.h>
    26 #include <stdio.h>
    27 #include <string.h>
    28 #include <signal.h>
    29 #ifndef WIN32
    30 #include <unistd.h>
    31 #endif
    32 
    33 #include <SDL/SDL.h>
    34 #include "mixer.h"
    35 
    36 
    37 static int audio_open = 0;
    38 static Mix_Music *music = NULL;
    39 
    40 void CleanUp(void)
    41 {
    42 	if ( audio_open ) {
    43 		Mix_CloseAudio();
    44 		audio_open = 0;
    45 	}
    46 	if ( music ) {
    47 		Mix_FreeMusic(music);
    48 		music = NULL;
    49 	}
    50 	SDL_Quit();
    51 }
    52 
    53 void Usage(char *argv0)
    54 {
    55 	fprintf(stderr, "Usage: %s [-8] [-r rate] [-s] <musicfile>\n", argv0);
    56 }
    57 	
    58 main(int argc, char *argv[])
    59 {
    60 	Uint32 audio_rate;
    61 	Uint16 audio_format;
    62 	int audio_channels;
    63 	int i;
    64 
    65 	/* Initialize variables */
    66 	audio_rate = 22050;
    67 	audio_format = AUDIO_S16;
    68 	audio_channels = 2;
    69 
    70 	/* Check command line usage */
    71 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
    72 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
    73 			++i;
    74 			audio_rate = atoi(argv[i]);
    75 		} else
    76 		if ( strcmp(argv[i], "-m") == 0 ) {
    77 			audio_channels = 1;
    78 		} else
    79 		if ( strcmp(argv[i], "-8") == 0 ) {
    80 			audio_format = AUDIO_U8;
    81 		} else {
    82 			Usage(argv[0]);
    83 			exit(1);
    84 		}
    85 	}
    86 	if ( ! argv[i] ) {
    87 		Usage(argv[0]);
    88 		exit(1);
    89 	}
    90 
    91 	/* Initialize the SDL library */
    92 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
    93 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    94 		exit(255);
    95 	}
    96 	atexit(CleanUp);
    97 	signal(SIGINT, exit);
    98 	signal(SIGTERM, exit);
    99 
   100 	/* Open the audio device */
   101 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
   102 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   103 		exit(2);
   104 	} else {
   105 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   106 		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
   107 			(audio_format&0xFF),
   108 			(audio_channels > 1) ? "stereo" : "mono");
   109 	}
   110 	audio_open = 1;
   111 
   112 	/* Set the external music player, if any */
   113 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   114 
   115 	/* Load the requested music file */
   116 	music = Mix_LoadMUS(argv[i]);
   117 	if ( music == NULL ) {
   118 		fprintf(stderr, "Couldn't load %s: %s\n",
   119 						argv[i], SDL_GetError());
   120 		exit(2);
   121 	}
   122 
   123 	/* Play and then exit */
   124 	Mix_PlayMusic(music,0);
   125 	while ( Mix_PlayingMusic() ) {
   126 		SDL_Delay(100);
   127 	}
   128 	exit(0);
   129 }