src/audio/SDL_audiocvt.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:38:15 +0000
changeset 252 e8157fcb3114
parent 0 74212992fb08
child 297 f6ffac90895c
permissions -rw-r--r--
Updated the source with the correct e-mail address
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Functions for audio drivers to perform runtime conversion of audio format */
slouken@0
    29
slouken@0
    30
#include <stdio.h>
slouken@0
    31
slouken@0
    32
#include "SDL_error.h"
slouken@0
    33
#include "SDL_audio.h"
slouken@0
    34
slouken@0
    35
slouken@0
    36
/* Effectively mix right and left channels into a single channel */
slouken@0
    37
void SDL_ConvertMono(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
    38
{
slouken@0
    39
	int i;
slouken@0
    40
	Sint32 sample;
slouken@0
    41
slouken@0
    42
#ifdef DEBUG_CONVERT
slouken@0
    43
	fprintf(stderr, "Converting to mono\n");
slouken@0
    44
#endif
slouken@0
    45
	switch (format&0x8018) {
slouken@0
    46
slouken@0
    47
		case AUDIO_U8: {
slouken@0
    48
			Uint8 *src, *dst;
slouken@0
    49
slouken@0
    50
			src = cvt->buf;
slouken@0
    51
			dst = cvt->buf;
slouken@0
    52
			for ( i=cvt->len_cvt/2; i; --i ) {
slouken@0
    53
				sample = src[0] + src[1];
slouken@0
    54
				if ( sample > 255 ) {
slouken@0
    55
					*dst = 255;
slouken@0
    56
				} else {
slouken@0
    57
					*dst = sample;
slouken@0
    58
				}
slouken@0
    59
				src += 2;
slouken@0
    60
				dst += 1;
slouken@0
    61
			}
slouken@0
    62
		}
slouken@0
    63
		break;
slouken@0
    64
slouken@0
    65
		case AUDIO_S8: {
slouken@0
    66
			Sint8 *src, *dst;
slouken@0
    67
slouken@0
    68
			src = (Sint8 *)cvt->buf;
slouken@0
    69
			dst = (Sint8 *)cvt->buf;
slouken@0
    70
			for ( i=cvt->len_cvt/2; i; --i ) {
slouken@0
    71
				sample = src[0] + src[1];
slouken@0
    72
				if ( sample > 127 ) {
slouken@0
    73
					*dst = 127;
slouken@0
    74
				} else
slouken@0
    75
				if ( sample < -128 ) {
slouken@0
    76
					*dst = -128;
slouken@0
    77
				} else {
slouken@0
    78
					*dst = sample;
slouken@0
    79
				}
slouken@0
    80
				src += 2;
slouken@0
    81
				dst += 1;
slouken@0
    82
			}
slouken@0
    83
		}
slouken@0
    84
		break;
slouken@0
    85
slouken@0
    86
		case AUDIO_U16: {
slouken@0
    87
			Uint8 *src, *dst;
slouken@0
    88
slouken@0
    89
			src = cvt->buf;
slouken@0
    90
			dst = cvt->buf;
slouken@0
    91
			if ( (format & 0x1000) == 0x1000 ) {
slouken@0
    92
				for ( i=cvt->len_cvt/4; i; --i ) {
slouken@0
    93
					sample = (Uint16)((src[0]<<8)|src[1])+
slouken@0
    94
					         (Uint16)((src[2]<<8)|src[3]);
slouken@0
    95
					if ( sample > 65535 ) {
slouken@0
    96
						dst[0] = 0xFF;
slouken@0
    97
						dst[1] = 0xFF;
slouken@0
    98
					} else {
slouken@0
    99
						dst[1] = (sample&0xFF);
slouken@0
   100
						sample >>= 8;
slouken@0
   101
						dst[0] = (sample&0xFF);
slouken@0
   102
					}
slouken@0
   103
					src += 4;
slouken@0
   104
					dst += 2;
slouken@0
   105
				}
slouken@0
   106
			} else {
slouken@0
   107
				for ( i=cvt->len_cvt/4; i; --i ) {
slouken@0
   108
					sample = (Uint16)((src[1]<<8)|src[0])+
slouken@0
   109
					         (Uint16)((src[3]<<8)|src[2]);
slouken@0
   110
					if ( sample > 65535 ) {
slouken@0
   111
						dst[0] = 0xFF;
slouken@0
   112
						dst[1] = 0xFF;
slouken@0
   113
					} else {
slouken@0
   114
						dst[0] = (sample&0xFF);
slouken@0
   115
						sample >>= 8;
slouken@0
   116
						dst[1] = (sample&0xFF);
slouken@0
   117
					}
slouken@0
   118
					src += 4;
slouken@0
   119
					dst += 2;
slouken@0
   120
				}
slouken@0
   121
			}
slouken@0
   122
		}
slouken@0
   123
		break;
slouken@0
   124
slouken@0
   125
		case AUDIO_S16: {
slouken@0
   126
			Uint8 *src, *dst;
slouken@0
   127
slouken@0
   128
			src = cvt->buf;
slouken@0
   129
			dst = cvt->buf;
slouken@0
   130
			if ( (format & 0x1000) == 0x1000 ) {
slouken@0
   131
				for ( i=cvt->len_cvt/4; i; --i ) {
slouken@0
   132
					sample = (Sint16)((src[0]<<8)|src[1])+
slouken@0
   133
					         (Sint16)((src[2]<<8)|src[3]);
slouken@0
   134
					if ( sample > 32767 ) {
slouken@0
   135
						dst[0] = 0x7F;
slouken@0
   136
						dst[1] = 0xFF;
slouken@0
   137
					} else
slouken@0
   138
					if ( sample < -32768 ) {
slouken@0
   139
						dst[0] = 0x80;
slouken@0
   140
						dst[1] = 0x00;
slouken@0
   141
					} else {
slouken@0
   142
						dst[1] = (sample&0xFF);
slouken@0
   143
						sample >>= 8;
slouken@0
   144
						dst[0] = (sample&0xFF);
slouken@0
   145
					}
slouken@0
   146
					src += 4;
slouken@0
   147
					dst += 2;
slouken@0
   148
				}
slouken@0
   149
			} else {
slouken@0
   150
				for ( i=cvt->len_cvt/4; i; --i ) {
slouken@0
   151
					sample = (Sint16)((src[1]<<8)|src[0])+
slouken@0
   152
					         (Sint16)((src[3]<<8)|src[2]);
slouken@0
   153
					if ( sample > 32767 ) {
slouken@0
   154
						dst[1] = 0x7F;
slouken@0
   155
						dst[0] = 0xFF;
slouken@0
   156
					} else
slouken@0
   157
					if ( sample < -32768 ) {
slouken@0
   158
						dst[1] = 0x80;
slouken@0
   159
						dst[0] = 0x00;
slouken@0
   160
					} else {
slouken@0
   161
						dst[0] = (sample&0xFF);
slouken@0
   162
						sample >>= 8;
slouken@0
   163
						dst[1] = (sample&0xFF);
slouken@0
   164
					}
slouken@0
   165
					src += 4;
slouken@0
   166
					dst += 2;
slouken@0
   167
				}
slouken@0
   168
			}
slouken@0
   169
		}
slouken@0
   170
		break;
slouken@0
   171
	}
slouken@0
   172
	cvt->len_cvt /= 2;
slouken@0
   173
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   174
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   175
	}
slouken@0
   176
}
slouken@0
   177
