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
     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 ( music ) {
    47 		Mix_FreeMusic(music);
    48 		music = NULL;
    49 	}
    50 	if ( audio_open ) {
    51 		Mix_CloseAudio();
    52 		audio_open = 0;
    53 	}
    54 	SDL_Quit();
    55 }
    56 
    57 void Usage(char *argv0)
    58 {
    59 	fprintf(stderr, "Usage: %s |-l] [-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 looping = 0;
    68 	int i;
    69 
    70 	/* Initialize variables */
    71 	audio_rate = 22050;
    72 	audio_format = AUDIO_S16;
    73 	audio_channels = 2;
    74 
    75 	/* Check command line usage */
    76 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
    77 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
    78 			++i;
    79 			audio_rate = atoi(argv[i]);
    80 		} else
    81 		if ( strcmp(argv[i], "-m") == 0 ) {
    82 			audio_channels = 1;
    83 		} else
    84 		if ( strcmp(argv[i], "-l") == 0 ) {
    85 			looping = -1;
    86 		} else
    87 		if ( strcmp(argv[i], "-8") == 0 ) {
    88 			audio_format = AUDIO_U8;
    89 		} else {
    90 			Usage(argv[0]);
    91 			exit(1);
    92 		}
    93 	}
    94 	if ( ! argv[i] ) {
    95 		Usage(argv[0]);
    96 		exit(1);
    97 	}
    98 
    99 	/* Initialize the SDL library */
   100 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   101 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   102 		exit(255);
   103 	}
   104 	atexit(CleanUp);
   105 	signal(SIGINT, exit);
   106 	signal(SIGTERM, exit);
   107 
   108 	/* Open the audio device */
   109 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
   110 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   111 		exit(2);
   112 	} else {
   113 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   114 		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
   115 			(audio_format&0xFF),
   116 			(audio_channels > 1) ? "stereo" : "mono");
   117 	}
   118 	audio_open = 1;
   119 
   120 	/* Set the external music player, if any */
   121 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   122 
   123 	/* Load the requested music file */
   124 	music = Mix_LoadMUS(argv[i]);
   125 	if ( music == NULL ) {
   126 		fprintf(stderr, "Couldn't load %s: %s\n",
   127 						argv[i], SDL_GetError());
   128 		exit(2);
   129 	}
   130 
   131 	/* Play and then exit */
   132 	Mix_FadeInMusic(music,looping,2000);
   133 	while ( Mix_PlayingMusic() ) {
   134 		SDL_Delay(100);
   135 	}
   136 	exit(0);
   137 }