playmus.c
author Sam Lantinga <slouken@lokigames.com>
Fri, 14 Jan 2000 18:59:44 +0000
changeset 34 25e00090b15c
parent 24 a25bb2d59ce8
child 47 103ac6380537
permissions -rw-r--r--
*** empty log message ***
     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.h"
    34 #include "SDL_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 [-i] |-l] [-8] [-r rate] [-b buffers] [-s] <musicfile>\n", argv0);
    60 }
    61 
    62 void Menu(void)
    63 {
    64 	char buf[10];
    65 
    66 	printf("Available commands: (p)ause (r)esume (h)alt > ");
    67 	fflush(stdin);
    68 	scanf("%s",buf);
    69 	switch(buf[0]){
    70 	case 'p': case 'P':
    71 		Mix_PauseMusic();
    72 		break;
    73 	case 'r': case 'R':
    74 		Mix_ResumeMusic();
    75 		break;
    76 	case 'h': case 'H':
    77 		Mix_HaltMusic();
    78 		break;
    79 	}
    80 	printf("Music playing: %s Paused: %s\n", Mix_PlayingMusic() ? "yes" : "no", 
    81 		   Mix_PausedMusic() ? "yes" : "no");
    82 }
    83 
    84 main(int argc, char *argv[])
    85 {
    86 	Uint32 audio_rate;
    87 	Uint16 audio_format;
    88 	int audio_channels;
    89 	int audio_buffers;
    90 	int looping = 0;
    91 	int interactive = 0;
    92 	int i;
    93 
    94 	/* Initialize variables */
    95 	audio_rate = 22050;
    96 	audio_format = AUDIO_S16;
    97 	audio_channels = 2;
    98 	audio_buffers = 4096;
    99 
   100 	/* Check command line usage */
   101 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   102 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   103 			++i;
   104 			audio_rate = atoi(argv[i]);
   105 		} else
   106 		if ( (strcmp(argv[i], "-b") == 0) && argv[i+1] ) {
   107 			++i;
   108 			audio_buffers = atoi(argv[i]);
   109 		} else
   110 		if ( strcmp(argv[i], "-m") == 0 ) {
   111 			audio_channels = 1;
   112 		} else
   113 		if ( strcmp(argv[i], "-l") == 0 ) {
   114 			looping = -1;
   115 		} else
   116 		if ( strcmp(argv[i], "-i") == 0 ) {
   117 			interactive = 1;
   118 		} else
   119 		if ( strcmp(argv[i], "-8") == 0 ) {
   120 			audio_format = AUDIO_U8;
   121 		} else {
   122 			Usage(argv[0]);
   123 			exit(1);
   124 		}
   125 	}
   126 	if ( ! argv[i] ) {
   127 		Usage(argv[0]);
   128 		exit(1);
   129 	}
   130 
   131 	/* Initialize the SDL library */
   132 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   133 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   134 		exit(255);
   135 	}
   136 	atexit(CleanUp);
   137 	signal(SIGINT, exit);
   138 	signal(SIGTERM, exit);
   139 
   140 	/* Open the audio device */
   141 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
   142 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   143 		exit(2);
   144 	} else {
   145 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   146 		printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
   147 			(audio_format&0xFF),
   148 			(audio_channels > 1) ? "stereo" : "mono", 
   149 			audio_buffers );
   150 	}
   151 	audio_open = 1;
   152 
   153 	/* Set the external music player, if any */
   154 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   155 
   156 	/* Load the requested music file */
   157 	music = Mix_LoadMUS(argv[i]);
   158 	if ( music == NULL ) {
   159 		fprintf(stderr, "Couldn't load %s: %s\n",
   160 						argv[i], SDL_GetError());
   161 		exit(2);
   162 	}
   163 
   164 	/* Play and then exit */
   165 	Mix_FadeInMusic(music,looping,2000);
   166 	while ( Mix_PlayingMusic() || Mix_PausedMusic() ) {
   167 		if(interactive)
   168 			Menu();
   169 		else
   170 			SDL_Delay(100);
   171 	}
   172 	exit(0);
   173 }