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