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