aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sfx/pcm_device/audiobuf.cpp
blob: 15c5bb765bf08e21b70271f6465395d6c9deffeb (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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/***************************************************************************
 audiobuf.c Copyright (C) 2003 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.


 Please contact the maintainer for bug reports or inquiries.

 Current Maintainer:

    Christoph Reichenbach (CR) <jameson@linuxgames.com>

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

#include "audiobuf.h"

#define NO_BUFFER_UNDERRUN		0
#define SAW_BUFFER_UNDERRUN		1
#define REPORTED_BUFFER_UNDERRUN	2

static int
buffer_underrun_status = NO_BUFFER_UNDERRUN;


static sfx_audio_buf_chunk_t *
sfx_audbuf_alloc_chunk(void) {
	sfx_audio_buf_chunk_t *ch = (sfx_audio_buf_chunk_t*)sci_malloc(sizeof(sfx_audio_buf_chunk_t));
	ch->used = 0;
	ch->next = NULL;
	ch->prev = NULL;

	return ch;
}

void
sfx_audbuf_init(sfx_audio_buf_t *buf, sfx_pcm_config_t pcm_conf) {
	int framesize = SFX_PCM_FRAME_SIZE(pcm_conf);
	byte silence[16];
	int silencew = pcm_conf.format & ~SFX_PCM_FORMAT_LMASK;

	/* Determine the correct 'silence' for the channel and install it */
	/* Conservatively assume stereo */
	if (pcm_conf.format & SFX_PCM_FORMAT_16) {
		if (pcm_conf.format & SFX_PCM_FORMAT_LE) {
			silence[0] = silencew & 0xff;
			silence[1] = (silencew >> 8) & 0xff;
		} else {
			silence[0] = (silencew >> 8) & 0xff;
			silence[1] = silencew & 0xff;
		}
		memcpy(silence + 2, silence, 2);
	} else {
		silence[0] = silencew;
		silence[1] = silencew;
	}

	buf->last = buf->first = sfx_audbuf_alloc_chunk();
	buf->unused = NULL;
	memcpy(buf->last_frame, silence, framesize);	 /* Initialise, in case we
							 ** underrun before the
							 ** first write  */
	buf->read_offset = 0;
	buf->framesize = framesize;
	buf->read_timestamp.secs = -1; /* Mark as inactive */
	buf->frames_nr = 0;
}

static void
sfx_audbuf_free_chain(sfx_audio_buf_chunk_t *b) {
	while (b) {
		sfx_audio_buf_chunk_t *n = b->next;
		sci_free(b);
		b = n;
	}
}

void
sfx_audbuf_free(sfx_audio_buf_t *buf) {
	sfx_audbuf_free_chain(buf->first);
	sfx_audbuf_free_chain(buf->unused);
	buf->first = buf->last = buf->unused = NULL;
	buf->read_offset = (int) 0xdeadbeef;
}

void
sfx_audbuf_write(sfx_audio_buf_t *buf, unsigned char *src, int frames) {
	/* In here, we compute PER BYTE */
	int data_left = buf->framesize * frames;

	if (!buf->last) {
		fprintf(stderr, "FATAL: Violation of audiobuf.h usage protocol: Must use 'init' before 'write'\n");
		exit(1);
	}

	if (buffer_underrun_status == SAW_BUFFER_UNDERRUN) {
		/* Print here to avoid threadsafeness issues */
		sciprintf("[audiobuf] Buffer underrun\n");
		buffer_underrun_status = REPORTED_BUFFER_UNDERRUN;
	}

	buf->frames_nr += frames;

	while (data_left) {
		int cpsize;
		int buf_free;
		buf_free = SFX_AUDIO_BUF_SIZE - buf->last->used;


		if (buf_free >= data_left)
			cpsize = data_left;
		else
			cpsize = buf_free;

		/* Copy and advance pointers */
		memcpy(buf->last->data + buf->last->used, src, cpsize);
		data_left -= cpsize;
		buf->last->used += cpsize;
		src += cpsize;

		if (buf->last->used == SFX_AUDIO_BUF_SIZE) {
			if (!buf->last->next) {
				sfx_audio_buf_chunk_t *old = buf->last;
				if (buf->unused) { /* Re-use old chunks */
					sfx_audio_buf_chunk_t *buf_next_unused = buf->unused->next;
					buf->unused->next = NULL;
					buf->unused->used = 0;

					buf->last->next = buf->unused;
					buf->unused = buf_next_unused;
				} else /* Allocate */
					buf->last->next =
					    sfx_audbuf_alloc_chunk();

				buf->last->prev = old;
			}

			buf->last = buf->last->next;
		}
	}

#ifdef TRACE_BUFFER
	{
		sfx_audio_buf_chunk_t *c = buf->first;
		int t = buf->read_offset;

		while (c) {
			fprintf(stderr, "-> [");
			for (; t < c->used; t++)
				fprintf(stderr, " %02x", c->data[t]);
			t = 0;
			fprintf(stderr, " ] ");
			c = c->next;
		}
		fprintf(stderr, "\n");
	}
#endif

	if (frames && (src - buf->framesize) != buf->last_frame)
		/* Backup last frame, unless we're already filling from it */
		memcpy(buf->last_frame, src - buf->framesize, buf->framesize);
}

