src/audio/SDL_audiocvt.c
changeset 0 74212992fb08
child 252 e8157fcb3114
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/SDL_audiocvt.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,642 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* Functions for audio drivers to perform runtime conversion of audio format */
    1.32 +
    1.33 +#include <stdio.h>
    1.34 +
    1.35 +#include "SDL_error.h"
    1.36 +#include "SDL_audio.h"
    1.37 +
    1.38 +
    1.39 +/* Effectively mix right and left channels into a single channel */
    1.40 +void SDL_ConvertMono(SDL_AudioCVT *cvt, Uint16 format)
    1.41 +{
    1.42 +	int i;
    1.43 +	Sint32 sample;
    1.44 +
    1.45 +#ifdef DEBUG_CONVERT
    1.46 +	fprintf(stderr, "Converting to mono\n");
    1.47 +#endif
    1.48 +	switch (format&0x8018) {
    1.49 +
    1.50 +		case AUDIO_U8: {
    1.51 +			Uint8 *src, *dst;
    1.52 +
    1.53 +			src = cvt->buf;
    1.54 +			dst = cvt->buf;
    1.55 +			for ( i=cvt->len_cvt/2; i; --i ) {
    1.56 +				sample = src[0] + src[1];
    1.57 +				if ( sample > 255 ) {
    1.58 +					*dst = 255;
    1.59 +				} else {
    1.60 +					*dst = sample;
    1.61 +				}
    1.62 +				src += 2;
    1.63 +				dst += 1;
    1.64 +			}
    1.65 +		}
    1.66 +		break;
    1.67 +
    1.68 +		case AUDIO_S8: {
    1.69 +			Sint8 *src, *dst;
    1.70 +
    1.71 +			src = (Sint8 *)cvt->buf;
    1.72 +			dst = (Sint8 *)cvt->buf;
    1.73 +			for ( i=cvt->len_cvt/2; i; --i ) {
    1.74 +				sample = src[0] + src[1];
    1.75 +				if ( sample > 127 ) {
    1.76 +					*dst = 127;
    1.77 +				} else
    1.78 +				if ( sample < -128 ) {
    1.79 +					*dst = -128;
    1.80 +				} else {
    1.81 +					*dst = sample;
    1.82 +				}
    1.83 +				src += 2;
    1.84 +				dst += 1;
    1.85 +			}
    1.86 +		}
    1.87 +		break;
    1.88 +
    1.89 +		case AUDIO_U16: {
    1.90 +			Uint8 *src, *dst;
    1.91 +
    1.92 +			src = cvt->buf;
    1.93 +			dst = cvt->buf;
    1.94 +			if ( (format & 0x1000) == 0x1000 ) {
    1.95 +				for ( i=cvt->len_cvt/4; i; --i ) {
    1.96 +					sample = (Uint16)((src[0]<<8)|src[1])+
    1.97 +					         (Uint16)((src[2]<<8)|src[3]);
    1.98 +					if ( sample > 65535 ) {
    1.99 +						dst[0] = 0xFF;
   1.100 +						dst[1] = 0xFF;
   1.101 +					} else {
   1.102 +						dst[1] = (sample&0xFF);
   1.103 +						sample >>= 8;
   1.104 +						dst[0] = (sample&0xFF);
   1.105 +					}
   1.106 +					src += 4;
   1.107 +					dst += 2;
   1.108 +				}
   1.109 +			} else {
   1.110 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.111 +					sample = (Uint16)((src[1]<<8)|src[0])+
   1.112 +					         (Uint16)((src[3]<<8)|src[2]);
   1.113 +					if ( sample > 65535 ) {
   1.114 +						dst[0] = 0xFF;
   1.115 +						dst[1] = 0xFF;
   1.116 +					} else {
   1.117 +						dst[0] = (sample&0xFF);
   1.118 +						sample >>= 8;
   1.119 +						dst[1] = (sample&0xFF);
   1.120 +					}
   1.121 +					src += 4;
   1.122 +					dst += 2;
   1.123 +				}
   1.124 +			}
   1.125 +		}
   1.126 +		break;
   1.127 +
   1.128 +		case AUDIO_S16: {
   1.129 +			Uint8 *src, *dst;
   1.130 +
   1.131 +			src = cvt->buf;
   1.132 +			dst = cvt->buf;
   1.133 +			if ( (format & 0x1000) == 0x1000 ) {
   1.134 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.135 +					sample = (Sint16)((src[0]<<8)|src[1])+
   1.136 +					         (Sint16)((src[2]<<8)|src[3]);
   1.137 +					if ( sample > 32767 ) {
   1.138 +						dst[0] = 0x7F;
   1.139 +						dst[1] = 0xFF;
   1.140 +					} else
   1.141 +					if ( sample < -32768 ) {
   1.142 +						dst[0] = 0x80;
   1.143 +						dst[1] = 0x00;
   1.144 +					} else {
   1.145 +						dst[1] = (sample&0xFF);
   1.146 +						sample >>= 8;
   1.147 +						dst[0] = (sample&0xFF);
   1.148 +					}
   1.149 +					src += 4;
   1.150 +					dst += 2;
   1.151 +				}
   1.152 +			} else {
   1.153 +				for ( i=cvt->len_cvt/4; i; --i ) {
   1.154 +					sample = (Sint16)((src[1]<<8)|src[0])+
   1.155 +					         (Sint16)((src[3]<<8)|src[2]);
   1.156 +					if ( sample > 32767 ) {
   1.157 +						dst[1] = 0x7F;
   1.158 +						dst[0] = 0xFF;
   1.159 +					} else
   1.160 +					if ( sample < -32768 ) {
   1.161 +						dst[1] = 0x80;
   1.162 +						dst[0] = 0x00;
   1.163 +					} else {
   1.164 +						dst[0] = (sample&0xFF);
   1.165 +						sample >>= 8;
   1.166 +						dst[1] = (sample&0xFF);
   1.167 +					}
   1.168 +					src += 4;
   1.169 +					dst += 2;
   1.170 +				}
   1.171 +			}
   1.172 +		}
   1.173 +		break;
   1.174 +	}
   1.175 +	cvt->len_cvt /= 2;
   1.176 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.177 +		cvt->filters[cvt->filter_index](cvt, format);
   1.178 +	}
   1.179 +}
   1.180 +
   1.181 +
   1.182 +/* Duplicate a mono channel to both stereo channels */
   1.183 +void SDL_ConvertStereo(SDL_AudioCVT *cvt, Uint16 format)
   1.184 +{
   1.185 +	int i;
   1.186 +
   1.187 +#ifdef DEBUG_CONVERT
   1.188 +	fprintf(stderr, "Converting to stereo\n");
   1.189 +#endif
   1.190 +	if ( (format & 0xFF) == 16 ) {
   1.191 +		Uint16 *src, *dst;
   1.192 +
   1.193 +		src = (Uint16 *)(cvt->buf+cvt->len_cvt);
   1.194 +		dst = (Uint16 *)(cvt->buf+cvt->len_cvt*2);
   1.195 +		for ( i=cvt->len_cvt/2; i; --i ) {
   1.196 +			dst -= 2;
   1.197 +			src -= 1;
   1.198 +			dst[0] = src[0];
   1.199 +			dst[1] = src[0];
   1.200 +		}
   1.201 +	} else {
   1.202 +		Uint8 *src, *dst;
   1.203 +
   1.204 +		src = cvt->buf+cvt->len_cvt;
   1.205 +		dst = cvt->buf+cvt->len_cvt*2;
   1.206 +		for ( i=cvt->len_cvt; i; --i ) {
   1.207 +			dst -= 2;
   1.208 +			src -= 1;
   1.209 +			dst[0] = src[0];
   1.210 +			dst[1] = src[0];
   1.211 +		}
   1.212 +	}
   1.213 +	cvt->len_cvt *= 2;
   1.214 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.215 +		cvt->filters[cvt->filter_index](cvt, format);
   1.216 +	}
   1.217 +}
   1.218 +
   1.219 +/* Convert 8-bit to 16-bit - LSB */
   1.220 +void SDL_Convert16LSB(SDL_AudioCVT *cvt, Uint16 format)
   1.221 +{
   1.222 +	int i;
   1.223 +	Uint8 *src, *dst;
   1.224 +
   1.225 +#ifdef DEBUG_CONVERT
   1.226 +	fprintf(stderr, "Converting to 16-bit LSB\n");
   1.227 +#endif
   1.228 +	src = cvt->buf+cvt->len_cvt;
   1.229 +	dst = cvt->buf+cvt->len_cvt*2;
   1.230 +	for ( i=cvt->len_cvt; i; --i ) {
   1.231 +		src -= 1;
   1.232 +		dst -= 2;
   1.233 +		dst[1] = *src;
   1.234 +		dst[0] = 0;
   1.235 +	}
   1.236 +	format = ((format & ~0x0008) | AUDIO_U16LSB);
   1.237 +	cvt->len_cvt *= 2;
   1.238 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.239 +		cvt->filters[cvt->filter_index](cvt, format);
   1.240 +	}
   1.241 +}
   1.242 +/* Convert 8-bit to 16-bit - MSB */
   1.243 +void SDL_Convert16MSB(SDL_AudioCVT *cvt, Uint16 format)
   1.244 +{
   1.245 +	int i;
   1.246 +	Uint8 *src, *dst;
   1.247 +
   1.248 +#ifdef DEBUG_CONVERT
   1.249 +	fprintf(stderr, "Converting to 16-bit MSB\n");
   1.250 +#endif
   1.251 +	src = cvt->buf+cvt->len_cvt;
   1.252 +	dst = cvt->buf+cvt->len_cvt*2;
   1.253 +	for ( i=cvt->len_cvt; i; --i ) {
   1.254 +		src -= 1;
   1.255 +		dst -= 2;
   1.256 +		dst[0] = *src;
   1.257 +		dst[1] = 0;
   1.258 +	}
   1.259 +	format = ((format & ~0x0008) | AUDIO_U16MSB);
   1.260 +	cvt->len_cvt *= 2;
   1.261 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.262 +		cvt->filters[cvt->filter_index](cvt, format);
   1.263 +	}
   1.264 +}
   1.265 +
   1.266 +/* Convert 16-bit to 8-bit */
   1.267 +void SDL_Convert8(SDL_AudioCVT *cvt, Uint16 format)
   1.268 +{
   1.269 +	int i;
   1.270 +	Uint8 *src, *dst;
   1.271 +
   1.272 +#ifdef DEBUG_CONVERT
   1.273 +	fprintf(stderr, "Converting to 8-bit\n");
   1.274 +#endif
   1.275 +	src = cvt->buf;
   1.276 +	dst = cvt->buf;
   1.277 +	if ( (format & 0x1000) != 0x1000 ) { /* Little endian */
   1.278 +		++src;
   1.279 +	}
   1.280 +	for ( i=cvt->len_cvt/2; i; --i ) {
   1.281 +		*dst = *src;
   1.282 +		src += 2;
   1.283 +		dst += 1;
   1.284 +	}
   1.285 +	format = ((format & ~0x9010) | AUDIO_U8);
   1.286 +	cvt->len_cvt /= 2;
   1.287 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.288 +		cvt->filters[cvt->filter_index](cvt, format);
   1.289 +	}
   1.290 +}
   1.291 +
   1.292 +/* Toggle signed/unsigned */
   1.293 +void SDL_ConvertSign(SDL_AudioCVT *cvt, Uint16 format)
   1.294 +{
   1.295 +	int i;
   1.296 +	Uint8 *data;
   1.297 +
   1.298 +#ifdef DEBUG_CONVERT
   1.299 +	fprintf(stderr, "Converting audio signedness\n");
   1.300 +#endif
   1.301 +	data = cvt->buf;
   1.302 +	if ( (format & 0xFF) == 16 ) {
   1.303 +		if ( (format & 0x1000) != 0x1000 ) { /* Little endian */
   1.304 +			++data;
   1.305 +		}
   1.306 +		for ( i=cvt->len_cvt/2; i; --i ) {
   1.307 +			*data ^= 0x80;
   1.308 +			data += 2;
   1.309 +		}
   1.310 +	} else {
   1.311 +		for ( i=cvt->len_cvt; i; --i ) {
   1.312 +			*data++ ^= 0x80;
   1.313 +		}
   1.314 +	}
   1.315 +	format = (format ^ 0x8000);
   1.316 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.317 +		cvt->filters[cvt->filter_index](cvt, format);
   1.318 +	}
   1.319 +}
   1.320 +
   1.321 +/* Toggle endianness */
   1.322 +void SDL_ConvertEndian(SDL_AudioCVT *cvt, Uint16 format)
   1.323 +{
   1.324 +	int i;
   1.325 +	Uint8 *data, tmp;
   1.326 +
   1.327 +#ifdef DEBUG_CONVERT
   1.328 +	fprintf(stderr, "Converting audio endianness\n");
   1.329 +#endif
   1.330 +	data = cvt->buf;
   1.331 +	for ( i=cvt->len_cvt/2; i; --i ) {
   1.332 +		tmp = data[0];
   1.333 +		data[0] = data[1];
   1.334 +		data[1] = tmp;
   1.335 +		data += 2;
   1.336 +	}
   1.337 +	format = (format ^ 0x1000);
   1.338 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.339 +		cvt->filters[cvt->filter_index](cvt, format);
   1.340 +	}
   1.341 +}
   1.342 +
   1.343 +/* Convert rate up by multiple of 2 */
   1.344 +void SDL_RateMUL2(SDL_AudioCVT *cvt, Uint16 format)
   1.345 +{
   1.346 +	int i;
   1.347 +	Uint8 *src, *dst;
   1.348 +
   1.349 +#ifdef DEBUG_CONVERT
   1.350 +	fprintf(stderr, "Converting audio rate * 2\n");
   1.351 +#endif
   1.352 +	src = cvt->buf+cvt->len_cvt;
   1.353 +	dst = cvt->buf+cvt->len_cvt*2;
   1.354 +	switch (format & 0xFF) {
   1.355 +		case 8:
   1.356 +			for ( i=cvt->len_cvt; i; --i ) {
   1.357 +				src -= 1;
   1.358 +				dst -= 2;
   1.359 +				dst[0] = src[0];
   1.360 +				dst[1] = src[0];
   1.361 +			}
   1.362 +			break;
   1.363 +		case 16:
   1.364 +			for ( i=cvt->len_cvt/2; i; --i ) {
   1.365 +				src -= 2;
   1.366 +				dst -= 4;
   1.367 +				dst[0] = src[0];
   1.368 +				dst[1] = src[1];
   1.369 +				dst[2] = src[0];
   1.370 +				dst[3] = src[1];
   1.371 +			}
   1.372 +			break;
   1.373 +	}
   1.374 +	cvt->len_cvt *= 2;
   1.375 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.376 +		cvt->filters[cvt->filter_index](cvt, format);
   1.377 +	}
   1.378 +}
   1.379 +
   1.380 +/* Convert rate down by multiple of 2 */
   1.381 +void SDL_RateDIV2(SDL_AudioCVT *cvt, Uint16 format)
   1.382 +{
   1.383 +	int i;
   1.384 +	Uint8 *src, *dst;
   1.385 +
   1.386 +#ifdef DEBUG_CONVERT
   1.387 +	fprintf(stderr, "Converting audio rate / 2\n");
   1.388 +#endif
   1.389 +	src = cvt->buf;
   1.390 +	dst = cvt->buf;
   1.391 +	switch (format & 0xFF) {
   1.392 +		case 8:
   1.393 +			for ( i=cvt->len_cvt/2; i; --i ) {
   1.394 +				dst[0] = src[0];
   1.395 +				src += 2;
   1.396 +				dst += 1;
   1.397 +			}
   1.398 +			break;
   1.399 +		case 16:
   1.400 +			for ( i=cvt->len_cvt/4; i; --i ) {
   1.401 +				dst[0] = src[0];
   1.402 +				dst[1] = src[1];
   1.403 +				src += 4;
   1.404 +				dst += 2;
   1.405 +			}
   1.406 +			break;
   1.407 +	}
   1.408 +	cvt->len_cvt /= 2;
   1.409 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.410 +		cvt->filters[cvt->filter_index](cvt, format);
   1.411 +	}
   1.412 +}
   1.413 +
   1.414 +/* Very slow rate conversion routine */
   1.415 +void SDL_RateSLOW(SDL_AudioCVT *cvt, Uint16 format)
   1.416 +{
   1.417 +	double ipos;
   1.418 +	int i, clen;
   1.419 +
   1.420 +#ifdef DEBUG_CONVERT
   1.421 +	fprintf(stderr, "Converting audio rate * %4.4f\n", 1.0/cvt->rate_incr);
   1.422 +#endif
   1.423 +	clen = (int)((double)cvt->len_cvt / cvt->rate_incr);
   1.424 +	if ( cvt->rate_incr > 1.0 ) {
   1.425 +		switch (format & 0xFF) {
   1.426 +			case 8: {
   1.427 +				Uint8 *output;
   1.428 +
   1.429 +				output = cvt->buf;
   1.430 +				ipos = 0.0;
   1.431 +				for ( i=clen; i; --i ) {
   1.432 +					*output = cvt->buf[(int)ipos];
   1.433 +					ipos += cvt->rate_incr;
   1.434 +					output += 1;
   1.435 +				}
   1.436 +			}
   1.437 +			break;
   1.438 +
   1.439 +			case 16: {
   1.440 +				Uint16 *output;
   1.441 +
   1.442 +				clen &= ~1;
   1.443 +				output = (Uint16 *)cvt->buf;
   1.444 +				ipos = 0.0;
   1.445 +				for ( i=clen/2; i; --i ) {
   1.446 +					*output=((Uint16 *)cvt->buf)[(int)ipos];
   1.447 +					ipos += cvt->rate_incr;
   1.448 +					output += 1;
   1.449 +				}
   1.450 +			}
   1.451 +			break;
   1.452 +		}
   1.453 +	} else {
   1.454 +		switch (format & 0xFF) {
   1.455 +			case 8: {
   1.456 +				Uint8 *output;
   1.457 +
   1.458 +				output = cvt->buf+clen;
   1.459 +				ipos = (double)cvt->len_cvt;
   1.460 +				for ( i=clen; i; --i ) {
   1.461 +					ipos -= cvt->rate_incr;
   1.462 +					output -= 1;
   1.463 +					*output = cvt->buf[(int)ipos];
   1.464 +				}
   1.465 +			}
   1.466 +			break;
   1.467 +
   1.468 +			case 16: {
   1.469 +				Uint16 *output;
   1.470 +
   1.471 +				clen &= ~1;
   1.472 +				output = (Uint16 *)(cvt->buf+clen);
   1.473 +				ipos = (double)cvt->len_cvt/2;
   1.474 +				for ( i=clen/2; i; --i ) {
   1.475 +					ipos -= cvt->rate_incr;
   1.476 +					output -= 1;
   1.477 +					*output=((Uint16 *)cvt->buf)[(int)ipos];
   1.478 +				}
   1.479 +			}
   1.480 +			break;
   1.481 +		}
   1.482 +	}
   1.483 +	cvt->len_cvt = clen;
   1.484 +	if ( cvt->filters[++cvt->filter_index] ) {
   1.485 +		cvt->filters[cvt->filter_index](cvt, format);
   1.486 +	}
   1.487 +}
   1.488 +
   1.489 +int SDL_ConvertAudio(SDL_AudioCVT *cvt)
   1.490 +{
   1.491 +	/* Make sure there's data to convert */
   1.492 +	if ( cvt->buf == NULL ) {
   1.493 +		SDL_SetError("No buffer allocated for conversion");
   1.494 +		return(-1);
   1.495 +	}
   1.496 +	/* Return okay if no conversion is necessary */
   1.497 +	cvt->len_cvt = cvt->len;
   1.498 +	if ( cvt->filters[0] == NULL ) {
   1.499 +		return(0);
   1.500 +	}
   1.501 +
   1.502 +	/* Set up the conversion and go! */
   1.503 +	cvt->filter_index = 0;
   1.504 +	cvt->filters[0](cvt, cvt->src_format);
   1.505 +	return(0);
   1.506 +}
   1.507 +
   1.508 +/* Creates a set of audio filters to convert from one format to another. 
   1.509 +   Returns -1 if the format conversion is not supported, or 1 if the
   1.510 +   audio filter is set up.
   1.511 +*/
   1.512 +  
   1.513 +int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
   1.514 +	Uint16 src_format, Uint8 src_channels, int src_rate,
   1.515 +	Uint16 dst_format, Uint8 dst_channels, int dst_rate)
   1.516 +{
   1.517 +	/* Start off with no conversion necessary */
   1.518 +	cvt->needed = 0;
   1.519 +	cvt->filter_index = 0;
   1.520 +	cvt->filters[0] = NULL;
   1.521 +	cvt->len_mult = 1;
   1.522 +	cvt->len_ratio = 1.0;
   1.523 +
   1.524 +	/* First filter:  Endian conversion from src to dst */
   1.525 +	if ( (src_format & 0x1000) != (dst_format & 0x1000)
   1.526 +	     && ((src_format & 0xff) != 8) ) {
   1.527 +		cvt->filters[cvt->filter_index++] = SDL_ConvertEndian;
   1.528 +	}
   1.529 +	
   1.530 +	/* Second filter: Sign conversion -- signed/unsigned */
   1.531 +	if ( (src_format & 0x8000) != (dst_format & 0x8000) ) {
   1.532 +		cvt->filters[cvt->filter_index++] = SDL_ConvertSign;
   1.533 +	}
   1.534 +
   1.535 +	/* Next filter:  Convert 16 bit <--> 8 bit PCM */
   1.536 +	if ( (src_format & 0xFF) != (dst_format & 0xFF) ) {
   1.537 +		switch (dst_format&0x10FF) {
   1.538 +			case AUDIO_U8:
   1.539 +				cvt->filters[cvt->filter_index++] =
   1.540 +							 SDL_Convert8;
   1.541 +				cvt->len_ratio /= 2;
   1.542 +				break;
   1.543 +			case AUDIO_U16LSB:
   1.544 +				cvt->filters[cvt->filter_index++] =
   1.545 +							SDL_Convert16LSB;
   1.546 +				cvt->len_mult *= 2;
   1.547 +				cvt->len_ratio *= 2;
   1.548 +				break;
   1.549 +			case AUDIO_U16MSB:
   1.550 +				cvt->filters[cvt->filter_index++] =
   1.551 +							SDL_Convert16MSB;
   1.552 +				cvt->len_mult *= 2;
   1.553 +				cvt->len_ratio *= 2;
   1.554 +				break;
   1.555 +		}
   1.556 +	}
   1.557 +
   1.558 +	/* Last filter:  Mono/Stereo conversion */
   1.559 +	if ( src_channels != dst_channels ) {
   1.560 +		while ( (src_channels*2) <= dst_channels ) {
   1.561 +			cvt->filters[cvt->filter_index++] = 
   1.562 +						SDL_ConvertStereo;
   1.563 +			cvt->len_mult *= 2;
   1.564 +			src_channels *= 2;
   1.565 +			cvt->len_ratio *= 2;
   1.566 +		}
   1.567 +		/* This assumes that 4 channel audio is in the format:
   1.568 +		     Left {front/back} + Right {front/back}
   1.569 +		   so converting to L/R stereo works properly.
   1.570 +		 */
   1.571 +		while ( ((src_channels%2) == 0) &&
   1.572 +				((src_channels/2) >= dst_channels) ) {
   1.573 +			cvt->filters[cvt->filter_index++] =
   1.574 +						 SDL_ConvertMono;
   1.575 +			src_channels /= 2;
   1.576 +			cvt->len_ratio /= 2;
   1.577 +		}
   1.578 +		if ( src_channels != dst_channels ) {
   1.579 +			/* Uh oh.. */;
   1.580 +		}
   1.581 +	}
   1.582 +
   1.583 +	/* Do rate conversion */
   1.584 +	cvt->rate_incr = 0.0;
   1.585 +	if ( (src_rate/100) != (dst_rate/100) ) {
   1.586 +		Uint32 hi_rate, lo_rate;
   1.587 +		int len_mult;
   1.588 +		double len_ratio;
   1.589 +		void (*rate_cvt)(SDL_AudioCVT *cvt, Uint16 format);
   1.590 +
   1.591 +		if ( src_rate > dst_rate ) {
   1.592 +			hi_rate = src_rate;
   1.593 +			lo_rate = dst_rate;
   1.594 +			rate_cvt = SDL_RateDIV2;
   1.595 +			len_mult = 1;
   1.596 +			len_ratio = 0.5;
   1.597 +		} else {
   1.598 +			hi_rate = dst_rate;
   1.599 +			lo_rate = src_rate;
   1.600 +			rate_cvt = SDL_RateMUL2;
   1.601 +			len_mult = 2;
   1.602 +			len_ratio = 2.0;
   1.603 +		}
   1.604 +		/* If hi_rate = lo_rate*2^x then conversion is easy */
   1.605 +		while ( ((lo_rate*2)/100) <= (hi_rate/100) ) {
   1.606 +			cvt->filters[cvt->filter_index++] = rate_cvt;
   1.607 +			cvt->len_mult *= len_mult;
   1.608 +			lo_rate *= 2;
   1.609 +			cvt->len_ratio *= len_ratio;
   1.610 +		}
   1.611 +		/* We may need a slow conversion here to finish up */
   1.612 +		if ( (lo_rate/100) != (hi_rate/100) ) {
   1.613 +#if 1
   1.614 +			/* The problem with this is that if the input buffer is
   1.615 +			   say 1K, and the conversion rate is say 1.1, then the
   1.616 +			   output buffer is 1.1K, which may not be an acceptable
   1.617 +			   buffer size for the audio driver (not a power of 2)
   1.618 +			*/
   1.619 +			/* For now, punt and hope the rate distortion isn't great.
   1.620 +			*/
   1.621 +#else
   1.622 +			if ( src_rate < dst_rate ) {
   1.623 +				cvt->rate_incr = (double)lo_rate/hi_rate;
   1.624 +				cvt->len_mult *= 2;
   1.625 +				cvt->len_ratio /= cvt->rate_incr;
   1.626 +			} else {
   1.627 +				cvt->rate_incr = (double)hi_rate/lo_rate;
   1.628 +				cvt->len_ratio *= cvt->rate_incr;
   1.629 +			}
   1.630 +			cvt->filters[cvt->filter_index++] = SDL_RateSLOW;
   1.631 +#endif
   1.632 +		}
   1.633 +	}
   1.634 +
   1.635 +	/* Set up the filter information */
   1.636 +	if ( cvt->filter_index != 0 ) {
   1.637 +		cvt->needed = 1;
   1.638 +		cvt->src_format = src_format;
   1.639 +		cvt->dst_format = dst_format;
   1.640 +		cvt->len = 0;
   1.641 +		cvt->buf = NULL;
   1.642 +		cvt->filters[cvt->filter_index] = NULL;
   1.643 +	}
   1.644 +	return(cvt->needed);
   1.645 +}