comparison src/audio/SDL_audiocvt.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
26 #include "SDL_audio.h" 26 #include "SDL_audio.h"
27 27
28 28
29 /* Effectively mix right and left channels into a single channel */ 29 /* Effectively mix right and left channels into a single channel */
30 void SDLCALL 30 void SDLCALL
31 SDL_ConvertMono (SDL_AudioCVT * cvt, Uint16 format) 31 SDL_ConvertMono(SDL_AudioCVT * cvt, Uint16 format)
32 { 32 {
33 int i; 33 int i;
34 Sint32 sample; 34 Sint32 sample;
35 35
36 #ifdef DEBUG_CONVERT 36 #ifdef DEBUG_CONVERT
37 fprintf (stderr, "Converting to mono\n"); 37 fprintf(stderr, "Converting to mono\n");
38 #endif 38 #endif
39 switch (format & 0x8018) { 39 switch (format & 0x8018) {
40 40
41 case AUDIO_U8: 41 case AUDIO_U8:
42 { 42 {
170 } 170 }
171 } 171 }
172 172
173 /* Discard top 4 channels */ 173 /* Discard top 4 channels */
174 void SDLCALL 174 void SDLCALL
175 SDL_ConvertStrip (SDL_AudioCVT * cvt, Uint16 format) 175 SDL_ConvertStrip(SDL_AudioCVT * cvt, Uint16 format)
176 { 176 {
177 int i; 177 int i;
178 Sint32 lsample, rsample; 178 Sint32 lsample, rsample;
179 179
180 #ifdef DEBUG_CONVERT 180 #ifdef DEBUG_CONVERT
181 fprintf (stderr, "Converting down to stereo\n"); 181 fprintf(stderr, "Converting down to stereo\n");
182 #endif 182 #endif
183 switch (format & 0x8018) { 183 switch (format & 0x8018) {
184 184
185 case AUDIO_U8: 185 case AUDIO_U8:
186 { 186 {
291 } 291 }
292 292
293 293
294 /* Discard top 2 channels of 6 */ 294 /* Discard top 2 channels of 6 */
295 void SDLCALL 295 void SDLCALL
296 SDL_ConvertStrip_2 (SDL_AudioCVT * cvt, Uint16 format) 296 SDL_ConvertStrip_2(SDL_AudioCVT * cvt, Uint16 format)
297 { 297 {
298 int i; 298 int i;
299 Sint32 lsample, rsample; 299 Sint32 lsample, rsample;
300 300
301 #ifdef DEBUG_CONVERT 301 #ifdef DEBUG_CONVERT
302 fprintf (stderr, "Converting 6 down to quad\n"); 302 fprintf(stderr, "Converting 6 down to quad\n");
303 #endif 303 #endif
304 switch (format & 0x8018) { 304 switch (format & 0x8018) {
305 305
306 case AUDIO_U8: 306 case AUDIO_U8:
307 { 307 {
411 } 411 }
412 } 412 }
413 413
414 /* Duplicate a mono channel to both stereo channels */ 414 /* Duplicate a mono channel to both stereo channels */
415 void SDLCALL 415 void SDLCALL
416 SDL_ConvertStereo (SDL_AudioCVT * cvt, Uint16 format) 416 SDL_ConvertStereo(SDL_AudioCVT * cvt, Uint16 format)
417 { 417 {
418 int i; 418 int i;
419 419
420 #ifdef DEBUG_CONVERT 420 #ifdef DEBUG_CONVERT
421 fprintf (stderr, "Converting to stereo\n"); 421 fprintf(stderr, "Converting to stereo\n");
422 #endif 422 #endif
423 if ((format & 0xFF) == 16) { 423 if ((format & 0xFF) == 16) {
424 Uint16 *src, *dst; 424 Uint16 *src, *dst;
425 425
426 src = (Uint16 *) (cvt->buf + cvt->len_cvt); 426 src = (Uint16 *) (cvt->buf + cvt->len_cvt);
450 } 450 }
451 451
452 452
453 /* Duplicate a stereo channel to a pseudo-5.1 stream */ 453 /* Duplicate a stereo channel to a pseudo-5.1 stream */
454 void SDLCALL 454 void SDLCALL
455 SDL_ConvertSurround (SDL_AudioCVT * cvt, Uint16 format) 455 SDL_ConvertSurround(SDL_AudioCVT * cvt, Uint16 format)
456 { 456 {
457 int i; 457 int i;
458 458
459 #ifdef DEBUG_CONVERT 459 #ifdef DEBUG_CONVERT
460 fprintf (stderr, "Converting stereo to surround\n"); 460 fprintf(stderr, "Converting stereo to surround\n");
461 #endif 461 #endif
462 switch (format & 0x8018) { 462 switch (format & 0x8018) {
463 463
464 case AUDIO_U8: 464 case AUDIO_U8:
465 { 465 {
632 } 632 }
633 633
634 634
635 /* Duplicate a stereo channel to a pseudo-4.0 stream */ 635 /* Duplicate a stereo channel to a pseudo-4.0 stream */
636 void SDLCALL 636 void SDLCALL
637 SDL_ConvertSurround_4 (SDL_AudioCVT * cvt, Uint16 format) 637 SDL_ConvertSurround_4(SDL_AudioCVT * cvt, Uint16 format)
638 { 638 {
639 int i; 639 int i;
640 640
641 #ifdef DEBUG_CONVERT 641 #ifdef DEBUG_CONVERT
642 fprintf (stderr, "Converting stereo to quad\n"); 642 fprintf(stderr, "Converting stereo to quad\n");
643 #endif 643 #endif
644 switch (format & 0x8018) { 644 switch (format & 0x8018) {
645 645
646 case AUDIO_U8: 646 case AUDIO_U8:
647 { 647 {
790 } 790 }
791 791
792 792
793 /* Convert 8-bit to 16-bit - LSB */ 793 /* Convert 8-bit to 16-bit - LSB */
794 void SDLCALL 794 void SDLCALL
795 SDL_Convert16LSB (SDL_AudioCVT * cvt, Uint16 format) 795 SDL_Convert16LSB(SDL_AudioCVT * cvt, Uint16 format)
796 { 796 {
797 int i; 797 int i;
798 Uint8 *src, *dst; 798 Uint8 *src, *dst;
799 799
800 #ifdef DEBUG_CONVERT 800 #ifdef DEBUG_CONVERT
801 fprintf (stderr, "Converting to 16-bit LSB\n"); 801 fprintf(stderr, "Converting to 16-bit LSB\n");
802 #endif 802 #endif
803 src = cvt->buf + cvt->len_cvt; 803 src = cvt->buf + cvt->len_cvt;
804 dst = cvt->buf + cvt->len_cvt * 2; 804 dst = cvt->buf + cvt->len_cvt * 2;
805 for (i = cvt->len_cvt; i; --i) { 805 for (i = cvt->len_cvt; i; --i) {
806 src -= 1; 806 src -= 1;
815 } 815 }
816 } 816 }
817 817
818 /* Convert 8-bit to 16-bit - MSB */ 818 /* Convert 8-bit to 16-bit - MSB */
819 void SDLCALL 819 void SDLCALL
820 SDL_Convert16MSB (SDL_AudioCVT * cvt, Uint16 format) 820 SDL_Convert16MSB(SDL_AudioCVT * cvt, Uint16 format)
821 { 821 {
822 int i; 822 int i;
823 Uint8 *src, *dst; 823 Uint8 *src, *dst;
824 824
825 #ifdef DEBUG_CONVERT 825 #ifdef DEBUG_CONVERT
826 fprintf (stderr, "Converting to 16-bit MSB\n"); 826 fprintf(stderr, "Converting to 16-bit MSB\n");
827 #endif 827 #endif
828 src = cvt->buf + cvt->len_cvt; 828 src = cvt->buf + cvt->len_cvt;
829 dst = cvt->buf + cvt->len_cvt * 2; 829 dst = cvt->buf + cvt->len_cvt * 2;
830 for (i = cvt->len_cvt; i; --i) { 830 for (i = cvt->len_cvt; i; --i) {
831 src -= 1; 831 src -= 1;
840 } 840 }
841 } 841 }
842 842
843 /* Convert 16-bit to 8-bit */ 843 /* Convert 16-bit to 8-bit */
844 void SDLCALL 844 void SDLCALL
845 SDL_Convert8 (SDL_AudioCVT * cvt, Uint16 format) 845 SDL_Convert8(SDL_AudioCVT * cvt, Uint16 format)
846 { 846 {
847 int i; 847 int i;
848 Uint8 *src, *dst; 848 Uint8 *src, *dst;
849 849
850 #ifdef DEBUG_CONVERT 850 #ifdef DEBUG_CONVERT
851 fprintf (stderr, "Converting to 8-bit\n"); 851 fprintf(stderr, "Converting to 8-bit\n");
852 #endif 852 #endif
853 src = cvt->buf; 853 src = cvt->buf;
854 dst = cvt->buf; 854 dst = cvt->buf;
855 if ((format & 0x1000) != 0x1000) { /* Little endian */ 855 if ((format & 0x1000) != 0x1000) { /* Little endian */
856 ++src; 856 ++src;
867 } 867 }
868 } 868 }
869 869
870 /* Toggle signed/unsigned */ 870 /* Toggle signed/unsigned */
871 void SDLCALL 871 void SDLCALL
872 SDL_ConvertSign (SDL_AudioCVT * cvt, Uint16 format) 872 SDL_ConvertSign(SDL_AudioCVT * cvt, Uint16 format)
873 { 873 {
874 int i; 874 int i;
875 Uint8 *data; 875 Uint8 *data;
876 876
877 #ifdef DEBUG_CONVERT 877 #ifdef DEBUG_CONVERT
878 fprintf (stderr, "Converting audio signedness\n"); 878 fprintf(stderr, "Converting audio signedness\n");
879 #endif 879 #endif
880 data = cvt->buf; 880 data = cvt->buf;
881 if ((format & 0xFF) == 16) { 881 if ((format & 0xFF) == 16) {
882 if ((format & 0x1000) != 0x1000) { /* Little endian */ 882 if ((format & 0x1000) != 0x1000) { /* Little endian */
883 ++data; 883 ++data;
897 } 897 }
898 } 898 }
899 899
900 /* Toggle endianness */ 900 /* Toggle endianness */
901 void SDLCALL 901 void SDLCALL
902 SDL_ConvertEndian (SDL_AudioCVT * cvt, Uint16 format) 902 SDL_ConvertEndian(SDL_AudioCVT * cvt, Uint16 format)
903 { 903 {
904 int i; 904 int i;
905 Uint8 *data, tmp; 905 Uint8 *data, tmp;
906 906
907 #ifdef DEBUG_CONVERT 907 #ifdef DEBUG_CONVERT
908 fprintf (stderr, "Converting audio endianness\n"); 908 fprintf(stderr, "Converting audio endianness\n");
909 #endif 909 #endif
910 data = cvt->buf; 910 data = cvt->buf;
911 for (i = cvt->len_cvt / 2; i; --i) { 911 for (i = cvt->len_cvt / 2; i; --i) {
912 tmp = data[0]; 912 tmp = data[0];
913 data[0] = data[1]; 913 data[0] = data[1];
920 } 920 }
921 } 921 }
922 922
923 /* Convert rate up by multiple of 2 */ 923 /* Convert rate up by multiple of 2 */
924 void SDLCALL 924 void SDLCALL
925 SDL_RateMUL2 (SDL_AudioCVT * cvt, Uint16 format) 925 SDL_RateMUL2(SDL_AudioCVT * cvt, Uint16 format)
926 { 926 {
927 int i; 927 int i;
928 Uint8 *src, *dst; 928 Uint8 *src, *dst;
929 929
930 #ifdef DEBUG_CONVERT 930 #ifdef DEBUG_CONVERT
931 fprintf (stderr, "Converting audio rate * 2\n"); 931 fprintf(stderr, "Converting audio rate * 2\n");
932 #endif 932 #endif
933 src = cvt->buf + cvt->len_cvt; 933 src = cvt->buf + cvt->len_cvt;
934 dst = cvt->buf + cvt->len_cvt * 2; 934 dst = cvt->buf + cvt->len_cvt * 2;
935 switch (format & 0xFF) { 935 switch (format & 0xFF) {
936 case 8: 936 case 8:
959 } 959 }
960 960
961 961
962 /* Convert rate up by multiple of 2, for stereo */ 962 /* Convert rate up by multiple of 2, for stereo */
963 void SDLCALL 963 void SDLCALL
964 SDL_RateMUL2_c2 (SDL_AudioCVT * cvt, Uint16 format) 964 SDL_RateMUL2_c2(SDL_AudioCVT * cvt, Uint16 format)
965 { 965 {
966 int i; 966 int i;
967 Uint8 *src, *dst; 967 Uint8 *src, *dst;
968 968
969 #ifdef DEBUG_CONVERT 969 #ifdef DEBUG_CONVERT
970 fprintf (stderr, "Converting audio rate * 2\n"); 970 fprintf(stderr, "Converting audio rate * 2\n");
971 #endif 971 #endif
972 src = cvt->buf + cvt->len_cvt; 972 src = cvt->buf + cvt->len_cvt;
973 dst = cvt->buf + cvt->len_cvt * 2; 973 dst = cvt->buf + cvt->len_cvt * 2;
974 switch (format & 0xFF) { 974 switch (format & 0xFF) {
975 case 8: 975 case 8:
1003 } 1003 }
1004 } 1004 }
1005 1005
1006 /* Convert rate up by multiple of 2, for quad */ 1006 /* Convert rate up by multiple of 2, for quad */
1007 void SDLCALL 1007 void SDLCALL
1008 SDL_RateMUL2_c4 (SDL_AudioCVT * cvt, Uint16 format) 1008 SDL_RateMUL2_c4(SDL_AudioCVT * cvt, Uint16 format)
1009 { 1009 {
1010 int i; 1010 int i;
1011 Uint8 *src, *dst; 1011 Uint8 *src, *dst;
1012 1012
1013 #ifdef DEBUG_CONVERT 1013 #ifdef DEBUG_CONVERT
1014 fprintf (stderr, "Converting audio rate * 2\n"); 1014 fprintf(stderr, "Converting audio rate * 2\n");
1015 #endif 1015 #endif
1016 src = cvt->buf + cvt->len_cvt; 1016 src = cvt->buf + cvt->len_cvt;
1017 dst = cvt->buf + cvt->len_cvt * 2; 1017 dst = cvt->buf + cvt->len_cvt * 2;
1018 switch (format & 0xFF) { 1018 switch (format & 0xFF) {
1019 case 8: 1019 case 8:
1060 } 1060 }
1061 1061
1062 1062
1063 /* Convert rate up by multiple of 2, for 5.1 */ 1063 /* Convert rate up by multiple of 2, for 5.1 */
1064 void SDLCALL 1064 void SDLCALL
1065 SDL_RateMUL2_c6 (SDL_AudioCVT * cvt, Uint16 format) 1065 SDL_RateMUL2_c6(SDL_AudioCVT * cvt, Uint16 format)
1066 { 1066 {
1067 int i; 1067 int i;
1068 Uint8 *src, *dst; 1068 Uint8 *src, *dst;
1069 1069
1070 #ifdef DEBUG_CONVERT 1070 #ifdef DEBUG_CONVERT
1071 fprintf (stderr, "Converting audio rate * 2\n"); 1071 fprintf(stderr, "Converting audio rate * 2\n");
1072 #endif 1072 #endif
1073 src = cvt->buf + cvt->len_cvt; 1073 src = cvt->buf + cvt->len_cvt;
1074 dst = cvt->buf + cvt->len_cvt * 2; 1074 dst = cvt->buf + cvt->len_cvt * 2;
1075 switch (format & 0xFF) { 1075 switch (format & 0xFF) {
1076 case 8: 1076 case 8:
1128 } 1128 }
1129 } 1129 }
1130 1130
1131 /* Convert rate down by multiple of 2 */ 1131 /* Convert rate down by multiple of 2 */
1132 void SDLCALL 1132 void SDLCALL
1133 SDL_RateDIV2 (SDL_AudioCVT * cvt, Uint16 format) 1133 SDL_RateDIV2(SDL_AudioCVT * cvt, Uint16 format)
1134 { 1134 {
1135 int i; 1135 int i;
1136 Uint8 *src, *dst; 1136 Uint8 *src, *dst;
1137 1137
1138 #ifdef DEBUG_CONVERT 1138 #ifdef DEBUG_CONVERT
1139 fprintf (stderr, "Converting audio rate / 2\n"); 1139 fprintf(stderr, "Converting audio rate / 2\n");
1140 #endif 1140 #endif
1141 src = cvt->buf; 1141 src = cvt->buf;
1142 dst = cvt->buf; 1142 dst = cvt->buf;
1143 switch (format & 0xFF) { 1143 switch (format & 0xFF) {
1144 case 8: 1144 case 8:
1164 } 1164 }
1165 1165
1166 1166
1167 /* Convert rate down by multiple of 2, for stereo */ 1167 /* Convert rate down by multiple of 2, for stereo */
1168 void SDLCALL 1168 void SDLCALL
1169 SDL_RateDIV2_c2 (SDL_AudioCVT * cvt, Uint16 format) 1169 SDL_RateDIV2_c2(SDL_AudioCVT * cvt, Uint16 format)
1170 { 1170 {
1171 int i; 1171 int i;
1172 Uint8 *src, *dst; 1172 Uint8 *src, *dst;
1173 1173
1174 #ifdef DEBUG_CONVERT 1174 #ifdef DEBUG_CONVERT
1175 fprintf (stderr, "Converting audio rate / 2\n"); 1175 fprintf(stderr, "Converting audio rate / 2\n");
1176 #endif 1176 #endif
1177 src = cvt->buf; 1177 src = cvt->buf;
1178 dst = cvt->buf; 1178 dst = cvt->buf;
1179 switch (format & 0xFF) { 1179 switch (format & 0xFF) {
1180 case 8: 1180 case 8:
1203 } 1203 }
1204 1204
1205 1205
1206 /* Convert rate down by multiple of 2, for quad */ 1206 /* Convert rate down by multiple of 2, for quad */
1207 void SDLCALL 1207 void SDLCALL
1208 SDL_RateDIV2_c4 (SDL_AudioCVT * cvt, Uint16 format) 1208 SDL_RateDIV2_c4(SDL_AudioCVT * cvt, Uint16 format)
1209 { 1209 {
1210 int i; 1210 int i;
1211 Uint8 *src, *dst; 1211 Uint8 *src, *dst;
1212 1212
1213 #ifdef DEBUG_CONVERT 1213 #ifdef DEBUG_CONVERT
1214 fprintf (stderr, "Converting audio rate / 2\n"); 1214 fprintf(stderr, "Converting audio rate / 2\n");
1215 #endif 1215 #endif
1216 src = cvt->buf; 1216 src = cvt->buf;
1217 dst = cvt->buf; 1217 dst = cvt->buf;
1218 switch (format & 0xFF) { 1218 switch (format & 0xFF) {
1219 case 8: 1219 case 8:
1247 } 1247 }
1248 } 1248 }
1249 1249
1250 /* Convert rate down by multiple of 2, for 5.1 */ 1250 /* Convert rate down by multiple of 2, for 5.1 */
1251 void SDLCALL 1251 void SDLCALL
1252 SDL_RateDIV2_c6 (SDL_AudioCVT * cvt, Uint16 format) 1252 SDL_RateDIV2_c6(SDL_AudioCVT * cvt, Uint16 format)
1253 { 1253 {
1254 int i; 1254 int i;
1255 Uint8 *src, *dst; 1255 Uint8 *src, *dst;
1256 1256
1257 #ifdef DEBUG_CONVERT 1257 #ifdef DEBUG_CONVERT
1258 fprintf (stderr, "Converting audio rate / 2\n"); 1258 fprintf(stderr, "Converting audio rate / 2\n");
1259 #endif 1259 #endif
1260 src = cvt->buf; 1260 src = cvt->buf;
1261 dst = cvt->buf; 1261 dst = cvt->buf;
1262 switch (format & 0xFF) { 1262 switch (format & 0xFF) {
1263 case 8: 1263 case 8:
1297 } 1297 }
1298 } 1298 }
1299 1299
1300 /* Very slow rate conversion routine */ 1300 /* Very slow rate conversion routine */
1301 void SDLCALL 1301 void SDLCALL
1302 SDL_RateSLOW (SDL_AudioCVT * cvt, Uint16 format) 1302 SDL_RateSLOW(SDL_AudioCVT * cvt, Uint16 format)
1303 { 1303 {
1304 double ipos; 1304 double ipos;
1305 int i, clen; 1305 int i, clen;
1306 1306
1307 #ifdef DEBUG_CONVERT 1307 #ifdef DEBUG_CONVERT
1308 fprintf (stderr, "Converting audio rate * %4.4f\n", 1.0 / cvt->rate_incr); 1308 fprintf(stderr, "Converting audio rate * %4.4f\n", 1.0 / cvt->rate_incr);
1309 #endif 1309 #endif
1310 clen = (int) ((double) cvt->len_cvt / cvt->rate_incr); 1310 clen = (int) ((double) cvt->len_cvt / cvt->rate_incr);
1311 if (cvt->rate_incr > 1.0) { 1311 if (cvt->rate_incr > 1.0) {
1312 switch (format & 0xFF) { 1312 switch (format & 0xFF) {
1313 case 8: 1313 case 8:
1376 cvt->filters[cvt->filter_index] (cvt, format); 1376 cvt->filters[cvt->filter_index] (cvt, format);
1377 } 1377 }
1378 } 1378 }
1379 1379
1380 int 1380 int
1381 SDL_ConvertAudio (SDL_AudioCVT * cvt) 1381 SDL_ConvertAudio(SDL_AudioCVT * cvt)
1382 { 1382 {
1383 /* Make sure there's data to convert */ 1383 /* Make sure there's data to convert */
1384 if (cvt->buf == NULL) { 1384 if (cvt->buf == NULL) {
1385 SDL_SetError ("No buffer allocated for conversion"); 1385 SDL_SetError("No buffer allocated for conversion");
1386 return (-1); 1386 return (-1);
1387 } 1387 }
1388 /* Return okay if no conversion is necessary */ 1388 /* Return okay if no conversion is necessary */
1389 cvt->len_cvt = cvt->len; 1389 cvt->len_cvt = cvt->len;
1390 if (cvt->filters[0] == NULL) { 1390 if (cvt->filters[0] == NULL) {
1401 Returns -1 if the format conversion is not supported, or 1 if the 1401 Returns -1 if the format conversion is not supported, or 1 if the
1402 audio filter is set up. 1402 audio filter is set up.
1403 */ 1403 */
1404 1404
1405 int 1405 int
1406 SDL_BuildAudioCVT (SDL_AudioCVT * cvt, 1406 SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
1407 Uint16 src_format, Uint8 src_channels, int src_rate, 1407 Uint16 src_format, Uint8 src_channels, int src_rate,
1408 Uint16 dst_format, Uint8 dst_channels, int dst_rate) 1408 Uint16 dst_format, Uint8 dst_channels, int dst_rate)
1409 { 1409 {
1410 /*printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n", 1410 /*printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
1411 src_format, dst_format, src_channels, dst_channels, src_rate, dst_rate);*/ 1411 src_format, dst_format, src_channels, dst_channels, src_rate, dst_rate);*/
1412 /* Start off with no conversion necessary */ 1412 /* Start off with no conversion necessary */
1413 cvt->needed = 0; 1413 cvt->needed = 0;