aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sfx/pcm_device/sdl.cpp
blob: c4c47ed2a1778411de4503b9f2231332f817e589 (plain)
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
/***************************************************************************
 sdl.c Copyright (C) 2002 Solomon Peachy, Claudio Matsuoka,
		     2003,04  Christoph Reichenbach

 This program may be modified and copied freely according to the terms of
 the GNU general public license (GPL), as long as the above copyright
 notice and the licensing information contained herein are preserved.

 Please refer to www.gnu.org for licensing details.

 This work is provided AS IS, without warranty of any kind, expressed or
 implied, including but not limited to the warranties of merchantibility,
 noninfringement, and fitness for a specific purpose. The author will not
 be held liable for any damage caused by this work or derivatives of it.

 By using this source code, you agree to the licensing terms as stated
 above.

***************************************************************************/

#include <sfx_pcm.h>
#include "audiobuf.h"

#ifdef HAVE_SDL

#if !defined(_MSC_VER)
#  include <sys/time.h>
#endif

#include <SDL_config.h>
#undef HAVE_ICONV
#undef HAVE_ICONV_H
#undef HAVE_ALLOCA_H

#include <SDL.h>

#define DELTA_TIME_LIMIT 10000 /* Report errors above this delta time */

static sfx_audio_buf_t audio_buffer;
static void (*sdl_sfx_timer_callback)(void *data);
static void *sdl_sfx_timer_data;
static int frame_size;
static int buf_size;
static int rate;
static long last_callback_secs, last_callback_usecs;
static int last_callback_len;

static sfx_timestamp_t
pcmout_sdl_output_timestamp(sfx_pcm_device_t *self) {
	/* Number of frames enqueued in the output device: */
	int delta = (buf_size - last_callback_len) / frame_size
	            /* Number of frames enqueued in the internal audio buffer: */
	            + audio_buffer.frames_nr;

	return sfx_timestamp_add(sfx_new_timestamp(last_callback_secs,
	                         last_callback_usecs,
	                         rate),
	                         delta);
}

FILE *fil = NULL;

static void
timer_sdl_internal_callback(void *userdata, byte *dest, int len) {
	sci_gettime(&last_callback_secs, &last_callback_usecs);
	last_callback_len = len;

	if (sdl_sfx_timer_callback)
		sdl_sfx_timer_callback(sdl_sfx_timer_data);

#if 0
	if (!sfx_audbuf_read_timestamp(&audio_buffer, &ts)) {
		int delta = (buf_size - len) / frame_size;
		sfx_timestamp_t real_ts;
		long deltatime;
		long sec2, usec2;
		sci_gettime(&sec2, &usec2);

		real_ts = sfx_timestamp_add(sfx_new_timestamp(sec, usec, rate), delta);

		deltatime = sfx_timestamp_usecs_diff(ts, real_ts);

		fprintf(stderr, "[SDL] Frames requested: %d  Playing %ld too late. Needed %ldus for computations.\n",
		        len / frame_size, deltatime,
		        (usec2 - usec) + (sec2 - sec)*1000000);

		if (abs(deltatime) > DELTA_TIME_LIMIT)
			sciprintf("[SND:SDL] Very high delta time for PCM playback: %ld too late (%d frames in the future)\n",
			          deltatime, sfx_timestamp_frame_diff(sfx_new_timestamp(sec, usec, rate), ts));

#if 0
		if (deltatime < 0) {
			/* Read and discard frames, explicitly underrunning */
			int max_read = len / frame_size;
			int frames_to_kill = sfx_timestamp_frame_diff(real_ts, ts);

			while (frames_to_kill) {
				int d = frames_to_kill > max_read ? max_read : frames_to_kill;
				sfx_audbuf_read(&audio_buffer, dest, d);
				frames_to_kill -= d;
			}
		}
#endif
	}
#endif
	sfx_audbuf_read(&audio_buffer, dest, len / frame_size);

#if 0
	if (!fil) {
		fil = fopen("/tmp/sdl.log", "w");
	}
	{
		int i;
		int end = len / frame_size;
		gint16 *d = dest;
		fprintf(fil, "Writing %d/%d\n", len, frame_size);
		for (i = 0; i < end; i++) {
			fprintf(fil, "\t%d\t%d\n", d[0], d[1]);
			d += 2;
		}
	}
#endif
}


