src/audio/SDL_audiocvt.c
changeset 942 41a59de7f2ed
parent 769 b8d311d90021
child 1011 4095d9ca23f2
     1.1 --- a/src/audio/SDL_audiocvt.c	Sat Aug 21 11:34:24 2004 +0000
     1.2 +++ b/src/audio/SDL_audiocvt.c	Sat Aug 21 12:27:02 2004 +0000
     1.3 @@ -175,6 +175,244 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 +/* Discard top 4 channels */
     1.8 +void SDL_ConvertStrip(SDL_AudioCVT *cvt, Uint16 format)
     1.9 +{
    1.10 +	int i;
    1.11 +	Sint32 lsample, rsample;
    1.12 +
    1.13 +#ifdef DEBUG_CONVERT
    1.14 +	fprintf(stderr, "Converting down to stereo\n");
    1.15 +#endif
    1.16 +	switch (format&0x8018) {
    1.17 +
    1.18 +		case AUDIO_U8: {
    1.19 +			Uint8 *src, *dst;
    1.20 +
    1.21 +			src = cvt->buf;
    1.22 +			dst = cvt->buf;
    1.23 +			for ( i=cvt->len_cvt/6; i; --i ) {
    1.24 +				lsample = src[0];
    1.25 +				rsample = src[1];
    1.26 +				dst[0] = lsample;
    1.27 +				dst[1] = rsample;
    1.28 +				src += 6;
    1.29 +				dst += 2;
    1.30 +			}
    1.31 +		}
    1.32 +		break;
    1.33 +
    1.34 +		case AUDIO_S8: {
    1.35 +			Sint8 *src, *dst;
    1.36 +
    1.37 +			src = (Sint8 *)cvt->buf;
    1.38 +			dst = (Sint8 *)cvt->buf;
    1.39 +			for ( i=cvt->len_cvt/6; i; --i ) {
    1.40 +				lsample = src[0];
    1.41 +				rsample = src[1];
    1.42 +				dst[0] = lsample;
    1.43 +				dst[1] = rsample;
    1.44 +				src += 6;
    1.45 +				dst += 2;
    1.46 +			}
    1.47 +		}
    1.48 +		break;
    1.49 +
    1.50 +		case AUDIO_U16: {
    1.51 +			Uint8 *src, *dst;
    1.52 +
    1.53 +			src = cvt->buf;
    1.54 +			dst = cvt->buf;
    1.55 +			if ( (format & 0x1000) == 0x1000 ) {
    1.56 +				for ( i=cvt->len_cvt/12; i; --i ) {
    1.57 +					lsample = (Uint16)((src[0]<<8)|src[1]);
    1.58 +					rsample = (Uint16)((src[2]<<8)|src[3]);
    1.59 +						dst[1] = (lsample&0xFF);
    1.60 +						lsample >>= 8;
    1.61 +						dst[0] = (lsample&0xFF);
    1.62 +						dst[3] = (rsample&0xFF);
    1.63 +						rsample >>= 8;
    1.64 +						dst[2] = (rsample&0xFF);
    1.65 +					src += 12;
    1.66 +					dst += 4;
    1.67 +				}
    1.68 +			} else {
    1.69 +				for ( i=cvt->len_cvt/12; i; --i ) {
    1.70 +					lsample = (Uint16)((src[1]<<8)|src[0]);
    1.71 +					rsample = (Uint16)((src[3]<<8)|src[2]);
    1.72 +						dst[0] = (lsample&0xFF);
    1.73 +						lsample >>= 8;
    1.74 +						dst[1] = (lsample&0xFF);
    1.75 +						dst[2] = (rsample&0xFF);
    1.76 +						rsample >>= 8;
    1.77 +						dst[3] = (rsample&0xFF);
    1.78 +					src += 12;
    1.79 +					dst += 4;
    1.80 +				}
    1.81 +			}
    1.82 +		}
    1.83 +		break;
    1.84 +
    1.85 +		case AUDIO_S16: {
    1.86 +			Uint8 *src, *dst;
    1.87 +
    1.88 +			src = cvt->buf;
    1.89 +			dst = cvt->buf;
    1.90 +			if ( (format & 0x1000) == 0x1000 ) {
    1.91 +				for ( i=cvt->len_cvt/12; i; --i ) {
    1.92 +					lsample = (Sint16)((src[0]<<8)|src[1]);
    1.93 +					rsample = (Sint16)((src[2]<<8)|src[3]);
    1.94 +						dst[1] = (lsample&0xFF);
    1.95 +						lsample >>= 8;
    1.96 +						dst[0] = (lsample&0xFF);
    1.97 +						dst[3] = (rsample&0xFF);
    1.98 +						rsample >>= 8;
    1.99 +						dst[2] = (rsample&0xFF);
   1.100 +					src += 12;
   1.101 +					dst += 4;
   1.102 +				}
   1.103 +			} else {
   1.104 +				for ( i=cvt->len_cvt/12; i; --i ) {
   1.105 +					lsample = (Sint16)((src[1]<<8)|src[0]);
   1.106 +					rsample = (Sint16)((src[3]<<8)|src[2]);
   1.107 +						dst[0] = (lsample&0xFF);
   1.108 +						lsample >>= 8;
   1.109 +						dst[1] = (lsample&0xFF);
   1.110 +						dst[2] = (rsample&0xFF);
   1.111 +						rsample >>= 8;
   1.112 +						dst[3] = (rsample&0xFF);
   1.113 +					src += 12;
   1.114 +					dst += 4;
   1.115 +				}
   1.116 +			}
   1.117 +		}
   1.118 +		break;
   1.119 +	}
   1.120 +	cvt->len_cvt /= 3;
   1.121 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.122 +		cvt->filters[cvt->filter_index](cvt, format);
   1.123 +	}
   1.124 +}
   1.125 +
   1.126 +
   1.127 +/* Discard top 2 channels of 6 */
   1.128 +void SDL_ConvertStrip_2(SDL_AudioCVT *cvt, Uint16 format)
   1.129 +{
   1.130 +	int i;
   1.131 +	Sint32 lsample, rsample;
   1.132 +
   1.133 +#ifdef DEBUG_CONVERT
   1.134 +	fprintf(stderr, "Converting 6 down to quad\n");
   1.135 +#endif
   1.136 +	switch (format&0x8018) {
   1.137 +
   1.138 +		case AUDIO_U8: {
   1.139 +			Uint8 *src, *dst;
   1.140 +
   1.141 +			src = cvt->buf;
   1.142 +			dst = cvt->buf;
   1.143 +			for ( i=cvt->len_cvt/4; i; --i ) {
   1.144 +				lsample = src[0];
   1.145 +				rsample = src[1];
   1.146 +				dst[0] = lsample;
   1.147 +				dst[1] = rsample;
   1.148 +				src += 4;
   1.149 +				dst += 2;
   1.150 +			}
   1.151 +		}
   1.152 +		break;
   1.153 +
   1.154 +		case AUDIO_S8: {
   1.155 +			Sint8 *src, *dst;
   1.156 +
   1.157 +			src = (Sint8 *)cvt->buf;
   1.158 +			dst = (Sint8 *)cvt->buf;
   1.159 +			for ( i=cvt->len_cvt/4; i; --i ) {
   1.160 +				lsample = src[0];
   1.161 +				rsample = src[1];
   1.162 +				dst[0] = lsample;
   1.163 +				dst[1] = rsample;
   1.164 +				src += 4;
   1.165 +				dst += 2;
   1.166 +			}
   1.167 +		}
   1.168 +		break;
   1.169 +
   1.170 +		case AUDIO_U16: {
   1.171 +			Uint8 *src, *dst;
   1.172 +
   1.173 +			src = cvt->buf;
   1.174 +			dst = cvt->buf;
   1.175 +			if ( (format & 0x1000) == 0x1000 ) {
   1.176 +				for ( i=cvt->len_cvt/8; i; --i ) {
   1.177 +					lsample = (Uint16)((src[0]<<8)|src[1]);
   1.178 +					rsample = (Uint16)((src[2]<<8)|src[3]);
   1.179 +						dst[1] = (lsample&0xFF);
   1.180 +						lsample >>= 8;
   1.181 +						dst[0] = (lsample&0xFF);
   1.182 +						dst[3] = (rsample&0xFF);
   1.183 +						rsample >>= 8;
   1.184 +						dst[2] = (rsample&0xFF);
   1.185 +					src += 8;
   1.186 +					dst += 4;
   1.187 +				}
   1.188 +			} else {
   1.189 +				for ( i=cvt->len_cvt/8; i; --i ) {
   1.190 +					lsample = (Uint16)((src[1]<<8)|src[0]);
   1.191 +					rsample = (Uint16)((src[3]<<8)|src[2]);
   1.192 +						dst[0] = (lsample&0xFF);
   1.193 +						lsample >>= 8;
   1.194 +						dst[1] = (lsample&0xFF);
   1.195 +						dst[2] = (rsample&0xFF);
   1.196 +						rsample >>= 8;
   1.197 +						dst[3] = (rsample&0xFF);
   1.198 +					src += 8;
   1.199 +					dst += 4;
   1.200 +				}
   1.201 +			}
   1.202 +		}
   1.203 +		break;
   1.204 +
   1.205 +		case AUDIO_S16: {
   1.206 +			Uint8 *src, *dst;
   1.207 +
   1.208 +			src = cvt->buf;
   1.209 +			dst = cvt->buf;
   1.210 +			if ( (format & 0x1000) == 0x1000 ) {
   1.211 +				for ( i=cvt->len_cvt/8; i; --i ) {
   1.212 +					lsample = (Sint16)((src[0]<<8)|src[1]);
   1.213 +					rsample = (Sint16)((src[2]<<8)|src[3]);
   1.214 +						dst[1] = (lsample&0xFF);
   1.215 +						lsample >>= 8;
   1.216 +						dst[0] = (lsample&0xFF);
   1.217 +						dst[3] = (rsample&0xFF);
   1.218 +						rsample >>= 8;
   1.219 +						dst[2] = (rsample&0xFF);
   1.220 +					src += 8;
   1.221 +					dst += 4;
   1.222 +				}
   1.223 +			} else {
   1.224 +				for ( i=cvt->len_cvt/8; i; --i ) {
   1.225 +					lsample = (Sint16)((src[1]<<8)|src[0]);
   1.226 +					rsample = (Sint16)((src[3]<<8)|src[2]);
   1.227 +						dst[0] = (lsample&0xFF);
   1.228 +						lsample >>= 8;
   1.229 +						dst[1] = (lsample&0xFF);
   1.230 +						dst[2] = (rsample&0xFF);
   1.231 +						rsample >>= 8;
   1.232 +						dst[3] = (rsample&0xFF);
   1.233 +					src += 8;
   1.234 +					dst += 4;
   1.235 +				}
   1.236 +			}
   1.237 +		}
   1.238 +		break;
   1.239 +	}
   1.240 +	cvt->len_cvt /= 2;
   1.241 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.242 +		cvt->filters[cvt->filter_index](cvt, format);
   1.243 +	}
   1.244 +}
   1.245  
   1.246  /* Duplicate a mono channel to both stereo channels */
   1.247  void SDL_ConvertStereo(SDL_AudioCVT *cvt, Uint16 format)
   1.248 @@ -213,6 +451,337 @@
   1.249  	}
   1.250  }
   1.251  
   1.252 +
   1.253 +/* Duplicate a stereo channel to a pseudo-5.1 stream */
   1.254 +void SDL_ConvertSurround(SDL_AudioCVT *cvt, Uint16 format)
   1.255 +{
   1.256 +	int i;
   1.257 +
   1.258 +#ifdef DEBUG_CONVERT
   1.259 +	fprintf(stderr, "Converting stereo to surround\n");
   1.260 +#endif
   1.261 +	switch (format&0x8018) {
   1.262 +
   1.263 +		case AUDIO_U8: {
   1.264 +			Uint8 *src, *dst, lf, rf, ce;
   1.265 +
   1.266 +			src = (Uint8 *)(cvt->buf+cvt->len_cvt);
   1.267 +			dst = (Uint8 *)(cvt->buf+cvt->len_cvt*3);
   1.268 +			for ( i=cvt->len_cvt; i; --i ) {
   1.269 +				dst -= 6;
   1.270 +				src -= 2;
   1.271 +				lf = src[0];
   1.272 +				rf = src[1];
   1.273 +				ce = (lf/2) + (rf/2);
   1.274 +				dst[0] = lf;
   1.275 +				dst[1] = rf;
   1.276 +				dst[2] = lf - ce;
   1.277 +				dst[3] = rf - ce;
   1.278 +				dst[4] = ce;
   1.279 +				dst[5] = ce;
   1.280 +			}
   1.281 +		}
   1.282 +		break;
   1.283 +
   1.284 +		case AUDIO_S8: {
   1.285 +			Sint8 *src, *dst, lf, rf, ce;
   1.286 +
   1.287 +			src = cvt->buf+cvt->len_cvt;
   1.288 +			dst = cvt->buf+cvt->len_cvt*3;
   1.289 +			for ( i=cvt->len_cvt; i; --i ) {
   1.290 +				dst -= 6;
   1.291 +				src -= 2;
   1.292 +				lf = src[0];
   1.293 +				rf = src[1];
   1.294 +				ce = (lf/2) + (rf/2);
   1.295 +				dst[0] = lf;
   1.296 +				dst[1] = rf;
   1.297 +				dst[2] = lf - ce;
   1.298 +				dst[3] = rf - ce;
   1.299 +				dst[4] = ce;
   1.300 +				dst[5] = ce;
   1.301 +			}
   1.302 +		}
   1.303 +		break;
   1.304 +
   1.305 +		case AUDIO_U16: {
   1.306 +			Uint8 *src, *dst;
   1.307 +			Uint16 lf, rf, ce, lr, rr;
   1.308 +
   1.309 +			src = cvt->buf+cvt->len_cvt;
   1.310 +			dst = cvt->buf+cvt->len_cvt*3;
   1.311 +
   1.312 +			if ( (format & 0x1000) == 0x1000 ) {
   1.313 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.314 +					dst -= 12;
   1.315 +					src -= 4;
   1.316 +					lf = (Uint16)((src[0]<<8)|src[1]);
   1.317 +					rf = (Uint16)((src[2]<<8)|src[3]);
   1.318 +					ce = (lf/2) + (rf/2);
   1.319 +					rr = lf - ce;
   1.320 +					lr = rf - ce;
   1.321 +						dst[1] = (lf&0xFF);
   1.322 +						dst[0] = ((lf>>8)&0xFF);
   1.323 +						dst[3] = (rf&0xFF);
   1.324 +						dst[2] = ((rf>>8)&0xFF);
   1.325 +
   1.326 +						dst[1+4] = (lr&0xFF);
   1.327 +						dst[0+4] = ((lr>>8)&0xFF);
   1.328 +						dst[3+4] = (rr&0xFF);
   1.329 +						dst[2+4] = ((rr>>8)&0xFF);
   1.330 +
   1.331 +						dst[1+8] = (ce&0xFF);
   1.332 +						dst[0+8] = ((ce>>8)&0xFF);
   1.333 +						dst[3+8] = (ce&0xFF);
   1.334 +						dst[2+8] = ((ce>>8)&0xFF);
   1.335 +				}
   1.336 +			} else {
   1.337 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.338 +					dst -= 12;
   1.339 +					src -= 4;
   1.340 +					lf = (Uint16)((src[1]<<8)|src[0]);
   1.341 +					rf = (Uint16)((src[3]<<8)|src[2]);
   1.342 +					ce = (lf/2) + (rf/2);
   1.343 +					rr = lf - ce;
   1.344 +					lr = rf - ce;
   1.345 +						dst[0] = (lf&0xFF);
   1.346 +						dst[1] = ((lf>>8)&0xFF);
   1.347 +						dst[2] = (rf&0xFF);
   1.348 +						dst[3] = ((rf>>8)&0xFF);
   1.349 +
   1.350 +						dst[0+4] = (lr&0xFF);
   1.351 +						dst[1+4] = ((lr>>8)&0xFF);
   1.352 +						dst[2+4] = (rr&0xFF);
   1.353 +						dst[3+4] = ((rr>>8)&0xFF);
   1.354 +
   1.355 +						dst[0+8] = (ce&0xFF);
   1.356 +						dst[1+8] = ((ce>>8)&0xFF);
   1.357 +						dst[2+8] = (ce&0xFF);
   1.358 +						dst[3+8] = ((ce>>8)&0xFF);
   1.359 +				}
   1.360 +			}
   1.361 +		}
   1.362 +		break;
   1.363 +
   1.364 +		case AUDIO_S16: {
   1.365 +			Uint8 *src, *dst;
   1.366 +			Sint16 lf, rf, ce, lr, rr;
   1.367 +
   1.368 +			src = cvt->buf+cvt->len_cvt;
   1.369 +			dst = cvt->buf+cvt->len_cvt*3;
   1.370 +
   1.371 +			if ( (format & 0x1000) == 0x1000 ) {
   1.372 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.373 +					dst -= 12;
   1.374 +					src -= 4;
   1.375 +					lf = (Sint16)((src[0]<<8)|src[1]);
   1.376 +					rf = (Sint16)((src[2]<<8)|src[3]);
   1.377 +					ce = (lf/2) + (rf/2);
   1.378 +					rr = lf - ce;
   1.379 +					lr = rf - ce;
   1.380 +						dst[1] = (lf&0xFF);
   1.381 +						dst[0] = ((lf>>8)&0xFF);
   1.382 +						dst[3] = (rf&0xFF);
   1.383 +						dst[2] = ((rf>>8)&0xFF);
   1.384 +
   1.385 +						dst[1+4] = (lr&0xFF);
   1.386 +						dst[0+4] = ((lr>>8)&0xFF);
   1.387 +						dst[3+4] = (rr&0xFF);
   1.388 +						dst[2+4] = ((rr>>8)&0xFF);
   1.389 +
   1.390 +						dst[1+8] = (ce&0xFF);
   1.391 +						dst[0+8] = ((ce>>8)&0xFF);
   1.392 +						dst[3+8] = (ce&0xFF);
   1.393 +						dst[2+8] = ((ce>>8)&0xFF);
   1.394 +				}
   1.395 +			} else {
   1.396 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.397 +					dst -= 12;
   1.398 +					src -= 4;
   1.399 +					lf = (Sint16)((src[1]<<8)|src[0]);
   1.400 +					rf = (Sint16)((src[3]<<8)|src[2]);
   1.401 +					ce = (lf/2) + (rf/2);
   1.402 +					rr = lf - ce;
   1.403 +					lr = rf - ce;
   1.404 +						dst[0] = (lf&0xFF);
   1.405 +						dst[1] = ((lf>>8)&0xFF);
   1.406 +						dst[2] = (rf&0xFF);
   1.407 +						dst[3] = ((rf>>8)&0xFF);
   1.408 +
   1.409 +						dst[0+4] = (lr&0xFF);
   1.410 +						dst[1+4] = ((lr>>8)&0xFF);
   1.411 +						dst[2+4] = (rr&0xFF);
   1.412 +						dst[3+4] = ((rr>>8)&0xFF);
   1.413 +
   1.414 +						dst[0+8] = (ce&0xFF);
   1.415 +						dst[1+8] = ((ce>>8)&0xFF);
   1.416 +						dst[2+8] = (ce&0xFF);
   1.417 +						dst[3+8] = ((ce>>8)&0xFF);
   1.418 +				}
   1.419 +			}
   1.420 +		}
   1.421 +		break;
   1.422 +	}
   1.423 +	cvt->len_cvt *= 3;
   1.424 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.425 +		cvt->filters[cvt->filter_index](cvt, format);
   1.426 +	}
   1.427 +}
   1.428 +
   1.429 +
   1.430 +/* Duplicate a stereo channel to a pseudo-4.0 stream */
   1.431 +void SDL_ConvertSurround_4(SDL_AudioCVT *cvt, Uint16 format)
   1.432 +{
   1.433 +	int i;
   1.434 +
   1.435 +#ifdef DEBUG_CONVERT
   1.436 +	fprintf(stderr, "Converting stereo to quad\n");
   1.437 +#endif
   1.438 +	switch (format&0x8018) {
   1.439 +
   1.440 +		case AUDIO_U8: {
   1.441 +			Uint8 *src, *dst, lf, rf, ce;
   1.442 +
   1.443 +			src = (Uint8 *)(cvt->buf+cvt->len_cvt);
   1.444 +			dst = (Uint8 *)(cvt->buf+cvt->len_cvt*2);
   1.445 +			for ( i=cvt->len_cvt; i; --i ) {
   1.446 +				dst -= 4;
   1.447 +				src -= 2;
   1.448 +				lf = src[0];
   1.449 +				rf = src[1];
   1.450 +				ce = (lf/2) + (rf/2);
   1.451 +				dst[0] = lf;
   1.452 +				dst[1] = rf;
   1.453 +				dst[2] = lf - ce;
   1.454 +				dst[3] = rf - ce;
   1.455 +			}
   1.456 +		}
   1.457 +		break;
   1.458 +
   1.459 +		case AUDIO_S8: {
   1.460 +			Sint8 *src, *dst, lf, rf, ce;
   1.461 +
   1.462 +			src = cvt->buf+cvt->len_cvt;
   1.463 +			dst = cvt->buf+cvt->len_cvt*2;
   1.464 +			for ( i=cvt->len_cvt; i; --i ) {
   1.465 +				dst -= 4;
   1.466 +				src -= 2;
   1.467 +				lf = src[0];
   1.468 +				rf = src[1];
   1.469 +				ce = (lf/2) + (rf/2);
   1.470 +				dst[0] = lf;
   1.471 +				dst[1] = rf;
   1.472 +				dst[2] = lf - ce;
   1.473 +				dst[3] = rf - ce;
   1.474 +			}
   1.475 +		}
   1.476 +		break;
   1.477 +
   1.478 +		case AUDIO_U16: {
   1.479 +			Uint8 *src, *dst;
   1.480 +			Uint16 lf, rf, ce, lr, rr;
   1.481 +
   1.482 +			src = cvt->buf+cvt->len_cvt;
   1.483 +			dst = cvt->buf+cvt->len_cvt*2;
   1.484 +
   1.485 +			if ( (format & 0x1000) == 0x1000 ) {
   1.486 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.487 +					dst -= 8;
   1.488 +					src -= 4;
   1.489 +					lf = (Uint16)((src[0]<<8)|src[1]);
   1.490 +					rf = (Uint16)((src[2]<<8)|src[3]);
   1.491 +					ce = (lf/2) + (rf/2);
   1.492 +					rr = lf - ce;
   1.493 +					lr = rf - ce;
   1.494 +						dst[1] = (lf&0xFF);
   1.495 +						dst[0] = ((lf>>8)&0xFF);
   1.496 +						dst[3] = (rf&0xFF);
   1.497 +						dst[2] = ((rf>>8)&0xFF);
   1.498 +
   1.499 +						dst[1+4] = (lr&0xFF);
   1.500 +						dst[0+4] = ((lr>>8)&0xFF);
   1.501 +						dst[3+4] = (rr&0xFF);
   1.502 +						dst[2+4] = ((rr>>8)&0xFF);
   1.503 +				}
   1.504 +			} else {
   1.505 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.506 +					dst -= 8;
   1.507 +					src -= 4;
   1.508 +					lf = (Uint16)((src[1]<<8)|src[0]);
   1.509 +					rf = (Uint16)((src[3]<<8)|src[2]);
   1.510 +					ce = (lf/2) + (rf/2);
   1.511 +					rr = lf - ce;
   1.512 +					lr = rf - ce;
   1.513 +						dst[0] = (lf&0xFF);
   1.514 +						dst[1] = ((lf>>8)&0xFF);
   1.515 +						dst[2] = (rf&0xFF);
   1.516 +						dst[3] = ((rf>>8)&0xFF);
   1.517 +
   1.518 +						dst[0+4] = (lr&0xFF);
   1.519 +						dst[1+4] = ((lr>>8)&0xFF);
   1.520 +						dst[2+4] = (rr&0xFF);
   1.521 +						dst[3+4] = ((rr>>8)&0xFF);
   1.522 +				}
   1.523 +			}
   1.524 +		}
   1.525 +		break;
   1.526 +
   1.527 +		case AUDIO_S16: {
   1.528 +			Uint8 *src, *dst;
   1.529 +			Sint16 lf, rf, ce, lr, rr;
   1.530 +
   1.531 +			src = cvt->buf+cvt->len_cvt;
   1.532 +			dst = cvt->buf+cvt->len_cvt*2;
   1.533 +
   1.534 +			if ( (format & 0x1000) == 0x1000 ) {
   1.535 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.536 +					dst -= 8;
   1.537 +					src -= 4;
   1.538 +					lf = (Sint16)((src[0]<<8)|src[1]);
   1.539 +					rf = (Sint16)((src[2]<<8)|src[3]);
   1.540 +					ce = (lf/2) + (rf/2);
   1.541 +					rr = lf - ce;
   1.542 +					lr = rf - ce;
   1.543 +						dst[1] = (lf&0xFF);
   1.544 +						dst[0] = ((lf>>8)&0xFF);
   1.545 +						dst[3] = (rf&0xFF);
   1.546 +						dst[2] = ((rf>>8)&0xFF);
   1.547 +
   1.548 +						dst[1+4] = (lr&0xFF);
   1.549 +						dst[0+4] = ((lr>>8)&0xFF);
   1.550 +						dst[3+4] = (rr&0xFF);
   1.551 +						dst[2+4] = ((rr>>8)&0xFF);
   1.552 +				}
   1.553 +			} else {
   1.554 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.555 +					dst -= 8;
   1.556 +					src -= 4;
   1.557 +					lf = (Sint16)((src[1]<<8)|src[0]);
   1.558 +					rf = (Sint16)((src[3]<<8)|src[2]);
   1.559 +					ce = (lf/2) + (rf/2);
   1.560 +					rr = lf - ce;
   1.561 +					lr = rf - ce;
   1.562 +						dst[0] = (lf&0xFF);
   1.563 +						dst[1] = ((lf>>8)&0xFF);
   1.564 +						dst[2] = (rf&0xFF);
   1.565 +						dst[3] = ((rf>>8)&0xFF);
   1.566 +
   1.567 +						dst[0+4] = (lr&0xFF);
   1.568 +						dst[1+4] = ((lr>>8)&0xFF);
   1.569 +						dst[2+4] = (rr&0xFF);
   1.570 +						dst[3+4] = ((rr>>8)&0xFF);
   1.571 +				}
   1.572 +			}
   1.573 +		}
   1.574 +		break;
   1.575 +	}
   1.576 +	cvt->len_cvt *= 2;
   1.577 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.578 +		cvt->filters[cvt->filter_index](cvt, format);
   1.579 +	}
   1.580 +}
   1.581 +
   1.582 +
   1.583  /* Convert 8-bit to 16-bit - LSB */
   1.584  void SDL_Convert16LSB(SDL_AudioCVT *cvt, Uint16 format)
   1.585  {
   1.586 @@ -374,6 +943,173 @@
   1.587  	}
   1.588  }
   1.589  
   1.590 +
   1.591 +/* Convert rate up by multiple of 2, for stereo */
   1.592 +void SDL_RateMUL2_c2(SDL_AudioCVT *cvt, Uint16 format)
   1.593 +{
   1.594 +	int i;
   1.595 +	Uint8 *src, *dst;
   1.596 +
   1.597 +#ifdef DEBUG_CONVERT
   1.598 +	fprintf(stderr, "Converting audio rate * 2\n");
   1.599 +#endif
   1.600 +	src = cvt->buf+cvt->len_cvt;
   1.601 +	dst = cvt->buf+cvt->len_cvt*2;
   1.602 +	switch (format & 0xFF) {
   1.603 +		case 8:
   1.604 +			for ( i=cvt->len_cvt/2; i; --i ) {
   1.605 +				src -= 2;
   1.606 +				dst -= 4;
   1.607 +				dst[0] = src[0];
   1.608 +				dst[1] = src[1];
   1.609 +				dst[2] = src[0];
   1.610 +				dst[3] = src[1];
   1.611 +			}
   1.612 +			break;
   1.613 +		case 16:
   1.614 +			for ( i=cvt->len_cvt/4; i; --i ) {
   1.615 +				src -= 4;
   1.616 +				dst -= 8;
   1.617 +				dst[0] = src[0];
   1.618 +				dst[1] = src[1];
   1.619 +				dst[2] = src[2];
   1.620 +				dst[3] = src[3];
   1.621 +				dst[4] = src[0];
   1.622 +				dst[5] = src[1];
   1.623 +				dst[6] = src[2];
   1.624 +				dst[7] = src[3];
   1.625 +			}
   1.626 +			break;
   1.627 +	}
   1.628 +	cvt->len_cvt *= 2;
   1.629 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.630 +		cvt->filters[cvt->filter_index](cvt, format);
   1.631 +	}
   1.632 +}
   1.633 +
   1.634 +/* Convert rate up by multiple of 2, for quad */
   1.635 +void SDL_RateMUL2_c4(SDL_AudioCVT *cvt, Uint16 format)
   1.636 +{
   1.637 +	int i;
   1.638 +	Uint8 *src, *dst;
   1.639 +
   1.640 +#ifdef DEBUG_CONVERT
   1.641 +	fprintf(stderr, "Converting audio rate * 2\n");
   1.642 +#endif
   1.643 +	src = cvt->buf+cvt->len_cvt;
   1.644 +	dst = cvt->buf+cvt->len_cvt*2;
   1.645 +	switch (format & 0xFF) {
   1.646 +		case 8:
   1.647 +			for ( i=cvt->len_cvt/4; i; --i ) {
   1.648 +				src -= 4;
   1.649 +				dst -= 8;
   1.650 +				dst[0] = src[0];
   1.651 +				dst[1] = src[1];
   1.652 +				dst[2] = src[2];
   1.653 +				dst[3] = src[3];
   1.654 +				dst[4] = src[0];
   1.655 +				dst[5] = src[1];
   1.656 +				dst[6] = src[2];
   1.657 +				dst[7] = src[3];
   1.658 +			}
   1.659 +			break;
   1.660 +		case 16:
   1.661 +			for ( i=cvt->len_cvt/8; i; --i ) {
   1.662 +				src -= 8;
   1.663 +				dst -= 16;
   1.664 +				dst[0] = src[0];
   1.665 +				dst[1] = src[1];
   1.666 +				dst[2] = src[2];
   1.667 +				dst[3] = src[3];
   1.668 +				dst[4] = src[4];
   1.669 +				dst[5] = src[5];
   1.670 +				dst[6] = src[6];
   1.671 +				dst[7] = src[7];
   1.672 +				dst[8] = src[0];
   1.673 +				dst[9] = src[1];
   1.674 +				dst[10] = src[2];
   1.675 +				dst[11] = src[3];
   1.676 +				dst[12] = src[4];
   1.677 +				dst[13] = src[5];
   1.678 +				dst[14] = src[6];
   1.679 +				dst[15] = src[7];
   1.680 +			}
   1.681 +			break;
   1.682 +	}
   1.683 +	cvt->len_cvt *= 2;
   1.684 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.685 +		cvt->filters[cvt->filter_index](cvt, format);
   1.686 +	}
   1.687 +}
   1.688 +
   1.689 +
   1.690 +/* Convert rate up by multiple of 2, for 5.1 */
   1.691 +void SDL_RateMUL2_c6(SDL_AudioCVT *cvt, Uint16 format)
   1.692 +{
   1.693 +	int i;
   1.694 +	Uint8 *src, *dst;
   1.695 +
   1.696 +#ifdef DEBUG_CONVERT
   1.697 +	fprintf(stderr, "Converting audio rate * 2\n");
   1.698 +#endif
   1.699 +	src = cvt->buf+cvt->len_cvt;
   1.700 +	dst = cvt->buf+cvt->len_cvt*2;
   1.701 +	switch (format & 0xFF) {
   1.702 +		case 8:
   1.703 +			for ( i=cvt->len_cvt/6; i; --i ) {
   1.704 +				src -= 6;
   1.705 +				dst -= 12;
   1.706 +				dst[0] = src[0];
   1.707 +				dst[1] = src[1];
   1.708 +				dst[2] = src[2];
   1.709 +				dst[3] = src[3];
   1.710 +				dst[4] = src[4];
   1.711 +				dst[5] = src[5];
   1.712 +				dst[6] = src[0];
   1.713 +				dst[7] = src[1];
   1.714 +				dst[8] = src[2];
   1.715 +				dst[9] = src[3];
   1.716 +				dst[10] = src[4];
   1.717 +				dst[11] = src[5];
   1.718 +			}
   1.719 +			break;
   1.720 +		case 16:
   1.721 +			for ( i=cvt->len_cvt/12; i; --i ) {
   1.722 +				src -= 12;
   1.723 +				dst -= 24;
   1.724 +				dst[0] = src[0];
   1.725 +				dst[1] = src[1];
   1.726 +				dst[2] = src[2];
   1.727 +				dst[3] = src[3];
   1.728 +				dst[4] = src[4];
   1.729 +				dst[5] = src[5];
   1.730 +				dst[6] = src[6];
   1.731 +				dst[7] = src[7];
   1.732 +				dst[8] = src[8];
   1.733 +				dst[9] = src[9];
   1.734 +				dst[10] = src[10];
   1.735 +				dst[11] = src[11];
   1.736 +				dst[12] = src[0];
   1.737 +				dst[13] = src[1];
   1.738 +				dst[14] = src[2];
   1.739 +				dst[15] = src[3];
   1.740 +				dst[16] = src[4];
   1.741 +				dst[17] = src[5];
   1.742 +				dst[18] = src[6];
   1.743 +				dst[19] = src[7];
   1.744 +				dst[20] = src[8];
   1.745 +				dst[21] = src[9];
   1.746 +				dst[22] = src[10];
   1.747 +				dst[23] = src[11];
   1.748 +			}
   1.749 +			break;
   1.750 +	}
   1.751 +	cvt->len_cvt *= 2;
   1.752 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.753 +		cvt->filters[cvt->filter_index](cvt, format);
   1.754 +	}
   1.755 +}
   1.756 +
   1.757  /* Convert rate down by multiple of 2 */
   1.758  void SDL_RateDIV2(SDL_AudioCVT *cvt, Uint16 format)
   1.759  {
   1.760 @@ -408,6 +1144,137 @@
   1.761  	}
   1.762  }
   1.763  
   1.764 +
   1.765 +/* Convert rate down by multiple of 2, for stereo */
   1.766 +void SDL_RateDIV2_c2(SDL_AudioCVT *cvt, Uint16 format)
   1.767 +{
   1.768 +	int i;
   1.769 +	Uint8 *src, *dst;
   1.770 +
   1.771 +#ifdef DEBUG_CONVERT
   1.772 +	fprintf(stderr, "Converting audio rate / 2\n");
   1.773 +#endif
   1.774 +	src = cvt->buf;
   1.775 +	dst = cvt->buf;
   1.776 +	switch (format & 0xFF) {
   1.777 +		case 8:
   1.778 +			for ( i=cvt->len_cvt/4; i; --i ) {
   1.779 +				dst[0] = src[0];
   1.780 +				dst[1] = src[1];
   1.781 +				src += 4;
   1.782 +				dst += 2;
   1.783 +			}
   1.784 +			break;
   1.785 +		case 16:
   1.786 +			for ( i=cvt->len_cvt/8; i; --i ) {
   1.787 +				dst[0] = src[0];
   1.788 +				dst[1] = src[1];
   1.789 +				dst[2] = src[2];
   1.790 +				dst[3] = src[3];
   1.791 +				src += 8;
   1.792 +				dst += 4;
   1.793 +			}
   1.794 +			break;
   1.795 +	}
   1.796 +	cvt->len_cvt /= 2;
   1.797 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.798 +		cvt->filters[cvt->filter_index](cvt, format);
   1.799 +	}
   1.800 +}
   1.801 +
   1.802 +
   1.803 +/* Convert rate down by multiple of 2, for quad */
   1.804 +void SDL_RateDIV2_c4(SDL_AudioCVT *cvt, Uint16 format)
   1.805 +{
   1.806 +	int i;
   1.807 +	Uint8 *src, *dst;
   1.808 +
   1.809 +#ifdef DEBUG_CONVERT
   1.810 +	fprintf(stderr, "Converting audio rate / 2\n");
   1.811 +#endif
   1.812 +	src = cvt->buf;
   1.813 +	dst = cvt->buf;
   1.814 +	switch (format & 0xFF) {
   1.815 +		case 8:
   1.816 +			for ( i=cvt->len_cvt/8; i; --i ) {
   1.817 +				dst[0] = src[0];
   1.818 +				dst[1] = src[1];
   1.819 +				dst[2] = src[2];
   1.820 +				dst[3] = src[3];
   1.821 +				src += 8;
   1.822 +				dst += 4;
   1.823 +			}
   1.824 +			break;
   1.825 +		case 16:
   1.826 +			for ( i=cvt->len_cvt/16; i; --i ) {
   1.827 +				dst[0] = src[0];
   1.828 +				dst[1] = src[1];
   1.829 +				dst[2] = src[2];
   1.830 +				dst[3] = src[3];
   1.831 +				dst[4] = src[4];
   1.832 +				dst[5] = src[5];
   1.833 +				dst[6] = src[6];
   1.834 +				dst[7] = src[7];
   1.835 +				src += 16;
   1.836 +				dst += 8;
   1.837 +			}
   1.838 +			break;
   1.839 +	}
   1.840 +	cvt->len_cvt /= 2;
   1.841 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.842 +		cvt->filters[cvt->filter_index](cvt, format);
   1.843 +	}
   1.844 +}
   1.845 +
   1.846 +/* Convert rate down by multiple of 2, for 5.1 */
   1.847 +void SDL_RateDIV2_c6(SDL_AudioCVT *cvt, Uint16 format)
   1.848 +{
   1.849 +	int i;
   1.850 +	Uint8 *src, *dst;
   1.851 +
   1.852 +#ifdef DEBUG_CONVERT
   1.853 +	fprintf(stderr, "Converting audio rate / 2\n");
   1.854 +#endif
   1.855 +	src = cvt->buf;
   1.856 +	dst = cvt->buf;
   1.857 +	switch (format & 0xFF) {
   1.858 +		case 8:
   1.859 +			for ( i=cvt->len_cvt/12; i; --i ) {
   1.860 +				dst[0] = src[0];
   1.861 +				dst[1] = src[1];
   1.862 +				dst[2] = src[2];
   1.863 +				dst[3] = src[3];
   1.864 +				dst[4] = src[4];
   1.865 +				dst[5] = src[5];
   1.866 +				src += 12;
   1.867 +				dst += 6;
   1.868 +			}
   1.869 +			break;
   1.870 +		case 16:
   1.871 +			for ( i=cvt->len_cvt/24; i; --i ) {
   1.872 +				dst[0] = src[0];
   1.873 +				dst[1] = src[1];
   1.874 +				dst[2] = src[2];
   1.875 +				dst[3] = src[3];
   1.876 +				dst[4] = src[4];
   1.877 +				dst[5] = src[5];
   1.878 +				dst[6] = src[6];
   1.879 +				dst[7] = src[7];
   1.880 +				dst[8] = src[8];
   1.881 +				dst[9] = src[9];
   1.882 +				dst[10] = src[10];
   1.883 +				dst[11] = src[11];
   1.884 +				src += 24;
   1.885 +				dst += 12;
   1.886 +			}
   1.887 +			break;
   1.888 +	}
   1.889 +	cvt->len_cvt /= 2;
   1.890 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.891 +		cvt->filters[cvt->filter_index](cvt, format);
   1.892 +	}
   1.893 +}
   1.894 +
   1.895  /* Very slow rate conversion routine */
   1.896  void SDL_RateSLOW(SDL_AudioCVT *cvt, Uint16 format)
   1.897  {
   1.898 @@ -511,6 +1378,8 @@
   1.899  	Uint16 src_format, Uint8 src_channels, int src_rate,
   1.900  	Uint16 dst_format, Uint8 dst_channels, int dst_rate)
   1.901  {
   1.902 +/*printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
   1.903 +		src_format, dst_format, src_channels, dst_channels, src_rate, dst_rate);*/
   1.904  	/* Start off with no conversion necessary */
   1.905  	cvt->needed = 0;
   1.906  	cvt->filter_index = 0;
   1.907 @@ -554,6 +1423,29 @@
   1.908  
   1.909  	/* Last filter:  Mono/Stereo conversion */
   1.910  	if ( src_channels != dst_channels ) {
   1.911 +		if ( (src_channels == 1) && (dst_channels > 1) ) {
   1.912 +			cvt->filters[cvt->filter_index++] = 
   1.913 +						SDL_ConvertStereo;
   1.914 +			cvt->len_mult *= 2;
   1.915 +			src_channels = 2;
   1.916 +			cvt->len_ratio *= 2;
   1.917 +		}
   1.918 +		if ( (src_channels == 2) &&
   1.919 +				(dst_channels == 6) ) {
   1.920 +			cvt->filters[cvt->filter_index++] =
   1.921 +						 SDL_ConvertSurround;
   1.922 +			src_channels = 6;
   1.923 +			cvt->len_mult *= 3;
   1.924 +			cvt->len_ratio *= 3;
   1.925 +		}
   1.926 +		if ( (src_channels == 2) &&
   1.927 +				(dst_channels == 4) ) {
   1.928 +			cvt->filters[cvt->filter_index++] =
   1.929 +						 SDL_ConvertSurround_4;
   1.930 +			src_channels = 4;
   1.931 +			cvt->len_mult *= 2;
   1.932 +			cvt->len_ratio *= 2;
   1.933 +		}
   1.934  		while ( (src_channels*2) <= dst_channels ) {
   1.935  			cvt->filters[cvt->filter_index++] = 
   1.936  						SDL_ConvertStereo;
   1.937 @@ -561,6 +1453,20 @@
   1.938  			src_channels *= 2;
   1.939  			cvt->len_ratio *= 2;
   1.940  		}
   1.941 +		if ( (src_channels == 6) &&
   1.942 +				(dst_channels <= 2) ) {
   1.943 +			cvt->filters[cvt->filter_index++] =
   1.944 +						 SDL_ConvertStrip;
   1.945 +			src_channels = 2;
   1.946 +			cvt->len_ratio /= 3;
   1.947 +		}
   1.948 +		if ( (src_channels == 6) &&
   1.949 +				(dst_channels == 4) ) {
   1.950 +			cvt->filters[cvt->filter_index++] =
   1.951 +						 SDL_ConvertStrip_2;
   1.952 +			src_channels = 4;
   1.953 +			cvt->len_ratio /= 2;
   1.954 +		}
   1.955  		/* This assumes that 4 channel audio is in the format:
   1.956  		     Left {front/back} + Right {front/back}
   1.957  		   so converting to L/R stereo works properly.
   1.958 @@ -588,13 +1494,25 @@
   1.959  		if ( src_rate > dst_rate ) {
   1.960  			hi_rate = src_rate;
   1.961  			lo_rate = dst_rate;
   1.962 -			rate_cvt = SDL_RateDIV2;
   1.963 +			switch (src_channels) {
   1.964 +				case 1: rate_cvt = SDL_RateDIV2; break;
   1.965 +				case 2: rate_cvt = SDL_RateDIV2_c2; break;
   1.966 +				case 4: rate_cvt = SDL_RateDIV2_c4; break;
   1.967 +				case 6: rate_cvt = SDL_RateDIV2_c6; break;
   1.968 +				default: return -1;
   1.969 +			}
   1.970  			len_mult = 1;
   1.971  			len_ratio = 0.5;
   1.972  		} else {
   1.973  			hi_rate = dst_rate;
   1.974  			lo_rate = src_rate;
   1.975 -			rate_cvt = SDL_RateMUL2;
   1.976 +			switch (src_channels) {
   1.977 +				case 1: rate_cvt = SDL_RateMUL2; break;
   1.978 +				case 2: rate_cvt = SDL_RateMUL2_c2; break;
   1.979 +				case 4: rate_cvt = SDL_RateMUL2_c4; break;
   1.980 +				case 6: rate_cvt = SDL_RateMUL2_c6; break;
   1.981 +				default: return -1;
   1.982 +			}
   1.983  			len_mult = 2;
   1.984  			len_ratio = 2.0;
   1.985  		}