1 /*
2 	snd_mix.c
3 
4 	portable code to mix sounds for snd_dma.c
5 
6 	Copyright (C) 1996-1997  Id Software, Inc.
7 
8 	This program is free software; you can redistribute it and/or
9 	modify it under the terms of the GNU General Public License
10 	as published by the Free Software Foundation; either version 2
11 	of the License, or (at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 	See the GNU General Public License for more details.
18 
19 	You should have received a copy of the GNU General Public License
20 	along with this program; if not, write to:
21 
22 		Free Software Foundation, Inc.
23 		59 Temple Place - Suite 330
24 		Boston, MA  02111-1307, USA
25 
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #endif
34 #ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 #endif
37 
38 #include "winquake.h"
39 
40 #include "QF/cvar.h"
41 #include "QF/sound.h"
42 #include "QF/sys.h"
43 
44 #include "compat.h"
45 #include "snd_internal.h"
46 
47 #define VOLSCALE 512.0				// so mixing is less likely to overflow
48 									// note: must be >= 255 due to the channel
49 									// volumes being 0-255.
50 
51 cvar_t     *snd_volume;
52 
53 unsigned    snd_paintedtime;				// sample PAIRS
54 
55 portable_samplepair_t snd_paintbuffer[PAINTBUFFER_SIZE * 2];
56 static int  max_overpaint;				// number of extra samples painted
57 										// due to phase shift
58 
59 /* CHANNEL MIXING */
60 
61 static inline int
check_channel_end(channel_t * ch,sfx_t * sfx,int count,unsigned ltime)62 check_channel_end (channel_t *ch, sfx_t *sfx, int count, unsigned ltime)
63 {
64 	if (count <= 0 || ltime >= ch->end) {
65 		if (sfx->loopstart != (unsigned) -1) {
66 			ch->pos = sfx->loopstart;
67 			ch->end = ltime + sfx->length - ch->pos;
68 		} else {			// channel just stopped
69 			ch->done = 1;
70 			return 1;
71 		}
72 	}
73 	return 0;
74 }
75 
76 static inline void
snd_paint_channel(channel_t * ch,sfxbuffer_t * sc,int count)77 snd_paint_channel (channel_t *ch, sfxbuffer_t *sc, int count)
78 {
79 	unsigned    pos;
80 	int         offs = 0;
81 	float      *samps;
82 
83 	if ((int) ch->pos < 0) {
84 		ch->pos += count;
85 		if ((int) ch->pos <= 0)
86 			return;
87 		offs = count - ch->pos;
88 		count -= offs;
89 		ch->pos = 0;
90 	}
91 	if (ch->pos < sc->pos || ch->pos - sc->pos >= sc->length)
92 		sc->setpos (sc, ch->pos);
93 	pos = (ch->pos - sc->pos + sc->tail) % sc->length;
94 	samps = sc->data + pos * sc->channels;
95 
96 	if (pos + count > sc->length) {
97 		unsigned    sub = sc->length - pos;
98 		sc->paint (offs, ch, samps, sub);
99 		sc->paint (offs + sub, ch, sc->data, count - sub);
100 	} else {
101 		sc->paint (offs, ch, samps, count);
102 	}
103 	ch->pos += count;
104 }
105 
106 void
SND_PaintChannels(unsigned endtime)107 SND_PaintChannels (unsigned endtime)
108 {
109 	unsigned    end, ltime;
110 	int         i, count;
111 	channel_t  *ch;
112 	sfx_t      *sfx;
113 	sfxbuffer_t *sc;
114 
115 	// clear the paint buffer
116 	for (i = 0; i < PAINTBUFFER_SIZE * 2; i++) {
117 		snd_paintbuffer[i].left = 0;
118 		snd_paintbuffer[i].right = 0;
119 	}
120 
121 	while (snd_paintedtime < endtime) {
122 		// if snd_paintbuffer is smaller than DMA buffer
123 		end = endtime;
124 		if (end - snd_paintedtime > PAINTBUFFER_SIZE)
125 			end = snd_paintedtime + PAINTBUFFER_SIZE;
126 
127 		max_overpaint = 0;
128 
129 		// paint in the channels.
130 		ch = snd_channels;
131 		for (i = 0; i < snd_total_channels; i++, ch++) {
132 			if (!(sfx = ch->sfx))
133 				continue;
134 			if (ch->stop || ch->done) {
135 				ch->done = 1;		// acknowledge stopped signal
136 				continue;
137 			}
138 			if (ch->pause)
139 				continue;
140 			sc = sfx->getbuffer (sfx);
141 			if (!sc) {				// something went wrong with the sfx
142 				printf ("XXXX sfx blew up!!!!\n");
143 				continue;
144 			}
145 
146 			if (!ch->end)
147 				ch->end = snd_paintedtime + sfx->length - ch->pos;
148 
149 			ltime = snd_paintedtime;
150 
151 			while (ltime < end) {		// paint up to end
152 				count = ((ch->end < end) ? ch->end : end) - ltime;
153 				if (count > 0) {
154 					if (ch->leftvol || ch->rightvol) {
155 						snd_paint_channel (ch, sc, count);
156 						if (sc->advance) {
157 							if (!sc->advance (sc, count)) {
158 								// this channel can no longer be used as its
159 								// source has died.
160 								ch->done = 1;
161 								break;
162 							}
163 						}
164 					}
165 					ltime += count;
166 				}
167 
168 				if (check_channel_end (ch, sfx, count, ltime))
169 					break;
170 			}
171 		}
172 
173 		// transfer out according to DMA format
174 		snd_shm->xfer (snd_paintbuffer, end - snd_paintedtime,
175 					   snd_volume->value);
176 
177 		memmove (snd_paintbuffer, snd_paintbuffer + end - snd_paintedtime,
178 				 max_overpaint * sizeof (snd_paintbuffer[0]));
179 		memset (snd_paintbuffer + max_overpaint, 0, sizeof (snd_paintbuffer)
180 				- max_overpaint * sizeof (snd_paintbuffer[0]));
181 
182 		snd_paintedtime = end;
183 	}
184 }
185 
186 static inline void
snd_mix_single(portable_samplepair_t * pair,float ** samp,float lvol,float rvol)187 snd_mix_single (portable_samplepair_t *pair, float **samp,
188 				float lvol, float rvol)
189 {
190 	float       single = *(*samp)++;
191 
192 	pair->left += single * lvol;
193 	pair->right += single * rvol;
194 }
195 
196 static inline void
snd_mix_pair(portable_samplepair_t * pair,float ** samp,float lvol,float rvol)197 snd_mix_pair (portable_samplepair_t *pair, float **samp,
198 			  float lvol, float rvol)
199 {
200 	float       left = *(*samp)++;
201 	float       right = *(*samp)++;
202 
203 	pair->left += left * lvol;
204 	pair->right += right * rvol;
205 }
206 
207 static inline void
snd_mix_tripple(portable_samplepair_t * pair,float ** samp,float lvol,float rvol)208 snd_mix_tripple (portable_samplepair_t *pair, float **samp,
209 				 float lvol, float rvol)
210 {
211 	float       left = *(*samp)++;
212 	float       center = *(*samp)++;
213 	float       right = *(*samp)++;
214 
215 	pair->left += left * lvol;
216 	pair->left += center * lvol;
217 	pair->right += center * rvol;
218 	pair->right += right * rvol;
219 }
220 
221 /*	mono
222 		center
223 	Spacializes the sound.
224 */
225 static void
snd_paint_mono(int offs,channel_t * ch,float * sfx,unsigned count)226 snd_paint_mono (int offs, channel_t *ch, float *sfx, unsigned count)
227 {
228 	float       leftvol, rightvol;
229 	unsigned    left_phase, right_phase;	// Never allowed < 0 anyway
230 	unsigned    i = 0;
231 	portable_samplepair_t *pair;
232 
233 	leftvol = ch->leftvol / VOLSCALE;
234 	rightvol = ch->rightvol / VOLSCALE;
235 
236 	max_overpaint = max (abs (ch->phase),
237 						 max (abs (ch->oldphase), max_overpaint));
238 
239 	pair = snd_paintbuffer + offs;
240 
241 	if (ch->phase >= 0) {
242 		left_phase = ch->phase;
243 		right_phase = 0;
244 	} else {
245 		left_phase = 0;
246 		right_phase = -ch->phase;
247 	}
248 
249 	if (ch->oldphase != ch->phase) {
250 		unsigned    old_phase_left, old_phase_right;
251 		unsigned    new_phase_left, new_phase_right;
252 		unsigned    count_left, count_right, c;
253 
254 		if (ch->oldphase >= 0) {
255 			old_phase_left = ch->oldphase;
256 			old_phase_right = 0;
257 		} else {
258 			old_phase_left = 0;
259 			old_phase_right = -ch->oldphase;
260 		}
261 		new_phase_left = left_phase;
262 		new_phase_right = right_phase;
263 
264 		if (new_phase_left > old_phase_left)
265 			count_left = 2 * (new_phase_left - old_phase_left);
266 		else
267 			count_left = old_phase_left - new_phase_left;
268 
269 		if (new_phase_right > old_phase_right)
270 			count_right = 2 * (new_phase_right - old_phase_right);
271 		else
272 			count_right = old_phase_right - new_phase_right;
273 
274 		c = min (count, max (count_right, count_left));
275 		count -= c;
276 		while (c) {
277 			float       left = sfx[i] * leftvol;
278 			float       right = sfx[i] * rightvol;
279 
280 			if (new_phase_left < old_phase_left) {
281 				if (!(count_left & 1)) {
282 					pair[i + old_phase_left].left += left;
283 					old_phase_left--;
284 				}
285 				count_left--;
286 			} else {
287 				if (new_phase_left > old_phase_left) {
288 					pair[i + old_phase_left].left += left;
289 					old_phase_left++;
290 				}
291 				pair[i + old_phase_left].left += left;
292 			}
293 
294 			if (new_phase_right < old_phase_right) {
295 				if (!(count_right & 1)) {
296 					pair[i + old_phase_right].right += right;
297 					old_phase_right--;
298 				}
299 				count_right--;
300 			} else {
301 				if (new_phase_right > old_phase_right) {
302 					pair[i + old_phase_right].right += right;
303 					old_phase_right++;
304 				}
305 				pair[i + old_phase_right].right += right;
306 			}
307 
308 			c--;
309 			i++;
310 		}
311 	}
312 
313 	for (i = 0; i < count; i++) {
314 		pair[i + left_phase].left += sfx[i] * leftvol;
315 		pair[i + right_phase].right += sfx[i] * rightvol;
316 	}
317 }
318 
319 /*	stereo
320 		left, right
321 	Does not spacialize the sound.
322 */
323 static void
snd_paint_stereo(int offs,channel_t * ch,float * samp,unsigned count)324 snd_paint_stereo (int offs, channel_t *ch, float *samp, unsigned count)
325 {
326 	portable_samplepair_t *pair;
327 	float       leftvol = ch->leftvol / VOLSCALE;
328 	float       rightvol = ch->rightvol / VOLSCALE;
329 
330 	pair = snd_paintbuffer + offs;
331 	while (count-- > 0) {
332 		snd_mix_pair (pair, &samp, leftvol, rightvol);
333 		pair++;
334 	}
335 }
336 
337 /*	1d surround
338 		left, center, right
339 	Does not spacialize the sound.
340 */
341 static void
snd_paint_3(int offs,channel_t * ch,float * samp,unsigned count)342 snd_paint_3 (int offs, channel_t *ch, float *samp, unsigned count)
343 {
344 	portable_samplepair_t *pair;
345 	float       leftvol = ch->leftvol / VOLSCALE;
346 	float       rightvol = ch->rightvol / VOLSCALE;
347 
348 	pair = snd_paintbuffer + offs;
349 	while (count-- > 0) {
350 		snd_mix_tripple (pair, &samp, leftvol, rightvol);
351 		pair++;
352 	}
353 }
354 
355 /*	quadraphonic surround
356 		front (left, right),
357 		rear (left, right)
358 	Does not spacialize the sound.
359 */
360 static void
snd_paint_4(int offs,channel_t * ch,float * samp,unsigned count)361 snd_paint_4 (int offs, channel_t *ch, float *samp, unsigned count)
362 {
363 	portable_samplepair_t *pair;
364 	float       leftvol = ch->leftvol / VOLSCALE;
365 	float       rightvol = ch->rightvol / VOLSCALE;
366 
367 	pair = snd_paintbuffer + offs;
368 	while (count-- > 0) {
369 		snd_mix_pair (pair, &samp, leftvol, rightvol);
370 		snd_mix_pair (pair, &samp, leftvol, rightvol);
371 		pair++;
372 	}
373 }
374 
375 /*	five-channel surround
376 		front (left, center, right),
377 		rear (left, right)
378 	Does not spacialize the sound.
379 */
380 static void
snd_paint_5(int offs,channel_t * ch,float * samp,unsigned count)381 snd_paint_5 (int offs, channel_t *ch, float *samp, unsigned count)
382 {
383 	portable_samplepair_t *pair;
384 	float       leftvol = ch->leftvol / VOLSCALE;
385 	float       rightvol = ch->rightvol / VOLSCALE;
386 
387 	pair = snd_paintbuffer + offs;
388 	while (count-- > 0) {
389 		snd_mix_tripple (pair, &samp, leftvol, rightvol);
390 		snd_mix_pair (pair, &samp, leftvol, rightvol);
391 		pair++;
392 	}
393 }
394 
395 /*	5.1 surround
396 		front (left, center, right),
397 		rear (left, right),
398 		lfe
399 	Does not spacialize the sound.
400 */
401 static void
snd_paint_6(int offs,channel_t * ch,float * samp,unsigned count)402 snd_paint_6 (int offs, channel_t *ch, float *samp, unsigned count)
403 {
404 	portable_samplepair_t *pair;
405 	float       leftvol = ch->leftvol / VOLSCALE;
406 	float       rightvol = ch->rightvol / VOLSCALE;
407 
408 	pair = snd_paintbuffer + offs;
409 	while (count-- > 0) {
410 		snd_mix_tripple (pair, &samp, leftvol, rightvol);
411 		snd_mix_pair (pair, &samp, leftvol, rightvol);
412 		snd_mix_single (pair, &samp, leftvol, rightvol);
413 		pair++;
414 	}
415 }
416 
417 /*	6.1 surround
418 		front (left, center, right),
419 		side (left, right),
420 		rear (center),
421 		lfe
422 	Does not spacialize the sound.
423 */
424 static void
snd_paint_7(int offs,channel_t * ch,float * samp,unsigned count)425 snd_paint_7 (int offs, channel_t *ch, float *samp, unsigned count)
426 {
427 	portable_samplepair_t *pair;
428 	float       leftvol = ch->leftvol / VOLSCALE;
429 	float       rightvol = ch->rightvol / VOLSCALE;
430 
431 	pair = snd_paintbuffer + offs;
432 	while (count-- > 0) {
433 		snd_mix_tripple (pair, &samp, leftvol, rightvol);
434 		snd_mix_pair (pair, &samp, leftvol, rightvol);
435 		snd_mix_single (pair, &samp, leftvol, rightvol);
436 		snd_mix_single (pair, &samp, leftvol, rightvol);
437 		pair++;
438 	}
439 }
440 
441 /*	7.1 surround
442 		front (left, center, right),
443 		side (left, right),
444 		rear (left, right),
445 		lfe
446 	Does not spacialize the sound.
447 */
448 static void
snd_paint_8(int offs,channel_t * ch,float * samp,unsigned count)449 snd_paint_8 (int offs, channel_t *ch, float *samp, unsigned count)
450 {
451 	portable_samplepair_t *pair;
452 	float       leftvol = ch->leftvol / VOLSCALE;
453 	float       rightvol = ch->rightvol / VOLSCALE;
454 
455 	pair = snd_paintbuffer + offs;
456 	while (count-- > 0) {
457 		snd_mix_tripple (pair, &samp, leftvol, rightvol);
458 		snd_mix_pair (pair, &samp, leftvol, rightvol);
459 		snd_mix_pair (pair, &samp, leftvol, rightvol);
460 		snd_mix_single (pair, &samp, leftvol, rightvol);
461 		pair++;
462 	}
463 }
464 
465 void
SND_SetPaint(sfxbuffer_t * sc)466 SND_SetPaint (sfxbuffer_t *sc)
467 {
468 	static sfxpaint_t *painters[] = {
469 		0,
470 		snd_paint_mono,
471 		snd_paint_stereo,
472 		snd_paint_3,
473 		snd_paint_4,
474 		snd_paint_5,
475 		snd_paint_6,
476 		snd_paint_7,
477 		snd_paint_8,
478 	};
479 
480 	wavinfo_t *info = sc->sfx->wavinfo (sc->sfx);
481 	if (info->channels > 8)
482 		Sys_Error ("illegal channel count %d", info->channels);
483 	sc->paint = painters[info->channels];
484 }
485