slouken@0
   178
slouken@0
   179
/* Duplicate a mono channel to both stereo channels */
slouken@0
   180
void SDL_ConvertStereo(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   181
{
slouken@0
   182
	int i;
slouken@0
   183
slouken@0
   184
#ifdef DEBUG_CONVERT
slouken@0
   185
	fprintf(stderr, "Converting to stereo\n");
slouken@0
   186
#endif
slouken@0
   187
	if ( (format & 0xFF) == 16 ) {
slouken@0
   188
		Uint16 *src, *dst;
slouken@0
   189
slouken@0
   190
		src = (Uint16 *)(cvt->buf+cvt->len_cvt);
slouken@0
   191
		dst = (Uint16 *)(cvt->buf+cvt->len_cvt*2);
slouken@0
   192
		for ( i=cvt->len_cvt/2; i; --i ) {
slouken@0
   193
			dst -= 2;
slouken@0
   194
			src -= 1;
slouken@0
   195
			dst[0] = src[0];
slouken@0
   196
			dst[1] = src[0];
slouken@0
   197
		}
slouken@0
   198
	} else {
slouken@0
   199
		Uint8 *src, *dst;
slouken@0
   200
slouken@0
   201
		src = cvt->buf+cvt->len_cvt;
slouken@0
   202
		dst = cvt->buf+cvt->len_cvt*2;
slouken@0
   203
		for ( i=cvt->len_cvt; i; --i ) {
slouken@0
   204
			dst -= 2;
slouken@0
   205
			src -= 1;
slouken@0
   206
			dst[0] = src[0];
slouken@0
   207
			dst[1] = src[0];
slouken@0
   208
		}
slouken@0
   209
	}
slouken@0
   210
	cvt->len_cvt *= 2;
slouken@0
   211
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   212
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   213
	}
slouken@0
   214
}
slouken@0
   215
