annotate alt_audio_convert.c @ 365:f61eadea1f44

More revisions from Frank.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 18 Jun 2002 21:49:44 +0000
parents c984aa6990f7
children eda146d666d1
rev   line source
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
2 * Extended Audio Converter for SDL (Simple DirectMedia Layer)
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
3 * Copyright (C) 2002 Frank Ranostaj
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
4 * Institute of Applied Physik
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
5 * Johann Wolfgang Goethe-Universität
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
6 * Frankfurt am Main, Germany
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
7 *
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
8 * This library is free software; you can redistribute it and/or
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
9 * modify it under the terms of the GNU Library General Public
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
10 * License as published by the Free Software Foundation; either
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
11 * version 2 of the License, or (at your option) any later version.
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
12 *
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
13 * This library is distributed in the hope that it will be useful,
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
16 * Library General Public License for more details.
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
17 *
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
18 * You should have received a copy of the GNU Library General Public
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
19 * License along with this library; if not, write to the Free
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
21 *
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
22 * Frank Ranostaj
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
23 * ranostaj@stud.uni-frankfurt.de
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
24 *
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
25 * (This code blatantly abducted for SDL_sound. Thanks, Frank! --ryan.)
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
26 */
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 #include "alt_audio_convert.h"
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 #include <stdlib.h>
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 #include <math.h>
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
32 /* just to make sure this is defined... */
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
33
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
34 #ifndef min
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
35 #define min(x, y) ( ((x) < (y)) ? (x) : (y) )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
36 #endif
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
37
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
38 #ifndef max
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
39 #define max(x, y) ( ((x) > (y)) ? (x) : (y) )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
40 #endif
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
41
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
42
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
43 /* some macros for "parsing" format */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
44
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
45 #define IS_8BIT(x) ((x).format & 0x0008)
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
46 #define IS_16BIT(x) ((x).format & 0x0010)
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
47 #define IS_FLOAT(x) ((x).format & 0x0020)
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
48 #define IS_SIGNED(x) ((x).format & 0x8000)
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
49 #define IS_SYSENDIAN(x) ((~AUDIO_U16SYS ^ (x).format) & 0x1000)
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 /*-------------------------------------------------------------------------*/
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
53 /* this filter (Kaiser-window beta=6.8) gives a decent -80dB attentuation */
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
54 static const int filter[_fsize / 2] =
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
55 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
56 0, 20798, 0, -6764, 0, 3863, 0, -2560,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
57 0, 1800, 0, -1295, 0, 936, 0, -671,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
58 0, 474, 0, -326, 0, 217, 0, -138,
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
59 0, 83, 0, -46, 0, 23, 0, -9
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
60 };
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
61
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
62
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
63 /*-------------------------------------------------------------------------*/
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
64 /* the purpose of the RateConverterBuffer is to provide a continous storage
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
65 for head and tail of the (sample)-buffer. This allows a simple and
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
66 perfomant implemantation of the sample rate converters. Depending of the
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
67 operation mode, two layouts for the RateConverterBuffer.inbuffer are
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
68 possible:
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
69
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
70 in the Loop Mode:
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
71 ... T-4 T-3 T-2 T-1 H+0 H+1 H+2 H+3 H+4 ...
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
72 |
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
73 linp, finp
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
74
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
75 in the Single Mode (non Loop):
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
76 ... T-4 T-3 T-2 T-1 0 0 0 ... 0 0 0 H+0 H+1 H+2 H+3 H+4 ...
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
77 | |
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
78 linp finp
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
79
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
80 The RateConverterBuffer allows an accurate attack and decay of the
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
81 filters in the rate Converters.
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
82
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
83 The pointer finp are actually shifted against the depicted position so
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
84 that on the first invocation of the rate converter the input of the
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
85 filter is nearly complete in the zero region, only one input value is
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
86 used. After the calculation of the first output value, the pointer are
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
87 incremented or decremented depending on down or up conversion and the
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
88 first two input value are taken into account. This procedure repeats
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
89 until the filter has processed all zeroes. The distance of the pointer
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
90 movement is stored in flength.
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
91
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
92 Further a pointer cinp to the sample buffer itself is stored. The pointer
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
93 to the sample buffer is shifted too, so that on the first use of this
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
94 pointer the filter is complete in the sample buffer. The pointer moves
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
95 over the sample buffer until it reaches the other end. The distance of
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
96 the movement is stored in clength.
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
97
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
98 Finally the decay of the filter is done by linp, llength like finp,
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
99 flength, but in reverse order.
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
100
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
101 buffer denotes the start or the end of the output buffer, depending
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
102 on direction of the rate conversion.
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
103
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
104 All pointer and length referring the buffer as Sint16. All length
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
105 are refering to the input buffer */
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
106
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
107 typedef struct
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
108 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
109 Sint16 inbuffer[6*_fsize];
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
110 Sint16 *finp, *cinp, *linp;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
111 Sint16 *buffer;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
112 int flength, clength, llength;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
113 } RateConverterBuffer;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
114
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 /* Mono (1 channel ) */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 #define Suffix(x) x##1
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 #include "filter_templates.h"
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 #undef Suffix
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 /* Stereo (2 channel ) */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 #define Suffix(x) x##2
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 #include "filter_templates.h"
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 #undef Suffix
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
126
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
127 /*-------------------------------------------------------------------------*/
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
128 static int ConvertAudio( Sound_AudioCVT *Data,
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
129 Uint8* buffer, int length, int mode )
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
130 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
131 AdapterC Temp;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
132 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
133
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
134 /* Make sure there's a converter */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
135 if( Data == NULL ) {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
136 SDL_SetError("No converter given");
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
137 return(-1);
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
138 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
140 /* Make sure there's data to convert */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
141 if( buffer == NULL ) {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
142 SDL_SetError("No buffer allocated for conversion");
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
143 return(-1);
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
144 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
145
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
146 /* Set up the conversion and go! */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
147 Temp.buffer = buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
148 Temp.mode = mode;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
149 Temp.filter = &Data->filter;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
150
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
151 for( i = 0; Data->adapter[i] != NULL; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
152 length = (*Data->adapter[i])( Temp, length);
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
153
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
154 return length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
155 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
156
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
157 int Sound_ConvertAudio( Sound_AudioCVT *Data )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
159 /* !!! FIXME: Try the looping stuff under certain circumstances? --ryan. */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
160 return ConvertAudio( Data, Data->buf, Data->len, 0 );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
161 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
164 /*-------------------------------------------------------------------------*/
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
165 static int expand8BitTo16BitSys( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
166 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
167 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
168 Uint8* inp = Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
169 Uint16* buffer = (Uint16*)Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
170 for( i = length; i--; )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
171 buffer[i] = inp[i]<<8;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
172 return 2*length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
173 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
175 static int expand8BitTo16BitWrong( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
176 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
177 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
178 Uint8* inp = Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
179 Uint16* buffer = (Uint16*)Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
180 for( i = length; i--; )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
181 buffer[i] = inp[i];
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
182 return 2*length;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 /*-------------------------------------------------------------------------*/
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
186 static int expand16BitToFloat( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
188 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
189 Sint16* inp = (Sint16*)Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
190 float* buffer = (float*)Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
191 for( i = length>>1; i--; )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
192 buffer[i] = inp[i]*(1./32767);
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
193 return 2*length;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
197 static int swapBytes( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
199 /*
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
200 * !!! FIXME !!!
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
201 *
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
202 *
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
203 * Use the faster SDL-Macros to swap
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
204 * - Frank
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
205 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
206
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
207 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
208 Uint16 a,b;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
209 Uint16* buffer = (Uint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
210 for( i = length>>1; i --; )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
211 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
212 a = b = buffer[i];
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
213 buffer[i] = ( a << 8 ) | ( b >> 8 );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
214 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
215 return length;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218 /*-------------------------------------------------------------------------*/
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
219 static int cutFloatTo16Bit( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
221 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
222 float* inp = (float*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
223 Sint16* buffer = (Sint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
224 length>>=2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
225 for( i = 0; i < length; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
226 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
227 if( inp[i] > 1. )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
228 buffer[i] = 32767;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
229 else if( inp[i] < -1. )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
230 buffer[i] = -32768;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
231 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
232 buffer[i] = 32767 * inp[i];
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
233 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
234 return 2*length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
235 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
236
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
237 /*-------------------------------------------------------------------------*/
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
238 static int cut16BitSysTo8Bit( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
239 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
240 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
241 Uint16* inp = (Uint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
242 Uint8* buffer = Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
243 length >>= 1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
244 for( i = 0; i < length; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
245 buffer[i] = inp[i]>>8;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
246 return length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
247 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
248
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
249 static int cut16BitWrongTo8Bit( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
250 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
251 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
252 Uint16* inp = (Uint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
253 Uint8* buffer = Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
254 length >>= 1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
255 for( i = 0; i < length; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
256 buffer[i] = inp[i] & 0xff;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
257 return length;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260 /*-------------------------------------------------------------------------*/
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
261 static int changeSigned( AdapterC Data, int length, int XOR )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
263 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
264 Uint32* buffer = (Uint32*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
265 for( i = length>>2; i--; )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
266 buffer[i] ^= XOR;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
267 for( i = 4*(length>>2); i < length; i++)
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
268 ((Uint8*)buffer)[i] ^= ((Uint8*)&XOR)[i&3];
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
269 return length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
270 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
271
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
272 static int changeSigned16BitSys( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
273 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
274 return changeSigned( Data, length, 0x80008000 );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
275 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
276
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
277 static int changeSigned16BitWrong( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
278 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
279 return changeSigned( Data, length, 0x00800080 );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
280 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
281
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
282 static int changeSigned8Bit( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
283 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
284 return changeSigned( Data, length, 0x80808080 );
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287 /*-------------------------------------------------------------------------*/
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
288 static int convertStereoToMonoS16Bit( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
290 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
291 Sint16* buffer = (Sint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
292 Sint16* src = (Sint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
293 length >>= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
294 for( i = 0; i < length; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
295 buffer[i] = ((int) *(src++) + *(src++) ) >> 1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
296 return 2*length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
297 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
299 static int convertStereoToMonoU16Bit( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
300 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
301 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
302 Uint16* buffer = (Uint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
303 Uint16* src = (Uint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
304 length >>= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
305 for( i = 0; i < length; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
306 buffer[i] = ((int) *(src++) + *(src++) ) >> 1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
307 return 2*length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
308 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
309
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
310 static int convertStereoToMonoS8Bit( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
311 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
312 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
313 Sint8* buffer = (Sint8*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
314 Sint8* src = (Sint8*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
315 length >>= 1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
316 for( i = 0; i < length; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
317 buffer[i] = ((int) *(src++) + *(src++) ) >> 1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
318 return length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
319 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
320
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
321 static int convertStereoToMonoU8Bit( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
322 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
323 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
324 Uint8* buffer = (Uint8*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
325 Uint8* src = (Uint8*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
326 length >>= 1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
327 for( i = 0; i < length; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
328 buffer[i] = ((int) *(src++) + *(src++) ) >> 1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
329 return length;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
330 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
331
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332 /*-------------------------------------------------------------------------*/
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
333 static int convertMonoToStereo16Bit( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
335 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
336 Uint16* buffer = (Uint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
337 Uint16* dst = (Uint16*)Data.buffer + length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
338 for( i = length>>1; i--; )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
339 *(--dst) = *(--dst) = buffer[i];
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
340 return 2*length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
341 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
343 static int convertMonoToStereo8Bit( AdapterC Data, int length )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
344 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
345 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
346 Uint8* buffer = Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
347 Uint8* dst = Data.buffer + 2*length;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
348 for( i = length; i--; )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
349 *(--dst) = *(--dst) = buffer[i];
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
350 return 2*length;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
354 static int minus5dB( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
355 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
356 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
357 Sint16* buffer = (Sint16*) Data.buffer;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
358 for(i = length>>1; i--; )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
359 buffer[i]= 38084 * buffer[i] >> 16;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
360 return length;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
361 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 /*-------------------------------------------------------------------------*/
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
364 static void initRateConverterBuffer( RateConverterBuffer *rcb,
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
365 AdapterC* Data, int length, int rel_size )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
366 {
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
367 int size, slength;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
368 int den, num;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
369 int i;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
370
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
371 den = Data->filter->denominator;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
372 num = Data->filter->numerator;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
373 size = _fsize * rel_size;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
374 length >>= 1;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
375 slength = rel_size > 0 ? length : -length;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
376
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
377 rcb->buffer = (Sint16*)( Data->buffer );
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
379 if( Data->mode & SDL_AI_Loop )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
380 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
381 // !!!FIXME: modulo length, take scale into account
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
382 for( i = 0; i < size; i++ )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
383 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
384 rcb->inbuffer[i] = rcb->buffer[length-size+i];
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
385 rcb->inbuffer[i+size] = rcb->buffer[i];
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
386 }
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
387 rcb->finp = rcb->linp = rcb->inbuffer + size;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
388 if( size < 0 )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
389 rcb->buffer += num * ( length + 2 * size ) / den;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
390 }
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
391 else
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
392 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
393 for( i = 0; i < size; i++ )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
394 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
395 int j;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
396 j = length-size+i;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
397 rcb->inbuffer[i] = j < 0 ? 0 : rcb->buffer[j];
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
398 rcb->inbuffer[i+size] = 0;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
399 rcb->inbuffer[i+2*size] = i < length ? rcb->buffer[i] : 0;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
400 }
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
401 // !!!FIXME: take lenght < size into account
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
402 rcb->finp = rcb->inbuffer + abs( 3*size/2 ) + size/2;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
403 rcb->linp = rcb->inbuffer + abs( 3*size/2 ) - size/2;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
404 rcb->flength = rcb->llength = 2*size;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
405 rcb->clength = slength - 2*size;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
406
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
407 if( size < 0 )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
408 rcb->buffer += num * ( length + 2 * size ) / den;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
409 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
410 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
411
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
412 static void nextRateConverterBuffer( RateConverterBuffer *rcb )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413 {
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
414 rcb->buffer++;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
415 rcb->finp++;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
416 rcb->cinp++;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
417 rcb->linp++;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
418 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
419
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
420 typedef Sint16* (*RateConverter)( Sint16*, Sint16*, int, VarFilter*, int*);
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
421 static int doRateConversion( RateConverterBuffer* rcb,
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
422 RateConverter ffp, VarFilter* filter )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
423 {
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
424 int pos = 0;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
425 Sint16 *outp;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
426 outp = rcb->buffer;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
427
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
428 outp = (*ffp)( outp, rcb->finp, rcb->flength, filter, &pos );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
429 outp = (*ffp)( outp, rcb->cinp, rcb->clength, filter, &pos );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
430 outp = (*ffp)( outp, rcb->linp, rcb->llength, filter, &pos );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
431 return 2 * abs( rcb->buffer - outp );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
432 }
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
433
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
434
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
435 /*-------------------------------------------------------------------------*/
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
436 static int doubleRateMono( AdapterC Data, int length )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
437 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
438 RateConverterBuffer rcb;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
439 initRateConverterBuffer( &rcb, &Data, length, 1 );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
440 return doRateConversion( &rcb, doubleRate1, NULL );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
441 }
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
442
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
443 static int doubleRateStereo( AdapterC Data, int length )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
444 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
445 RateConverterBuffer rcb;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
446 initRateConverterBuffer( &rcb, &Data, length, 2 );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
447 doRateConversion( &rcb, doubleRate2, NULL );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
448 nextRateConverterBuffer( &rcb );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
449 return 2 + doRateConversion( &rcb, doubleRate2, NULL );
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
450 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
451
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
452 /*-------------------------------------------------------------------------*/
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
453 static int halfRateMono( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
454 {
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
455 RateConverterBuffer rcb;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
456 initRateConverterBuffer( &rcb, &Data, length, -1 );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
457 return doRateConversion( &rcb, halfRate1, NULL );
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
458 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
459
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
460 static int halfRateStereo( AdapterC Data, int length )
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
461 {
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
462 RateConverterBuffer rcb;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
463 initRateConverterBuffer( &rcb, &Data, length, -2 );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
464 doRateConversion( &rcb, halfRate2, NULL );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
465 nextRateConverterBuffer( &rcb );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
466 return 2 + doRateConversion( &rcb, halfRate2, NULL );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
467 }
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
468
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
469 /*-------------------------------------------------------------------------*/
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
470 static int increaseRateMono( AdapterC Data, int length )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
471 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
472 RateConverterBuffer rcb;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
473 initRateConverterBuffer( &rcb, &Data, length, 2 );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
474 return doRateConversion( &rcb, increaseRate1, Data.filter );
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
475 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
476
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
477 static int increaseRateStereo( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
478 {
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
479 RateConverterBuffer rcb;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
480 initRateConverterBuffer( &rcb, &Data, length, 4 );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
481 doRateConversion( &rcb, increaseRate2, Data.filter );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
482 nextRateConverterBuffer( &rcb );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
483 return 2 + doRateConversion( &rcb, increaseRate2, Data.filter );
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
484 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
485
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
486 /*-------------------------------------------------------------------------*/
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
487 static int decreaseRateMono( AdapterC Data, int length )
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
488 {
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
489 RateConverterBuffer rcb;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
490 initRateConverterBuffer( &rcb, &Data, length, -2 );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
491 return doRateConversion( &rcb, decreaseRate1, Data.filter );
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
492 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
493
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
494 static int decreaseRateStereo( AdapterC Data, int length )
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
495 {
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
496 RateConverterBuffer rcb;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
497 initRateConverterBuffer( &rcb, &Data, length, -4 );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
498 doRateConversion( &rcb, decreaseRate2, Data.filter );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
499 nextRateConverterBuffer( &rcb );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
500 return doRateConversion( &rcb, decreaseRate2, Data.filter );
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
501 }
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
502
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
503
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
504 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
505 typedef struct{
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
506 Sint16 denominator;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
507 Sint16 numerator;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
508 } Fraction;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
509
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
510 /* gives a maximal error of 3% and typical less than 0.2% */
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
511 static Fraction findFraction( float Value )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
512 {
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
513 const Sint8 frac[95]={
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
514 2, -1, /* /1 */
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
515 1, 3, -1, /* /2 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
516 2, 4, 5, -1, /* /3 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
517 3, 5, 7, -1, /* /4 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
518 3, 4, 6, 7, 8, 9, -1, /* /5 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
519 5, 7, 11, -1, /* /6 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
520 4, 5, 6, 8, 9, 10, 11, 12, 13, -1, /* /7 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
521 5, 7, 9, 11, 13, 15, -1, /* /8 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
522 5, 7, 8, 10, 11, 13, 14, 16, -1, /* /9 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
523 7, 9, 11, 13, -1, /* /10 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
524 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, -1, /* /11 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
525 7, 11, 13, -1, /* /12 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
526 7, 8, 9, 10, 11, 12, 14, 15, 16, -1, /* /13 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
527 9, 11, 13, 15, -1, /* /14 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
528 8, 11, 13, 14, 16, -1, /* /15 */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
529 9, 11, 13, 15 }; /* /16 */
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
530
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
531
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
532 Fraction Result = {0,0};
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
533 int n,num=1,den=1;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
534
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
535 float RelErr, BestErr = 0;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
536 if( Value < 31/64. || Value > 64/31. ) return Result;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
537
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
538 for( n = 0; n < sizeof(frac); num=frac[n++] )
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
539 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
540 if( num < 0 ) den++;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
541 RelErr = Value * num / den;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
542 RelErr = min( RelErr, 1/RelErr );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
543 if( RelErr > BestErr )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
544 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
545 BestErr = RelErr;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
546 Result.denominator = den;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
547 Result.numerator = num;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
548 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
549 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
550 return Result;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
551 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
552
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
553 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
554 static float sinc( float x )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
555 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
556 if( x > -1e-24 && x < 1e-24 ) return 1.;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
557 else return sin(x)/x;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
558 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
559
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
560 static void calculateVarFilter( Sint16* dst,
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
561 float Ratio, float phase, float scale )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
562 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
563 const Uint16 KaiserWindow7[]= {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
564 22930, 16292, 14648, 14288, 14470, 14945, 15608, 16404,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
565 17304, 18289, 19347, 20467, 21644, 22872, 24145, 25460,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
566 26812, 28198, 29612, 31052, 32513, 33991, 35482, 36983,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
567 38487, 39993, 41494, 42986, 44466, 45928, 47368, 48782,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
568 50165, 51513, 52821, 54086, 55302, 56466, 57575, 58624,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
569 59610, 60529, 61379, 62156, 62858, 63483, 64027, 64490,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
570 64870, 65165, 65375, 65498, 65535, 65484, 65347, 65124,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
571 64815, 64422, 63946, 63389, 62753, 62039, 61251, 60391 };
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
572 int i;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
573 float w;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
574 const float fg = -.018 + .5 * Ratio;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
575 const float omega = 2 * M_PI * fg;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
576 phase -= 63;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
577 for( i = 0; i < 64; i++)
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
578 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
579 w = scale * ( KaiserWindow7[i] * ( i + 1 ));
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
580 dst[i] = w * sinc( omega * (i+phase) );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
581 dst[127-i] = w * sinc( omega * (127-i+phase) );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
582 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
583 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
584
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
585 typedef struct{
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
586 float scale;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
587 int incr;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
588 } VarFilterMode;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
589
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
590 const VarFilterMode Up = { 0.0211952, -1 };
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
591 const VarFilterMode Down = { 0.0364733, 1 };
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
592
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
593 static void setupVarFilter( VarFilter* filter,
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
594 float Ratio, VarFilterMode Direction )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
595 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
596 int i,n,d;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
597 Fraction IRatio;
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
598 float phase = 0.;
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
599 IRatio = findFraction( Ratio );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
600 Ratio = min( Ratio, 1/Ratio );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
601
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
602 n = IRatio.numerator;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
603 d = IRatio.denominator;
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
604 filter->denominator = d;
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
605 filter->numerator = n;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
606
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
607 for( i = 0; i < d; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
608 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
609 if( phase >= n )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
610 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
611 phase -= d;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
612 filter->incr[i] = Direction.incr;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
613 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
614 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
615 filter->incr[i] = 1+Direction.incr;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
616
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
617 calculateVarFilter( filter->c[i], Ratio, phase/(float)n,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
618 Direction.scale );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
619 phase += d;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
620 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
621 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
622
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
623 /*-------------------------------------------------------------------------*/
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
624 static void createRateConverter( Sound_AudioCVT *Data, int* fip,
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
625 int SrcRate, int DestRate, int Channel )
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
626 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
627 int filter_index = *fip;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
628
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
629 int VarPos = 0;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
630 int Mono = 2 - Channel;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
631 float Ratio = DestRate;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
632 *fip = -1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
633
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
634
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
635 if( SrcRate < 1 || SrcRate > 1<<18 ||
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
636 DestRate < 1 || DestRate > 1<<18 ) return;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
637 Ratio /= SrcRate;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
638
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
639 if( Ratio > 1.)
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
640 VarPos = filter_index++;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
641 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
642 Data->adapter[filter_index++] = minus5dB;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
643
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
644 while( Ratio > 64./31.)
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
645 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
646 Data->adapter[filter_index++] =
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
647 Mono ? doubleRateMono : doubleRateStereo;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
648 Ratio /= 2.;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
649 Data->len_mult *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
650 Data->add *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
651 Data->add += _fsize;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
652 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
653
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
654 while( Ratio < 31./64. )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
655 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
656 Data->adapter[filter_index++] =
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
657 Mono ? halfRateMono : halfRateStereo;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
658 Ratio *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
659 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
660
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
661 if( Ratio > 1. )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
662 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
663 setupVarFilter( &Data->filter, Ratio, Up );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
664 Data->adapter[VarPos] =
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
665 Mono ? increaseRateMono : increaseRateStereo;
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
666 Data->len_mult *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
667 Data->add *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
668 Data->add += _fsize;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
669 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
670 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
671 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
672 setupVarFilter( &Data->filter, Ratio, Down );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
673 Data->adapter[filter_index++] =
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
674 Mono ? decreaseRateMono : decreaseRateStereo;
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
675 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
676 *fip = filter_index;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
677 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
678
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
679 /*-------------------------------------------------------------------------*/
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
680 static void createFormatConverter16Bit(Sound_AudioCVT *Data, int* fip,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
681 SDL_AudioSpec src, SDL_AudioSpec dst )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
682 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
683 int filter_index = *fip;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
684
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
685 if( src.channels == 2 && dst.channels == 1 )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
686 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
687 Data->add /= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
688 Data->len_mult /= 2;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
689
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
690 if( !IS_SYSENDIAN(src) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
691 Data->adapter[filter_index++] = swapBytes;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
692
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
693 if( IS_SIGNED(src) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
694 Data->adapter[filter_index++] = convertStereoToMonoS16Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
695 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
696 Data->adapter[filter_index++] = convertStereoToMonoU16Bit;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
697
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
698 if( !IS_SYSENDIAN(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
699 Data->adapter[filter_index++] = swapBytes;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
700 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
701 else if( IS_SYSENDIAN(src) != IS_SYSENDIAN(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
702 Data->adapter[filter_index++] = swapBytes;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
703
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
704 if( IS_SIGNED(src) != IS_SIGNED(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
705 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
706 if( IS_SYSENDIAN(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
707 Data->adapter[filter_index++] = changeSigned16BitSys;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
708 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
709 Data->adapter[filter_index++] = changeSigned16BitSys;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
710 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
711
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
712 if( src.channels == 1 && dst.channels == 2 )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
713 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
714 Data->add *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
715 Data->len_mult *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
716 Data->adapter[filter_index++] = convertMonoToStereo16Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
717 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
718
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
719 *fip = filter_index;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
720 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
721
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
722 /*-------------------------------------------------------------------------*/
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
723 static void createFormatConverter8Bit(Sound_AudioCVT *Data, int *fip,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
724 SDL_AudioSpec src, SDL_AudioSpec dst )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
725 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
726 int filter_index = *fip;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
727 if( IS_16BIT(src) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
728 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
729 Data->add /= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
730 Data->len_mult /= 2;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
731
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
732 if( IS_SYSENDIAN(src) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
733 Data->adapter[filter_index++] = cut16BitSysTo8Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
734 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
735 Data->adapter[filter_index++] = cut16BitWrongTo8Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
736 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
737
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
738 if( src.channels == 2 && dst.channels == 1 )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
739 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
740 Data->add /= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
741 Data->len_mult /= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
742
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
743 if( IS_SIGNED(src) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
744 Data->adapter[filter_index++] = convertStereoToMonoS8Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
745 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
746 Data->adapter[filter_index++] = convertStereoToMonoU8Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
747 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
748
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
749 if( IS_SIGNED(src) != IS_SIGNED(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
750 Data->adapter[filter_index++] = changeSigned8Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
751
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
752 if( src.channels == 1 && dst.channels == 2 )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
753 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
754 Data->add *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
755 Data->len_mult *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
756 Data->adapter[filter_index++] = convertMonoToStereo8Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
757 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
758
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
759 if( !IS_8BIT(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
760 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
761 Data->add *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
762 Data->len_mult *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
763 if( IS_SYSENDIAN(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
764 Data->adapter[filter_index++] = expand8BitTo16BitSys;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
765 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
766 Data->adapter[filter_index++] = expand8BitTo16BitWrong;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
767 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
768
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
769 *fip = filter_index;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
770 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
771
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
772 /*-------------------------------------------------------------------------*/
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
773 static void createFormatConverter(Sound_AudioCVT *Data, int *fip,
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
774 SDL_AudioSpec src, SDL_AudioSpec dst )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
775 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
776 int filter_index = *fip;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
777
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
778 if( IS_FLOAT(src) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
779 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
780 Data->adapter[filter_index++] = cutFloatTo16Bit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
781 Data->len_mult /= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
782 Data->add /= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
783 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
784
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
785 if( IS_8BIT(src) || IS_8BIT(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
786 createFormatConverter8Bit( Data, &filter_index, src, dst);
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
787 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
788 createFormatConverter16Bit( Data, &filter_index, src, dst);
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
789
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
790 if( IS_FLOAT(dst) )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
791 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
792 Data->adapter[filter_index++] = expand16BitToFloat;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
793 Data->len_mult *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
794 Data->add *= 2;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
795 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
796
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
797 *fip = filter_index;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
798 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
799
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
800
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
801 /*-------------------------------------------------------------------------*/
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
802 int BuildAudioCVT( Sound_AudioCVT *Data,
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
803 SDL_AudioSpec src, SDL_AudioSpec dst )
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
804 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
805 SDL_AudioSpec intrm;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
806 int filter_index = 0;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
807
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
808 if( Data == NULL ) return -1;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
809 Data->len_mult = 1.;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
810 Data->add = 0;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
811
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
812 /* Check channels */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
813 if( src.channels < 1 || src.channels > 2 ||
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
814 dst.channels < 1 || dst.channels > 2 ) goto error_exit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
815
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
816 /* If no frequency conversion is needed, go straight to dst format */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
817 if( src.freq == dst.freq )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
818 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
819 createFormatConverter( Data, &filter_index, src, dst );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
820 goto sucess_exit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
821 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
822
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
823 /* Convert to signed 16Bit System-Endian */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
824 intrm.format = AUDIO_S16SYS;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
825 intrm.channels = min( src.channels, dst.channels );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
826 createFormatConverter( Data, &filter_index, src, intrm );
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
827
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
828 /* Do rate conversion */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
829 if( src.channels == 2 && dst.channels == 2 )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
830 createRateConverter( Data, &filter_index, src.freq, dst.freq, 2 );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
831 else
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
832 createRateConverter( Data, &filter_index, src.freq, dst.freq, 1 );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
833 /* propagate error */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
834 if( filter_index < 0 ) goto error_exit;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
835
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
836 /* Convert to final format */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
837 createFormatConverter( Data, &filter_index, intrm, dst );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
838
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
839 /* Set up the filter information */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
840 sucess_exit:
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
841 /* !!! FIXME: Is it okay to assign NULL to a function pointer?
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
842 Borland says no. -frank */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
843 Data->adapter[filter_index] = NULL;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
844 return 0;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
845
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
846 error_exit:
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
847 /* !!! FIXME: Is it okay to assign NULL to a function pointer?
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
848 Borland says no. -frank */
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
849 Data->adapter[0] = NULL;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
850 return -1;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
851 }
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
852
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
853 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
854 static char *fmt_to_str(Uint16 fmt)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
855 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
856 switch (fmt)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
857 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
858 case AUDIO_U8: return " U8";
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
859 case AUDIO_S8: return " S8";
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
860 case AUDIO_U16MSB: return "U16MSB";
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
861 case AUDIO_S16MSB: return "S16MSB";
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
862 case AUDIO_U16LSB: return "U16LSB";
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
863 case AUDIO_S16LSB: return "S16LSB";
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
864 }
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
865 return "??????";
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
866 }
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
867
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
868 #define AdapterDesc(x) { x, #x }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
869
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
870 static void show_AudioCVT( Sound_AudioCVT *Data )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
871 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
872 int i,j;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
873 const struct{ int (*adapter) ( AdapterC, int); Sint8 *name; }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
874 AdapterDescription[] = {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
875 AdapterDesc(expand8BitTo16BitSys),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
876 AdapterDesc(expand8BitTo16BitWrong),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
877 AdapterDesc(expand16BitToFloat),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
878 AdapterDesc(swapBytes),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
879 AdapterDesc(cut16BitSysTo8Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
880 AdapterDesc(cut16BitWrongTo8Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
881 AdapterDesc(cutFloatTo16Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
882 AdapterDesc(changeSigned16BitSys),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
883 AdapterDesc(changeSigned16BitWrong),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
884 AdapterDesc(changeSigned8Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
885 AdapterDesc(convertStereoToMonoS16Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
886 AdapterDesc(convertStereoToMonoU16Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
887 AdapterDesc(convertStereoToMonoS8Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
888 AdapterDesc(convertStereoToMonoU8Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
889 AdapterDesc(convertMonoToStereo16Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
890 AdapterDesc(convertMonoToStereo8Bit),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
891 AdapterDesc(minus5dB),
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
892 AdapterDesc(doubleRateMono),
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
893 AdapterDesc(doubleRateStereo),
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
894 AdapterDesc(halfRateMono),
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
895 AdapterDesc(halfRateStereo),
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
896 AdapterDesc(increaseRateMono),
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
897 AdapterDesc(increaseRateStereo),
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
898 AdapterDesc(decreaseRateMono),
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
899 AdapterDesc(decreaseRateStereo),
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
900 { NULL, "----------NULL-----------" }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
901 };
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
902 const int AdapterDescMax = sizeof(AdapterDescription)
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
903 / sizeof(*AdapterDescription);
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
904
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
905 fprintf( stderr, "\nAdapter List: \n" );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
906 for( i = 0; i < 32; i++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
907 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
908 for( j = 0; j < AdapterDescMax; j++ )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
909 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
910 if( Data->adapter[i] == AdapterDescription[j].adapter )
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
911 {
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
912 fprintf( stderr, " %s\n", AdapterDescription[j].name );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
913 if( Data->adapter[i] == NULL ) return;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
914 goto cont;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
915 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
916 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
917 fprintf( stderr, " Error: unknown adapter\n" );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
918 cont:
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
919 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
920 fprintf( stderr, " Error: NULL adapter missing\n" );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
921 }
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
922
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
923
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
924 int Sound_BuildAudioCVT(Sound_AudioCVT *Data,
365
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
925 Uint16 src_format, Uint8 src_channels, int src_rate,
f61eadea1f44 More revisions from Frank.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
926 Uint16 dst_format, Uint8 dst_channels, int dst_rate)
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
927 {
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
928 SDL_AudioSpec src, dst;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
929 int ret;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
930
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
931 fprintf (stderr,
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
932 "Sound_BuildAudioCVT() :\n"
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
933 "-----------------------\n"
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
934 "format: %s -> %s\n"
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
935 "channels: %6d -> %6d\n"
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
936 "rate: %6d -> %6d\n",
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
937 fmt_to_str (src_format), fmt_to_str (dst_format),
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
938 src_channels, dst_channels,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
939 src_rate, dst_rate);
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
940
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
941 src.format = src_format;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
942 src.channels = src_channels;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
943 src.freq = src_rate;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
944
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
945 dst.format = dst_format;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
946 dst.channels = dst_channels;
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
947 dst.freq = dst_rate;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
948
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
949 ret = BuildAudioCVT( Data, src, dst );
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
950
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
951 show_AudioCVT( Data );
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
952 fprintf (stderr, "\n"
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
953 "return value: %d \n", ret );
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
954
360
c984aa6990f7 Fixes and enhancements from Frank Ranostaj.
Ryan C. Gordon <icculus@icculus.org>
parents: 347
diff changeset
955 return ret;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
956 }