int
sfx_audbuf_read(sfx_audio_buf_t *buf, unsigned char *dest, int frames) {
	int written = 0;

	if (frames <= 0)
		return 0;

	if (buf->read_timestamp.secs >= 0) {
		/* Have a timestamp? Must update it! */
		buf->read_timestamp =
		    sfx_timestamp_add(buf->read_timestamp,
		                      frames);

	}

	buf->frames_nr -= frames;
	if (buf->frames_nr < 0)
		buf->frames_nr = 0;

#ifdef TRACE_BUFFER
	{
		sfx_audio_buf_chunk_t *c = buf->first;
		int t = buf->read_offset;

		while (c) {
			fprintf(stderr, "-> [");
			for (; t < c->used; t++)
				fprintf(stderr, " %02x", c->data[t]);
			t = 0;
			fprintf(stderr, " ] ");
			c = c->next;
		}
		fprintf(stderr, "\n");
	}
#endif

	while (frames) {
		int data_needed = frames * buf->framesize;
		int rdbytes = data_needed;
		int rdframes;

		if (rdbytes > buf->first->used - buf->read_offset)
			rdbytes = buf->first->used - buf->read_offset;

		memcpy(dest, buf->first->data + buf->read_offset, rdbytes);

		buf->read_offset += rdbytes;
		dest += rdbytes;

		if (buf->read_offset == SFX_AUDIO_BUF_SIZE) {
			/* Continue to next, enqueue the current chunk as
			** being unused */
			sfx_audio_buf_chunk_t *lastfirst = buf->first;

			buf->first = buf->first->next;
			lastfirst->next = buf->unused;
			buf->unused = lastfirst;

			buf->read_offset = 0;
		}

		rdframes = (rdbytes / buf->framesize);
		frames -= rdframes;
		written += rdframes;

		if (frames &&
		        (!buf->first || buf->read_offset == buf->first->used)) {
			fprintf(stderr, "Underrun by %d frames at %d\n", frames,
			        buf->read_timestamp.frame_rate);
			/* Buffer underrun! */
			if (!buffer_underrun_status == NO_BUFFER_UNDERRUN) {
				buffer_underrun_status = SAW_BUFFER_UNDERRUN;
			}
			do {
				memcpy(dest, buf->last_frame, buf->framesize);
				dest += buf->framesize;
			} while (--frames);
		}

	}

	return written;
}

#if 0
static void
_sfx_audbuf_rewind_stream(sfx_audio_buf_t *buf, int delta) {
	if (delta > buf->frames_nr)
		delta = buf->frames_nr;


	fprintf(stderr, "Rewinding %d\n", delta);
	buf->frames_nr -= delta;

	/* From here on, 'delta' means the number of BYTES to remove */
	delta *= buf->framesize;

	while (delta) {
		if (buf->last->used >= delta) {
			fprintf(stderr, "Subtracting from  %d  %d\n", buf->last->used, delta);
			buf->last->used -= delta;
			delta = 0;
		} else {
			fprintf(stderr, "Must do block-unuse\n");
			delta -= buf->last->used;
			buf->last->used = 0;
			buf->last->next = buf->unused;
			buf->unused = buf->last;
			buf->last = buf->unused->prev;
			buf->unused->prev = NULL;
		}
	}
}
#endif

void
sfx_audbuf_write_timestamp(sfx_audio_buf_t *buf, sfx_timestamp_t ts) {
	sfx_timestamp_t newstamp;

	newstamp = sfx_timestamp_add(ts, -buf->frames_nr);


	if (buf->read_timestamp.secs <= 0)
		/* Initial stamp */
		buf->read_timestamp = newstamp;
	else {
		int delta = sfx_timestamp_frame_diff(newstamp, buf->read_timestamp);
		long s1, s2, s3, u1, u2, u3;
		sfx_timestamp_gettime(&(buf->read_timestamp), &s1, &u1);
		sfx_timestamp_gettime(&(newstamp), &s2, &u2);
		sfx_timestamp_gettime(&(ts), &s3, &u3);

		if (delta < 0) {
#if 0
			/*			fprintf(stderr, "[SFX-BUF] audiobuf.c: Timestamp delta %d at %d: Must rewind (not implemented yet)\n",
						delta, buf->read_timestamp.frame_rate);*/
			_sfx_audbuf_rewind_stream(buf, -delta);
			buf->read_timestamp = newstamp;
#endif
		} else if (delta > 0) {
			fprintf(stderr, "[SFX-BUF] audiobuf.c: Timestamp delta %d at %d: Filling in as silence frames\n",
			        delta, buf->read_timestamp.frame_rate);
			/* Fill up with silence */
			while (delta--) {
				sfx_audbuf_write(buf, buf->last_frame, 1);
			}
			buf->read_timestamp = newstamp;
		}
	}
}

int
sfx_audbuf_read_timestamp(sfx_audio_buf_t *buf, sfx_timestamp_t *ts) {
	if (buf->read_timestamp.secs > 0) {
		*ts = buf->read_timestamp;
		return 0;
	} else {
		ts->secs = -1;
		ts->usecs = -1;
		ts->frame_offset = -1;
		ts->frame_rate = -1;
		return 1; /* No timestamp */
	}
}