slouken@0
   216
/* Convert 8-bit to 16-bit - LSB */
slouken@0
   217
void SDL_Convert16LSB(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   218
{
slouken@0
   219
	int i;
slouken@0
   220
	Uint8 *src, *dst;
slouken@0
   221
slouken@0
   222
#ifdef DEBUG_CONVERT
slouken@0
   223
	fprintf(stderr, "Converting to 16-bit LSB\n");
slouken@0
   224
#endif
slouken@0
   225
	src = cvt->buf+cvt->len_cvt;
slouken@0
   226
	dst = cvt->buf+cvt->len_cvt*2;
slouken@0
   227
	for ( i=cvt->len_cvt; i; --i ) {
slouken@0
   228
		src -= 1;
slouken@0
   229
		dst -= 2;
slouken@0
   230
		dst[1] = *src;
slouken@0
   231
		dst[0] = 0;
slouken@0
   232
	}
slouken@0
   233
	format = ((format & ~0x0008) | AUDIO_U16LSB);
slouken@0
   234
	cvt->len_cvt *= 2;
slouken@0
   235
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   236
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   237
	}
slouken@0
   238
}
slouken@0
   239
/* Convert 8-bit to 16-bit - MSB */
slouken@0
   240
void SDL_Convert16MSB(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   241
{
slouken@0
   242
	int i;
slouken@0
   243
	Uint8 *src, *dst;
slouken@0
   244
slouken@0
   245
#ifdef DEBUG_CONVERT
slouken@0
   246
	fprintf(stderr, "Converting to 16-bit MSB\n");
slouken@0
   247
#endif
slouken@0
   248
	src = cvt->buf+cvt->len_cvt;
slouken@0
   249
	dst = cvt->buf+cvt->len_cvt*2;
slouken@0
   250
	for ( i=cvt->len_cvt; i; --i ) {
slouken@0
   251
		src -= 1;
slouken@0
   252
		dst -= 2;
slouken@0
   253
		dst[0] = *src;
slouken@0
   254
		dst[1] = 0;
slouken@0
   255
	}
slouken@0
   256
	format = ((format & ~0x0008) | AUDIO_U16MSB);
slouken@0
   257
	cvt->len_cvt *= 2;
slouken@0
   258
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   259
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   260
	}
slouken@0
   261
}
slouken@0
   262
slouken@0
   263
