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