This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_audiocvt.c
1075 lines (947 loc) · 33.9 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
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
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
26
/* Functions for audio drivers to perform runtime conversion of audio format */
#include "SDL_audio.h"
27
#include "SDL_audio_c.h"
28
29
/* #define DEBUG_CONVERT */
30
31
32
33
/* !!! FIXME */
#ifndef assert
#define assert(x)
34
35
#endif
36
/* Effectively mix right and left channels into a single channel */
37
38
static void SDLCALL
SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat format)
39
{
40
41
int i;
Sint32 sample;
42
43
#ifdef DEBUG_CONVERT
44
fprintf(stderr, "Converting to mono\n");
45
#endif
46
switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
47
48
49
50
51
52
53
54
case AUDIO_U8:
{
Uint8 *src, *dst;
src = cvt->buf;
dst = cvt->buf;
for (i = cvt->len_cvt / 2; i; --i) {
sample = src[0] + src[1];
55
*dst = (Uint8) (sample / 2);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
src += 2;
dst += 1;
}
}
break;
case AUDIO_S8:
{
Sint8 *src, *dst;
src = (Sint8 *) cvt->buf;
dst = (Sint8 *) cvt->buf;
for (i = cvt->len_cvt / 2; i; --i) {
sample = src[0] + src[1];
70
*dst = (Sint8) (sample / 2);
71
72
73
74
75
76
77
78
79
80
81
82
src += 2;
dst += 1;
}
}
break;
case AUDIO_U16:
{
Uint8 *src, *dst;
src = cvt->buf;
dst = cvt->buf;
83
if (SDL_AUDIO_ISBIGENDIAN(format)) {
84
85
86
for (i = cvt->len_cvt / 4; i; --i) {
sample = (Uint16) ((src[0] << 8) | src[1]) +
(Uint16) ((src[2] << 8) | src[3]);
87
88
89
90
sample /= 2;
dst[1] = (sample & 0xFF);
sample >>= 8;
dst[0] = (sample & 0xFF);
91
92
93
94
95
96
97
src += 4;
dst += 2;
}
} else {
for (i = cvt->len_cvt / 4; i; --i) {
sample = (Uint16) ((src[1] << 8) | src[0]) +
(Uint16) ((src[3] << 8) | src[2]);
98
99
100
101
sample /= 2;
dst[0] = (sample & 0xFF);
sample >>= 8;
dst[1] = (sample & 0xFF);
102
103
104
105
106
107
108
109
110
111
112
113
114
src += 4;
dst += 2;
}
}
}
break;
case AUDIO_S16:
{
Uint8 *src, *dst;
src = cvt->buf;
dst = cvt->buf;
115
if (SDL_AUDIO_ISBIGENDIAN(format)) {
116
117
118
for (i = cvt->len_cvt / 4; i; --i) {
sample = (Sint16) ((src[0] << 8) | src[1]) +
(Sint16) ((src[2] << 8) | src[3]);
119
120
121
122
sample /= 2;
dst[1] = (sample & 0xFF);
sample >>= 8;
dst[0] = (sample & 0xFF);
123
124
125
126
127
128
129
src += 4;
dst += 2;
}
} else {
for (i = cvt->len_cvt / 4; i; --i) {
sample = (Sint16) ((src[1] << 8) | src[0]) +
(Sint16) ((src[3] << 8) | src[2]);
130
131
132
133
sample /= 2;
dst[0] = (sample & 0xFF);
sample >>= 8;
dst[1] = (sample & 0xFF);
134
135
136
137
138
139
140
src += 4;
dst += 2;
}
}
}
break;
141
case AUDIO_S32:
142
{
143
144
145
146
147
const Uint32 *src = (const Uint32 *) cvt->buf;
Uint32 *dst = (Uint32 *) cvt->buf;
if (SDL_AUDIO_ISBIGENDIAN(format)) {
for (i = cvt->len_cvt / 8; i; --i, src += 2) {
const Sint64 added =
148
149
(((Sint64) (Sint32) SDL_SwapBE32(src[0])) +
((Sint64) (Sint32) SDL_SwapBE32(src[1])));
150
*(dst++) = SDL_SwapBE32((Uint32) ((Sint32) (added / 2)));
151
152
}
} else {
153
154
for (i = cvt->len_cvt / 8; i; --i, src += 2) {
const Sint64 added =
155
156
(((Sint64) (Sint32) SDL_SwapLE32(src[0])) +
((Sint64) (Sint32) SDL_SwapLE32(src[1])));
157
*(dst++) = SDL_SwapLE32((Uint32) ((Sint32) (added / 2)));
158
159
160
161
162
}
}
}
break;
163
case AUDIO_F32:
164
{
165
166
const float *src = (const float *) cvt->buf;
float *dst = (float *) cvt->buf;
167
168
if (SDL_AUDIO_ISBIGENDIAN(format)) {
for (i = cvt->len_cvt / 8; i; --i, src += 2) {
169
170
const float src1 = SDL_SwapFloatBE(src[0]);
const float src2 = SDL_SwapFloatBE(src[1]);
171
const double added = ((double) src1) + ((double) src2);
172
173
const float halved = (float) (added * 0.5);
*(dst++) = SDL_SwapFloatBE(halved);
174
175
}
} else {
176
for (i = cvt->len_cvt / 8; i; --i, src += 2) {
177
178
const float src1 = SDL_SwapFloatLE(src[0]);
const float src2 = SDL_SwapFloatLE(src[1]);
179
const double added = ((double) src1) + ((double) src2);
180
181
const float halved = (float) (added * 0.5);
*(dst++) = SDL_SwapFloatLE(halved);
182
183
184
185
186
}
}
}
break;
}
187
188
cvt->len_cvt /= 2;
189
190
191
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
}
192
193
194
}
195
196
197
/* Discard top 4 channels */
static void SDLCALL
SDL_ConvertStrip(SDL_AudioCVT * cvt, SDL_AudioFormat format)
198
{
199
int i;
200
201
#ifdef DEBUG_CONVERT
202
fprintf(stderr, "Converting down from 6 channels to stereo\n");
203
#endif
204
205
#define strip_chans_6_to_2(type) \
206
207
208
209
210
211
212
213
214
215
{ \
const type *src = (const type *) cvt->buf; \
type *dst = (type *) cvt->buf; \
for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
dst[0] = src[0]; \
dst[1] = src[1]; \
src += 6; \
dst += 2; \
} \
}
216
217
218
/* this function only cares about typesize, and data as a block of bits. */
switch (SDL_AUDIO_BITSIZE(format)) {
219
220
221
222
223
224
225
226
227
case 8:
strip_chans_6_to_2(Uint8);
break;
case 16:
strip_chans_6_to_2(Uint16);
break;
case 32:
strip_chans_6_to_2(Uint32);
break;
228
}
229
230
#undef strip_chans_6_to_2
231
232
233
234
235
236
cvt->len_cvt /= 3;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
}
}
237
238
239
240
241
242
243
/* Discard top 2 channels of 6 */
static void SDLCALL
SDL_ConvertStrip_2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
{
int i;
244
245
246
247
#ifdef DEBUG_CONVERT
fprintf(stderr, "Converting 6 down to quad\n");
#endif
248
249
#define strip_chans_6_to_4(type) \
250
251
252
253
254
255
256
257
258
259
260
{ \
const type *src = (const type *) cvt->buf; \
type *dst = (type *) cvt->buf; \
for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
dst[0] = src[0]; \
dst[1] = src[1]; \
dst[2] = src[2]; \
dst[3] = src[3]; \
src += 6; \
dst += 4; \
} \
261
}
262
263
264
/* this function only cares about typesize, and data as a block of bits. */
switch (SDL_AUDIO_BITSIZE(format)) {
265
266
267
268
269
270
271
272
273
case 8:
strip_chans_6_to_4(Uint8);
break;
case 16:
strip_chans_6_to_4(Uint16);
break;
case 32:
strip_chans_6_to_4(Uint32);
break;
274
275
}
276
#undef strip_chans_6_to_4
277
278
279
cvt->len_cvt /= 6;
cvt->len_cvt *= 4;
280
281
282
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
}
283
}
284
285
/* Duplicate a mono channel to both stereo channels */
286
287
static void SDLCALL
SDL_ConvertStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format)
288
{
289
int i;
290
291
#ifdef DEBUG_CONVERT
292
fprintf(stderr, "Converting to stereo\n");
293
#endif
294
295
#define dup_chans_1_to_2(type) \
296
297
298
299
300
301
302
303
{ \
const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
type *dst = (type *) (cvt->buf + cvt->len_cvt * 2); \
for (i = cvt->len_cvt / 2; i; --i, --src) { \
const type val = *src; \
dst -= 2; \
dst[0] = dst[1] = val; \
} \
304
}
305
306
307
/* this function only cares about typesize, and data as a block of bits. */
switch (SDL_AUDIO_BITSIZE(format)) {
308
309
310
311
312
313
314
315
316
case 8:
dup_chans_1_to_2(Uint8);
break;
case 16:
dup_chans_1_to_2(Uint16);
break;
case 32:
dup_chans_1_to_2(Uint32);
break;
317
318
}
319
#undef dup_chans_1_to_2
320
321
322
323
324
cvt->len_cvt *= 2;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
}
325
326
}
327
328
/* Duplicate a stereo channel to a pseudo-5.1 stream */
329
330
static void SDLCALL
SDL_ConvertSurround(SDL_AudioCVT * cvt, SDL_AudioFormat format)
331
{
332
int i;
333
334
#ifdef DEBUG_CONVERT
335
fprintf(stderr, "Converting stereo to surround\n");
336
#endif
337
338
switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
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
case AUDIO_U8:
{
Uint8 *src, *dst, lf, rf, ce;
src = (Uint8 *) (cvt->buf + cvt->len_cvt);
dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 3);
for (i = cvt->len_cvt; i; --i) {
dst -= 6;
src -= 2;
lf = src[0];
rf = src[1];
ce = (lf / 2) + (rf / 2);
dst[0] = lf;
dst[1] = rf;
dst[2] = lf - ce;
dst[3] = rf - ce;
dst[4] = ce;
dst[5] = ce;
}
}
break;
case AUDIO_S8:
{
Sint8 *src, *dst, lf, rf, ce;
src = (Sint8 *) cvt->buf + cvt->len_cvt;
dst = (Sint8 *) cvt->buf + cvt->len_cvt * 3;
for (i = cvt->len_cvt; i; --i) {
dst -= 6;
src -= 2;
lf = src[0];
rf = src[1];
ce = (lf / 2) + (rf / 2);
dst[0] = lf;
dst[1] = rf;
dst[2] = lf - ce;
dst[3] = rf - ce;
dst[4] = ce;
dst[5] = ce;
}
}
break;
case AUDIO_U16:
{
Uint8 *src, *dst;
Uint16 lf, rf, ce, lr, rr;
src = cvt->buf + cvt->len_cvt;
dst = cvt->buf + cvt->len_cvt * 3;
391
if (SDL_AUDIO_ISBIGENDIAN(format)) {
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
441
442
443
444
445
446
447
448
449
450
for (i = cvt->len_cvt / 4; i; --i) {
dst -= 12;
src -= 4;
lf = (Uint16) ((src[0] << 8) | src[1]);
rf = (Uint16) ((src[2] << 8) | src[3]);
ce = (lf / 2) + (rf / 2);
rr = lf - ce;
lr = rf - ce;
dst[1] = (lf & 0xFF);
dst[0] = ((lf >> 8) & 0xFF);
dst[3] = (rf & 0xFF);
dst[2] = ((rf >> 8) & 0xFF);
dst[1 + 4] = (lr & 0xFF);
dst[0 + 4] = ((lr >> 8) & 0xFF);
dst[3 + 4] = (rr & 0xFF);
dst[2 + 4] = ((rr >> 8) & 0xFF);
dst[1 + 8] = (ce & 0xFF);
dst[0 + 8] = ((ce >> 8) & 0xFF);
dst[3 + 8] = (ce & 0xFF);
dst[2 + 8] = ((ce >> 8) & 0xFF);
}
} else {
for (i = cvt->len_cvt / 4; i; --i) {
dst -= 12;
src -= 4;
lf = (Uint16) ((src[1] << 8) | src[0]);
rf = (Uint16) ((src[3] << 8) | src[2]);
ce = (lf / 2) + (rf / 2);
rr = lf - ce;
lr = rf - ce;
dst[0] = (lf & 0xFF);
dst[1] = ((lf >> 8) & 0xFF);
dst[2] = (rf & 0xFF);
dst[3] = ((rf >> 8) & 0xFF);
dst[0 + 4] = (lr & 0xFF);
dst[1 + 4] = ((lr >> 8) & 0xFF);
dst[2 + 4] = (rr & 0xFF);
dst[3 + 4] = ((rr >> 8) & 0xFF);
dst[0 + 8] = (ce & 0xFF);
dst[1 + 8] = ((ce >> 8) & 0xFF);
dst[2 + 8] = (ce & 0xFF);
dst[3 + 8] = ((ce >> 8) & 0xFF);
}
}
}
break;
case AUDIO_S16:
{
Uint8 *src, *dst;
Sint16 lf, rf, ce, lr, rr;
src = cvt->buf + cvt->len_cvt;
dst = cvt->buf + cvt->len_cvt * 3;
451
if (SDL_AUDIO_ISBIGENDIAN(format)) {
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
for (i = cvt->len_cvt / 4; i; --i) {
dst -= 12;
src -= 4;
lf = (Sint16) ((src[0] << 8) | src[1]);
rf = (Sint16) ((src[2] << 8) | src[3]);
ce = (lf / 2) + (rf / 2);
rr = lf - ce;
lr = rf - ce;
dst[1] = (lf & 0xFF);
dst[0] = ((lf >> 8) & 0xFF);
dst[3] = (rf & 0xFF);
dst[2] = ((rf >> 8) & 0xFF);
dst[1 + 4] = (lr & 0xFF);
dst[0 + 4] = ((lr >> 8) & 0xFF);
dst[3 + 4] = (rr & 0xFF);
dst[2 + 4] = ((rr >> 8) & 0xFF);
dst[1 + 8] = (ce & 0xFF);
dst[0 + 8] = ((ce >> 8) & 0xFF);
dst[3 + 8] = (ce & 0xFF);
dst[2 + 8] = ((ce >> 8) & 0xFF);
}
} else {
for (i = cvt->len_cvt / 4; i; --i) {
dst -= 12;
src -= 4;
lf = (Sint16) ((src[1] << 8) | src[0]);
rf = (Sint16) ((src[3] << 8) | src[2]);
ce = (lf / 2) + (rf / 2);
rr = lf - ce;
lr = rf - ce;
dst[0] = (lf & 0xFF);
dst[1] = ((lf >> 8) & 0xFF);
dst[2] = (rf & 0xFF);
dst[3] = ((rf >> 8) & 0xFF);
dst[0 + 4] = (lr & 0xFF);
dst[1 + 4] = ((lr >> 8) & 0xFF);
dst[2 + 4] = (rr & 0xFF);
dst[3 + 4] = ((rr >> 8) & 0xFF);
dst[0 + 8] = (ce & 0xFF);
dst[1 + 8] = ((ce >> 8) & 0xFF);
dst[2 + 8] = (ce & 0xFF);
dst[3 + 8] = ((ce >> 8) & 0xFF);
}
}
}
break;
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
538
539
540
541
542
543
case AUDIO_S32:
{
Sint32 lf, rf, ce;
const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
if (SDL_AUDIO_ISBIGENDIAN(format)) {
for (i = cvt->len_cvt / 8; i; --i) {
dst -= 6;
src -= 2;
lf = (Sint32) SDL_SwapBE32(src[0]);
rf = (Sint32) SDL_SwapBE32(src[1]);
ce = (lf / 2) + (rf / 2);
dst[0] = SDL_SwapBE32((Uint32) lf);
dst[1] = SDL_SwapBE32((Uint32) rf);
dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
dst[4] = SDL_SwapBE32((Uint32) ce);
dst[5] = SDL_SwapBE32((Uint32) ce);
}
} else {
for (i = cvt->len_cvt / 8; i; --i) {
dst -= 6;
src -= 2;
lf = (Sint32) SDL_SwapLE32(src[0]);
rf = (Sint32) SDL_SwapLE32(src[1]);
ce = (lf / 2) + (rf / 2);
dst[0] = src[0];
dst[1] = src[1];
dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
dst[4] = SDL_SwapLE32((Uint32) ce);
dst[5] = SDL_SwapLE32((Uint32) ce);
}
}
}
break;
case AUDIO_F32:
{
float lf, rf, ce;
544
545
const float *src = (const float *) cvt->buf + cvt->len_cvt;
float *dst = (float *) cvt->buf + cvt->len_cvt * 3;
546
547
548
549
550
if (SDL_AUDIO_ISBIGENDIAN(format)) {
for (i = cvt->len_cvt / 8; i; --i) {
dst -= 6;
src -= 2;
551
552
lf = SDL_SwapFloatBE(src[0]);
rf = SDL_SwapFloatBE(src[1]);
553
554
555
ce = (lf * 0.5f) + (rf * 0.5f);
dst[0] = src[0];
dst[1] = src[1];
556
557
558
dst[2] = SDL_SwapFloatBE(lf - ce);
dst[3] = SDL_SwapFloatBE(rf - ce);
dst[4] = dst[5] = SDL_SwapFloatBE(ce);
559
560
561
562
563
}
} else {
for (i = cvt->len_cvt / 8; i; --i) {
dst -= 6;
src -= 2;
564
565
lf = SDL_SwapFloatLE(src[0]);
rf = SDL_SwapFloatLE(src[1]);
566
567
568
ce = (lf * 0.5f) + (rf * 0.5f);
dst[0] = src[0];
dst[1] = src[1];
569
570
571
dst[2] = SDL_SwapFloatLE(lf - ce);
dst[3] = SDL_SwapFloatLE(rf - ce);
dst[4] = dst[5] = SDL_SwapFloatLE(ce);
572
573
574
575
576
}
}
}
break;
577
578
579
580
581
}
cvt->len_cvt *= 3;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
}
582
583
584
585
}
/* Duplicate a stereo channel to a pseudo-4.0 stream */
586
587
static void SDLCALL
SDL_ConvertSurround_4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
588
{
589
int i;
590
591
#ifdef DEBUG_CONVERT
592
fprintf(stderr, "Converting stereo to quad\n");
593
#endif
594
595
switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
case AUDIO_U8:
{
Uint8 *src, *dst, lf, rf, ce;
src = (Uint8 *) (cvt->buf + cvt->len_cvt);
dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 2);
for (i = cvt->len_cvt; i; --i) {
dst -= 4;
src -= 2;
lf = src[0];
rf = src[1];
ce = (lf / 2) + (rf / 2);
dst[0] = lf;
dst[1] = rf;
dst[2] = lf - ce;
dst[3] = rf - ce;
}
}
break;
case AUDIO_S8:
{
Sint8 *src, *dst, lf, rf, ce;
src = (Sint8 *) cvt->buf + cvt->len_cvt;
dst = (Sint8 *) cvt->buf + cvt->len_cvt * 2;
for (i = cvt->len_cvt; i; --i) {
dst -= 4;
src -= 2;
lf = src[0];
rf = src[1];
ce = (lf / 2) + (rf / 2);
dst[0] = lf;
dst[1] = rf;
dst[2] = lf - ce;
dst[3] = rf - ce;
}
}
break;
case AUDIO_U16:
{
Uint8 *src, *dst;
Uint16 lf, rf, ce, lr, rr;
src = cvt->buf + cvt->len_cvt;
dst = cvt->buf + cvt->len_cvt * 2;
644
if (SDL_AUDIO_ISBIGENDIAN(format)) {
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
for (i = cvt->len_cvt / 4; i; --i) {
dst -= 8;
src -= 4;
lf = (Uint16) ((src[0] << 8) | src[1]);
rf = (Uint16) ((src[2] << 8) | src[3]);
ce = (lf / 2) + (rf / 2);
rr = lf - ce;
lr = rf - ce;
dst[1] = (lf & 0xFF);
dst[0] = ((lf >> 8) & 0xFF);
dst[3] = (rf & 0xFF);
dst[2] = ((rf >> 8) & 0xFF);
dst[1 + 4] = (lr & 0xFF);
dst[0 + 4] = ((lr >> 8) & 0xFF);
dst[3 + 4] = (rr & 0xFF);
dst[2 + 4] = ((rr >> 8) & 0xFF);
}
} else {
for (i = cvt->len_cvt / 4; i; --i) {
dst -= 8;
src -= 4;
lf = (Uint16) ((src[1] << 8) | src[0]);
rf = (Uint16) ((src[3] << 8) | src[2]);
ce = (lf / 2) + (rf / 2);
rr = lf - ce;
lr = rf - ce;
dst[0] = (lf & 0xFF);
dst[1] = ((lf >> 8) & 0xFF);
dst[2] = (rf & 0xFF);
dst[3] = ((rf >> 8) & 0xFF);
dst[0 + 4] = (lr & 0xFF);
dst[1 + 4] = ((lr >> 8) & 0xFF);
dst[2 + 4] = (rr & 0xFF);
dst[3 + 4] = ((rr >> 8) & 0xFF);
}
}
}
break;
case AUDIO_S16:
{
Uint8 *src, *dst;
Sint16 lf, rf, ce, lr, rr;
src = cvt->buf + cvt->len_cvt;
dst = cvt->buf + cvt->len_cvt * 2;
694
if (SDL_AUDIO_ISBIGENDIAN(format)) {
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
for (i = cvt->len_cvt / 4; i; --i) {
dst -= 8;
src -= 4;
lf = (Sint16) ((src[0] << 8) | src[1]);
rf = (Sint16) ((src[2] << 8) | src[3]);
ce = (lf / 2) + (rf / 2);
rr = lf - ce;
lr = rf - ce;
dst[1] = (lf & 0xFF);
dst[0] = ((lf >> 8) & 0xFF);
dst[3] = (rf & 0xFF);
dst[2] = ((rf >> 8) & 0xFF);
dst[1 + 4] = (lr & 0xFF);
dst[0 + 4] = ((lr >> 8) & 0xFF);
dst[3 + 4] = (rr & 0xFF);
dst[2 + 4] = ((rr >> 8) & 0xFF);
}
} else {
for (i = cvt->len_cvt / 4; i; --i) {
dst -= 8;
src -= 4;
lf = (Sint16) ((src[1] << 8) | src[0]);
rf = (Sint16) ((src[3] << 8) | src[2]);
ce = (lf / 2) + (rf / 2);
rr = lf - ce;
lr = rf - ce;
dst[0] = (lf & 0xFF);
dst[1] = ((lf >> 8) & 0xFF);
dst[2] = (rf & 0xFF);
dst[3] = ((rf >> 8) & 0xFF);
dst[0 + 4] = (lr & 0xFF);
dst[1 + 4] = ((lr >> 8) & 0xFF);
dst[2 + 4] = (rr & 0xFF);
dst[3 + 4] = ((rr >> 8) & 0xFF);
}
}
}
break;
736
737
738
739
740
case AUDIO_S32:
{
const Uint32 *src = (const Uint32 *) (cvt->buf + cvt->len_cvt);
Uint32 *dst = (Uint32 *) (cvt->buf + cvt->len_cvt * 2);
Sint32 lf, rf, ce;
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
if (SDL_AUDIO_ISBIGENDIAN(format)) {
for (i = cvt->len_cvt / 8; i; --i) {
dst -= 4;
src -= 2;
lf = (Sint32) SDL_SwapBE32(src[0]);
rf = (Sint32) SDL_SwapBE32(src[1]);
ce = (lf / 2) + (rf / 2);
dst[0] = src[0];
dst[1] = src[1];
dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
}
} else {
for (i = cvt->len_cvt / 8; i; --i) {
dst -= 4;
src -= 2;
lf = (Sint32) SDL_SwapLE32(src[0]);
rf = (Sint32) SDL_SwapLE32(src[1]);
ce = (lf / 2) + (rf / 2);
dst[0] = src[0];
dst[1] = src[1];
dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
}
}
}
break;
769
770
771
772
773
}
cvt->len_cvt *= 2;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
}
774
775
776
}
777
778
int
SDL_ConvertAudio(SDL_AudioCVT * cvt)
779
{
780
781
782
/* !!! FIXME: (cvt) should be const; stack-copy it here. */
/* !!! FIXME: (actually, we can't...len_cvt needs to be updated. Grr.) */
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/* Make sure there's data to convert */
if (cvt->buf == NULL) {
SDL_SetError("No buffer allocated for conversion");
return (-1);
}
/* Return okay if no conversion is necessary */
cvt->len_cvt = cvt->len;
if (cvt->filters[0] == NULL) {
return (0);
}
/* Set up the conversion and go! */
cvt->filter_index = 0;
cvt->filters[0] (cvt, cvt->src_format);
return (0);
798
799
}
800
801
802
803
804
805
806
807
808
static SDL_AudioFilter
SDL_HandTunedTypeCVT(SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
{
/*
* Fill in any future conversions that are specialized to a
* processor, platform, compiler, or library here.
*/
809
return NULL; /* no specialized converter code available. */
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
}
/*
* Find a converter between two data types. We try to select a hand-tuned
* asm/vectorized/optimized function first, and then fallback to an
* autogenerated function that is customized to convert between two
* specific data types.
*/
static int
SDL_BuildAudioTypeCVT(SDL_AudioCVT * cvt,
SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
{
if (src_fmt != dst_fmt) {
const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt);
const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt);
SDL_AudioFilter filter = SDL_HandTunedTypeCVT(src_fmt, dst_fmt);
/* No hand-tuned converter? Try the autogenerated ones. */
if (filter == NULL) {
int i;
for (i = 0; sdl_audio_type_filters[i].filter != NULL; i++) {
const SDL_AudioTypeFilters *filt = &sdl_audio_type_filters[i];
if ((filt->src_fmt == src_fmt) && (filt->dst_fmt == dst_fmt)) {
filter = filt->filter;
break;
}
}
if (filter == NULL) {
840
return -1; /* Still no matching converter?! */
841
842
843
844
845
846
847
848
849
850
851
852
853
}
}
/* Update (cvt) with filter details... */
cvt->filters[cvt->filter_index++] = filter;
if (src_bitsize < dst_bitsize) {
const int mult = (dst_bitsize / src_bitsize);
cvt->len_mult *= mult;
cvt->len_ratio *= mult;
} else if (src_bitsize > dst_bitsize) {
cvt->len_ratio /= (src_bitsize / dst_bitsize);
}
854
return 1; /* added a converter. */
855
856
}
857
return 0; /* no conversion necessary. */
858
859
}
860
861
862
863
static SDL_AudioFilter
SDL_HandTunedResampleCVT(SDL_AudioCVT * cvt, int dst_channels,
int src_rate, int dst_rate)
864
{
865
866
867
/*
* Fill in any future conversions that are specialized to a
* processor, platform, compiler, or library here.
868
869
*/
870
return NULL; /* no specialized converter code available. */
871
872
}
873
874
static int
SDL_FindFrequencyMultiple(const int src_rate, const int dst_rate)
875
{
876
int retval = 0;
877
878
879
880
881
/* If we only built with the arbitrary resamplers, ignore multiples. */
#if !LESS_RESAMPLERS
int lo, hi;
int div;
882
883
884
885
assert(src_rate != 0);
assert(dst_rate != 0);
assert(src_rate != dst_rate);
886
887
888
889
if (src_rate < dst_rate) {
lo = src_rate;
hi = dst_rate;
890
} else {
891
892
lo = dst_rate;
hi = src_rate;
893
894
}
895
896
/* zero means "not a supported multiple" ... we only do 2x and 4x. */
if ((hi % lo) != 0)
897
return 0; /* not a multiple. */
898
899
900
901
div = hi / lo;
retval = ((div == 2) || (div == 4)) ? div : 0;
#endif
902
903
return retval;
904
905
}
906
907
908
static int
SDL_BuildAudioResampleCVT(SDL_AudioCVT * cvt, int dst_channels,
int src_rate, int dst_rate)
909
{
910
911
912
if (src_rate != dst_rate) {
SDL_AudioFilter filter = SDL_HandTunedResampleCVT(cvt, dst_channels,
src_rate, dst_rate);
913
914
915
916
917
/* No hand-tuned converter? Try the autogenerated ones. */
if (filter == NULL) {
int i;
const int upsample = (src_rate < dst_rate) ? 1 : 0;
918
919
const int multiple =
SDL_FindFrequencyMultiple(src_rate, dst_rate);
920
921
922
923
924
925
926
927
928
929
930
for (i = 0; sdl_audio_rate_filters[i].filter != NULL; i++) {
const SDL_AudioRateFilters *filt = &sdl_audio_rate_filters[i];
if ((filt->fmt == cvt->dst_format) &&
(filt->channels == dst_channels) &&
(filt->upsample == upsample) &&
(filt->multiple == multiple)) {
filter = filt->filter;
break;
}
}
931
932
933
934
935
if (filter == NULL) {
return -1; /* Still no matching converter?! */
}
}
936
937
938
939
940
/* Update (cvt) with filter details... */
cvt->filters[cvt->filter_index++] = filter;
if (src_rate < dst_rate) {
const double mult = ((double) dst_rate) / ((double) src_rate);
941
cvt->len_mult *= (int) SDL_ceil(mult);
942
943
944
945
cvt->len_ratio *= mult;
} else {
cvt->len_ratio /= ((double) src_rate) / ((double) dst_rate);
}
946
947
return 1; /* added a converter. */
948
949
}
950
return 0; /* no conversion necessary. */
951
}
952
953
954
955
956
/* Creates a set of audio filters to convert from one format to another.
Returns -1 if the format conversion is not supported, 0 if there's
no conversion needed, or 1 if the audio filter is set up.
957
*/
958
959
960
int
SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
961
962
SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate,
SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate)
963
{
964
965
966
967
968
969
970
971
/*
* !!! FIXME: reorder filters based on which grow/shrink the buffer.
* !!! FIXME: ideally, we should do everything that shrinks the buffer
* !!! FIXME: first, so we don't have to process as many bytes in a given
* !!! FIXME: filter and abuse the CPU cache less. This might not be as
* !!! FIXME: good in practice as it sounds in theory, though.
*/
972
973
974
975
976
977
978
/* there are no unsigned types over 16 bits, so catch this upfront. */
if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
return -1;
}
if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
return -1;
}
979
980
981
982
983
/* prevent possible divisions by zero, etc. */
if ((src_rate == 0) || (dst_rate == 0)) {
return -1;
}
984
#ifdef DEBUG_CONVERT
985
printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
986
987
src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
#endif
988
989
/* Start off with no conversion necessary */
990
SDL_zerop(cvt);
991
992
cvt->src_format = src_fmt;
cvt->dst_format = dst_fmt;
993
994
995
996
997
cvt->needed = 0;
cvt->filter_index = 0;
cvt->filters[0] = NULL;
cvt->len_mult = 1;
cvt->len_ratio = 1.0;
998
cvt->rate_incr = ((double) dst_rate) / ((double) src_rate);
999
1000
/* Convert data types, if necessary. Updates (cvt). */