Debian Codegraph @

code/client/snd_mix.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // snd_mix.c -- portable code to mix sounds for snd_dma.c
23
24 #include "client.h"
25 #include "snd_local.h"
26
27 static portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE];
28 static int snd_vol;
29
30 int* snd_p;
31 int snd_linear_count;
32 short* snd_out;
33
34 #if defined(__GNUC__) || !id386
35
36 void S_WriteLinearBlastStereo16 (void)
37 {
38 int i;
39 int val;
40
41 for (i=0 ; i<snd_linear_count ; i+=2)
42 {
43 val = snd_p[i]>>8;
44 if (val > 0x7fff)
45 snd_out[i] = 0x7fff;
46 else if (val < -32768)
47 snd_out[i] = -32768;
48 else
49 snd_out[i] = val;
50
51 val = snd_p[i+1]>>8;
52 if (val > 0x7fff)
53 snd_out[i+1] = 0x7fff;
54 else if (val < -32768)
55 snd_out[i+1] = -32768;
56 else
57 snd_out[i+1] = val;
58 }
59 }
60
61 #else // MSVC on i386
62
63 __declspec( naked ) void S_WriteLinearBlastStereo16 (void)
64 {
65 __asm {
66
67 push edi
68 push ebx
69 mov ecx,ds:dword ptr[snd_linear_count]
70 mov ebx,ds:dword ptr[snd_p]
71 mov edi,ds:dword ptr[snd_out]
72 LWLBLoopTop:
73 mov eax,ds:dword ptr[-8+ebx+ecx*4]
74 sar eax,8
75 cmp eax,07FFFh
76 jg LClampHigh
77 cmp eax,0FFFF8000h
78 jnl LClampDone
79 mov eax,0FFFF8000h
80 jmp LClampDone
81 LClampHigh:
82 mov eax,07FFFh
83 LClampDone:
84 mov edx,ds:dword ptr[-4+ebx+ecx*4]
85 sar edx,8
86 cmp edx,07FFFh
87 jg LClampHigh2
88 cmp edx,0FFFF8000h
89 jnl LClampDone2
90 mov edx,0FFFF8000h
91 jmp LClampDone2
92 LClampHigh2:
93 mov edx,07FFFh
94 LClampDone2:
95 shl edx,16
96 and eax,0FFFFh
97 or edx,eax
98 mov ds:dword ptr[-4+edi+ecx*2],edx
99 sub ecx,2
100 jnz LWLBLoopTop
101 pop ebx
102 pop edi
103 ret
104 }
105 }
106
107 #endif
108
109 void S_TransferStereo16 (unsigned long *pbuf, int endtime)
110 {
111 int lpos;
112 int ls_paintedtime;
113
114 snd_p = (int *) paintbuffer;
115 ls_paintedtime = s_paintedtime;
116
117 while (ls_paintedtime < endtime)
118 {
119 // handle recirculating buffer issues
120 lpos = ls_paintedtime % dma.fullsamples;
121
122 snd_out = (short *) pbuf + (lpos<<1); // lpos * dma.channels
123
124 snd_linear_count = dma.fullsamples - lpos;
125 if (ls_paintedtime + snd_linear_count > endtime)
126 snd_linear_count = endtime - ls_paintedtime;
127
128 snd_linear_count <<= 1; // snd_linear_count *= dma.channels
129
130 // write a linear blast of samples
131 S_WriteLinearBlastStereo16 ();
132
133 snd_p += snd_linear_count;
134 ls_paintedtime += (snd_linear_count>>1); // snd_linear_count / dma.channels
135
136 if( CL_VideoRecording( ) )
137 CL_WriteAVIAudioFrame( (byte *)snd_out, snd_linear_count << 1 ); // snd_linear_count * (dma.samplebits/8)
138 }
139 }
140
141 /*
142 ===================
143 S_TransferPaintBuffer
144
145 ===================
146 */
147 void S_TransferPaintBuffer(int endtime)
148 {
149 int out_idx;
150 int count;
151 int *p;
152 int step;
153 int val;
154 int i;
155 unsigned long *pbuf;
156
157 pbuf = (unsigned long *)dma.buffer;
158
159
160 if ( s_testsound->integer ) {
161 // write a fixed sine wave
162 count = (endtime - s_paintedtime);
163 for (i=0 ; i<count ; i++)
164 paintbuffer[i].left = paintbuffer[i].right = sin((s_paintedtime+i)*0.1)*20000*256;
165 }
166
167
168 if (dma.samplebits == 16 && dma.channels == 2)
169 { // optimized case
170 S_TransferStereo16 (pbuf, endtime);
171 }
172 else
173 { // general case
174 p = (int *) paintbuffer;
175 count = (endtime - s_paintedtime) * dma.channels;
176 out_idx = ((unsigned int)s_paintedtime * dma.channels) % dma.samples;
177 step = 3 - MIN(dma.channels, 2);
178
179 if ((dma.isfloat) && (dma.samplebits == 32))
180 {
181 float *out = (float *) pbuf;
182 for (i=0 ; i<count ; i++)
183 {
184 if ((i % dma.channels) >= 2)
185 {
186 val = 0;
187 }
188 else
189 {
190 val = *p >> 8;
191 p+= step;
192 }
193 if (val > 0x7fff)
194 val = 0x7fff;
195 else if (val < -32767) /* clamp to one less than max to make division max out at -1.0f. */
196 val = -32767;
197 out[out_idx] = ((float) val) / 32767.0f;
198 out_idx = (out_idx + 1) % dma.samples;
199 }
200 }
201 else if (dma.samplebits == 16)
202 {
203 short *out = (short *) pbuf;
204 for (i=0 ; i<count ; i++)
205 {
206 if ((i % dma.channels) >= 2)
207 {
208 val = 0;
209 }
210 else
211 {
212 val = *p >> 8;
213 p+= step;
214 }
215 if (val > 0x7fff)
216 val = 0x7fff;
217 else if (val < -32768)
218 val = -32768;
219 out[out_idx] = val;
220 out_idx = (out_idx + 1) % dma.samples;
221 }
222 }
223 else if (dma.samplebits == 8)
224 {
225 unsigned char *out = (unsigned char *) pbuf;
226 for (i=0 ; i<count ; i++)
227 {
228 if ((i % dma.channels) >= 2)
229 {
230 val = 0;
231 }
232 else
233 {
234 val = *p >> 8;
235 p+= step;
236 }
237 if (val > 0x7fff)
238 val = 0x7fff;
239 else if (val < -32768)
240 val = -32768;
241 out[out_idx] = (val>>8) + 128;
242 out_idx = (out_idx + 1) % dma.samples;
243 }
244 }
245 }
246 }
247
248
249 /*
250 ===============================================================================
251
252 CHANNEL MIXING
253
254 ===============================================================================
255 */
256
257 static void S_PaintChannelFrom16_scalar( channel_t *ch, const sfx_t *sc, int count, int sampleOffset, int bufferOffset ) {
258 int data, aoff, boff;
259 int leftvol, rightvol;
260 int i, j;
261 portable_samplepair_t *samp;
262 sndBuffer *chunk;
263 short *samples;
264 float ooff, fdata[2], fdiv, fleftvol, frightvol;
265
266 if (sc->soundChannels <= 0) {
267 return;
268 }
269
270 samp = &paintbuffer[ bufferOffset ];
271
272 if (ch->doppler) {
273 sampleOffset = sampleOffset*ch->oldDopplerScale;
274 }
275
276 if ( sc->soundChannels == 2 ) {
277 sampleOffset *= sc->soundChannels;
278
279 if ( sampleOffset & 1 ) {
280 sampleOffset &= ~1;
281 }
282 }
283
284 chunk = sc->soundData;
285 while (sampleOffset>=SND_CHUNK_SIZE) {
286 chunk = chunk->next;
287 sampleOffset -= SND_CHUNK_SIZE;
288 if (!chunk) {
289 chunk = sc->soundData;
290 }
291 }
292
293 if (!ch->doppler || ch->dopplerScale==1.0f) {
294 leftvol = ch->leftvol*snd_vol;
295 rightvol = ch->rightvol*snd_vol;
296 samples = chunk->sndChunk;
297 for ( i=0 ; i<count ; i++ ) {
298 data = samples[sampleOffset++];
299 samp[i].left += (data * leftvol)>>8;
300
301 if ( sc->soundChannels == 2 ) {
302 data = samples[sampleOffset++];
303 }
304 samp[i].right += (data * rightvol)>>8;
305
306 if (sampleOffset == SND_CHUNK_SIZE) {
307 chunk = chunk->next;
308 samples = chunk->sndChunk;
309 sampleOffset = 0;
310 }
311 }
312 } else {
313 fleftvol = ch->leftvol*snd_vol;
314 frightvol = ch->rightvol*snd_vol;
315
316 ooff = sampleOffset;
317 samples = chunk->sndChunk;
318
319
320
321
322 for ( i=0 ; i<count ; i++ ) {
323
324 aoff = ooff;
325 ooff = ooff + ch->dopplerScale * sc->soundChannels;
326 boff = ooff;
327 fdata[0] = fdata[1] = 0;
328 for (j=aoff; j<boff; j += sc->soundChannels) {
329 if (j == SND_CHUNK_SIZE) {
330 chunk = chunk->next;
331 if (!chunk) {
332 chunk = sc->soundData;
333 }
334 samples = chunk->sndChunk;
335 ooff -= SND_CHUNK_SIZE;
336 }
337 if ( sc->soundChannels == 2 ) {
338 fdata[0] += samples[j&(SND_CHUNK_SIZE-1)];
339 fdata[1] += samples[(j+1)&(SND_CHUNK_SIZE-1)];
340 } else {
341 fdata[0] += samples[j&(SND_CHUNK_SIZE-1)];
342 fdata[1] += samples[j&(SND_CHUNK_SIZE-1)];
343 }
344 }
345 fdiv = 256 * (boff-aoff) / sc->soundChannels;
346 samp[i].left += (fdata[0] * fleftvol)/fdiv;
347 samp[i].right += (fdata[1] * frightvol)/fdiv;
348 }
349 }
350 }
351
352 static void S_PaintChannelFrom16( channel_t *ch, const sfx_t *sc, int count, int sampleOffset, int bufferOffset ) {
353 #if idppc_altivec
354 if (com_altivec->integer) {
355 // must be in a separate translation unit or G3 systems will crash.
356 S_PaintChannelFrom16_altivec( paintbuffer, snd_vol, ch, sc, count, sampleOffset, bufferOffset );
357 return;
358 }
359 #endif
360 S_PaintChannelFrom16_scalar( ch, sc, count, sampleOffset, bufferOffset );
361 }
362
363 void S_PaintChannelFromWavelet( channel_t *ch, sfx_t *sc, int count, int sampleOffset, int bufferOffset ) {
364 int data;
365 int leftvol, rightvol;
366 int i;
367 portable_samplepair_t *samp;
368 sndBuffer *chunk;
369 short *samples;
370
371 leftvol = ch->leftvol*snd_vol;
372 rightvol = ch->rightvol*snd_vol;
373
374 i = 0;
375 samp = &paintbuffer[ bufferOffset ];
376 chunk = sc->soundData;
377 while (sampleOffset>=(SND_CHUNK_SIZE_FLOAT*4)) {
378 chunk = chunk->next;
379 sampleOffset -= (SND_CHUNK_SIZE_FLOAT*4);
380 i++;
381 }
382
383 if (i!=sfxScratchIndex || sfxScratchPointer != sc) {
384 decodeWavelet(chunk, sfxScratchBuffer);
385 sfxScratchIndex = i;
386 sfxScratchPointer = sc;
387 }
388
389 samples = sfxScratchBuffer;
390
391 for ( i=0 ; i<count ; i++ ) {
392 data = samples[sampleOffset++];
393 samp[i].left += (data * leftvol)>>8;
394 samp[i].right += (data * rightvol)>>8;
395
396 if (sampleOffset == SND_CHUNK_SIZE*2) {
397 chunk = chunk->next;
398 decodeWavelet(chunk, sfxScratchBuffer);
399 sfxScratchIndex++;
400 sampleOffset = 0;
401 }
402 }
403 }
404
405 void S_PaintChannelFromADPCM( channel_t *ch, sfx_t *sc, int count, int sampleOffset, int bufferOffset ) {
406 int data;
407 int leftvol, rightvol;
408 int i;
409 portable_samplepair_t *samp;
410 sndBuffer *chunk;
411 short *samples;
412
413 leftvol = ch->leftvol*snd_vol;
414 rightvol = ch->rightvol*snd_vol;
415
416 i = 0;
417 samp = &paintbuffer[ bufferOffset ];
418 chunk = sc->soundData;
419
420 if (ch->doppler) {
421 sampleOffset = sampleOffset*ch->oldDopplerScale;
422 }
423
424 while (sampleOffset>=(SND_CHUNK_SIZE*4)) {
425 chunk = chunk->next;
426 sampleOffset -= (SND_CHUNK_SIZE*4);
427 i++;
428 }
429
430 if (i!=sfxScratchIndex || sfxScratchPointer != sc) {
431 S_AdpcmGetSamples( chunk, sfxScratchBuffer );
432 sfxScratchIndex = i;
433 sfxScratchPointer = sc;
434 }
435
436 samples = sfxScratchBuffer;
437
438 for ( i=0 ; i<count ; i++ ) {
439 data = samples[sampleOffset++];
440 samp[i].left += (data * leftvol)>>8;
441 samp[i].right += (data * rightvol)>>8;
442
443 if (sampleOffset == SND_CHUNK_SIZE*4) {
444 chunk = chunk->next;
445 S_AdpcmGetSamples( chunk, sfxScratchBuffer);
446 sampleOffset = 0;
447 sfxScratchIndex++;
448 }
449 }
450 }
451
452 void S_PaintChannelFromMuLaw( channel_t *ch, sfx_t *sc, int count, int sampleOffset, int bufferOffset ) {
453 int data;
454 int leftvol, rightvol;
455 int i;
456 portable_samplepair_t *samp;
457 sndBuffer *chunk;
458 byte *samples;
459 float ooff;
460
461 leftvol = ch->leftvol*snd_vol;
462 rightvol = ch->rightvol*snd_vol;
463
464 samp = &paintbuffer[ bufferOffset ];
465 chunk = sc->soundData;
466 while (sampleOffset>=(SND_CHUNK_SIZE*2)) {
467 chunk = chunk->next;
468 sampleOffset -= (SND_CHUNK_SIZE*2);
469 if (!chunk) {
470 chunk = sc->soundData;
471 }
472 }
473
474 if (!ch->doppler) {
475 samples = (byte *)chunk->sndChunk + sampleOffset;
476 for ( i=0 ; i<count ; i++ ) {
477 data = mulawToShort[*samples];
478 samp[i].left += (data * leftvol)>>8;
479 samp[i].right += (data * rightvol)>>8;
480 samples++;
481 if (chunk != NULL && samples == (byte *)chunk->sndChunk+(SND_CHUNK_SIZE*2)) {
482 chunk = chunk->next;
483 samples = (byte *)chunk->sndChunk;
484 }
485 }
486 } else {
487 ooff = sampleOffset;
488 samples = (byte *)chunk->sndChunk;
489 for ( i=0 ; i<count ; i++ ) {
490 data = mulawToShort[samples[(int)(ooff)]];
491 ooff = ooff + ch->dopplerScale;
492 samp[i].left += (data * leftvol)>>8;
493 samp[i].right += (data * rightvol)>>8;
494 if (ooff >= SND_CHUNK_SIZE*2) {
495 chunk = chunk->next;
496 if (!chunk) {
497 chunk = sc->soundData;
498 }
499 samples = (byte *)chunk->sndChunk;
500 ooff = 0.0;
501 }
502 }
503 }
504 }
505
506 /*
507 ===================
508 S_PaintChannels
509 ===================
510 */
511 void S_PaintChannels( int endtime ) {
512 int i;
513 int end;
514 int stream;
515 channel_t *ch;
516 sfx_t *sc;
517 int ltime, count;
518 int sampleOffset;
519
520 if(s_muted->integer)
521 snd_vol = 0;
522 else
523 snd_vol = s_volume->value*255;
524
525 //Com_Printf ("%i to %i\n", s_paintedtime, endtime);
526 while ( s_paintedtime < endtime ) {
527 // if paintbuffer is smaller than DMA buffer
528 // we may need to fill it multiple times
529 end = endtime;
530 if ( endtime - s_paintedtime > PAINTBUFFER_SIZE ) {
531 end = s_paintedtime + PAINTBUFFER_SIZE;
532 }
533
534 // clear the paint buffer and mix any raw samples...
535 Com_Memset(paintbuffer, 0, sizeof (paintbuffer));
536 for (stream = 0; stream < MAX_RAW_STREAMS; stream++) {
537 if ( s_rawend[stream] >= s_paintedtime ) {
538 // copy from the streaming sound source
539 const portable_samplepair_t *rawsamples = s_rawsamples[stream];
540 const int stop = (end < s_rawend[stream]) ? end : s_rawend[stream];
541 for ( i = s_paintedtime ; i < stop ; i++ ) {
542 const int s = i&(MAX_RAW_SAMPLES-1);
543 paintbuffer[i-s_paintedtime].left += rawsamples[s].left;
544 paintbuffer[i-s_paintedtime].right += rawsamples[s].right;
545 }
546 }
547 }
548
549 // paint in the channels.
550 ch = s_channels;
551 for ( i = 0; i < MAX_CHANNELS ; i++, ch++ ) {
552 if ( !ch->thesfx || (ch->leftvol<0.25 && ch->rightvol<0.25 )) {
553 continue;
554 }
555
556 ltime = s_paintedtime;
557 sc = ch->thesfx;
558
559 if (sc->soundData==NULL || sc->soundLength==0) {
560 continue;
561 }
562
563 sampleOffset = ltime - ch->startSample;
564 count = end - ltime;
565 if ( sampleOffset + count > sc->soundLength ) {
566 count = sc->soundLength - sampleOffset;
567 }
568
569 if ( count > 0 ) {
570 if( sc->soundCompressionMethod == 1) {
571 S_PaintChannelFromADPCM (ch, sc, count, sampleOffset, ltime - s_paintedtime);
572 } else if( sc->soundCompressionMethod == 2) {
573 S_PaintChannelFromWavelet (ch, sc, count, sampleOffset, ltime - s_paintedtime);
574 } else if( sc->soundCompressionMethod == 3) {
575 S_PaintChannelFromMuLaw (ch, sc, count, sampleOffset, ltime - s_paintedtime);
576 } else {
577 S_PaintChannelFrom16 (ch, sc, count, sampleOffset, ltime - s_paintedtime);
578 }
579 }
580 }
581
582 // paint in the looped channels.
583 ch = loop_channels;
584 for ( i = 0; i < numLoopChannels ; i++, ch++ ) {
585 if ( !ch->thesfx || (!ch->leftvol && !ch->rightvol )) {
586 continue;
587 }
588
589 ltime = s_paintedtime;
590 sc = ch->thesfx;
591
592 if (sc->soundData==NULL || sc->soundLength==0) {
593 continue;
594 }
595 // we might have to make two passes if it
596 // is a looping sound effect and the end of
597 // the sample is hit
598 do {
599 sampleOffset = (ltime % sc->soundLength);
600
601 count = end - ltime;
602 if ( sampleOffset + count > sc->soundLength ) {
603 count = sc->soundLength - sampleOffset;
604 }
605
606 if ( count > 0 ) {
607 if( sc->soundCompressionMethod == 1) {
608 S_PaintChannelFromADPCM (ch, sc, count, sampleOffset, ltime - s_paintedtime);
609 } else if( sc->soundCompressionMethod == 2) {
610 S_PaintChannelFromWavelet (ch, sc, count, sampleOffset, ltime - s_paintedtime);
611 } else if( sc->soundCompressionMethod == 3) {
612 S_PaintChannelFromMuLaw (ch, sc, count, sampleOffset, ltime - s_paintedtime);
613 } else {
614 S_PaintChannelFrom16 (ch, sc, count, sampleOffset, ltime - s_paintedtime);
615 }
616 ltime += count;
617 }
618 } while ( ltime < end);
619 }
620
621 // transfer out according to DMA format
622 S_TransferPaintBuffer( end );
623 s_paintedtime = end;
624 }
625 }