/* Convert 16-bit to 8-bit */
slouken@0
   264
void SDL_Convert8(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   265
{
slouken@0
   266
	int i;
slouken@0
   267
	Uint8 *src, *dst;
slouken@0
   268
slouken@0
   269
#ifdef DEBUG_CONVERT
slouken@0
   270
	fprintf(stderr, "Converting to 8-bit\n");
slouken@0
   271
#endif
slouken@0
   272
	src = cvt->buf;
slouken@0
   273
	dst = cvt->buf;
slouken@0
   274
	if ( (format & 0x1000) != 0x1000 ) { /* Little endian */
slouken@0
   275
		++src;
slouken@0
   276
	}
slouken@0
   277
	for ( i=cvt->len_cvt/2; i; --i ) {
slouken@0
   278
		*dst = *src;
slouken@0
   279
		src += 2;
slouken@0
   280
		dst += 1;
slouken@0
   281
	}
slouken@0
   282
	format = ((format & ~0x9010) | AUDIO_U8);
slouken@0
   283
	cvt->len_cvt /= 2;
slouken@0
   284
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   285
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   286
	}
slouken@0
   287
}
slouken@0
   288
slouken@0
   289
/* Toggle signed/unsigned */
slouken@0
   290
void SDL_ConvertSign(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   291
{
slouken@0
   292
	int i;
slouken@0
   293
	Uint8 *data;
slouken@0
   294
slouken@0
   295
#ifdef DEBUG_CONVERT
slouken@0
   296
	fprintf(stderr, "Converting audio signedness\n");
slouken@0
   297
#endif
slouken@0
   298
	data = cvt->buf;
slouken@0
   299
	if ( (format & 0xFF) == 16 ) {
slouken@0
   300
		if ( (format & 0x1000) != 0x1000 ) { /* Little endian */
slouken@0
   301
			++data;
slouken@0
   302
		}
slouken@0
   303
		for ( i=cvt->len_cvt/2; i; --i ) {
slouken@0
   304
			*data ^= 0x80;
slouken@0
   305
			data += 2;
slouken@0
   306
		}
slouken@0
   307
	} else {
slouken@0
   308
		for ( i=cvt->len_cvt; i; --i ) {
slouken@0
   309
			*data++ ^= 0x80;
slouken@0
   310
		}
slouken@0
   311
	}
slouken@0
   312
	format = (format ^ 0x8000);
slouken@0
   313
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   314
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   315
	}
slouken@0
   316
}
slouken@0
   317
slouken@0
   318
/* Toggle endianness */
slouken@0
   319
void SDL_ConvertEndian(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   320
{
slouken@0
   321
	int i;
slouken@0
   322
	Uint8 *data, tmp;
slouken@0
   323
slouken@0
   324
#ifdef DEBUG_CONVERT
slouken@0
   325
	fprintf(stderr, "Converting audio endianness\n");
slouken@0
   326
#endif
slouken@0
   327
	data = cvt->buf;
slouken@0
   328
	for ( i=cvt->len_cvt/2; i; --i ) {
slouken@0
   329
		tmp = data[0];
slouken@0
   330
		data[0] = data[1];
slouken@0
   331
		data[1] = tmp;
slouken@0
   332
		data += 2;
slouken@0
   333
	}
slouken@0
   334
	format = (format ^ 0x1000);
slouken@0
   335
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   336
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   337
	}
slouken@0
   338
}
slouken@0
   339
slouken@0
   340
/* Convert rate up by multiple of 2 */
slouken@0
   341
