playwave.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 05 Oct 2000 00:52:16 +0000
changeset 70 8ce14b7f7f6f
parent 47 103ac6380537
child 83 c95d43cc6d38
permissions -rw-r--r--
Use the default audio format for this endianness
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@0
    57
	
slouken@0
    58
main(int argc, char *argv[])
slouken@0
    59
{
slouken@47
    60
	int audio_rate;
slouken@0
    61
	Uint16 audio_format;
slouken@0
    62
	int audio_channels;
megastep@21
    63
	int loops = 0;
slouken@0
    64
	int i;
slouken@0
    65
slouken@0
    66
	/* Initialize variables */
slouken@70
    67
	audio_rate = MIX_DEFAULT_FREQUENCY;
slouken@70
    68
	audio_format = MIX_DEFAULT_FORMAT;
slouken@0
    69
	audio_channels = 2;
slouken@0
    70
slouken@0
    71
	/* Check command line usage */
slouken@0
    72
	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
slouken@0
    73
		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
slouken@0
    74
			++i;
slouken@0
    75
			audio_rate = atoi(argv[i]);
slouken@0
    76
		} else
slouken@0
    77
		if ( strcmp(argv[i], "-m") == 0 ) {
slouken@0
    78
			audio_channels = 1;
slouken@0
    79
		} else
megastep@21
    80
		if ( strcmp(argv[i], "-l") == 0 ) {
megastep@21
    81
			loops = -1;
megastep@21
    82
		} else
slouken@0
    83
		if ( strcmp(argv[i], "-8") == 0 ) {
slouken@0
    84
			audio_format = AUDIO_U8;
slouken@0
    85
		} else {
slouken@0
    86
			Usage(argv[0]);
slouken@0
    87
			exit(1);
slouken@0
    88
		}
slouken@0
    89
	}
slouken@0
    90
	if ( ! argv[i] ) {
slouken@0
    91
		Usage(argv[0]);
slouken@0
    92
		exit(1);
slouken@0
    93
	}
slouken@0
    94
slouken@0
    95
	/* Initialize the SDL library */
slouken@0
    96
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
    97
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
    98
		exit(255);
slouken@0
    99
	}
slouken@0
   100
	atexit(CleanUp);
slouken@0
   101
	signal(SIGINT, exit);
slouken@0
   102
	signal(SIGTERM, exit);
slouken@0
   103
slouken@0
   104
	/* Open the audio device */
slouken@0
   105
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
slouken@0
   106
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
   107
		exit(2);
slouken@0
   108
	} else {
slouken@0
   109
		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
megastep@21
   110
		printf("Opened audio at %d Hz %d bit %s", audio_rate,
slouken@0
   111
			(audio_format&0xFF),
slouken@0
   112
			(audio_channels > 1) ? "stereo" : "mono");
megastep@21
   113
		if ( loops ) {
megastep@21
   114
		  printf(" (looping)\n");
megastep@21
   115
		} else {
megastep@21
   116
		  putchar('\n');
megastep@21
   117
		}
slouken@0
   118
	}
slouken@0
   119
	audio_open = 1;
slouken@0
   120
slouken@0
   121
	/* Load the requested wave file */
slouken@0
   122
	wave = Mix_LoadWAV(argv[i]);
slouken@0
   123
	if ( wave == NULL ) {
slouken@0
   124
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
   125
						argv[i], SDL_GetError());
slouken@0
   126
		exit(2);
slouken@0
   127
	}
slouken@0
   128
slouken@0
   129
	/* Play and then exit */
megastep@21
   130
	Mix_PlayChannel(0, wave, loops);
slouken@0
   131
	while ( Mix_Playing(0) ) {
slouken@0
   132
		SDL_Delay(100);
slouken@0
   133
	}
slouken@0
   134
	exit(0);
slouken@0
   135
}