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