void SDL_RateMUL2(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   342
{
slouken@0
   343
	int i;
slouken@0
   344
	Uint8 *src, *dst;
slouken@0
   345
slouken@0
   346
#ifdef DEBUG_CONVERT
slouken@0
   347
	fprintf(stderr, "Converting audio rate * 2\n");
slouken@0
   348
#endif
slouken@0
   349
	src = cvt->buf+cvt->len_cvt;
slouken@0
   350
	dst = cvt->buf+cvt->len_cvt*2;
slouken@0
   351
	switch (format & 0xFF) {
slouken@0
   352
		case 8:
slouken@0
   353
			for ( i=cvt->len_cvt; i; --i ) {
slouken@0
   354
				src -= 1;
slouken@0
   355
				dst -= 2;
slouken@0
   356
				dst[0] = src[0];
slouken@0
   357
				dst[1] = src[0];
slouken@0
   358
			}
slouken@0
   359
			break;
slouken@0
   360
		case 16:
slouken@0
   361
			for ( i=cvt->len_cvt/2; i; --i ) {
slouken@0
   362
				src -= 2;
slouken@0
   363
				dst -= 4;
slouken@0
   364
				dst[0] = src[0];
slouken@0
   365
				dst[1] = src[1];
slouken@0
   366
				dst[2] = src[0];
slouken@0
   367
				dst[3] = src[1];
slouken@0
   368
			}
slouken@0
   369
			break;
slouken@0
   370
	}
slouken@0
   371
	cvt->len_cvt *= 2;
slouken@0
   372
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   373
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   374
	}
slouken@0
   375
}
slouken@0
   376
slouken@0
   377
/* Convert rate down by multiple of 2 */
slouken@0
   378
void SDL_RateDIV2(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   379
{
slouken@0
   380
	int i;
slouken@0
   381
	Uint8 *src, *dst;
slouken@0
   382
slouken@0
   383
#ifdef DEBUG_CONVERT
slouken@0
   384
	fprintf(stderr, "Converting audio rate / 2\n");
slouken@0
   385
#endif
slouken@0
   386
	src = cvt->buf;
slouken@0
   387
	dst = cvt->buf;
slouken@0
   388
	switch (format & 0xFF) {
slouken@0
   389
		case 8:
slouken@0
   390
			for ( i=cvt->len_cvt/2; i; --i ) {
slouken@0
   391
				dst[0] = src[0];
slouken@0
   392
				src += 2;
slouken@0
   393
				dst += 1;
slouken@0
   394
			}
slouken@0
   395
			break;
slouken@0
   396
		case 16:
slouken@0
   397
			for ( i=cvt->len_cvt/4; i; --i ) {
slouken@0
   398
				dst[0] = src[0];
slouken@0
   399
				dst[1] = src[1];
slouken@0
   400
				src += 4;
slouken@0
   401
				dst += 2;
slouken@0
   402
			}
slouken@0
   403
			break;
slouken@0
   404
	}
slouken@0
   405
	cvt->len_cvt /= 2;
slouken@0
   406
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   407
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   408
	}
slouken@0
   409
}
slouken@0
   410
slouken@0
   411
/* Very slow rate conversion routine */
slouken@0
   412
