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