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