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