src/audio/SDL_audiocvt.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1011 4095d9ca23f2
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* Functions for audio drivers to perform runtime conversion of audio format */
    24 
    25 #include <stdio.h>
    26 
    27 #include "SDL_error.h"
    28 #include "SDL_audio.h"
    29 
    30 
    31 /* Effectively mix right and left channels into a single channel */
    32 void SDL_ConvertMono(SDL_AudioCVT *cvt, Uint16 format)
    33 {
    34 	int i;
    35 	Sint32 sample;
    36 
    37 #ifdef DEBUG_CONVERT
    38 	fprintf(stderr, "Converting to mono\n");
    39 #endif
    40 	switch (format&0x8018) {
    41 
    42 		case AUDIO_U8: {
    43 			Uint8 *src, *dst;
    44 
    45 			src = cvt->buf;
    46 			dst = cvt->buf;
    47 			for ( i=cvt->len_cvt/2; i; --i ) {
    48 				sample = src[0] + src[1];
    49 				if ( sample > 255 ) {
    50 					*dst = 255;
    51 				} else {
    52 					*dst = sample;
    53 				}
    54 				src += 2;
    55 				dst += 1;
    56 			}
    57 		}
    58 		break;
    59 
    60 		case AUDIO_S8: {
    61 			Sint8 *src, *dst;
    62 
    63 			src = (Sint8 *)cvt->buf;
    64 			dst = (Sint8 *)cvt->buf;
    65 			for ( i=cvt->len_cvt/2; i; --i ) {
    66 				sample = src[0] + src[1];
    67 				if ( sample > 127 ) {
    68 					*dst = 127;
    69 				} else
    70 				if ( sample < -128 ) {
    71 					*dst = -128;
    72 				} else {
    73 					*dst = sample;
    74 				}
    75 				src += 2;
    76 				dst += 1;
    77 			}
    78 		}
    79 		break;
    80 
    81 		case AUDIO_U16: {
    82 			Uint8 *src, *dst;
    83 
    84 			src = cvt->buf;
    85 			dst = cvt->buf;
    86 			if ( (format & 0x1000) == 0x1000 ) {
    87 				for ( i=cvt->len_cvt/4; i; --i ) {
    88 					sample = (Uint16)((src[0]<<8)|src[1])+
    89 					         (Uint16)((src[2]<<8)|src[3]);
    90 					if ( sample > 65535 ) {
    91 						dst[0] = 0xFF;
    92 						dst[1] = 0xFF;
    93 					} else {
    94 						dst[1] = (sample&0xFF);
    95 						sample >>= 8;
    96 						dst[0] = (sample&0xFF);
    97 					}
    98 					src += 4;
    99 					dst += 2;
   100 				}
   101 			} else {
   102 				for ( i=cvt->len_cvt/4; i; --i ) {
   103 					sample = (Uint16)((src[1]<<8)|src[0])+
   104 					         (Uint16)((src[3]<<8)|src[2]);
   105 					if ( sample > 65535 ) {
   106 						dst[0] = 0xFF;
   107 						dst[1] = 0xFF;
   108 					} else {
   109 						dst[0] = (sample&0xFF);
   110 						sample >>= 8;
   111 						dst[1] = (sample&0xFF);
   112 					}
   113 					src += 4;
   114 					dst += 2;
   115 				}
   116 			}
   117 		}
   118 		break;
   119 
   120 		case AUDIO_S16: {
   121 			Uint8 *src, *dst;
   122 
   123 			src = cvt->buf;
   124 			dst = cvt->buf;
   125 			if ( (format & 0x1000) == 0x1000 ) {
   126 				for ( i=cvt->len_cvt/4; i; --i ) {
   127 					sample = (Sint16)((src[0]<<8)|src[1])+
   128 					         (Sint16)((src[2]<<8)|src[3]);
   129 					if ( sample > 32767 ) {
   130 						dst[0] = 0x7F;
   131 						dst[1] = 0xFF;
   132 					} else
   133 					if ( sample < -32768 ) {
   134 						dst[0] = 0x80;
   135 						dst[1] = 0x00;
   136 					} else {
   137 						dst[1] = (sample&0xFF);
   138 						sample >>= 8;
   139 						dst[0] = (sample&0xFF);
   140 					}
   141 					src += 4;
   142 					dst += 2;
   143 				}
   144 			} else {
   145 				for ( i=cvt->len_cvt/4; i; --i ) {
   146 					sample = (Sint16)((src[1]<<8)|src[0])+
   147 					         (Sint16)((src[3]<<8)|src[2]);
   148 					if ( sample > 32767 ) {
   149 						dst[1] = 0x7F;
   150 						dst[0] = 0xFF;
   151 					} else
   152 					if ( sample < -32768 ) {
   153 						dst[1] = 0x80;
   154 						dst[0] = 0x00;
   155 					} else {
   156 						dst[0] = (sample&0xFF);
   157 						sample >>= 8;
   158 						dst[1] = (sample&0xFF);
   159 					}
   160 					src += 4;
   161 					dst += 2;
   162 				}
   163 			}
   164 		}
   165 		break;
   166 	}
   167 	cvt->len_cvt /= 2;
   168 	if ( cvt->filters[++cvt->filter_index] ) {
   169 		cvt->filters[cvt->filter_index](cvt, format);
   170 	}
   171 }
   172 
   173 /* Discard top 4 channels */
   174 void SDL_ConvertStrip(SDL_AudioCVT *cvt, Uint16 format)
   175 {
   176 	int i;
   177 	Sint32 lsample, rsample;
   178 
   179 #ifdef DEBUG_CONVERT
   180 	fprintf(stderr, "Converting down to stereo\n");
   181 #endif
   182 	switch (format&0x8018) {
   183 
   184 		case AUDIO_U8: {
   185 			Uint8 *src, *dst;
   186 
   187 			src = cvt->buf;
   188 			dst = cvt->buf;
   189 			for ( i=cvt->len_cvt/6; i; --i ) {
   190 				lsample = src[0];
   191 				rsample = src[1];
   192 				dst[0] = lsample;
   193 				dst[1] = rsample;
   194 				src += 6;
   195 				dst += 2;
   196 			}
   197 		}
   198 		break;
   199 
   200 		case AUDIO_S8: {
   201 			Sint8 *src, *dst;
   202 
   203 			src = (Sint8 *)cvt->buf;
   204 			dst = (Sint8 *)cvt->buf;
   205 			for ( i=cvt->len_cvt/6; i; --i ) {
   206 				lsample = src[0];
   207 				rsample = src[1];
   208 				dst[0] = lsample;
   209 				dst[1] = rsample;
   210 				src += 6;
   211 				dst += 2;
   212 			}
   213 		}
   214 		break;
   215 
   216 		case AUDIO_U16: {
   217 			Uint8 *src, *dst;
   218 
   219 			src = cvt->buf;
   220 			dst = cvt->buf;
   221 			if ( (format & 0x1000) == 0x1000 ) {
   222 				for ( i=cvt->len_cvt/12; i; --i ) {
   223 					lsample = (Uint16)((src[0]<<8)|src[1]);
   224 					rsample = (Uint16)((src[2]<<8)|src[3]);
   225 						dst[1] = (lsample&0xFF);
   226 						lsample >>= 8;
   227 						dst[0] = (lsample&0xFF);
   228 						dst[3] = (rsample&0xFF);
   229 						rsample >>= 8;
   230 						dst[2] = (rsample&0xFF);
   231 					src += 12;
   232 					dst += 4;
   233 				}
   234 			} else {
   235 				for ( i=cvt->len_cvt/12; i; --i ) {
   236 					lsample = (Uint16)((src[1]<<8)|src[0]);
   237 					rsample = (Uint16)((src[3]<<8)|src[2]);
   238 						dst[0] = (lsample&0xFF);
   239 						lsample >>= 8;
   240 						dst[1] = (lsample&0xFF);
   241 						dst[2] = (rsample&0xFF);
   242 						rsample >>= 8;
   243 						dst[3] = (rsample&0xFF);
   244 					src += 12;
   245 					dst += 4;
   246 				}
   247 			}
   248 		}
   249 		break;
   250 
   251 		case AUDIO_S16: {
   252 			Uint8 *src, *dst;
   253 
   254 			src = cvt->buf;
   255 			dst = cvt->buf;
   256 			if ( (format & 0x1000) == 0x1000 ) {
   257 				for ( i=cvt->len_cvt/12; i; --i ) {
   258 					lsample = (Sint16)((src[0]<<8)|src[1]);
   259 					rsample = (Sint16)((src[2]<<8)|src[3]);
   260 						dst[1] = (lsample&0xFF);
   261 						lsample >>= 8;
   262 						dst[0] = (lsample&0xFF);
   263 						dst[3] = (rsample&0xFF);
   264 						rsample >>= 8;
   265 						dst[2] = (rsample&0xFF);
   266 					src += 12;
   267 					dst += 4;
   268 				}
   269 			} else {
   270 				for ( i=cvt->len_cvt/12; i; --i ) {
   271 					lsample = (Sint16)((src[1]<<8)|src[0]);
   272 					rsample = (Sint16)((src[3]<<8)|src[2]);
   273 						dst[0] = (lsample&0xFF);
   274 						lsample >>= 8;
   275 						dst[1] = (lsample&0xFF);
   276 						dst[2] = (rsample&0xFF);
   277 						rsample >>= 8;
   278 						dst[3] = (rsample&0xFF);
   279 					src += 12;
   280 					dst += 4;
   281 				}
   282 			}
   283 		}
   284 		break;
   285 	}
   286 	cvt->len_cvt /= 3;
   287 	if ( cvt->filters[++cvt->filter_index] ) {
   288 		cvt->filters[cvt->filter_index](cvt, format);
   289 	}
   290 }
   291 
   292 
   293 /* Discard top 2 channels of 6 */
   294 void SDL_ConvertStrip_2(SDL_AudioCVT *cvt, Uint16 format)
   295 {
   296 	int i;
   297 	Sint32 lsample, rsample;
   298 
   299 #ifdef DEBUG_CONVERT
   300 	fprintf(stderr, "Converting 6 down to quad\n");
   301 #endif
   302 	switch (format&0x8018) {
   303 
   304 		case AUDIO_U8: {
   305 			Uint8 *src, *dst;
   306 
   307 			src = cvt->buf;
   308 			dst = cvt->buf;
   309 			for ( i=cvt->len_cvt/4; i; --i ) {
   310 				lsample = src[0];
   311 				rsample = src[1];
   312 				dst[0] = lsample;
   313 				dst[1] = rsample;
   314 				src += 4;
   315 				dst += 2;
   316 			}
   317 		}
   318 		break;
   319 
   320 		case AUDIO_S8: {
   321 			Sint8 *src, *dst;
   322 
   323 			src = (Sint8 *)cvt->buf;
   324 			dst = (Sint8 *)cvt->buf;
   325 			for ( i=cvt->len_cvt/4; i; --i ) {
   326 				lsample = src[0];
   327 				rsample = src[1];
   328 				dst[0] = lsample;
   329 				dst[1] = rsample;
   330 				src += 4;
   331 				dst += 2;
   332 			}
   333 		}
   334 		break;
   335 
   336 		case AUDIO_U16: {
   337 			Uint8 *src, *dst;
   338 
   339 			src = cvt->buf;
   340 			dst = cvt->buf;
   341 			if ( (format & 0x1000) == 0x1000 ) {
   342 				for ( i=cvt->len_cvt/8; i; --i ) {
   343 					lsample = (Uint16)((src[0]<<8)|src[1]);
   344 					rsample = (Uint16)((src[2]<<8)|src[3]);
   345 						dst[1] = (lsample&0xFF);
   346 						lsample >>= 8;
   347 						dst[0] = (lsample&0xFF);
   348 						dst[3] = (rsample&0xFF);
   349 						rsample >>= 8;
   350 						dst[2] = (rsample&0xFF);
   351 					src += 8;
   352 					dst += 4;
   353 				}
   354 			} else {
   355 				for ( i=cvt->len_cvt/8; i; --i ) {
   356 					lsample = (Uint16)((src[1]<<8)|src[0]);
   357 					rsample = (Uint16)((src[3]<<8)|src[2]);
   358 						dst[0] = (lsample&0xFF);
   359 						lsample >>= 8;
   360 						dst[1] = (lsample&0xFF);
   361 						dst[2] = (rsample&0xFF);
   362 						rsample >>= 8;
   363 						dst[3] = (rsample&0xFF);
   364 					src += 8;
   365 					dst += 4;
   366 				}
   367 			}
   368 		}
   369 		break;
   370 
   371 		case AUDIO_S16: {
   372 			Uint8 *src, *dst;
   373 
   374 			src = cvt->buf;
   375 			dst = cvt->buf;
   376 			if ( (format & 0x1000) == 0x1000 ) {
   377 				for ( i=cvt->len_cvt/8; i; --i ) {
   378 					lsample = (Sint16)((src[0]<<8)|src[1]);
   379 					rsample = (Sint16)((src[2]<<8)|src[3]);
   380 						dst[1] = (lsample&0xFF);
   381 						lsample >>= 8;
   382 						dst[0] = (lsample&0xFF);
   383 						dst[3] = (rsample&0xFF);
   384 						rsample >>= 8;
   385 						dst[2] = (rsample&0xFF);
   386 					src += 8;
   387 					dst += 4;
   388 				}
   389 			} else {
   390 				for ( i=cvt->len_cvt/8; i; --i ) {
   391 					lsample = (Sint16)((src[1]<<8)|src[0]);
   392 					rsample = (Sint16)((src[3]<<8)|src[2]);
   393 						dst[0] = (lsample&0xFF);
   394 						lsample >>= 8;
   395 						dst[1] = (lsample&0xFF);
   396 						dst[2] = (rsample&0xFF);
   397 						rsample >>= 8;
   398 						dst[3] = (rsample&0xFF);
   399 					src += 8;
   400 					dst += 4;
   401 				}
   402 			}
   403 		}
   404 		break;
   405 	}
   406 	cvt->len_cvt /= 2;
   407 	if ( cvt->filters[++cvt->filter_index] ) {
   408 		cvt->filters[cvt->filter_index](cvt, format);
   409 	}
   410 }
   411 
   412 /* Duplicate a mono channel to both stereo channels */
   413 void SDL_ConvertStereo(SDL_AudioCVT *cvt, Uint16 format)
   414 {
   415 	int i;
   416 
   417 #ifdef DEBUG_CONVERT
   418 	fprintf(stderr, "Converting to stereo\n");
   419 #endif
   420 	if ( (format & 0xFF) == 16 ) {
   421 		Uint16 *src, *dst;
   422 
   423 		src = (Uint16 *)(cvt->buf+cvt->len_cvt);
   424 		dst = (Uint16 *)(cvt->buf+cvt->len_cvt*2);
   425 		for ( i=cvt->len_cvt/2; i; --i ) {
   426 			dst -= 2;
   427 			src -= 1;
   428 			dst[0] = src[0];
   429 			dst[1] = src[0];
   430 		}
   431 	} else {
   432 		Uint8 *src, *dst;
   433 
   434 		src = cvt->buf+cvt->len_cvt;
   435 		dst = cvt->buf+cvt->len_cvt*2;
   436 		for ( i=cvt->len_cvt; i; --i ) {
   437 			dst -= 2;
   438 			src -= 1;
   439 			dst[0] = src[0];
   440 			dst[1] = src[0];
   441 		}
   442 	}
   443 	cvt->len_cvt *= 2;
   444 	if ( cvt->filters[++cvt->filter_index] ) {
   445 		cvt->filters[cvt->filter_index](cvt, format);
   446 	}
   447 }
   448 
   449 
   450 /* Duplicate a stereo channel to a pseudo-5.1 stream */
   451 void SDL_ConvertSurround(SDL_AudioCVT *cvt, Uint16 format)
   452 {
   453 	int i;
   454 
   455 #ifdef DEBUG_CONVERT
   456 	fprintf(stderr, "Converting stereo to surround\n");
   457 #endif
   458 	switch (format&0x8018) {
   459 
   460 		case AUDIO_U8: {
   461 			Uint8 *src, *dst, lf, rf, ce;
   462 
   463 			src = (Uint8 *)(cvt->buf+cvt->len_cvt);
   464 			dst = (Uint8 *)(cvt->buf+cvt->len_cvt*3);
   465 			for ( i=cvt->len_cvt; i; --i ) {
   466 				dst -= 6;
   467 				src -= 2;
   468 				lf = src[0];
   469 				rf = src[1];
   470 				ce = (lf/2) + (rf/2);
   471 				dst[0] = lf;
   472 				dst[1] = rf;
   473 				dst[2] = lf - ce;
   474 				dst[3] = rf - ce;
   475 				dst[4] = ce;
   476 				dst[5] = ce;
   477 			}
   478 		}
   479 		break;
   480 
   481 		case AUDIO_S8: {
   482 			Sint8 *src, *dst, lf, rf, ce;
   483 
   484 			src = (Sint8 *)cvt->buf+cvt->len_cvt;
   485 			dst = (Sint8 *)cvt->buf+cvt->len_cvt*3;
   486 			for ( i=cvt->len_cvt; i; --i ) {
   487 				dst -= 6;
   488 				src -= 2;
   489 				lf = src[0];
   490 				rf = src[1];
   491 				ce = (lf/2) + (rf/2);
   492 				dst[0] = lf;
   493 				dst[1] = rf;
   494 				dst[2] = lf - ce;
   495 				dst[3] = rf - ce;
   496 				dst[4] = ce;
   497 				dst[5] = ce;
   498 			}
   499 		}
   500 		break;
   501 
   502 		case AUDIO_U16: {
   503 			Uint8 *src, *dst;
   504 			Uint16 lf, rf, ce, lr, rr;
   505 
   506 			src = cvt->buf+cvt->len_cvt;
   507 			dst = cvt->buf+cvt->len_cvt*3;
   508 
   509 			if ( (format & 0x1000) == 0x1000 ) {
   510 				for ( i=cvt->len_cvt/4; i; --i ) {
   511 					dst -= 12;
   512 					src -= 4;
   513 					lf = (Uint16)((src[0]<<8)|src[1]);
   514 					rf = (Uint16)((src[2]<<8)|src[3]);
   515 					ce = (lf/2) + (rf/2);
   516 					rr = lf - ce;
   517 					lr = rf - ce;
   518 						dst[1] = (lf&0xFF);
   519 						dst[0] = ((lf>>8)&0xFF);
   520 						dst[3] = (rf&0xFF);
   521 						dst[2] = ((rf>>8)&0xFF);
   522 
   523 						dst[1+4] = (lr&0xFF);
   524 						dst[0+4] = ((lr>>8)&0xFF);
   525 						dst[3+4] = (rr&0xFF);
   526 						dst[2+4] = ((rr>>8)&0xFF);
   527 
   528 						dst[1+8] = (ce&0xFF);
   529 						dst[0+8] = ((ce>>8)&0xFF);
   530 						dst[3+8] = (ce&0xFF);
   531 						dst[2+8] = ((ce>>8)&0xFF);
   532 				}
   533 			} else {
   534 				for ( i=cvt->len_cvt/4; i; --i ) {
   535 					dst -= 12;
   536 					src -= 4;
   537 					lf = (Uint16)((src[1]<<8)|src[0]);
   538 					rf = (Uint16)((src[3]<<8)|src[2]);
   539 					ce = (lf/2) + (rf/2);
   540 					rr = lf - ce;
   541 					lr = rf - ce;
   542 						dst[0] = (lf&0xFF);
   543 						dst[1] = ((lf>>8)&0xFF);
   544 						dst[2] = (rf&0xFF);
   545 						dst[3] = ((rf>>8)&0xFF);
   546 
   547 						dst[0+4] = (lr&0xFF);
   548 						dst[1+4] = ((lr>>8)&0xFF);
   549 						dst[2+4] = (rr&0xFF);
   550 						dst[3+4] = ((rr>>8)&0xFF);
   551 
   552 						dst[0+8] = (ce&0xFF);
   553 						dst[1+8] = ((ce>>8)&0xFF);
   554 						dst[2+8] = (ce&0xFF);
   555 						dst[3+8] = ((ce>>8)&0xFF);
   556 				}
   557 			}
   558 		}
   559 		break;
   560 
   561 		case AUDIO_S16: {
   562 			Uint8 *src, *dst;
   563 			Sint16 lf, rf, ce, lr, rr;
   564 
   565 			src = cvt->buf+cvt->len_cvt;
   566 			dst = cvt->buf+cvt->len_cvt*3;
   567 
   568 			if ( (format & 0x1000) == 0x1000 ) {
   569 				for ( i=cvt->len_cvt/4; i; --i ) {
   570 					dst -= 12;
   571 					src -= 4;
   572 					lf = (Sint16)((src[0]<<8)|src[1]);
   573 					rf = (Sint16)((src[2]<<8)|src[3]);
   574 					ce = (lf/2) + (rf/2);
   575 					rr = lf - ce;
   576 					lr = rf - ce;
   577 						dst[1] = (lf&0xFF);
   578 						dst[0] = ((lf>>8)&0xFF);
   579 						dst[3] = (rf&0xFF);
   580 						dst[2] = ((rf>>8)&0xFF);
   581 
   582 						dst[1+4] = (lr&0xFF);
   583 						dst[0+4] = ((lr>>8)&0xFF);
   584 						dst[3+4] = (rr&0xFF);
   585 						dst[2+4] = ((rr>>8)&0xFF);
   586 
   587 						dst[1+8] = (ce&0xFF);
   588 						dst[0+8] = ((ce>>8)&0xFF);
   589 						dst[3+8] = (ce&0xFF);
   590 						dst[2+8] = ((ce>>8)&0xFF);
   591 				}
   592 			} else {
   593 				for ( i=cvt->len_cvt/4; i; --i ) {
   594 					dst -= 12;
   595 					src -= 4;
   596 					lf = (Sint16)((src[1]<<8)|src[0]);
   597 					rf = (Sint16)((src[3]<<8)|src[2]);
   598 					ce = (lf/2) + (rf/2);
   599 					rr = lf - ce;
   600 					lr = rf - ce;
   601 						dst[0] = (lf&0xFF);
   602 						dst[1] = ((lf>>8)&0xFF);
   603 						dst[2] = (rf&0xFF);
   604 						dst[3] = ((rf>>8)&0xFF);
   605 
   606 						dst[0+4] = (lr&0xFF);
   607 						dst[1+4] = ((lr>>8)&0xFF);
   608 						dst[2+4] = (rr&0xFF);
   609 						dst[3+4] = ((rr>>8)&0xFF);
   610 
   611 						dst[0+8] = (ce&0xFF);
   612 						dst[1+8] = ((ce>>8)&0xFF);
   613 						dst[2+8] = (ce&0xFF);
   614 						dst[3+8] = ((ce>>8)&0xFF);
   615 				}
   616 			}
   617 		}
   618 		break;
   619 	}
   620 	cvt->len_cvt *= 3;
   621 	if ( cvt->filters[++cvt->filter_index] ) {
   622 		cvt->filters[cvt->filter_index](cvt, format);
   623 	}
   624 }
   625 
   626 
   627 /* Duplicate a stereo channel to a pseudo-4.0 stream */
   628 void SDL_ConvertSurround_4(SDL_AudioCVT *cvt, Uint16 format)
   629 {
   630 	int i;
   631 
   632 #ifdef DEBUG_CONVERT
   633 	fprintf(stderr, "Converting stereo to quad\n");
   634 #endif
   635 	switch (format&0x8018) {
   636 
   637 		case AUDIO_U8: {
   638 			Uint8 *src, *dst, lf, rf, ce;
   639 
   640 			src = (Uint8 *)(cvt->buf+cvt->len_cvt);
   641 			dst = (Uint8 *)(cvt->buf+cvt->len_cvt*2);
   642 			for ( i=cvt->len_cvt; i; --i ) {
   643 				dst -= 4;
   644 				src -= 2;
   645 				lf = src[0];
   646 				rf = src[1];
   647 				ce = (lf/2) + (rf/2);
   648 				dst[0] = lf;
   649 				dst[1] = rf;
   650 				dst[2] = lf - ce;
   651 				dst[3] = rf - ce;
   652 			}
   653 		}
   654 		break;
   655 
   656 		case AUDIO_S8: {
   657 			Sint8 *src, *dst, lf, rf, ce;
   658 
   659 			src = (Sint8 *)cvt->buf+cvt->len_cvt;
   660 			dst = (Sint8 *)cvt->buf+cvt->len_cvt*2;
   661 			for ( i=cvt->len_cvt; i; --i ) {
   662 				dst -= 4;
   663 				src -= 2;
   664 				lf = src[0];
   665 				rf = src[1];
   666 				ce = (lf/2) + (rf/2);
   667 				dst[0] = lf;
   668 				dst[1] = rf;
   669 				dst[2] = lf - ce;
   670 				dst[3] = rf - ce;
   671 			}
   672 		}
   673 		break;
   674 
   675 		case AUDIO_U16: {
   676 			Uint8 *src, *dst;
   677 			Uint16 lf, rf, ce, lr, rr;
   678 
   679 			src = cvt->buf+cvt->len_cvt;
   680 			dst = cvt->buf+cvt->len_cvt*2;
   681 
   682 			if ( (format & 0x1000) == 0x1000 ) {
   683 				for ( i=cvt->len_cvt/4; i; --i ) {
   684 					dst -= 8;
   685 					src -= 4;
   686 					lf = (Uint16)((src[0]<<8)|src[1]);
   687 					rf = (Uint16)((src[2]<<8)|src[3]);
   688 					ce = (lf/2) + (rf/2);
   689 					rr = lf - ce;
   690 					lr = rf - ce;
   691 						dst[1] = (lf&0xFF);
   692 						dst[0] = ((lf>>8)&0xFF);
   693 						dst[3] = (rf&0xFF);
   694 						dst[2] = ((rf>>8)&0xFF);
   695 
   696 						dst[1+4] = (lr&0xFF);
   697 						dst[0+4] = ((lr>>8)&0xFF);
   698 						dst[3+4] = (rr&0xFF);
   699 						dst[2+4] = ((rr>>8)&0xFF);
   700 				}
   701 			} else {
   702 				for ( i=cvt->len_cvt/4; i; --i ) {
   703 					dst -= 8;
   704 					src -= 4;
   705 					lf = (Uint16)((src[1]<<8)|src[0]);
   706 					rf = (Uint16)((src[3]<<8)|src[2]);
   707 					ce = (lf/2) + (rf/2);
   708 					rr = lf - ce;
   709 					lr = rf - ce;
   710 						dst[0] = (lf&0xFF);
   711 						dst[1] = ((lf>>8)&0xFF);
   712 						dst[2] = (rf&0xFF);
   713 						dst[3] = ((rf>>8)&0xFF);
   714 
   715 						dst[0+4] = (lr&0xFF);
   716 						dst[1+4] = ((lr>>8)&0xFF);
   717 						dst[2+4] = (rr&0xFF);
   718 						dst[3+4] = ((rr>>8)&0xFF);
   719 				}
   720 			}
   721 		}
   722 		break;
   723 
   724 		case AUDIO_S16: {
   725 			Uint8 *src, *dst;
   726 			Sint16 lf, rf, ce, lr, rr;
   727 
   728 			src = cvt->buf+cvt->len_cvt;
   729 			dst = cvt->buf+cvt->len_cvt*2;
   730 
   731 			if ( (format & 0x1000) == 0x1000 ) {
   732 				for ( i=cvt->len_cvt/4; i; --i ) {
   733 					dst -= 8;
   734 					src -= 4;
   735 					lf = (Sint16)((src[0]<<8)|src[1]);
   736 					rf = (Sint16)((src[2]<<8)|src[3]);
   737 					ce = (lf/2) + (rf/2);
   738 					rr = lf - ce;
   739 					lr = rf - ce;
   740 						dst[1] = (lf&0xFF);
   741 						dst[0] = ((lf>>8)&0xFF);
   742 						dst[3] = (rf&0xFF);
   743 						dst[2] = ((rf>>8)&0xFF);
   744 
   745 						dst[1+4] = (lr&0xFF);
   746 						dst[0+4] = ((lr>>8)&0xFF);
   747 						dst[3+4] = (rr&0xFF);
   748 						dst[2+4] = ((rr>>8)&0xFF);
   749 				}
   750 			} else {
   751 				for ( i=cvt->len_cvt/4; i; --i ) {
   752 					dst -= 8;
   753 					src -= 4;
   754 					lf = (Sint16)((src[1]<<8)|src[0]);
   755 					rf = (Sint16)((src[3]<<8)|src[2]);
   756 					ce = (lf/2) + (rf/2);
   757 					rr = lf - ce;
   758 					lr = rf - ce;
   759 						dst[0] = (lf&0xFF);
   760 						dst[1] = ((lf>>8)&0xFF);
   761 						dst[2] = (rf&0xFF);
   762 						dst[3] = ((rf>>8)&0xFF);
   763 
   764 						dst[0+4] = (lr&0xFF);
   765 						dst[1+4] = ((lr>>8)&0xFF);
   766 						dst[2+4] = (rr&0xFF);
   767 						dst[3+4] = ((rr>>8)&0xFF);
   768 				}
   769 			}
   770 		}
   771 		break;
   772 	}
   773 	cvt->len_cvt *= 2;
   774 	if ( cvt->filters[++cvt->filter_index] ) {
   775 		cvt->filters[cvt->filter_index](cvt, format);
   776 	}
   777 }
   778 
   779 
   780 /* Convert 8-bit to 16-bit - LSB */
   781 void SDL_Convert16LSB(SDL_AudioCVT *cvt, Uint16 format)
   782 {
   783 	int i;
   784 	Uint8 *src, *dst;
   785 
   786 #ifdef DEBUG_CONVERT
   787 	fprintf(stderr, "Converting to 16-bit LSB\n");
   788 #endif
   789 	src = cvt->buf+cvt->len_cvt;
   790 	dst = cvt->buf+cvt->len_cvt*2;
   791 	for ( i=cvt->len_cvt; i; --i ) {
   792 		src -= 1;
   793 		dst -= 2;
   794 		dst[1] = *src;
   795 		dst[0] = 0;
   796 	}
   797 	format = ((format & ~0x0008) | AUDIO_U16LSB);
   798 	cvt->len_cvt *= 2;
   799 	if ( cvt->filters[++cvt->filter_index] ) {
   800 		cvt->filters[cvt->filter_index](cvt, format);
   801 	}
   802 }
   803 /* Convert 8-bit to 16-bit - MSB */
   804 void SDL_Convert16MSB(SDL_AudioCVT *cvt, Uint16 format)
   805 {
   806 	int i;
   807 	Uint8 *src, *dst;
   808 
   809 #ifdef DEBUG_CONVERT
   810 	fprintf(stderr, "Converting to 16-bit MSB\n");
   811 #endif
   812 	src = cvt->buf+cvt->len_cvt;
   813 	dst = cvt->buf+cvt->len_cvt*2;
   814 	for ( i=cvt->len_cvt; i; --i ) {
   815 		src -= 1;
   816 		dst -= 2;
   817 		dst[0] = *src;
   818 		dst[1] = 0;
   819 	}
   820 	format = ((format & ~0x0008) | AUDIO_U16MSB);
   821 	cvt->len_cvt *= 2;
   822 	if ( cvt->filters[++cvt->filter_index] ) {
   823 		cvt->filters[cvt->filter_index](cvt, format);
   824 	}
   825 }
   826 
   827 /* Convert 16-bit to 8-bit */
   828 void SDL_Convert8(SDL_AudioCVT *cvt, Uint16 format)
   829 {
   830 	int i;
   831 	Uint8 *src, *dst;
   832 
   833 #ifdef DEBUG_CONVERT
   834 	fprintf(stderr, "Converting to 8-bit\n");
   835 #endif
   836 	src = cvt->buf;
   837 	dst = cvt->buf;
   838 	if ( (format & 0x1000) != 0x1000 ) { /* Little endian */
   839 		++src;
   840 	}
   841 	for ( i=cvt->len_cvt/2; i; --i ) {
   842 		*dst = *src;
   843 		src += 2;
   844 		dst += 1;
   845 	}
   846 	format = ((format & ~0x9010) | AUDIO_U8);
   847 	cvt->len_cvt /= 2;
   848 	if ( cvt->filters[++cvt->filter_index] ) {
   849 		cvt->filters[cvt->filter_index](cvt, format);
   850 	}
   851 }
   852 
   853 /* Toggle signed/unsigned */
   854 void SDL_ConvertSign(SDL_AudioCVT *cvt, Uint16 format)
   855 {
   856 	int i;
   857 	Uint8 *data;
   858 
   859 #ifdef DEBUG_CONVERT
   860 	fprintf(stderr, "Converting audio signedness\n");
   861 #endif
   862 	data = cvt->buf;
   863 	if ( (format & 0xFF) == 16 ) {
   864 		if ( (format & 0x1000) != 0x1000 ) { /* Little endian */
   865 			++data;
   866 		}
   867 		for ( i=cvt->len_cvt/2; i; --i ) {
   868 			*data ^= 0x80;
   869 			data += 2;
   870 		}
   871 	} else {
   872 		for ( i=cvt->len_cvt; i; --i ) {
   873 			*data++ ^= 0x80;
   874 		}
   875 	}
   876 	format = (format ^ 0x8000);
   877 	if ( cvt->filters[++cvt->filter_index] ) {
   878 		cvt->filters[cvt->filter_index](cvt, format);
   879 	}
   880 }
   881 
   882 /* Toggle endianness */
   883 void SDL_ConvertEndian(SDL_AudioCVT *cvt, Uint16 format)
   884 {
   885 	int i;
   886 	Uint8 *data, tmp;
   887 
   888 #ifdef DEBUG_CONVERT
   889 	fprintf(stderr, "Converting audio endianness\n");
   890 #endif
   891 	data = cvt->buf;
   892 	for ( i=cvt->len_cvt/2; i; --i ) {
   893 		tmp = data[0];
   894 		data[0] = data[1];
   895 		data[1] = tmp;
   896 		data += 2;
   897 	}
   898 	format = (format ^ 0x1000);
   899 	if ( cvt->filters[++cvt->filter_index] ) {
   900 		cvt->filters[cvt->filter_index](cvt, format);
   901 	}
   902 }
   903 
   904 /* Convert rate up by multiple of 2 */
   905 void SDL_RateMUL2(SDL_AudioCVT *cvt, Uint16 format)
   906 {
   907 	int i;
   908 	Uint8 *src, *dst;
   909 
   910 #ifdef DEBUG_CONVERT
   911 	fprintf(stderr, "Converting audio rate * 2\n");
   912 #endif
   913 	src = cvt->buf+cvt->len_cvt;
   914 	dst = cvt->buf+cvt->len_cvt*2;
   915 	switch (format & 0xFF) {
   916 		case 8:
   917 			for ( i=cvt->len_cvt; i; --i ) {
   918 				src -= 1;
   919 				dst -= 2;
   920 				dst[0] = src[0];
   921 				dst[1] = src[0];
   922 			}
   923 			break;
   924 		case 16:
   925 			for ( i=cvt->len_cvt/2; i; --i ) {
   926 				src -= 2;
   927 				dst -= 4;
   928 				dst[0] = src[0];
   929 				dst[1] = src[1];
   930 				dst[2] = src[0];
   931 				dst[3] = src[1];
   932 			}
   933 			break;
   934 	}
   935 	cvt->len_cvt *= 2;
   936 	if ( cvt->filters[++cvt->filter_index] ) {
   937 		cvt->filters[cvt->filter_index](cvt, format);
   938 	}
   939 }
   940 
   941 
   942 /* Convert rate up by multiple of 2, for stereo */
   943 void SDL_RateMUL2_c2(SDL_AudioCVT *cvt, Uint16 format)
   944 {
   945 	int i;
   946 	Uint8 *src, *dst;
   947 
   948 #ifdef DEBUG_CONVERT
   949 	fprintf(stderr, "Converting audio rate * 2\n");
   950 #endif
   951 	src = cvt->buf+cvt->len_cvt;
   952 	dst = cvt->buf+cvt->len_cvt*2;
   953 	switch (format & 0xFF) {
   954 		case 8:
   955 			for ( i=cvt->len_cvt/2; i; --i ) {
   956 				src -= 2;
   957 				dst -= 4;
   958 				dst[0] = src[0];
   959 				dst[1] = src[1];
   960 				dst[2] = src[0];
   961 				dst[3] = src[1];
   962 			}
   963 			break;
   964 		case 16:
   965 			for ( i=cvt->len_cvt/4; i; --i ) {
   966 				src -= 4;
   967 				dst -= 8;
   968 				dst[0] = src[0];
   969 				dst[1] = src[1];
   970 				dst[2] = src[2];
   971 				dst[3] = src[3];
   972 				dst[4] = src[0];
   973 				dst[5] = src[1];
   974 				dst[6] = src[2];
   975 				dst[7] = src[3];
   976 			}
   977 			break;
   978 	}
   979 	cvt->len_cvt *= 2;
   980 	if ( cvt->filters[++cvt->filter_index] ) {
   981 		cvt->filters[cvt->filter_index](cvt, format);
   982 	}
   983 }
   984 
   985 /* Convert rate up by multiple of 2, for quad */
   986 void SDL_RateMUL2_c4(SDL_AudioCVT *cvt, Uint16 format)
   987 {
   988 	int i;
   989 	Uint8 *src, *dst;
   990 
   991 #ifdef DEBUG_CONVERT
   992 	fprintf(stderr, "Converting audio rate * 2\n");
   993 #endif
   994 	src = cvt->buf+cvt->len_cvt;
   995 	dst = cvt->buf+cvt->len_cvt*2;
   996 	switch (format & 0xFF) {
   997 		case 8:
   998 			for ( i=cvt->len_cvt/4; i; --i ) {
   999 				src -= 4;
  1000 				dst -= 8;
  1001 				dst[0] = src[0];
  1002 				dst[1] = src[1];
  1003 				dst[2] = src[2];
  1004 				dst[3] = src[3];
  1005 				dst[4] = src[0];
  1006 				dst[5] = src[1];
  1007 				dst[6] = src[2];
  1008 				dst[7] = src[3];
  1009 			}
  1010 			break;
  1011 		case 16:
  1012 			for ( i=cvt->len_cvt/8; i; --i ) {
  1013 				src -= 8;
  1014 				dst -= 16;
  1015 				dst[0] = src[0];
  1016 				dst[1] = src[1];
  1017 				dst[2] = src[2];
  1018 				dst[3] = src[3];
  1019 				dst[4] = src[4];
  1020 				dst[5] = src[5];
  1021 				dst[6] = src[6];
  1022 				dst[7] = src[7];
  1023 				dst[8] = src[0];
  1024 				dst[9] = src[1];
  1025 				dst[10] = src[2];
  1026 				dst[11] = src[3];
  1027 				dst[12] = src[4];
  1028 				dst[13] = src[5];
  1029 				dst[14] = src[6];
  1030 				dst[15] = src[7];
  1031 			}
  1032 			break;
  1033 	}
  1034 	cvt->len_cvt *= 2;
  1035 	if ( cvt->filters[++cvt->filter_index] ) {
  1036 		cvt->filters[cvt->filter_index](cvt, format);
  1037 	}
  1038 }
  1039 
  1040 
  1041 /* Convert rate up by multiple of 2, for 5.1 */
  1042 void SDL_RateMUL2_c6(SDL_AudioCVT *cvt, Uint16 format)
  1043 {
  1044 	int i;
  1045 	Uint8 *src, *dst;
  1046 
  1047 #ifdef DEBUG_CONVERT
  1048 	fprintf(stderr, "Converting audio rate * 2\n");
  1049 #endif
  1050 	src = cvt->buf+cvt->len_cvt;
  1051 	dst = cvt->buf+cvt->len_cvt*2;
  1052 	switch (format & 0xFF) {
  1053 		case 8:
  1054 			for ( i=cvt->len_cvt/6; i; --i ) {
  1055 				src -= 6;
  1056 				dst -= 12;
  1057 				dst[0] = src[0];
  1058 				dst[1] = src[1];
  1059 				dst[2] = src[2];
  1060 				dst[3] = src[3];
  1061 				dst[4] = src[4];
  1062 				dst[5] = src[5];
  1063 				dst[6] = src[0];
  1064 				dst[7] = src[1];
  1065 				dst[8] = src[2];
  1066 				dst[9] = src[3];
  1067 				dst[10] = src[4];
  1068 				dst[11] = src[5];
  1069 			}
  1070 			break;
  1071 		case 16:
  1072 			for ( i=cvt->len_cvt/12; i; --i ) {
  1073 				src -= 12;
  1074 				dst -= 24;
  1075 				dst[0] = src[0];
  1076 				dst[1] = src[1];
  1077 				dst[2] = src[2];
  1078 				dst[3] = src[3];
  1079 				dst[4] = src[4];
  1080 				dst[5] = src[5];
  1081 				dst[6] = src[6];
  1082 				dst[7] = src[7];
  1083 				dst[8] = src[8];
  1084 				dst[9] = src[9];
  1085 				dst[10] = src[10];
  1086 				dst[11] = src[11];
  1087 				dst[12] = src[0];
  1088 				dst[13] = src[1];
  1089 				dst[14] = src[2];
  1090 				dst[15] = src[3];
  1091 				dst[16] = src[4];
  1092 				dst[17] = src[5];
  1093 				dst[18] = src[6];
  1094 				dst[19] = src[7];
  1095 				dst[20] = src[8];
  1096 				dst[21] = src[9];
  1097 				dst[22] = src[10];
  1098 				dst[23] = src[11];
  1099 			}
  1100 			break;
  1101 	}
  1102 	cvt->len_cvt *= 2;
  1103 	if ( cvt->filters[++cvt->filter_index] ) {
  1104 		cvt->filters[cvt->filter_index](cvt, format);
  1105 	}
  1106 }
  1107 
  1108 /* Convert rate down by multiple of 2 */
  1109 void SDL_RateDIV2(SDL_AudioCVT *cvt, Uint16 format)
  1110 {
  1111 	int i;
  1112 	Uint8 *src, *dst;
  1113 
  1114 #ifdef DEBUG_CONVERT
  1115 	fprintf(stderr, "Converting audio rate / 2\n");
  1116 #endif
  1117 	src = cvt->buf;
  1118 	dst = cvt->buf;
  1119 	switch (format & 0xFF) {
  1120 		case 8:
  1121 			for ( i=cvt->len_cvt/2; i; --i ) {
  1122 				dst[0] = src[0];
  1123 				src += 2;
  1124 				dst += 1;
  1125 			}
  1126 			break;
  1127 		case 16:
  1128 			for ( i=cvt->len_cvt/4; i; --i ) {
  1129 				dst[0] = src[0];
  1130 				dst[1] = src[1];
  1131 				src += 4;
  1132 				dst += 2;
  1133 			}
  1134 			break;
  1135 	}
  1136 	cvt->len_cvt /= 2;
  1137 	if ( cvt->filters[++cvt->filter_index] ) {
  1138 		cvt->filters[cvt->filter_index](cvt, format);
  1139 	}
  1140 }
  1141 
  1142 
  1143 /* Convert rate down by multiple of 2, for stereo */
  1144 void SDL_RateDIV2_c2(SDL_AudioCVT *cvt, Uint16 format)
  1145 {
  1146 	int i;
  1147 	Uint8 *src, *dst;
  1148 
  1149 #ifdef DEBUG_CONVERT
  1150 	fprintf(stderr, "Converting audio rate / 2\n");
  1151 #endif
  1152 	src = cvt->buf;
  1153 	dst = cvt->buf;
  1154 	switch (format & 0xFF) {
  1155 		case 8:
  1156 			for ( i=cvt->len_cvt/4; i; --i ) {
  1157 				dst[0] = src[0];
  1158 				dst[1] = src[1];
  1159 				src += 4;
  1160 				dst += 2;
  1161 			}
  1162 			break;
  1163 		case 16:
  1164 			for ( i=cvt->len_cvt/8; i; --i ) {
  1165 				dst[0] = src[0];
  1166 				dst[1] = src[1];
  1167 				dst[2] = src[2];
  1168 				dst[3] = src[3];
  1169 				src += 8;
  1170 				dst += 4;
  1171 			}
  1172 			break;
  1173 	}
  1174 	cvt->len_cvt /= 2;
  1175 	if ( cvt->filters[++cvt->filter_index] ) {
  1176 		cvt->filters[cvt->filter_index](cvt, format);
  1177 	}
  1178 }
  1179 
  1180 
  1181 /* Convert rate down by multiple of 2, for quad */
  1182 void SDL_RateDIV2_c4(SDL_AudioCVT *cvt, Uint16 format)
  1183 {
  1184 	int i;
  1185 	Uint8 *src, *dst;
  1186 
  1187 #ifdef DEBUG_CONVERT
  1188 	fprintf(stderr, "Converting audio rate / 2\n");
  1189 #endif
  1190 	src = cvt->buf;
  1191 	dst = cvt->buf;
  1192 	switch (format & 0xFF) {
  1193 		case 8:
  1194 			for ( i=cvt->len_cvt/8; i; --i ) {
  1195 				dst[0] = src[0];
  1196 				dst[1] = src[1];
  1197 				dst[2] = src[2];
  1198 				dst[3] = src[3];
  1199 				src += 8;
  1200 				dst += 4;
  1201 			}
  1202 			break;
  1203 		case 16:
  1204 			for ( i=cvt->len_cvt/16; i; --i ) {
  1205 				dst[0] = src[0];
  1206 				dst[1] = src[1];
  1207 				dst[2] = src[2];
  1208 				dst[3] = src[3];
  1209 				dst[4] = src[4];
  1210 				dst[5] = src[5];
  1211 				dst[6] = src[6];
  1212 				dst[7] = src[7];
  1213 				src += 16;
  1214 				dst += 8;
  1215 			}
  1216 			break;
  1217 	}
  1218 	cvt->len_cvt /= 2;
  1219 	if ( cvt->filters[++cvt->filter_index] ) {
  1220 		cvt->filters[cvt->filter_index](cvt, format);
  1221 	}
  1222 }
  1223 
  1224 /* Convert rate down by multiple of 2, for 5.1 */
  1225 void SDL_RateDIV2_c6(SDL_AudioCVT *cvt, Uint16 format)
  1226 {
  1227 	int i;
  1228 	Uint8 *src, *dst;
  1229 
  1230 #ifdef DEBUG_CONVERT
  1231 	fprintf(stderr, "Converting audio rate / 2\n");
  1232 #endif
  1233 	src = cvt->buf;
  1234 	dst = cvt->buf;
  1235 	switch (format & 0xFF) {
  1236 		case 8:
  1237 			for ( i=cvt->len_cvt/12; i; --i ) {
  1238 				dst[0] = src[0];
  1239 				dst[1] = src[1];
  1240 				dst[2] = src[2];
  1241 				dst[3] = src[3];
  1242 				dst[4] = src[4];
  1243 				dst[5] = src[5];
  1244 				src += 12;
  1245 				dst += 6;
  1246 			}
  1247 			break;
  1248 		case 16:
  1249 			for ( i=cvt->len_cvt/24; i; --i ) {
  1250 				dst[0] = src[0];
  1251 				dst[1] = src[1];
  1252 				dst[2] = src[2];
  1253 				dst[3] = src[3];
  1254 				dst[4] = src[4];
  1255 				dst[5] = src[5];
  1256 				dst[6] = src[6];
  1257 				dst[7] = src[7];
  1258 				dst[8] = src[8];
  1259 				dst[9] = src[9];
  1260 				dst[10] = src[10];
  1261 				dst[11] = src[11];
  1262 				src += 24;
  1263 				dst += 12;
  1264 			}
  1265 			break;
  1266 	}
  1267 	cvt->len_cvt /= 2;
  1268 	if ( cvt->filters[++cvt->filter_index] ) {
  1269 		cvt->filters[cvt->filter_index](cvt, format);
  1270 	}
  1271 }
  1272 
  1273 /* Very slow rate conversion routine */
  1274 void SDL_RateSLOW(SDL_AudioCVT *cvt, Uint16 format)
  1275 {
  1276 	double ipos;
  1277 	int i, clen;
  1278 
  1279 #ifdef DEBUG_CONVERT
  1280 	fprintf(stderr, "Converting audio rate * %4.4f\n", 1.0/cvt->rate_incr);
  1281 #endif
  1282 	clen = (int)((double)cvt->len_cvt / cvt->rate_incr);
  1283 	if ( cvt->rate_incr > 1.0 ) {
  1284 		switch (format & 0xFF) {
  1285 			case 8: {
  1286 				Uint8 *output;
  1287 
  1288 				output = cvt->buf;
  1289 				ipos = 0.0;
  1290 				for ( i=clen; i; --i ) {
  1291 					*output = cvt->buf[(int)ipos];
  1292 					ipos += cvt->rate_incr;
  1293 					output += 1;
  1294 				}
  1295 			}
  1296 			break;
  1297 
  1298 			case 16: {
  1299 				Uint16 *output;
  1300 
  1301 				clen &= ~1;
  1302 				output = (Uint16 *)cvt->buf;
  1303 				ipos = 0.0;
  1304 				for ( i=clen/2; i; --i ) {
  1305 					*output=((Uint16 *)cvt->buf)[(int)ipos];
  1306 					ipos += cvt->rate_incr;
  1307 					output += 1;
  1308 				}
  1309 			}
  1310 			break;
  1311 		}
  1312 	} else {
  1313 		switch (format & 0xFF) {
  1314 			case 8: {
  1315 				Uint8 *output;
  1316 
  1317 				output = cvt->buf+clen;
  1318 				ipos = (double)cvt->len_cvt;
  1319 				for ( i=clen; i; --i ) {
  1320 					ipos -= cvt->rate_incr;
  1321 					output -= 1;
  1322 					*output = cvt->buf[(int)ipos];
  1323 				}
  1324 			}
  1325 			break;
  1326 
  1327 			case 16: {
  1328 				Uint16 *output;
  1329 
  1330 				clen &= ~1;
  1331 				output = (Uint16 *)(cvt->buf+clen);
  1332 				ipos = (double)cvt->len_cvt/2;
  1333 				for ( i=clen/2; i; --i ) {
  1334 					ipos -= cvt->rate_incr;
  1335 					output -= 1;
  1336 					*output=((Uint16 *)cvt->buf)[(int)ipos];
  1337 				}
  1338 			}
  1339 			break;
  1340 		}
  1341 	}
  1342 	cvt->len_cvt = clen;
  1343 	if ( cvt->filters[++cvt->filter_index] ) {
  1344 		cvt->filters[cvt->filter_index](cvt, format);
  1345 	}
  1346 }
  1347 
  1348 int SDL_ConvertAudio(SDL_AudioCVT *cvt)
  1349 {
  1350 	/* Make sure there's data to convert */
  1351 	if ( cvt->buf == NULL ) {
  1352 		SDL_SetError("No buffer allocated for conversion");
  1353 		return(-1);
  1354 	}
  1355 	/* Return okay if no conversion is necessary */
  1356 	cvt->len_cvt = cvt->len;
  1357 	if ( cvt->filters[0] == NULL ) {
  1358 		return(0);
  1359 	}
  1360 
  1361 	/* Set up the conversion and go! */
  1362 	cvt->filter_index = 0;
  1363 	cvt->filters[0](cvt, cvt->src_format);
  1364 	return(0);
  1365 }
  1366 
  1367 /* Creates a set of audio filters to convert from one format to another. 
  1368    Returns -1 if the format conversion is not supported, or 1 if the
  1369    audio filter is set up.
  1370 */
  1371   
  1372 int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
  1373 	Uint16 src_format, Uint8 src_channels, int src_rate,
  1374 	Uint16 dst_format, Uint8 dst_channels, int dst_rate)
  1375 {
  1376 /*printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
  1377 		src_format, dst_format, src_channels, dst_channels, src_rate, dst_rate);*/
  1378 	/* Start off with no conversion necessary */
  1379 	cvt->needed = 0;
  1380 	cvt->filter_index = 0;
  1381 	cvt->filters[0] = NULL;
  1382 	cvt->len_mult = 1;
  1383 	cvt->len_ratio = 1.0;
  1384 
  1385 	/* First filter:  Endian conversion from src to dst */
  1386 	if ( (src_format & 0x1000) != (dst_format & 0x1000)
  1387 	     && ((src_format & 0xff) != 8) ) {
  1388 		cvt->filters[cvt->filter_index++] = SDL_ConvertEndian;
  1389 	}
  1390 	
  1391 	/* Second filter: Sign conversion -- signed/unsigned */
  1392 	if ( (src_format & 0x8000) != (dst_format & 0x8000) ) {
  1393 		cvt->filters[cvt->filter_index++] = SDL_ConvertSign;
  1394 	}
  1395 
  1396 	/* Next filter:  Convert 16 bit <--> 8 bit PCM */
  1397 	if ( (src_format & 0xFF) != (dst_format & 0xFF) ) {
  1398 		switch (dst_format&0x10FF) {
  1399 			case AUDIO_U8:
  1400 				cvt->filters[cvt->filter_index++] =
  1401 							 SDL_Convert8;
  1402 				cvt->len_ratio /= 2;
  1403 				break;
  1404 			case AUDIO_U16LSB:
  1405 				cvt->filters[cvt->filter_index++] =
  1406 							SDL_Convert16LSB;
  1407 				cvt->len_mult *= 2;
  1408 				cvt->len_ratio *= 2;
  1409 				break;
  1410 			case AUDIO_U16MSB:
  1411 				cvt->filters[cvt->filter_index++] =
  1412 							SDL_Convert16MSB;
  1413 				cvt->len_mult *= 2;
  1414 				cvt->len_ratio *= 2;
  1415 				break;
  1416 		}
  1417 	}
  1418 
  1419 	/* Last filter:  Mono/Stereo conversion */
  1420 	if ( src_channels != dst_channels ) {
  1421 		if ( (src_channels == 1) && (dst_channels > 1) ) {
  1422 			cvt->filters[cvt->filter_index++] = 
  1423 						SDL_ConvertStereo;
  1424 			cvt->len_mult *= 2;
  1425 			src_channels = 2;
  1426 			cvt->len_ratio *= 2;
  1427 		}
  1428 		if ( (src_channels == 2) &&
  1429 				(dst_channels == 6) ) {
  1430 			cvt->filters[cvt->filter_index++] =
  1431 						 SDL_ConvertSurround;
  1432 			src_channels = 6;
  1433 			cvt->len_mult *= 3;
  1434 			cvt->len_ratio *= 3;
  1435 		}
  1436 		if ( (src_channels == 2) &&
  1437 				(dst_channels == 4) ) {
  1438 			cvt->filters[cvt->filter_index++] =
  1439 						 SDL_ConvertSurround_4;
  1440 			src_channels = 4;
  1441 			cvt->len_mult *= 2;
  1442 			cvt->len_ratio *= 2;
  1443 		}
  1444 		while ( (src_channels*2) <= dst_channels ) {
  1445 			cvt->filters[cvt->filter_index++] = 
  1446 						SDL_ConvertStereo;
  1447 			cvt->len_mult *= 2;
  1448 			src_channels *= 2;
  1449 			cvt->len_ratio *= 2;
  1450 		}
  1451 		if ( (src_channels == 6) &&
  1452 				(dst_channels <= 2) ) {
  1453 			cvt->filters[cvt->filter_index++] =
  1454 						 SDL_ConvertStrip;
  1455 			src_channels = 2;
  1456 			cvt->len_ratio /= 3;
  1457 		}
  1458 		if ( (src_channels == 6) &&
  1459 				(dst_channels == 4) ) {
  1460 			cvt->filters[cvt->filter_index++] =
  1461 						 SDL_ConvertStrip_2;
  1462 			src_channels = 4;
  1463 			cvt->len_ratio /= 2;
  1464 		}
  1465 		/* This assumes that 4 channel audio is in the format:
  1466 		     Left {front/back} + Right {front/back}
  1467 		   so converting to L/R stereo works properly.
  1468 		 */
  1469 		while ( ((src_channels%2) == 0) &&
  1470 				((src_channels/2) >= dst_channels) ) {
  1471 			cvt->filters[cvt->filter_index++] =
  1472 						 SDL_ConvertMono;
  1473 			src_channels /= 2;
  1474 			cvt->len_ratio /= 2;
  1475 		}
  1476 		if ( src_channels != dst_channels ) {
  1477 			/* Uh oh.. */;
  1478 		}
  1479 	}
  1480 
  1481 	/* Do rate conversion */
  1482 	cvt->rate_incr = 0.0;
  1483 	if ( (src_rate/100) != (dst_rate/100) ) {
  1484 		Uint32 hi_rate, lo_rate;
  1485 		int len_mult;
  1486 		double len_ratio;
  1487 		void (*rate_cvt)(SDL_AudioCVT *cvt, Uint16 format);
  1488 
  1489 		if ( src_rate > dst_rate ) {
  1490 			hi_rate = src_rate;
  1491 			lo_rate = dst_rate;
  1492 			switch (src_channels) {
  1493 				case 1: rate_cvt = SDL_RateDIV2; break;
  1494 				case 2: rate_cvt = SDL_RateDIV2_c2; break;
  1495 				case 4: rate_cvt = SDL_RateDIV2_c4; break;
  1496 				case 6: rate_cvt = SDL_RateDIV2_c6; break;
  1497 				default: return -1;
  1498 			}
  1499 			len_mult = 1;
  1500 			len_ratio = 0.5;
  1501 		} else {
  1502 			hi_rate = dst_rate;
  1503 			lo_rate = src_rate;
  1504 			switch (src_channels) {
  1505 				case 1: rate_cvt = SDL_RateMUL2; break;
  1506 				case 2: rate_cvt = SDL_RateMUL2_c2; break;
  1507 				case 4: rate_cvt = SDL_RateMUL2_c4; break;
  1508 				case 6: rate_cvt = SDL_RateMUL2_c6; break;
  1509 				default: return -1;
  1510 			}
  1511 			len_mult = 2;
  1512 			len_ratio = 2.0;
  1513 		}
  1514 		/* If hi_rate = lo_rate*2^x then conversion is easy */
  1515 		while ( ((lo_rate*2)/100) <= (hi_rate/100) ) {
  1516 			cvt->filters[cvt->filter_index++] = rate_cvt;
  1517 			cvt->len_mult *= len_mult;
  1518 			lo_rate *= 2;
  1519 			cvt->len_ratio *= len_ratio;
  1520 		}
  1521 		/* We may need a slow conversion here to finish up */
  1522 		if ( (lo_rate/100) != (hi_rate/100) ) {
  1523 #if 1
  1524 			/* The problem with this is that if the input buffer is
  1525 			   say 1K, and the conversion rate is say 1.1, then the
  1526 			   output buffer is 1.1K, which may not be an acceptable
  1527 			   buffer size for the audio driver (not a power of 2)
  1528 			*/
  1529 			/* For now, punt and hope the rate distortion isn't great.
  1530 			*/
  1531 #else
  1532 			if ( src_rate < dst_rate ) {
  1533 				cvt->rate_incr = (double)lo_rate/hi_rate;
  1534 				cvt->len_mult *= 2;
  1535 				cvt->len_ratio /= cvt->rate_incr;
  1536 			} else {
  1537 				cvt->rate_incr = (double)hi_rate/lo_rate;
  1538 				cvt->len_ratio *= cvt->rate_incr;
  1539 			}
  1540 			cvt->filters[cvt->filter_index++] = SDL_RateSLOW;
  1541 #endif
  1542 		}
  1543 	}
  1544 
  1545 	/* Set up the filter information */
  1546 	if ( cvt->filter_index != 0 ) {
  1547 		cvt->needed = 1;
  1548 		cvt->src_format = src_format;
  1549 		cvt->dst_format = dst_format;
  1550 		cvt->len = 0;
  1551 		cvt->buf = NULL;
  1552 		cvt->filters[cvt->filter_index] = NULL;
  1553 	}
  1554 	return(cvt->needed);
  1555 }