File: mme_audio.c

package info (click to toggle)
imaze 1.4-12
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 1,692 kB
  • ctags: 1,294
  • sloc: ansic: 14,086; sh: 305; makefile: 238
file content (297 lines) | stat: -rw-r--r-- 6,943 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
** - - -  iMaze  - - -
**
** Copyright (c) 1993-2001 by Hans-Ulrich Kiel & Joerg Czeranski
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** 1. Redistributions of source code must retain the above copyright
**    notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
** 3. The name of the authors may not be used to endorse or promote
**    products derived from this software without specific prior written
**    permission.
** 4. The name ``iMaze'' may not be used for products derived from this
**    software unless a prefix or a suffix is added to the name.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
** INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
** IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
**
**
** File: mme_audio.c
**
** Comment:
**  Plays sounds on DECs via the MME server
*/

#include <stdlib.h>
#include <string.h>
#include <mme/mme_api.h>

#include "argv.h"
#include "fehler.h"
#include "system.h"
#include "ereignisse.h"
#include "audio.h"
#include "audio_hw.h"

static char sccsid[] = "@(#)mme_audio.c	3.12 12/03/01";


static int device_name_callback(char *name);


struct arg_option audio_hw_opts[] =
{
	{ Arg_Callback, "D", (void *)device_name_callback, "set MME device",
		"number" },
	{ Arg_End }
};


/* samples per second */
#define SAMPLING_RATE 8000

#define	SAMPLE_BLOCK_SIZE (SAMPLING_RATE / 10)

static int audio_descriptor; /* descriptor for the pipe to the mmeserver */

static int blocks_pending; /* number of blocks sent to the mmeserver*/
static int next_block; /* index for block/block_header */

static UINT device = WAVE_MAPPER;

static HWAVEOUT wo_handle;
static HPSTR block[2];
static LPWAVEHDR block_header[2];


static void audio_error(char *text)
{
	static char *message[] = { "iMaze - Sound Error", "", NULL, NULL };

	message[2] = text;
	uebler_fehler(message, NULL);
}


static void wave_callback(HANDLE wo_handle, UINT msg, DWORD instance,
	LPARAM par1, LPARAM par2)
{
	if (msg == MM_WOM_DONE)
		blocks_pending--;
}


static void audio_callback(void)
{
	while (mmeCheckForCallbacks())
		mmeProcessCallbacks();
}


static int device_name_callback(char *name)
{
	char *end_p;
	unsigned long dev;

	dev = strtoul(name, &end_p, 10);
	if (dev >= 10000 || *end_p != 0)
		return 1; /* error */

	device = dev;
	return 0; /* success */
}


/* up to here local part                       */
/***********************************************/
/* from here on global part                    */


void audio_init(void)
{
	blocks_pending = 0;
}


void audio_hw_start(void)
{
}


void audio_hw_close(void)
{
	audio_select_descriptor(audio_descriptor, (void (*)(void))NULL, 0);

	if (blocks_pending)
	{
		if (waveOutReset(wo_handle) != MMSYSERR_NOERROR)
			audio_error("Can't reset MME");
	}

	if (waveOutClose(wo_handle) != MMSYSERR_NOERROR)
		audio_error("Can't close MME");
}


int audio_hw_protect(void)
{
	/* not needed, we don't use signals */
	return 0;
}


void audio_hw_unprotect(int state)
{
	/* not needed, we don't use signals */
}


int audio_hw_is_busy(void)
{
	return blocks_pending > 1; /* don't write more than two blocks */
}


int audio_hw_is_multiwrite_allowed(void)
{
	return 1; /* always try to write more than once */
}


int audio_hw_write(unsigned char *buffer, int length)
{
	MMRESULT err;

	if (length < 1 || blocks_pending > 1)
		return -1;

	if (length > SAMPLE_BLOCK_SIZE)
		length = SAMPLE_BLOCK_SIZE;

	memcpy(block[next_block], buffer, length);
	block_header[next_block]->dwBufferLength = length;

	if ((err = waveOutWrite(wo_handle, block_header[next_block],
		sizeof *block_header[next_block])) != MMSYSERR_NOERROR)
		audio_error("Can't send to MME");

	blocks_pending++;
	next_block = (next_block + 1) % 2;

	audio_callback(); /* don't forget events */

	return length;
}


void audio_hw_hint_waiting(void)
{
}


void audio_hw_hint_not_waiting(void)
{
}


int audio_open(char ***message_p)
{
	LPWAVEFORMAT wo_format;
	MMRESULT err;
	static char *message[] = { "iMaze - Sound Error", "", NULL, NULL };

	if (waveOutGetNumDevs() == 0)
	{
		message[2] = "No MME server or audio device present";
		*message_p = message;

		return 0; /* fail */
	}

	wo_format = mmeAllocMem(sizeof *wo_format);
	if (wo_format == NULL)
		audio_error("Can't allocate MME memory");

	memset(wo_format, 0, sizeof *wo_format);
	wo_format->wFormatTag = WAVE_FORMAT_MULAW;
	wo_format->nChannels = 1;
	wo_format->nSamplesPerSec = SAMPLING_RATE;
	wo_format->nAvgBytesPerSec = SAMPLING_RATE;
	wo_format->nBlockAlign = 1;

	if ((err = waveOutOpen(&wo_handle, device, wo_format,
		wave_callback, 0, CALLBACK_FUNCTION | WAVE_OPEN_SHAREABLE)) !=
		MMSYSERR_NOERROR)
	{
		message[2] = "Can't open MME device";
		*message_p = message;

		return err == MMSYSERR_ALLOCATED ? 2 : 0;
	}

	audio_descriptor = mmeServerFileDescriptor();

	block[0] = mmeAllocBuffer(SAMPLE_BLOCK_SIZE);
	if (block[0] == NULL)
		audio_error("Can't allocate MME memory");

	block[1] = mmeAllocBuffer(SAMPLE_BLOCK_SIZE);
	if (block[1] == NULL)
		audio_error("Can't allocate MME memory");

	block_header[0] = mmeAllocMem(sizeof *block_header[0]);
	if (block_header[0] == NULL)
		audio_error("Can't allocate MME memory");

	block_header[1] = mmeAllocMem(sizeof *block_header[1]);
	if (block_header[1] == NULL)
		audio_error("Can't allocate MME memory");

	memset(block_header[0], 0, sizeof *block_header[0]);
	block_header[0]->lpData = block[0];
	block_header[0]->dwBufferLength = SAMPLE_BLOCK_SIZE;
	block_header[0]->dwFlags = 0;
	block_header[0]->dwLoops = 0;

	memset(block_header[1], 0, sizeof *block_header[1]);
	block_header[1]->lpData = block[1];
	block_header[1]->dwBufferLength = SAMPLE_BLOCK_SIZE;
	block_header[1]->dwFlags = 0;
	block_header[1]->dwLoops = 0;

	blocks_pending = 0;
	next_block = 0;
	audio_select_descriptor(audio_descriptor, audio_callback, 0);

	audio_callback(); /* don't forget events */

	return 1; /* success */
}


int audio_hw_get_buffer_size(void)
{
	return SAMPLE_BLOCK_SIZE;
}


int audio_hw_uses_ulaw(void)
{
	return 1; /* yes */
}