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()
slouken@0
     1
/*
slouken@0
     2
    PLAYWAVE:  A test application for the SDL mixer library.
slouken@0
     3
    Copyright (C) 1997-1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This program is free software; you can redistribute it and/or modify
slouken@0
     6
    it under the terms of the GNU General Public License as published by
slouken@0
     7
    the Free Software Foundation; either version 2 of the License, or
slouken@0
     8
    (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This program is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
slouken@0
    13
    GNU General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU General Public License
slouken@0
    16
    along with this program; if not, write to the Free Software
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
slouken@0
    25
#include <stdlib.h>
slouken@0
    26
#include <stdio.h>
slouken@0
    27
#include <string.h>
slouken@0
    28
#include <signal.h>
slouken@47
    29
#ifdef unix
slouken@0
    30
#include <unistd.h>
slouken@0
    31
#endif
slouken@0
    32
slouken@24
    33
#include "SDL.h"
slouken@34
    34
#include "SDL_mixer.h"
slouken@0
    35
slouken@0
    36
slouken@0
    37
static int audio_open = 0;
slouken@0
    38
static Mix_Chunk *wave = NULL;
slouken@0
    39
slouken@0
    40
void CleanUp(void)
slouken@0
    41
{
megastep@21
    42
	if ( wave ) {
megastep@21
    43
		Mix_FreeChunk(wave);
megastep@21
    44
		wave = NULL;
megastep@21
    45
	}
slouken@0
    46
	if ( audio_open ) {
slouken@0
    47
		Mix_CloseAudio();
slouken@0
    48
		audio_open = 0;
slouken@0
    49
	}
slouken@0
    50
	SDL_Quit();
slouken@0
    51
}
slouken@0
    52
slouken@0
    53
void Usage(char *argv0)
slouken@0
    54
{
megastep@21
    55
	fprintf(stderr, "Usage: %s [-8] [-r rate] [-l] [-m] <wavefile>\n", argv0);
slouken@0
    56
}
slouken@92
    57
slouken@92
    58
slouken@92
    59
/*#define TEST_MIX_CHANNELFINISHED*/
slouken@92
    60
#ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
slouken@92
    61
static volatile int channel_is_done = 0;
slouken@92
    62
static void channel_complete_callback(int chan)
slouken@92
    63
{
slouken@92
    64
	Mix_Chunk *done_chunk = Mix_GetChunk(chan);
slouken@92
    65
	printf("We were just alerted that Mixer channel #%d is done.\n", chan);
slouken@92
    66
	printf("Channel's chunk pointer is (%p).\n", done_chunk);
slouken@92
    67
	printf(" Which %s correct.\n", (wave == done_chunk) ? "is" : "is NOT");
slouken@92
    68
	channel_is_done = 1;
slouken@92
    69
}
slouken@92
    70
#endif
slouken@92
    71
slouken@92
    72
slouken@0
    73
main(int argc, char *argv[])
slouken@0
    74
{
slouken@47
    75
	int audio_rate;
slouken@0
    76
	Uint16 audio_format;
slouken@0
    77
	int audio_channels;
megastep@21
    78
	int loops = 0;
slouken@0
    79
	int i;
slouken@0
    80
slouken@0
    81
	/* Initialize variables */
slouken@70
    82
	audio_rate = MIX_DEFAULT_FREQUENCY;
slouken@70
    83
	audio_format = MIX_DEFAULT_FORMAT;
slouken@0
    84
	audio_channels = 2;
slouken@0
    85
slouken@0
    86
	/* Check command line usage */
slouken@0
    87
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@0
    88
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@0
    89
			++i;
slouken@0
    90
			audio_rate = atoi(argv[i]);
slouken@0
    91
		} else
slouken@0
    92
		if ( strcmp(argv[i], "-m") == 0 ) {
slouken@0
    93
			audio_channels = 1;
slouken@0
    94
		} else
megastep@21
    95
		if ( strcmp(argv[i], "-l") == 0 ) {
megastep@21
    96
			loops = -1;
megastep@21
    97
		} else
slouken@0
    98
		if ( strcmp(argv[i], "-8") == 0 ) {
slouken@0
    99
			audio_format = AUDIO_U8;
slouken@0
   100
		} else {
slouken@0
   101
			Usage(argv[0]);
slouken@83
   102
			return(1);
slouken@0
   103
		}
slouken@0
   104
	}
slouken@0
   105
	if ( ! argv[i] ) {
slouken@0
   106
		Usage(argv[0]);
slouken@83
   107
		return(1);
slouken@0
   108
	}
slouken@0
   109
slouken@0
   110
	/* Initialize the SDL library */
slouken@0
   111
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
   112
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@83
   113
		return(255);
slouken@0
   114
	}
slouken@0
   115
	atexit(CleanUp);
slouken@0
   116
	signal(SIGINT, exit);
slouken@0
   117
	signal(SIGTERM, exit);
slouken@0
   118
slouken@0
   119
	/* Open the audio device */
slouken@0
   120
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
slouken@0
   121
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@83
   122
		return(2);
slouken@0
   123
	} else {
slouken@0
   124
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
megastep@21
   125
		printf("Opened audio at %d Hz %d bit %s", audio_rate,
slouken@0
   126
			(audio_format&0xFF),
slouken@0
   127
			(audio_channels > 1) ? "stereo" : "mono");
megastep@21
   128
		if ( loops ) {
megastep@21
   129
		  printf(" (looping)\n");
megastep@21
   130
		} else {
megastep@21
   131
		  putchar('\n');
megastep@21
   132
		}
slouken@0
   133
	}
slouken@0
   134
	audio_open = 1;
slouken@0
   135
slouken@0
   136
	/* Load the requested wave file */
slouken@0
   137
	wave = Mix_LoadWAV(argv[i]);
slouken@0
   138
	if ( wave == NULL ) {
slouken@0
   139
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
   140
						argv[i], SDL_GetError());
slouken@83
   141
		return(2);
slouken@0
   142
	}
slouken@0
   143
slouken@92
   144
#ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
slouken@92
   145
	setbuf(stdout, NULL);
slouken@92
   146
	Mix_ChannelFinished(channel_complete_callback);
slouken@92
   147
#endif
slouken@92
   148
slouken@0
   149
	/* Play and then exit */
megastep@21
   150
	Mix_PlayChannel(0, wave, loops);
slouken@92
   151
slouken@92
   152
#ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
slouken@92
   153
	while (!channel_is_done) {
slouken@92
   154
		SDL_Delay(100);
slouken@92
   155
	}
slouken@92
   156
#else
slouken@0
   157
	while ( Mix_Playing(0) ) {
slouken@0
   158
		SDL_Delay(100);
slouken@0
   159
	}
slouken@92
   160
#endif
slouken@92
   161
slouken@83
   162
	return(0);
slouken@0
   163
}