comparison src/audio/SDL_audiocvt.c @ 3880:74e828c64315 SDL-1.2

Fixed bug #292 I might be on crack here. It looks like SDL_ConvertMono() in src/audio/SDL_audiocvt.c adds the left and right channels of a stereo stream together, and clamps the new mono channel if it would overflow. Shouldn't it be dividing by 2 to average the two sample points instead of clamping? Otherwise the mono sample point's volume doubles in the conversion. This would also make the conversion faster, as it replaces two branches per sample frame with a bitwise shift. --ryan.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 24 Sep 2006 15:45:37 +0000
parents 4e23720e4278
children a1b03ba2fcd0
comparison
equal deleted inserted replaced
3879:c82c1870c77a 3880:74e828c64315
42 42
43 src = cvt->buf; 43 src = cvt->buf;
44 dst = cvt->buf; 44 dst = cvt->buf;
45 for ( i=cvt->len_cvt/2; i; --i ) { 45 for ( i=cvt->len_cvt/2; i; --i ) {
46 sample = src[0] + src[1]; 46 sample = src[0] + src[1];
47 if ( sample > 255 ) { 47 *dst = (Uint8)(sample / 2);
48 *dst = 255;
49 } else {
50 *dst = (Uint8)sample;
51 }
52 src += 2; 48 src += 2;
53 dst += 1; 49 dst += 1;
54 } 50 }
55 } 51 }
56 break; 52 break;
60 56
61 src = (Sint8 *)cvt->buf; 57 src = (Sint8 *)cvt->buf;
62 dst = (Sint8 *)cvt->buf; 58 dst = (Sint8 *)cvt->buf;
63 for ( i=cvt->len_cvt/2; i; --i ) { 59 for ( i=cvt->len_cvt/2; i; --i ) {
64 sample = src[0] + src[1]; 60 sample = src[0] + src[1];
65 if ( sample > 127 ) { 61 *dst = (Sint8)(sample / 2);
66 *dst = 127;
67 } else
68 if ( sample < -128 ) {
69 *dst = -128;
70 } else {
71 *dst = (Sint8)sample;
72 }
73 src += 2; 62 src += 2;
74 dst += 1; 63 dst += 1;
75 } 64 }
76 } 65 }
77 break; 66 break;
83 dst = cvt->buf; 72 dst = cvt->buf;
84 if ( (format & 0x1000) == 0x1000 ) { 73 if ( (format & 0x1000) == 0x1000 ) {
85 for ( i=cvt->len_cvt/4; i; --i ) { 74 for ( i=cvt->len_cvt/4; i; --i ) {
86 sample = (Uint16)((src[0]<<8)|src[1])+ 75 sample = (Uint16)((src[0]<<8)|src[1])+
87 (Uint16)((src[2]<<8)|src[3]); 76 (Uint16)((src[2]<<8)|src[3]);
88 if ( sample > 65535 ) { 77 sample /= 2;
89 dst[0] = 0xFF; 78 dst[1] = (sample&0xFF);
90 dst[1] = 0xFF; 79 sample >>= 8;
91 } else { 80 dst[0] = (sample&0xFF);
92 dst[1] = (sample&0xFF);
93 sample >>= 8;
94 dst[0] = (sample&0xFF);
95 }
96 src += 4; 81 src += 4;
97 dst += 2; 82 dst += 2;
98 } 83 }
99 } else { 84 } else {
100 for ( i=cvt->len_cvt/4; i; --i ) { 85 for ( i=cvt->len_cvt/4; i; --i ) {
101 sample = (Uint16)((src[1]<<8)|src[0])+ 86 sample = (Uint16)((src[1]<<8)|src[0])+
102 (Uint16)((src[3]<<8)|src[2]); 87 (Uint16)((src[3]<<8)|src[2]);
103 if ( sample > 65535 ) { 88 sample /= 2;
104 dst[0] = 0xFF; 89 dst[0] = (sample&0xFF);
105 dst[1] = 0xFF; 90 sample >>= 8;
106 } else { 91 dst[1] = (sample&0xFF);
107 dst[0] = (sample&0xFF);
108 sample >>= 8;
109 dst[1] = (sample&0xFF);
110 }
111 src += 4; 92 src += 4;
112 dst += 2; 93 dst += 2;
113 } 94 }
114 } 95 }
115 } 96 }
122 dst = cvt->buf; 103 dst = cvt->buf;
123 if ( (format & 0x1000) == 0x1000 ) { 104 if ( (format & 0x1000) == 0x1000 ) {
124 for ( i=cvt->len_cvt/4; i; --i ) { 105 for ( i=cvt->len_cvt/4; i; --i ) {
125 sample = (Sint16)((src[0]<<8)|src[1])+ 106 sample = (Sint16)((src[0]<<8)|src[1])+
126 (Sint16)((src[2]<<8)|src[3]); 107 (Sint16)((src[2]<<8)|src[3]);
127 if ( sample > 32767 ) { 108 sample /= 2;
128 dst[0] = 0x7F; 109 dst[1] = (sample&0xFF);
129 dst[1] = 0xFF; 110 sample >>= 8;
130 } else 111 dst[0] = (sample&0xFF);
131 if ( sample < -32768 ) {
132 dst[0] = 0x80;
133 dst[1] = 0x00;
134 } else {
135 dst[1] = (sample&0xFF);
136 sample >>= 8;
137 dst[0] = (sample&0xFF);
138 }
139 src += 4; 112 src += 4;
140 dst += 2; 113 dst += 2;
141 } 114 }
142 } else { 115 } else {
143 for ( i=cvt->len_cvt/4; i; --i ) { 116 for ( i=cvt->len_cvt/4; i; --i ) {
144 sample = (Sint16)((src[1]<<8)|src[0])+ 117 sample = (Sint16)((src[1]<<8)|src[0])+
145 (Sint16)((src[3]<<8)|src[2]); 118 (Sint16)((src[3]<<8)|src[2]);
146 if ( sample > 32767 ) { 119 sample /= 2;
147 dst[1] = 0x7F; 120 dst[0] = (sample&0xFF);
148 dst[0] = 0xFF; 121 sample >>= 8;
149 } else 122 dst[1] = (sample&0xFF);
150 if ( sample < -32768 ) {
151 dst[1] = 0x80;
152 dst[0] = 0x00;
153 } else {
154 dst[0] = (sample&0xFF);
155 sample >>= 8;
156 dst[1] = (sample&0xFF);
157 }
158 src += 4; 123 src += 4;
159 dst += 2; 124 dst += 2;
160 } 125 }
161 } 126 }
162 } 127 }