Skip to content

Latest commit

 

History

History
553 lines (454 loc) · 17.2 KB

effect_position.c

File metadata and controls

553 lines (454 loc) · 17.2 KB
 
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
/*
MIXERLIB: An audio mixer library based on the SDL library
Copyright (C) 1997-1999 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This file by Ryan C. Gordon (icculus@linuxgames.com)
These are some internally supported special effects that use SDL_mixer's
effect callback API. They are meant for speed over quality. :)
*/
/* $Id$ */
#include <stdio.h>
#include <stdlib.h>
Sep 11, 2001
Sep 11, 2001
29
30
#include <string.h>
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
#include "SDL.h"
#include "SDL_mixer.h"
#define __MIX_INTERNAL_EFFECT__
#include "effects_internal.h"
/* profile code:
#include <sys/time.h>
#include <unistd.h>
struct timeval tv1;
struct timeval tv2;
gettimeofday(&tv1, NULL);
... do your thing here ...
gettimeofday(&tv2, NULL);
printf("%ld\n", tv2.tv_usec - tv1.tv_usec);
*/
/*
* Positional effects...panning, distance attenuation, etc.
*/
typedef struct _Eff_positionargs
{
volatile float left_f;
volatile float right_f;
volatile Uint8 left_u8;
volatile Uint8 right_u8;
volatile float distance_f;
volatile Uint8 distance_u8;
volatile int in_use;
volatile int channels;
} position_args;
static position_args **pos_args_array = NULL;
static position_args *pos_args_global = NULL;
static int position_channels = 0;
/* This just frees up the callback-specific data. */
static void _Eff_PositionDone(int channel, void *udata)
{
if (channel < 0) {
if (pos_args_global != NULL) {
free(pos_args_global);
pos_args_global = NULL;
}
}
else if (pos_args_array[channel] != NULL) {
free(pos_args_array[channel]);
pos_args_array[channel] = NULL;
}
}
static void _Eff_position_u8(int chan, void *stream, int len, void *udata)
{
volatile position_args *args = (volatile position_args *) udata;
Uint8 *ptr = (Uint8 *) stream;
int i;
/*
* if there's only a mono channnel (the only way we wouldn't have
* a len divisible by 2 here), then left_f and right_f are always
* 1.0, and are therefore throwaways.
*/
if (len % sizeof (Uint16) != 0) {
*(ptr++) = (Uint8) (((float) *ptr) * args->distance_f);
len--;
}
for (i = 0; i < len; i += sizeof (Uint8) * 2) {
*(ptr++) = (Uint8)((((float) *ptr) * args->left_f) * args->distance_f);
*(ptr++) = (Uint8)((((float) *ptr) * args->right_f) * args->distance_f);
}
}
/*
* This one runs about 10.1 times faster than the non-table version, with
* no loss in quality. It does, however, require 64k of memory for the
* lookup table. Also, this will only update position information once per
* call; the non-table version always checks the arguments for each sample,
* in case the user has called Mix_SetPanning() or whatnot again while this
* callback is running.
*/
static void _Eff_position_table_u8(int chan, void *stream, int len, void *udata)
{
volatile position_args *args = (volatile position_args *) udata;
Uint8 *ptr = (Uint8 *) stream;
Uint32 *p;
int i;
Uint8 *l = ((Uint8 *) _Eff_volume_table) + (256 * args->left_u8);
Uint8 *r = ((Uint8 *) _Eff_volume_table) + (256 * args->right_u8);
Uint8 *d = ((Uint8 *) _Eff_volume_table) + (256 * args->distance_u8);
/*
* if there's only a mono channnel, then l[] and r[] are always
* volume 255, and are therefore throwaways. Still, we have to
* be sure not to overrun the audio buffer...
*/
while (len % sizeof (Uint32) != 0) {
*(ptr++) = d[l[*ptr]];
if (args->channels == 2)
*(ptr++) = d[r[*ptr]];
len -= args->channels;
}
p = (Uint32 *) ptr;
for (i = 0; i < len; i += sizeof (Uint32)) {
#if (SDL_BYTE_ORDER == SDL_BIG_ENDIAN)
*(p++) = (d[l[(*p & 0xFF000000) >> 24]] << 24) |
(d[r[(*p & 0x00FF0000) >> 16]] << 16) |
(d[l[(*p & 0x0000FF00) >> 8]] << 8) |
(d[r[(*p & 0x000000FF) ]] ) ;
#else
*(p++) = (d[r[(*p & 0xFF000000) >> 24]] << 24) |
(d[l[(*p & 0x00FF0000) >> 16]] << 16) |
(d[r[(*p & 0x0000FF00) >> 8]] << 8) |
(d[l[(*p & 0x000000FF) ]] ) ;
#endif
}
}
static void _Eff_position_s8(int chan, void *stream, int len, void *udata)
{
volatile position_args *args = (volatile position_args *) udata;
Sint8 *ptr = (Sint8 *) stream;
int i;
/*
* if there's only a mono channnel (the only way we wouldn't have
* a len divisible by 2 here), then left_f and right_f are always
* 1.0, and are therefore throwaways.
*/
if (len % sizeof (Sint16) != 0) {
*(ptr++) = (Sint8) (((float) *ptr) * args->distance_f);
len--;
}
for (i = 0; i < len; i += sizeof (Sint8) * 2) {
*(ptr++) = (Sint8)((((float) *ptr) * args->left_f) * args->distance_f);
*(ptr++) = (Sint8)((((float) *ptr) * args->right_f) * args->distance_f);
}
}
/*
* This one runs about 10.1 times faster than the non-table version, with
* no loss in quality. It does, however, require 64k of memory for the
* lookup table. Also, this will only update position information once per
* call; the non-table version always checks the arguments for each sample,
* in case the user has called Mix_SetPanning() or whatnot again while this
* callback is running.
*/
static void _Eff_position_table_s8(int chan, void *stream, int len, void *udata)
{
volatile position_args *args = (volatile position_args *) udata;
Sint8 *ptr = (Sint8 *) stream;
Uint32 *p;
int i;
Nov 30, 2001
Nov 30, 2001
197
198
Sint8 *l = ((Sint8 *) _Eff_volume_table) + (256 * args->left_u8);
Sint8 *r = ((Sint8 *) _Eff_volume_table) + (256 * args->right_u8);
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
Sint8 *d = ((Sint8 *) _Eff_volume_table) + (256 * args->distance_u8);
while (len % sizeof (Uint32) != 0) {
*(ptr++) = d[l[*ptr]];
if (args->channels > 1)
*(ptr++) = d[r[*ptr]];
len -= args->channels;
}
p = (Uint32 *) ptr;
for (i = 0; i < len; i += sizeof (Uint32)) {
#if (SDL_BYTE_ORDER == SDL_BIG_ENDIAN)
*(p++) = (d[l[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
(d[r[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
(d[l[((Sint16)(Sint8)((*p & 0x0000FF00) >> 8))+128]] << 8) |
(d[r[((Sint16)(Sint8)((*p & 0x000000FF) ))+128]] ) ;
#else
*(p++) = (d[r[((Sint16)(Sint8)((*p & 0xFF000000) >> 24))+128]] << 24) |
(d[l[((Sint16)(Sint8)((*p & 0x00FF0000) >> 16))+128]] << 16) |
(d[r[((Sint16)(Sint8)((*p & 0x0000FF00) >> 8))+128]] << 8) |
(d[l[((Sint16)(Sint8)((*p & 0x000000FF) ))+128]] ) ;
#endif
}
}
/* !!! FIXME : Optimize the code for 16-bit samples? */
static void _Eff_position_u16lsb(int chan, void *stream, int len, void *udata)
{
volatile position_args *args = (volatile position_args *) udata;
Uint16 *ptr = (Uint16 *) stream;
int i;
for (i = 0; i < len; i += sizeof (Uint16) * 2) {
#if (SDL_BYTE_ORDER == SDL_BIG_ENDIAN)
Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) *
args->left_f) * args->distance_f);
Uint16 swapr = (Uint16) (((float) SDL_Swap16(*(ptr+1))) *
args->right_f) * args->distance_f);
*(ptr++) = (Uint16) SDL_Swap16(swapl);
*(ptr++) = (Uint16) SDL_Swap16(swapr);
#else
*(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f);
*(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f);
#endif
}
}
static void _Eff_position_s16lsb(int chan, void *stream, int len, void *udata)
{
/* 16 signed bits (lsb) * 2 channels. */
volatile position_args *args = (volatile position_args *) udata;
Sint16 *ptr = (Sint16 *) stream;
int i;
for (i = 0; i < len; i += sizeof (Sint16) * 2) {
#if (SDL_BYTE_ORDER == SDL_BIG_ENDIAN)
Sint16 swapl = (Sint16) ((((float) SDL_Swap16(*(ptr))) *
args->left_f) * args->distance_f);
Sint16 swapr = (Sint16) (((float) SDL_Swap16(*(ptr+1))) *
args->right_f) * args->distance_f);
*(ptr++) = (Sint16) SDL_Swap16(swapl);
*(ptr++) = (Sint16) SDL_Swap16(swapr);
#else
*(ptr++) = (Sint16) ((((float) *ptr)*args->left_f)*args->distance_f);
*(ptr++) = (Sint16) ((((float) *ptr)*args->right_f)*args->distance_f);
#endif
}
}
static void _Eff_position_u16msb(int chan, void *stream, int len, void *udata)
{
/* 16 signed bits (lsb) * 2 channels. */
volatile position_args *args = (volatile position_args *) udata;
Uint16 *ptr = (Uint16 *) stream;
int i;
for (i = 0; i < len; i += sizeof (Sint16) * 2) {
#if (SDL_BYTE_ORDER == SDL_LIL_ENDIAN)
Uint16 swapl = (Uint16) ((((float) SDL_Swap16(*(ptr))) *
args->left_f) * args->distance_f);
Uint16 swapr = (Uint16) (((float) SDL_Swap16(*(ptr+1))) *
args->right_f) * args->distance_f);
*(ptr++) = (Uint16) SDL_Swap16(swapl);
*(ptr++) = (Uint16) SDL_Swap16(swapr);
#else
*(ptr++) = (Uint16) ((((float) *ptr)*args->left_f)*args->distance_f);
*(ptr++) = (Uint16) ((((float) *ptr)*args->right_f)*args->distance_f);
#endif
}
}
static void _Eff_position_s16msb(int chan, void *stream, int len, void *udata)
{
/* 16 signed bits (lsb) * 2 channels. */
volatile position_args *args = (volatile position_args *) udata;
Sint16 *ptr = (Sint16 *) stream;
int i;
for (i = 0; i < len; i += sizeof (Sint16) * 2) {
#if (SDL_BYTE_ORDER == SDL_LIL_ENDIAN)
Sint16 swapl = (Sint16) ((((float) SDL_Swap16(*(ptr))) *
args->left_f) * args->distance_f);
Sint16 swapr = (Sint16) (((float) SDL_Swap16(*(ptr+1))) *
args->right_f) * args->distance_f);
*(ptr++) = (Sint16) SDL_Swap16(swapl);
*(ptr++) = (Sint16) SDL_Swap16(swapr);
#else
*(ptr++) = (Sint16) ((((float) *ptr)*args->left_f)*args->distance_f);
*(ptr++) = (Sint16) ((((float) *ptr)*args->right_f)*args->distance_f);
#endif
}
}
static void init_position_args(position_args *args)
{
memset(args, '\0', sizeof (position_args));
args->in_use = 0;
args->left_u8 = args->right_u8 = args->distance_u8 = 255;
args->left_f = args->right_f = args->distance_f = 1.0f;
Mix_QuerySpec(NULL, NULL, (int *) &args->channels);
}
static position_args *get_position_arg(int channel)
{
void *rc;
int i;
if (channel < 0) {
if (pos_args_global == NULL) {
pos_args_global = malloc(sizeof (position_args));
if (pos_args_global == NULL) {
Mix_SetError("Out of memory");
return(NULL);
}
init_position_args(pos_args_global);
}
return(pos_args_global);
}
if (channel >= position_channels) {
rc = realloc(pos_args_array, (channel + 1) * sizeof (position_args *));
if (rc == NULL) {
Mix_SetError("Out of memory");
return(NULL);
}
pos_args_array = (position_args **) rc;
for (i = position_channels; i <= channel; i++) {
pos_args_array[i] = NULL;
}
position_channels = channel + 1;
}
if (pos_args_array[channel] == NULL) {
pos_args_array[channel] = (position_args *)malloc(sizeof(position_args));
if (pos_args_array[channel] == NULL) {
Mix_SetError("Out of memory");
return(NULL);
}
init_position_args(pos_args_array[channel]);
}
return(pos_args_array[channel]);
}
static Mix_EffectFunc_t get_position_effect_func(Uint16 format)
{
Mix_EffectFunc_t f = NULL;
switch (format) {
case AUDIO_U8:
f = (_Eff_build_volume_table_u8()) ? _Eff_position_table_u8 :
_Eff_position_u8;
break;
case AUDIO_S8:
f = (_Eff_build_volume_table_s8()) ? _Eff_position_table_s8 :
_Eff_position_s8;
break;
case AUDIO_U16LSB:
f = _Eff_position_u16lsb;
break;
case AUDIO_S16LSB:
f = _Eff_position_s16lsb;
break;
case AUDIO_U16MSB:
f = _Eff_position_u16msb;
break;
case AUDIO_S16MSB:
f = _Eff_position_s16msb;
break;
default:
Mix_SetError("Unsupported audio format");
}
return(f);
}
int Mix_SetPanning(int channel, Uint8 left, Uint8 right)
{
Mix_EffectFunc_t f = NULL;
int channels;
Uint16 format;
position_args *args = NULL;
Mix_QuerySpec(NULL, &format, &channels);
if (channels != 2) /* it's a no-op; we call that successful. */
return(1);
f = get_position_effect_func(format);
if (f == NULL)
return(0);
args = get_position_arg(channel);
if (!args)
return(0);
/* it's a no-op; unregister the effect, if it's registered. */
if ((args->distance_u8 == 255) && (left == 255) &&
(right == 255) && (args->in_use))
{
return(Mix_UnregisterEffect(channel, f));
}
args->left_u8 = left;
args->right_u8 = right;
Sep 11, 2001
Sep 11, 2001
441
442
args->left_f = ((float) left) / 255.0f;
args->right_f = ((float) right) / 255.0f;
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
if (!args->in_use) {
args->in_use = 1;
return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
}
return(1);
}
int Mix_SetDistance(int channel, Uint8 distance)
{
Mix_EffectFunc_t f = NULL;
Uint16 format;
position_args *args = NULL;
Mix_QuerySpec(NULL, &format, NULL);
f = get_position_effect_func(format);
if (f == NULL)
return(0);
args = get_position_arg(channel);
if (!args)
return(0);
distance = 255 - distance; /* flip it to our scale. */
/* it's a no-op; unregister the effect, if it's registered. */
if ((distance == 255) && (args->left_u8 == 255) &&
(args->right_u8 == 255) && (args->in_use))
{
return(Mix_UnregisterEffect(channel, f));
}
args->distance_u8 = distance;
Sep 11, 2001
Sep 11, 2001
477
args->distance_f = ((float) distance) / 255.0f;
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
if (!args->in_use) {
args->in_use = 1;
return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
}
return(1);
}
int Mix_SetPosition(int channel, Sint16 angle, Uint8 distance)
{
Mix_EffectFunc_t f = NULL;
Uint16 format;
int channels;
position_args *args = NULL;
Uint8 left = 255, right = 255;
Mix_QuerySpec(NULL, &format, &channels);
f = get_position_effect_func(format);
if (f == NULL)
return(0);
/* unwind the angle...it'll be between 0 and 359. */
while (angle >= 360) angle -= 360;
while (angle < 0) angle += 360;
args = get_position_arg(channel);
if (!args)
return(0);
/* it's a no-op; unregister the effect, if it's registered. */
if ((!distance) && (!angle) && (args->in_use))
return(Mix_UnregisterEffect(channel, f));
if (channels == 2)
{
/*
* We only attenuate by position if the angle falls on the far side
* of center; That is, an angle that's due north would not attenuate
* either channel. Due west attenuates the right channel to 0.0, and
* due east attenuates the left channel to 0.0. Slightly east of
* center attenuates the left channel a little, and the right channel
* not at all. I think of this as occlusion by one's own head. :)
*
* ...so, we split our angle circle into four quadrants...
*/
if (angle < 90) {
left = 255 - ((Uint8) (255.0f * (((float) angle) / 89.0f)));
} else if (angle < 180) {
left = (Uint8) (255.0f * (((float) (angle - 90)) / 89.0f));
} else if (angle < 270) {
right = 255 - ((Uint8) (255.0f * (((float) (angle - 180)) / 89.0f)));
} else {
right = (Uint8) (255.0f * (((float) (angle - 270)) / 89.0f));
}
}
distance = 255 - distance; /* flip it to scale Mix_SetDistance() uses. */
args->left_u8 = left;
Sep 11, 2001
Sep 11, 2001
538
args->left_f = ((float) left) / 255.0f;
539
args->right_u8 = right;
Sep 11, 2001
Sep 11, 2001
540
args->right_f = ((float) right) / 255.0f;
541
args->distance_u8 = distance;
Sep 11, 2001
Sep 11, 2001
542
args->distance_f = ((float) distance) / 255.0f;
543
544
545
546
547
548
549
550
551
552
if (!args->in_use) {
args->in_use = 1;
return(Mix_RegisterEffect(channel, f, _Eff_PositionDone, (void *) args));
}
return(1);
}
/* end of effects_position.c ... */