static int
pcmout_sdl_init(sfx_pcm_device_t *self) {
	SDL_AudioSpec a;

	if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) != 0) {
		fprintf(stderr, "[SND:SDL] Error while initialising: %s\n", SDL_GetError());
		return -1;
	}

	a.freq = 44100; /* FIXME */
#ifdef WORDS_BIGENDIAN
	a.format = AUDIO_S16MSB; /* FIXME */
#else
	a.format = AUDIO_S16LSB; /* FIXME */
#endif
	a.channels = 2; /* FIXME */
	a.samples = 2048; /* FIXME */
	a.callback = timer_sdl_internal_callback;
	a.userdata = NULL;

	if (SDL_OpenAudio(&a, NULL) < 0) {
		fprintf(stderr, "[SND:SDL] Error while opening audio: %s\n", SDL_GetError());
		return SFX_ERROR;
	}

	buf_size = a.samples;
	rate = a.freq;

	self->buf_size = a.samples << 1; /* Looks like they're using double size */
	self->conf.rate = a.freq;
	self->conf.stereo = a.channels > 1;
	self->conf.format = SFX_PCM_FORMAT_S16_NATIVE;

	frame_size = SFX_PCM_FRAME_SIZE(self->conf);

	sfx_audbuf_init(&audio_buffer, self->conf);
	SDL_PauseAudio(0);

	return SFX_OK;
}

int
pcmout_sdl_output(sfx_pcm_device_t *self, byte *buf,
                  int count, sfx_timestamp_t *ts) {
	if (ts)
		sfx_audbuf_write_timestamp(&audio_buffer, *ts);
	sfx_audbuf_write(&audio_buffer, buf, count);
	return SFX_OK;
}


static int
pcmout_sdl_set_option(sfx_pcm_device_t *self, char *name, char *value) {
	return SFX_ERROR; /* Option not supported */
}

static void
pcmout_sdl_exit(sfx_pcm_device_t *self) {
	SDL_PauseAudio(1);
	SDL_CloseAudio();
	sfx_audbuf_free(&audio_buffer);

	SDL_QuitSubSystem(SDL_INIT_AUDIO);

	if (!SDL_WasInit(SDL_INIT_EVERYTHING)) {
		sciprintf("[SND:SDL] No active SDL subsystems found.. shutting down SDL\n");
		SDL_Quit();
	}
}


static int
timer_sdl_set_option(char *name, char *value) {
	return SFX_ERROR;
}


static int
timer_sdl_init(void (*callback)(void *data), void *data) {
	sdl_sfx_timer_callback = callback;
	sdl_sfx_timer_data = data;


	return SFX_OK;
}

static int
timer_sdl_stop(void) {
	sdl_sfx_timer_callback = NULL;

	return SFX_OK;
}

static int
timer_sdl_block(void) {
	SDL_LockAudio();

	return SFX_OK;
}

static int
timer_sdl_unblock(void) {
	SDL_UnlockAudio();

	return SFX_OK;
}

#define SDL_PCM_VERSION "0.1"

sfx_timer_t pcmout_sdl_timer = {
	"sdl-pcm-timer",
	SDL_PCM_VERSION,
	0,
	0,
	timer_sdl_set_option,
	timer_sdl_init,
	timer_sdl_stop,
	timer_sdl_block,
	timer_sdl_unblock
};

sfx_pcm_device_t sfx_pcm_driver_sdl = {
	"sdl",
	SDL_PCM_VERSION,
	pcmout_sdl_init,
	pcmout_sdl_exit,
	pcmout_sdl_set_option,
	pcmout_sdl_output,
	pcmout_sdl_output_timestamp,
	{0, 0, 0},
	0,
	&pcmout_sdl_timer,
	NULL
};

#endif