playmus.c
author Stephane Peter <megastep@lokigames.com>
Mon, 01 Nov 1999 00:40:43 +0000
changeset 13 1e489d8b97d8
parent 10 2fbd0414a1fc
child 23 0578063e33d8
permissions -rw-r--r--
- Massive bugfixes for pausing/resuming music (handled in a generic way)
- New functions to query the paused state of either music or channels
- Added interactive mode (-i) to playmus, to aid debugging
     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 [-i] |-l] [-8] [-r rate] [-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 looping = 0;
    90 	int interactive = 0;
    91 	int i;
    92 
    93 	/* Initialize variables */
    94 	audio_rate = 22050;
    95 	audio_format = AUDIO_S16;
    96 	audio_channels = 2;
    97 
    98 	/* Check command line usage */
    99 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   100 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   101 			++i;
   102 			audio_rate = atoi(argv[i]);
   103 		} else
   104 		if ( strcmp(argv[i], "-m") == 0 ) {
   105 			audio_channels = 1;
   106 		} else
   107 		if ( strcmp(argv[i], "-l") == 0 ) {
   108 			looping = -1;
   109 		} else
   110 		if ( strcmp(argv[i], "-i") == 0 ) {
   111 			interactive = 1;
   112 		} else
   113 		if ( strcmp(argv[i], "-8") == 0 ) {
   114 			audio_format = AUDIO_U8;
   115 		} else {
   116 			Usage(argv[0]);
   117 			exit(1);
   118 		}
   119 	}
   120 	if ( ! argv[i] ) {
   121 		Usage(argv[0]);
   122 		exit(1);
   123 	}
   124 
   125 	/* Initialize the SDL library */
   126 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   127 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   128 		exit(255);
   129 	}
   130 	atexit(CleanUp);
   131 	signal(SIGINT, exit);
   132 	signal(SIGTERM, exit);
   133 
   134 	/* Open the audio device */
   135 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
   136 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   137 		exit(2);
   138 	} else {
   139 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   140 		printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
   141 			(audio_format&0xFF),
   142 			(audio_channels > 1) ? "stereo" : "mono");
   143 	}
   144 	audio_open = 1;
   145 
   146 	/* Set the external music player, if any */
   147 	Mix_SetMusicCMD(getenv("MUSIC_CMD"));
   148 
   149 	/* Load the requested music file */
   150 	music = Mix_LoadMUS(argv[i]);
   151 	if ( music == NULL ) {
   152 		fprintf(stderr, "Couldn't load %s: %s\n",
   153 						argv[i], SDL_GetError());
   154 		exit(2);
   155 	}
   156 
   157 	/* Play and then exit */
   158 	Mix_FadeInMusic(music,looping,2000);
   159 	while ( Mix_PlayingMusic() || Mix_PausedMusic() ) {
   160 		if(interactive)
   161 			Menu();
   162 		else
   163 			SDL_Delay(100);
   164 	}
   165 	exit(0);
   166 }