void SDL_RateSLOW(SDL_AudioCVT *cvt, Uint16 format)
slouken@0
   413
{
slouken@0
   414
	double ipos;
slouken@0
   415
	int i, clen;
slouken@0
   416
slouken@0
   417
#ifdef DEBUG_CONVERT
slouken@0
   418
	fprintf(stderr, "Converting audio rate * %4.4f\n", 1.0/cvt->rate_incr);
slouken@0
   419
#endif
slouken@0
   420
	clen = (int)((double)cvt->len_cvt / cvt->rate_incr);
slouken@0
   421
	if ( cvt->rate_incr > 1.0 ) {
slouken@0
   422
		switch (format & 0xFF) {
slouken@0
   423
			case 8: {
slouken@0
   424
				Uint8 *output;
slouken@0
   425
slouken@0
   426
				output = cvt->buf;
slouken@0
   427
				ipos = 0.0;
slouken@0
   428
				for ( i=clen; i; --i ) {
slouken@0
   429
					*output = cvt->buf[(int)ipos];
slouken@0
   430
					ipos += cvt->rate_incr;
slouken@0
   431
					output += 1;
slouken@0
   432
				}
slouken@0
   433
			}
slouken@0
   434
			break;
slouken@0
   435
slouken@0
   436
			case 16: {
slouken@0
   437
				Uint16 *output;
slouken@0
   438
slouken@0
   439
				clen &= ~1;
slouken@0
   440
				output = (Uint16 *)cvt->buf;
slouken@0
   441
				ipos = 0.0;
slouken@0
   442
				for ( i=clen/2; i; --i ) {
slouken@0
   443
					*output=((Uint16 *)cvt->buf)[(int)ipos];
slouken@0
   444
					ipos += cvt->rate_incr;
slouken@0
   445
					output += 1;
slouken@0
   446
				}
slouken@0
   447
			}
slouken@0
   448
			break;
slouken@0
   449
		}
slouken@0
   450
	} else {
slouken@0
   451
		switch (format & 0xFF) {
slouken@0
   452
			case 8: {
slouken@0
   453
				Uint8 *output;
slouken@0
   454
slouken@0
   455
				output = cvt->buf+clen;
slouken@0
   456
				ipos = (double)cvt->len_cvt;
slouken@0
   457
				for ( i=clen; i; --i ) {
slouken@0
   458
					ipos -= cvt->rate_incr;
slouken@0
   459
					output -= 1;
slouken@0
   460
					*output = cvt->buf[(int)ipos];
slouken@0
   461
				}
slouken@0
   462
			}
slouken@0
   463
			break;
slouken@0
   464
slouken@0
   465
			case 16: {
slouken@0
   466
				Uint16 *output;
slouken@0
   467
slouken@0
   468
				clen &= ~1;
slouken@0
   469
				output = (Uint16 *)(cvt->buf+clen);
slouken@0
   470
				ipos = (double)cvt->len_cvt/2;
slouken@0
   471
				for ( i=clen/2; i; --i ) {
slouken@0
   472
					ipos -= cvt->rate_incr;
slouken@0
   473
					output -= 1;
slouken@0
   474
					*output=((Uint16 *)cvt->buf)[(int)ipos];
slouken@0
   475
				}
slouken@0
   476
			}
slouken@0
   477
			break;
slouken@0
   478
		}
slouken@0
   479
	}
slouken@0
   480
	cvt->len_cvt = clen;
slouken@0
   481
	if ( cvt->filters[++cvt->filter_index] ) {
slouken@0
   482
		cvt->filters[cvt->filter_index](cvt, format);
slouken@0
   483
	}
slouken@0
   484
}
slouken@0
   485
slouken@0
   486
int SDL_ConvertAudio(SDL_AudioCVT *cvt)
slouken@0
   487
{
slouken@0
   488
	/* Make sure there's data to convert */
slouken@0
   489
	if ( cvt->buf == NULL ) {
slouken@0
   490
		SDL_SetError("No buffer allocated for conversion");
slouken@0
   491
		return(-1);
slouken@0
   492
	}
slouken@0
   493
	/* Return okay if no conversion is necessary */
slouken@0
   494
	cvt->len_cvt = cvt->len;
slouken@0
   495
	if ( cvt->filters[0] == NULL ) {
slouken@0
   496
		return(0);
slouken@0
   497
	}
slouken@0
   498
slouken@0
   499
	/* Set up the conversion and go! */
slouken@0
   500
	cvt->filter_index = 0;
slouken@0
   501
	cvt->filters[0](cvt, cvt->src_format);
slouken@0
   502
	return(0);
slouken@0
   503
}
slouken@0
   504
slouken@0
   505
/* Creates a set of audio filters to convert from one format to another. 
slouken@0
   506
   Returns -1 if the format conversion is not supported, or 1 if the
slouken@0
   507
   audio filter is set up.
slouken@0
   508
*/
slouken@0
   509
  
slouken@0
   510
int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
slouken@0
   511
	Uint16 src_format, Uint8 src_channels, int src_rate,
