File: playwave.cpp

package info (click to toggle)
maelstrom 3.0.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,952 kB
  • sloc: cpp: 10,947; sh: 3,406; ansic: 2,781; makefile: 175
file content (174 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
    PLAYWAVE:  A WAVE file player using the maclib and SDL libraries
    Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* Very simple WAVE player */

#include <signal.h>
#include <string.h>
#include <stdlib.h>

#include "SDL.h"
#include "SDL_audio.h"
#include "SDL_mutex.h"
#include "Mac_Wave.h"

static SDL_mutex *done;
static Wave *wave;
static Uint8 silence;

void fillerup(void *unused, Uint8 *stream, int len)
{
	Sint32 waveleft;

	/* Are we done? */
	if ( wave->DataLeft() == 0 ) {
		memset(stream, silence, len);
		SDL_mutexV(done);
		return;
	}

	/* Fill streaming buffer */
	waveleft = wave->DataLeft();
	if ( waveleft > len )
		waveleft = len;
	memcpy(stream, wave->Data(), waveleft);
	wave->Forward(waveleft);
}

void CleanUp(int status)
{
	SDL_CloseAudio();
	SDL_DestroyMutex(done);
	SDL_Quit();
	delete wave;
	exit(status);
}

int main(int argc, char *argv[])
{
	Mac_Resource *macx;
	Mac_ResData  *snd;
	SDL_AudioSpec *spec;
	Uint16        rate;

	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		exit(1);
	}
	rate = 0;
	if ( (argc >= 3) && (strcmp(argv[1], "-rate") == 0) ) {
		int i;
		rate = (Uint16)atoi(argv[2]);
		for ( i=3; argv[i]; ++i ) {
			argv[i-2] = argv[i];
		}
		argv[i-2] = NULL;
		argc -= 2;
	}
	switch (argc) {
		case 2:
			/* Load the wave file into memory */
			wave = new Wave(argv[1], rate);
			if ( wave->Error() ) {
				fprintf(stderr, "%s\n", wave->Error());
				delete wave;
				exit(255);
			}
			break;
		case 3:
			macx = new Mac_Resource(argv[1]);
			if ( macx->Error() ) {
				fprintf(stderr, "%s\n", macx->Error());
				delete macx;
				exit(255);
			}
			if ( (argv[2][0] >= '0') && (argv[2][0] <= '9') )
				snd = macx->Resource("snd ", atoi(argv[2]));
			else
				snd = macx->Resource("snd ", argv[2]);
			if ( snd == NULL ) {
				fprintf(stderr, "%s\n", macx->Error());
				delete macx;
				exit(255);
			}
			wave = new Wave(snd, rate);
			delete macx;
			if ( wave->Error() ) {
				fprintf(stderr, "%s\n", wave->Error());
				delete wave;
				exit(255);
			}
			break;
		default:
	fprintf(stderr, "Usage: %s [-rate <rate>] <wavefile>\n", argv[0]);
	fprintf(stderr, "or..\n");
	fprintf(stderr, "       %s [-rate <rate>] <snd_fork> [soundnum]\n",
								argv[0]);
			exit(1);
	}
	spec = wave->Spec();
	silence = ((spec->format&AUDIO_U8) ? 0x80 : 0x00);
	spec->callback = fillerup;

#ifdef SAVE_THE_WAVES
	if ( wave->Save("save.wav") < 0 )
		fprintf(stderr, "Warning: %s\n", wave->Error());
#endif

	/* Create a semaphore to wait for end of play */
	done=SDL_CreateMutex();
	if ( done == NULL ) {
		fprintf(stderr, "%s\n", SDL_GetError());
		SDL_Quit();
		exit(255);
	}
	SDL_mutexP(done);	/* Prime it for blocking */

	/* Set the signals */
#ifdef SIGHUP
	signal(SIGHUP, CleanUp);
#endif
	signal(SIGINT, CleanUp);
#ifdef SIGQUIT
	signal(SIGQUIT, CleanUp);
#endif
	signal(SIGTERM, CleanUp);

	/* Show what audio format we're playing */
	printf("Playing %#.2f seconds (%d bit %s) at %u Hz\n", 
		(double)(wave->DataLeft()/wave->SampleSize())/wave->Frequency(),
			wave->BitsPerSample(),
			wave->Stereo() ? "stereo" : "mono", wave->Frequency());

	/* Start the audio device */
	if ( SDL_OpenAudio(spec, NULL) < 0 ) {
		fprintf(stderr, "%s\n", SDL_GetError());
		CleanUp(255);
	}

	/* Let the audio run, waiting until finished */
	SDL_PauseAudio(0);
	SDL_mutexP(done);

	/* We're done! */
	CleanUp(0);

	/* Not reached */
	return 0;
}