File: glue-audio.c

package info (click to toggle)
faumachine 20180503-4
  • links: PTS
  • area: main
  • in suites: buster
  • size: 61,272 kB
  • sloc: ansic: 272,290; makefile: 6,199; asm: 4,251; sh: 3,022; perl: 886; xml: 563; pascal: 311; lex: 214; vhdl: 204
file content (299 lines) | stat: -rw-r--r-- 5,355 bytes parent folder | download
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
298
299
/*
 * Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */
#include "config.h"

#include <sys/time.h>
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include "glue-audio_driver.h"
#include "glue-log.h"
#include "glue-main.h"

#include "sig_sound.h"	/* FIXME */

static const char *desired_audio_module = NULL;


/* currently implemented drivers */

#ifdef USE_OSS_AUDIO
extern ao_functions_t audio_out_oss;
#endif
#ifdef USE_ESD
extern ao_functions_t audio_out_esd;
#endif
#ifdef USE_PULSE
extern ao_functions_t audio_out_pulse;
#endif
#ifdef HAVE_ALSA5
extern ao_functions_t audio_out_alsa5;
#endif
#ifdef HAVE_ALSA9
extern ao_functions_t audio_out_alsa9;
#endif
#ifdef HAVE_ALSA1X
extern ao_functions_t audio_out_alsa1x;
#endif
#ifdef USE_MACOSX_AUDIO
extern ao_functions_t audio_out_macosx;
#endif
extern ao_functions_t audio_out_null;
extern ao_functions_t audio_out_file;

ao_functions_t* audio_out_drivers[] =
{
// wrappers:
#ifdef USE_PULSE
	&audio_out_pulse,
#endif
#ifdef USE_ESD
	&audio_out_esd,
#endif

// native:
#ifdef USE_MACOSX_AUDIO
	&audio_out_macosx,
#endif
#ifdef HAVE_ALSA1X
	&audio_out_alsa1x,
#endif
#ifdef HAVE_ALSA9
	&audio_out_alsa9,
#endif
#ifdef HAVE_ALSA5
	&audio_out_alsa5,
#endif
#ifdef USE_OSS_AUDIO
        &audio_out_oss,
#endif
        &audio_out_null,
	&audio_out_file,
	NULL
};

ao_functions_t *audio_out = NULL;

static enum { STALLED, PLAYING } state;
static uint8_t buffer[0x40000];
static unsigned int bufsize;
static unsigned long long next_event;


static void
audio_flush(void)
{
	unsigned int count;
	int ret;

	switch (state) {
	case STALLED:
		if (bufsize < sizeof(buffer) / 2) {
			return;
		}
		// fprintf(stderr, "%s: started\n", __FUNCTION__);
		state = PLAYING;
		break;

	case PLAYING:
		if (audio_out->get_delay() == 0.0) {
			// fprintf(stderr, "%s: stopped\n", __FUNCTION__);
			state = STALLED;
			return;
		}
		break;

	default:
		assert(0);
	}

	count = audio_out->get_space();
	if (bufsize < count) {
		count = bufsize;
	}
	if (0 < count) {
		ret = audio_out->play(buffer, count);
		assert(0 <= ret
		    && ret <= count);

		memmove(buffer, &buffer[ret], bufsize - ret);
		bufsize -= ret;
	}
}

void
audio_play(int16_t *buf)
{
	if (! audio_out) {
		/* We don't have any audio output device. */
		return;
	}

	if (bufsize + SIG_SOUND_MTU < sizeof(buffer)) {
		memcpy(&buffer[bufsize], buf, SIG_SOUND_MTU);
		bufsize += SIG_SOUND_MTU;
	} else {
		/* Skip bytes! */
		// fprintf(stderr, "%s: skipping frame.\n", __FUNCTION__);
	}

	audio_flush();
}

extern void
audio_event(void *s)
{
	audio_flush();

	next_event += TIME_HZ / 128;
	time_call_at(next_event, audio_event, (void *) 0);
}

void
audio_init(void)
{
	if (audio_out) {
		next_event = 0;
		time_call_at(next_event, audio_event, (void *) 0);
	}
}

void
audio_exit(void)
{
}

static sigjmp_buf audio_create_state;

static void __attribute__((__noreturn__))
audio_create_break(int sig)
{
	audio_out = NULL;

	siglongjmp(audio_create_state, 1);
}

void
audio_create(void)
{
	struct sigaction sa, osa;
	struct itimerval it, oit;
	int i;
	int ret;

	/*
	 * Abort audio initialization after about 1sec.
	 */
	if (sigsetjmp(audio_create_state, 1) != 0) {
		goto abort;
	}

	/*
	 * Set alarm signal handler and real-time timer.
	 */
	sa.sa_handler = audio_create_break;
	sa.sa_flags = 0;
	sigfillset(&sa.sa_mask);
	ret = sigaction(SIGALRM, &sa, &osa);
	assert(0 <= ret);

	it.it_interval.tv_sec = 0;
	it.it_interval.tv_usec = 0;
	it.it_value.tv_sec = 5; 
	it.it_value.tv_usec = 0;
	ret = setitimer(ITIMER_REAL, &it, &oit);
	assert(0 <= ret);

	/* Try opening audio device. */
	for (i = 0; audio_out_drivers[i]; i++) {
		if ((desired_audio_module == NULL
		  || strcmp(audio_out_drivers[i]->info->short_name,
			       desired_audio_module) == 0)
		 && audio_out_drivers[i]->init()) {
			audio_out = audio_out_drivers[i];
			break;
		}
	}

abort:	;
	/*
	 * Restore real-time timer and alarm signal handler.
	 */
	ret = setitimer(ITIMER_REAL, &oit, (struct itimerval *) 0);
	assert(0 <= ret);

	ret = sigaction(SIGALRM, &osa, (struct sigaction *) 0);
	assert(0 <= ret);

	if (audio_out) {
		fprintf(stderr, "INFO: Using %s.\n", audio_out->info->name);
	} else {
		fprintf(stderr, "ERROR: %s.\n",
				"Can't open any audio output device.\n");
		return;
	}

	state = STALLED;
	bufsize = 0;
}

void
audio_destroy(void)
{
	if (audio_out) {
		audio_out->uninit(0);
		audio_out = NULL;
	}
}

void
audio_usage(void)
{
	fprintf(stderr, "\t-a <audio_module>: use <audio_module> during simulation\n");
}

int
audio_handle_args(int *argc, char **argv)
{
	int na = *argc;

	desired_audio_module = getenv("FAUM_AUDIO");

	while (0 < na) {
		assert(argv != NULL);
		if (*argv
		 && strcmp(*argv, "-a") == 0) {
			(*argc)--;
			na--;

			if (*(argv + 1) == NULL) {
				return -1;
			}

			desired_audio_module = *(argv + 1);
			(*argc)--;
			na--;

			/* remove these two arguments. */
			memmove(argv, argv + 2, 
				(na + 1)  * sizeof(char *));
			continue;
		}

		na--;
		argv++;
	}

	return 0;
}