playwave.c
author Sam Lantinga <slouken@lokigames.com>
Sun, 10 Jun 2001 04:48:15 +0000
changeset 92 2a30d721bd79
parent 83 c95d43cc6d38
child 113 c0c3018bd787
permissions -rw-r--r--
Ryan C. Gordon - Thu, 7 Jun 2001 13:15:51
* Added Mix_ChannelFinished() and Mix_GetChunk()
     1 /*
     2     PLAYWAVE:  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 #ifdef unix
    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_Chunk *wave = NULL;
    39 
    40 void CleanUp(void)
    41 {
    42 	if ( wave ) {
    43 		Mix_FreeChunk(wave);
    44 		wave = NULL;
    45 	}
    46 	if ( audio_open ) {
    47 		Mix_CloseAudio();
    48 		audio_open = 0;
    49 	}
    50 	SDL_Quit();
    51 }
    52 
    53 void Usage(char *argv0)
    54 {
    55 	fprintf(stderr, "Usage: %s [-8] [-r rate] [-l] [-m] <wavefile>\n", argv0);
    56 }
    57 
    58 
    59 /*#define TEST_MIX_CHANNELFINISHED*/
    60 #ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
    61 static volatile int channel_is_done = 0;
    62 static void channel_complete_callback(int chan)
    63 {
    64 	Mix_Chunk *done_chunk = Mix_GetChunk(chan);
    65 	printf("We were just alerted that Mixer channel #%d is done.\n", chan);
    66 	printf("Channel's chunk pointer is (%p).\n", done_chunk);
    67 	printf(" Which %s correct.\n", (wave == done_chunk) ? "is" : "is NOT");
    68 	channel_is_done = 1;
    69 }
    70 #endif
    71 
    72 
    73 main(int argc, char *argv[])
    74 {
    75 	int audio_rate;
    76 	Uint16 audio_format;
    77 	int audio_channels;
    78 	int loops = 0;
    79 	int i;
    80 
    81 	/* Initialize variables */
    82 	audio_rate = MIX_DEFAULT_FREQUENCY;
    83 	audio_format = MIX_DEFAULT_FORMAT;
    84 	audio_channels = 2;
    85 
    86 	/* Check command line usage */
    87 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
    88 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
    89 			++i;
    90 			audio_rate = atoi(argv[i]);
    91 		} else
    92 		if ( strcmp(argv[i], "-m") == 0 ) {
    93 			audio_channels = 1;
    94 		} else
    95 		if ( strcmp(argv[i], "-l") == 0 ) {
    96 			loops = -1;
    97 		} else
    98 		if ( strcmp(argv[i], "-8") == 0 ) {
    99 			audio_format = AUDIO_U8;
   100 		} else {
   101 			Usage(argv[0]);
   102 			return(1);
   103 		}
   104 	}
   105 	if ( ! argv[i] ) {
   106 		Usage(argv[0]);
   107 		return(1);
   108 	}
   109 
   110 	/* Initialize the SDL library */
   111 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   112 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   113 		return(255);
   114 	}
   115 	atexit(CleanUp);
   116 	signal(SIGINT, exit);
   117 	signal(SIGTERM, exit);
   118 
   119 	/* Open the audio device */
   120 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
   121 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   122 		return(2);
   123 	} else {
   124 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   125 		printf("Opened audio at %d Hz %d bit %s", audio_rate,
   126 			(audio_format&0xFF),
   127 			(audio_channels > 1) ? "stereo" : "mono");
   128 		if ( loops ) {
   129 		  printf(" (looping)\n");
   130 		} else {
   131 		  putchar('\n');
   132 		}
   133 	}
   134 	audio_open = 1;
   135 
   136 	/* Load the requested wave file */
   137 	wave = Mix_LoadWAV(argv[i]);
   138 	if ( wave == NULL ) {
   139 		fprintf(stderr, "Couldn't load %s: %s\n",
   140 						argv[i], SDL_GetError());
   141 		return(2);
   142 	}
   143 
   144 #ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
   145 	setbuf(stdout, NULL);
   146 	Mix_ChannelFinished(channel_complete_callback);
   147 #endif
   148 
   149 	/* Play and then exit */
   150 	Mix_PlayChannel(0, wave, loops);
   151 
   152 #ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
   153 	while (!channel_is_done) {
   154 		SDL_Delay(100);
   155 	}
   156 #else
   157 	while ( Mix_Playing(0) ) {
   158 		SDL_Delay(100);
   159 	}
   160 #endif
   161 
   162 	return(0);
   163 }