slouken@0
   512
	Uint16 dst_format, Uint8 dst_channels, int dst_rate)
slouken@0
   513
{
slouken@0
   514
	/* Start off with no conversion necessary */
slouken@0
   515
	cvt->needed = 0;
slouken@0
   516
	cvt->filter_index = 0;
slouken@0
   517
	cvt->filters[0] = NULL;
slouken@0
   518
	cvt->len_mult = 1;
slouken@0
   519
	cvt->len_ratio = 1.0;
slouken@0
   520
slouken@0
   521
	/* First filter:  Endian conversion from src to dst */
slouken@0
   522
	if ( (src_format & 0x1000) != (dst_format & 0x1000)
slouken@0
   523
	     && ((src_format & 0xff) != 8) ) {
slouken@0
   524
		cvt->filters[cvt->filter_index++] = SDL_ConvertEndian;
slouken@0
   525
	}
slouken@0
   526
	
slouken@0
   527
	/* Second filter: Sign conversion -- signed/unsigned */
slouken@0
   528
	if ( (src_format & 0x8000) != (dst_format & 0x8000) ) {
slouken@0
   529
		cvt->filters[cvt->filter_index++] = SDL_ConvertSign;
slouken@0
   530
	}
slouken@0
   531
slouken@0
   532
	/* Next filter:  Convert 16 bit <--> 8 bit PCM */
slouken@0
   533
	if ( (src_format & 0xFF) != (dst_format & 0xFF) ) {
slouken@0
   534
		switch (dst_format&0x10FF) {
slouken@0
   535
			case AUDIO_U8:
slouken@0
   536
				cvt->filters[cvt->filter_index++] =
slouken@0
   537
							 SDL_Convert8;
slouken@0
   538
				cvt->len_ratio /= 2;
slouken@0
   539
				break;
slouken@0
   540
			case AUDIO_U16LSB:
slouken@0
   541
				cvt->filters[cvt->filter_index++] =
slouken@0
   542
							SDL_Convert16LSB;
slouken@0
   543
				cvt->len_mult *= 2;
slouken@0
   544
				cvt->len_ratio *= 2;
slouken@0
   545
				break;
slouken@0
   546
			case AUDIO_U16MSB:
slouken@0
   547
				cvt->filters[cvt->filter_index++] =
slouken@0
   548
							SDL_Convert16MSB;
slouken@0
   549
				cvt->len_mult *= 2;
slouken@0
   550
				cvt->len_ratio *= 2;
slouken@0
   551
				break;
slouken@0
   552
		}
slouken@0
   553
	}
slouken@0
   554
slouken@0
   555
	/* Last filter:  Mono/Stereo conversion */
slouken@0
   556
	if ( src_channels != dst_channels ) {
slouken@0
   557
		while ( (src_channels*2) <= dst_channels ) {
slouken@0
   558
			cvt->filters[cvt->filter_index++] = 
slouken@0
   559
						SDL_ConvertStereo;
slouken@0
   560
			cvt->len_mult *= 2;
slouken@0
   561
			src_channels *= 2;
slouken@0
   562
			cvt->len_ratio *= 2;
slouken@0
   563
		}
slouken@0
   564
		/* This assumes that 4 channel audio is in the format:
slouken@0
   565
		     Left {front/back} + Right {front/back}
slouken@0
   566
		   so converting to L/R stereo works properly.
slouken@0
   567
		 */
slouken@0
   568
		while ( ((src_channels%2) == 0) &&
slouken@0
   569
				((src_channels/2) >= dst_channels) ) {
slouken@0
   570
			cvt->filters[cvt->filter_index++] =
slouken@0
   571
						 SDL_ConvertMono;
slouken@0
   572
			src_channels /= 2;
slouken@0
   573
			cvt->len_ratio /= 2;
slouken@0
   574
		}
slouken@0
   575
		if ( src_channels != dst_channels ) {
slouken@0
   576
			/* Uh oh.. */;
slouken@0
   577
		}
slouken@0
   578
	}
slouken@0
   579
slouken@0
   580
	/* Do rate conversion */
slouken@0
   581
	cvt->rate_incr = 0.0;
slouken@0
   582
	if ( (src_rate/100) != (dst_rate/100) ) {
slouken@0
   583
		Uint32 hi_rate, lo_rate;
slouken@0
   584
		int len_mult;
slouken@0
   585
		double len_ratio;
slouken@0
   586
		void (*rate_cvt)(SDL_AudioCVT *cvt, Uint16 format);
slouken@0
   587
slouken@0
   588
		if ( src_rate > dst_rate ) {
slouken@0
   589
			hi_rate = src_rate;
slouken@0
   590
			lo_rate = dst_rate;
slouken@0
   591
			rate_cvt = SDL_RateDIV2;
slouken@0
   592
			len_mult = 1;
slouken@0
   593
			len_ratio = 0.5;
slouken@0
   594
		} else {
slouken@0
   595
			hi_rate = dst_rate;
slouken@0
   596
			lo_rate = src_rate;
slouken@0
   597
			rate_cvt = SDL_RateMUL2;
slouken@0
   598
			len_mult = 2;
slouken@0
   599
			len_ratio = 2.0;
slouken@0
   600
		}
slouken@0
   601
		/* If hi_rate = lo_rate*2^x then conversion is easy */
slouken@0
   602
		while ( ((lo_rate*2)/100) <= (hi_rate/100) ) {
slouken@0
   603
			cvt->filters[cvt->filter_index++] = rate_cvt;
slouken@0
   604
			cvt->len_mult *= len_mult;
slouken@0
   605
			lo_rate *= 2;
slouken@0
   606
			cvt->len_ratio *= len_ratio;
slouken@0
   607
		}
slouken@0
   608
		/* We may need a slow conversion here to finish up */
slouken@0
   609
		if ( (lo_rate/100) != (hi_rate/100) ) {
slouken@0
   610
#if 1
slouken@0
   611
			/* The problem with this is that if the input buffer is
slouken@0
   612
			   say 1K, and the conversion rate is say 1.1, then the
slouken@0
   613
			   output buffer is 1.1K, which may not be an acceptable
slouken@0
   614
			   buffer size for the audio driver (not a power of 2)
slouken@0
   615
			*/
slouken@0
   616
			/* For now, punt and hope the rate distortion isn't great.
slouken@0
   617
			*/
slouken@0
   618
#else
slouken@0
   619
			if ( src_rate < dst_rate ) {
slouken@0
   620
				cvt->rate_incr = (double)lo_rate/hi_rate;
slouken@0
   621
				cvt->len_mult *= 2;
slouken@0
   622
				cvt->len_ratio /= cvt->rate_incr;
slouken@0
   623
			} else {
slouken@0
   624
				cvt->rate_incr = (double)hi_rate/lo_rate;
slouken@0
   625
				cvt->len_ratio *= cvt->rate_incr;
slouken@0
   626
			}
slouken@0
   627
			cvt->filters[cvt->filter_index++] = SDL_RateSLOW;
slouken@0
   628
#endif
slouken@0
   629
		}
slouken@0
   630
	}
slouken@0
   631
slouken@0
   632
	/* Set up the filter information */
slouken@0
   633
	if ( cvt->filter_index != 0 ) {
slouken@0
   634
		cvt->needed = 1;
slouken@0
   635
		cvt->src_format = src_format;
slouken@0
   636
		cvt->dst_format = dst_format;
slouken@0
   637
		cvt->len = 0;
slouken@0
   638
		cvt->buf = NULL;
slouken@0
   639
		cvt->filters[cvt->filter_index] = NULL;
slouken@0
   640
	}
slouken@0
   641
	return(cvt->needed);
slouken@0
   642
}