annotate alt_audio_convert.c @ 355:5dbf2f9cde00

Added docs stuff.
author Ryan C. Gordon <icculus@icculus.org>
date Fri, 07 Jun 2002 13:26:13 +0000
parents 778cee61e1be
children c984aa6990f7
rev   line source
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 Extended Audio Converter for SDL (Simple DirectMedia Layer)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 Copyright (C) 2002 Frank Ranostaj
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 Institute of Applied Physik
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 Johann Wolfgang Goethe-Universität
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 Frankfurt am Main, Germany
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 This library is free software; you can redistribute it and/or
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 modify it under the terms of the GNU Library General Public
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 License as published by the Free Software Foundation; either
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 version 2 of the License, or (at your option) any later version.
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 This library is distributed in the hope that it will be useful,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 Library General Public License for more details.
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 You should have received a copy of the GNU Library General Public
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19 License along with this library; if not, write to the Free
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 Frank Ranostaj
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 ranostaj@stud.uni-frankfurt.de
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 (This code blatantly abducted for SDL_sound. Thanks, Frank! --ryan.)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 */
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
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 #include <stdlib.h>
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 #include <math.h>
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 /*provisorisch*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 #define AUDIO_8 (4)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 #define AUDIO_16WRONG (8)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 #define AUDIO_FORMAT (AUDIO_8 | AUDIO_16WRONG)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 #define AUDIO_SIGN (1)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 /* this filter (Kaiser-window beta=6.8) gives a decent -80dB attentuation */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 static const int filter[_fsize/2] = {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 0, 20798, 0, -6764, 0, 3863, 0, -2560, 0, 1800, 0, -1295, 0, 936, 0,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 -671,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45 0, 474, 0, -326, 0, 217, 0, -138, 0, 83, 0, -46, 0, 23, 0, -9
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 };
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 /* Mono (1 channel ) */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 #define Suffix(x) x##1
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50 #include "filter_templates.h"
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 #undef Suffix
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 /* Stereo (2 channel ) */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 #define Suffix(x) x##2
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 #include "filter_templates.h"
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 #undef Suffix
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 /* !!! FIXME: Lose all the "short" vars for "Sint16", etc. */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
61 int Sound_ConvertAudio( Sound_AudioCVT *Data )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 AdapterC Temp;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 /* !!! FIXME: Try the looping stuff under certain circumstances? --ryan. */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 int mode = 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 /* Make sure there's a converter */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 if( Data == NULL ) {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 SDL_SetError("No converter given");
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 return(-1);
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 /* Make sure there's data to convert */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 if( Data->buf == NULL ) {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 SDL_SetError("No buffer allocated for conversion");
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 return(-1);
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 /* Set up the conversion and go! */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 Temp.buffer = (short*)Data->buf;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 Temp.mode = mode;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 Temp.filter = &Data->filter;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 Data->len_cvt = Data->len;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 for( i = 0; Data->adapter[i] != NULL; i++ )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 Data->len_cvt = (*Data->adapter[i])( Temp, Data->len_cvt);
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 return(0);
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
94 static int expand8BitTo16Bit( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 char* inp = (char*)Data.buffer-1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 short* buffer = Data.buffer-1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 for( i = length; i; i-- )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 buffer[i] = inp[i]<<8;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 return length*2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
105 static int swapBytes( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 unsigned short a,b;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 short* buffer = Data.buffer;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110 for( i = 0; i < length; i++ )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 a = b = buffer[i];
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 a <<= 8;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 b >>= 8;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 buffer[i] = a | b;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 return length;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
121 static int cut16BitTo8Bit( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 short* inp = Data.buffer-1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 char* buffer = (char*)Data.buffer-1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 for( i = 0; i < length; i++ )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 buffer[i] = inp[i]>>8;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 return length/2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
132 static int changeSigned( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 short* buffer = Data.buffer;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136 for( i = 0; i < length; i++ )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137 buffer[i] ^= 0x8000;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 return length;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
142 static int convertStereoToMono( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145 short* buffer = Data.buffer;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 /*
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148 * !!! FIXME: Can we avoid the division in this loop and just keep
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 * !!! FIXME: a second index variable? --ryan.
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 for( i = 0; i < length; i+=2 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 buffer[i/2] = ((int)buffer[i] + buffer[i+1] ) >> 1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 return length/2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
157 static int convertMonoToStereo( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 int i;
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
160 short* buffer = Data.buffer-1;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 length *= 2;
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 /*
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 * !!! FIXME: Can we avoid the division in this loop and just keep
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 * !!! FIXME: a second index variable? --ryan.
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 for( i = length; i; i-=2 )
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
168 buffer[i] = buffer [i-1] = buffer[i/2];
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
169 return length;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
173 static int minus5dB( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 short* buffer = Data.buffer;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 for(i = length; i >= 0; i--)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 buffer[i]= 38084 * buffer[i] >> 16;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 return length;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
183 static int doubleRateStereo( AdapterC Data, int length )
338
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 _doubleRate2( Data.buffer, Data.mode, length/2 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 return 2*_doubleRate2( Data.buffer+1, Data.mode, length/2 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
189 static int doubleRateMono( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 return _doubleRate1( Data.buffer, Data.mode, length );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
195 static int halfRateStereo( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 _halfRate2( Data.buffer, Data.mode, length/2 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 return 2*_halfRate2( Data.buffer+1, Data.mode, length/2 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
201 static int halfRateMono( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 return _halfRate2( Data.buffer, Data.mode, length );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
207 static int varRateStereo( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 _varRate2( Data.buffer, Data.mode, Data.filter, length/2 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 return 2*_varRate2( Data.buffer+1, Data.mode, Data.filter, length/2 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
213 static int varRateMono( AdapterC Data, int length )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 return _varRate1( Data.buffer, Data.mode, Data.filter, length );
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 typedef struct{
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 short denominator;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221 short numerator;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222 } Fraction;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 /*-------------------------------------------------------------------------*/
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
225 static Fraction findFraction( float Value )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 /* gives a maximal error of 3% and typical less than 0.2% */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 const char frac[96]={
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 1, 2, -1, /* /1 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 1, 3, -1, /* /2 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 2, 4, 5, -1, /* /3 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 3, 5, 7, -1, /* /4 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 3, 4, 6, 7, 8, 9, -1, /* /5 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 5, 7, 11, -1, /* /6 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 4, 5, 6, 8, 9, 10, 11, 12, 13, -1, /* /7 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 5, 7, 9, 11, 13, 15, -1, /* /8 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 5, 7, 8, 10, 11, 13, 14, 16, -1, /* /9 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 7, 9, 11, 13, -1, /* /10 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, -1, /* /11 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 7, 11, 13, -1, /* /12 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 7, 8, 9, 10, 11, 12, 14, 15, 16, -1, /* /13 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 9, 11, 13, 15, -1, /* /14 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 8, 11, 13, 14, 16, -1, /* /15 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 9, 11, 13, 15 }; /* /16 */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 Fraction Result = {0,0};
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 int n,num,den=2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 float RelErr, BestErr = 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 if( Value < 31/64. || Value > 64/31. ) return Result;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 for( n = 0; n < sizeof(frac); num=frac[++n] )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 if( num < 0 ) den++;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 RelErr = Value * num / den;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 RelErr = ( RelErr < (1/RelErr) ? RelErr : 1/RelErr );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 if( RelErr > BestErr )
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 BestErr = RelErr;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261 Result.denominator = den;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262 Result.numerator = num;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265 return Result;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
266 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
267
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
268
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
269 static float sinc( float x )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
270 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
271 if( x > -1e-24 && x < 1e-24 ) return 1.;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
272 else return sin(x)/x;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
273 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
275 static void calculateVarFilter( short* dst, float Ratio, float phase,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
276 float scale )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
277 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
278 const unsigned short KaiserWindow7[]= {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
279 22930, 16292, 14648, 14288, 14470, 14945, 15608, 16404,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
280 17304, 18289, 19347, 20467, 21644, 22872, 24145, 25460,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
281 26812, 28198, 29612, 31052, 32513, 33991, 35482, 36983,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
282 38487, 39993, 41494, 42986, 44466, 45928, 47368, 48782,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
283 50165, 51513, 52821, 54086, 55302, 56466, 57575, 58624,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284 59610, 60529, 61379, 62156, 62858, 63483, 64027, 64490,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285 64870, 65165, 65375, 65498, 65535, 65484, 65347, 65124,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286 64815, 64422, 63946, 63389, 62753, 62039, 61251, 60391 };
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 float w;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289 const float fg = -.018 + .5 / Ratio;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290 const float omega = 2 * M_PI * fg;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 phase -= 63;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 for( i = 0; i < 64; i++)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 w = scale * ( KaiserWindow7[i] * ( i + 1 ));
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 dst[i] = w * sinc( omega * (i+phase) );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 dst[127-i] = w * sinc( omega * (127-i+phase) );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
299
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300 typedef struct{
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 float scale;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 int incr;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 } VarFilterMode;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
305 static const VarFilterMode Up = { 0.0211952, 0 };
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
306 static const VarFilterMode Down = { 0.0364733, 2 };
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
307
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
308
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
309 static void setupVarFilter( VarFilter* filter,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
310 float Ratio, VarFilterMode Direction )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312 int i,n,d;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 Fraction IRatio;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 float phase;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315 IRatio = findFraction( Ratio );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317 if ( (1/Ratio) < Ratio )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318 Ratio = 1/Ratio;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
320 n = IRatio.numerator;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
321 d = IRatio.denominator;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
322 filter->pos_mod = n;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
323
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
324 for( i = 0; i < d; i++ )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 if( phase >= n )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
327 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
328 phase -= d;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
329 filter->incr[i] = Direction.incr;
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 else
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332 filter->incr[i] = 1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 calculateVarFilter( filter->c[i], Ratio, phase/(float)n,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 Direction.scale );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336 phase += d;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
337 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
338 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
340 static int createRateConverter( Sound_AudioCVT *Data, int filter_index,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
341 int SrcRate, int DestRate, int Channel )
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
343 int VarPos = 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 int Mono = 2 - Channel;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345 float Ratio = DestRate;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
346 if( SrcRate < 1 || SrcRate > 1<<18 ||
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
347 DestRate < 1 || DestRate > 1<<18 ) return -1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348 if( SrcRate == DestRate ) return filter_index;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
349 Ratio /= SrcRate;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 if( Ratio > 1.0)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352 VarPos = filter_index++;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353 else
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
354 {
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
355 fprintf (stderr, "Filter: minus5dB\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
356 Data->adapter[filter_index++] = minus5dB;
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
357 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
358
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
359 while( Ratio > 64.0/31.0)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
360 {
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
361 fprintf (stderr, "Filter: %s\n",
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
362 Mono ? "doubleRateMono" : "doubleRateStereo");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 Data->adapter[filter_index++] =
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364 Mono ? doubleRateMono : doubleRateStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365 Ratio /= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
366 Data->len_mult *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
367 Data->add *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
368 Data->add += _fsize;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
369 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
370
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
371 while( Ratio < 31.0/64.0 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
372 {
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
373 fprintf (stderr, "Filter: %s\n",
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
374 Mono ? "halfRateMono" : "halfRateStereo");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
375 Data->adapter[filter_index++] =
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
376 Mono ? halfRateMono : halfRateStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
377 Ratio *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
379
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
380 if( Ratio > 1.0 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
381 {
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
382 fprintf (stderr, "Filter: %s\n",
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
383 Mono ? "varRateMono" : "varRateStereo");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
384 setupVarFilter( &Data->filter, Ratio, Up );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
385 Data->adapter[VarPos] =
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
386 Mono ? varRateMono : varRateStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
387 Data->len_mult *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
388 Data->add *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
389 Data->add += _fsize;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
390 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
391 else
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
392 {
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
393 fprintf (stderr, "Filter: %s\n",
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
394 Mono ? "varRateMono" : "varRateStereo");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395 setupVarFilter( &Data->filter, Ratio, Down );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
396 Data->adapter[filter_index++] =
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
397 Mono ? varRateMono : varRateStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
398 }
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
399 return filter_index;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
400 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
401
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
402 static int BuildAudioCVT(Sound_AudioCVT *Data,
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
403 Uint16 src_format, Uint8 src_channels, int src_rate,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
404 Uint16 dst_format, Uint8 dst_channels, int dst_rate)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
405 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
406 int filter_index = 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
407
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
408 if( Data == NULL ) return -1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
409 Data->len_mult = 1.0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
410 Data->add = 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
411
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
412 /* Check channels */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413 if( src_channels < 1 || src_channels > 2 ||
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
414 dst_channels < 1 || dst_channels > 2 ) goto error_exit;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
415
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
416 /* First filter: Size/Endian conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
417 switch( src_format & AUDIO_FORMAT)
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 case AUDIO_8:
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
420 fprintf (stderr, "Filter: expand8BitTo16Bit\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
421 Data->adapter[filter_index++] = expand8BitTo16Bit;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
422 Data->len_mult *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
423 break;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
424 case AUDIO_16WRONG:
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
425 fprintf (stderr, "Filter: swapBytes\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
426 Data->adapter[filter_index++] = swapBytes;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
427 break;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
428 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
429
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
430 /* Second adapter: Sign conversion -- unsigned/signed */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
431 if( src_format & AUDIO_SIGN )
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
432 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
433 fprintf (stderr, "Filter: changeSigned\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
434 Data->adapter[filter_index++] = changeSigned;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
435 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
436
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
437 /* Third adapter: Stereo->Mono conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
438 if( src_channels == 2 && dst_channels == 1 )
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
439 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
440 fprintf (stderr, "convertStereoToMono\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
441 Data->adapter[filter_index++] = convertStereoToMono;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
442 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
443
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
444 /* Do rate conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
445 if( src_channels == 2 && dst_channels == 2 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
446 filter_index = createRateConverter( Data, filter_index,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
447 src_rate, dst_rate, 2 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
448 else
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
449 filter_index = createRateConverter( Data, filter_index,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
450 src_rate, dst_rate, 1 );
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 if( filter_index < 0 ) goto error_exit; /* propagate error */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
453
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
454 /* adapter: Mono->Stereo conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
455 if( src_channels == 1 && dst_channels == 2 ){
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
456 fprintf (stderr, "Filter: convertMonoToStereo\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
457 Data->adapter[filter_index++] = convertMonoToStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
458 Data->add *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
459 Data->len_mult *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
460 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
461
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
462 /* adapter: final Sign conversion -- unsigned/signed */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
463 if( dst_format & AUDIO_SIGN )
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
464 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
465 fprintf (stderr, "Filter: changeSigned\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
466 Data->adapter[filter_index++] = changeSigned;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
467 }
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
468
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
469 /* final adapter: Size/Endian conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
470 switch( dst_format & AUDIO_FORMAT)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
471 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
472 case AUDIO_8:
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
473 fprintf (stderr, "Filter: cut16BitTo8Bit\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
474 Data->adapter[filter_index++] = cut16BitTo8Bit;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
475 break;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
476 case AUDIO_16WRONG:
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
477 fprintf (stderr, "Filter: swapBytes\n");
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
478 Data->adapter[filter_index++] = swapBytes;
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
479 break;
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
480 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
481 /* Set up the filter information */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
482 Data->adapter[filter_index] = NULL;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
483 Data->needed = (filter_index > 0);
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
484 return 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
485
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
486 error_exit:
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
487 Data->adapter[0] = NULL;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
488 return -1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
489 }
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
490
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
491 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
492 * Frank's audio converter has its own ideas about how to represent audio
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
493 * format, so at least for a transition period we use this to glue his code
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
494 * to our's.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
495 *
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
496 * + The expand8BitTo16Bit filter will only convert to system byte order.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
497 * + The cut16BitTo8Bit filter will only convert from system byte order.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
498 * + The changeSigned filter only works on 16-bit samples, system byte order.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
499 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
500
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
501 static char *fmt_to_str(Uint16 fmt)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
502 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
503 switch (fmt)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
504 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
505 case AUDIO_U8: return " U8"; break;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
506 case AUDIO_S8: return " S8"; break;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
507 case AUDIO_U16MSB: return "U16MSB"; break;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
508 case AUDIO_S16MSB: return "S16MSB"; break;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
509 case AUDIO_U16LSB: return "U16LSB"; break;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
510 case AUDIO_S16LSB: return "S16LSB"; break;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
511 }
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
512 return "??????";
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
513 }
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
514
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
515 #define IS_8BIT(x) ((x) & 0x0008)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
516 #define IS_16BIT(x) ((x) & 0x0010)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
517 #define ENDIAN(x) ((x) & 0x1000)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
518 #define SIGNED(x) ((x) & 0x8000)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
519
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
520 int Sound_BuildAudioCVT(Sound_AudioCVT *Data,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
521 Uint16 src_in_format, Uint8 src_channels, int src_rate,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
522 Uint16 dst_in_format, Uint8 dst_channels, int dst_rate)
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
523 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
524 Uint16 src_format = 0;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
525 Uint16 dst_format = 0;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
526
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
527 fprintf (stderr,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
528 "format: %s -> %s\n"
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
529 "channels: %6d -> %6d\n"
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
530 "rate: %6d -> %6d\n",
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
531 fmt_to_str (src_in_format), fmt_to_str (dst_in_format),
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
532 src_channels, dst_channels,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
533 src_rate, dst_rate);
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
534
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
535 if ( IS_8BIT(src_in_format) && IS_16BIT(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
536 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
537 src_format |= AUDIO_8;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
538
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
539 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
540 * Signedness and byte-order changes must wait until the data
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
541 * has been converted to 16-bit samples.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
542 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
543 if ( SIGNED(src_in_format) != SIGNED(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
544 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
545 dst_format |= AUDIO_SIGN;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
546 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
547
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
548 if ( ENDIAN(dst_in_format) != ENDIAN(AUDIO_U16SYS) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
549 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
550 dst_format |= AUDIO_16WRONG;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
551 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
552 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
553 else if ( IS_16BIT(src_in_format) && IS_8BIT(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
554 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
555 dst_format |= AUDIO_8;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
556
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
557 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
558 * Byte-order and signedness changes must be made before the data
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
559 * has been converted to 8-bit samples.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
560 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
561 if ( ENDIAN(src_in_format) != ENDIAN(AUDIO_U16SYS) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
562 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
563 src_format |= AUDIO_16WRONG;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
564 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
565
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
566 if ( SIGNED(src_in_format) != SIGNED(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
567 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
568 src_format |= AUDIO_SIGN;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
569 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
570 } /* else if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
571 else if ( IS_16BIT(src_in_format) && IS_16BIT(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
572 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
573 if ( ENDIAN(src_in_format) != ENDIAN(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
574 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
575 if ( ENDIAN(src_in_format) == ENDIAN(AUDIO_U16SYS) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
576 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
577 dst_format |= AUDIO_16WRONG;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
578
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
579 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
580 * The data is already is system byte order, so any
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
581 * signedness change has to be made before changing byte
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
582 * order.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
583 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
584 if ( SIGNED(src_in_format) != SIGNED(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
585 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
586 src_format |= AUDIO_SIGN;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
587 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
588 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
589 else
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
590 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
591 src_format |= AUDIO_16WRONG;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
592
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
593 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
594 * The data is not in system byte order, so any signedness
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
595 * change has to be made after changing byte order.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
596 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
597 if ( SIGNED(src_in_format) != SIGNED(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
598 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
599 dst_format |= AUDIO_SIGN;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
600 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
601 } /* else */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
602 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
603 else if ( ENDIAN(src_in_format) != SIGNED(AUDIO_U16SYS) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
604 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
605 if ( SIGNED(src_in_format) != SIGNED(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
606 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
607 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
608 * !!! FIXME !!!
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
609 *
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
610 * The changeSigned filter only works on system byte
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
611 * order. In this case, both source and destination is
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
612 * in opposite byte order, but the sign has to changed
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
613 * so we need to convert to system byte order, change
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
614 * sign, and then convert back to the original byte
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
615 * order again. This is not an optimal solution.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
616 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
617 src_format |= ( AUDIO_16WRONG | AUDIO_SIGN );
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
618 dst_format |= AUDIO_16WRONG;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
619 } /* if */
347
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
620
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
621 if ( src_rate != dst_rate )
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
622 {
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
623 /*
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
624 * !!! FIXME !!!
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
625 *
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
626 * The audio conversion filter probably only works if the
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
627 * data is in system byte order. So we need to convert to
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
628 * system byte order, and then back to original byte
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
629 * order. This is not an optimal solution.
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
630 */
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
631 src_format |= AUDIO_16WRONG;
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
632 dst_format |= AUDIO_16WRONG;
778cee61e1be Mono-to-stereo fixes, more fprintf debugging output.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
633 }
342
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
634 } /* else if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
635 else if ( SIGNED(src_in_format) != SIGNED(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
636 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
637 src_format |= AUDIO_SIGN;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
638 } /* else if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
639 } /* else if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
640 else if ( IS_8BIT(src_in_format) && IS_8BIT(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
641 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
642 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
643 * !!! FIXME !!!
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
644 *
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
645 * The changeSigned filter only works on 16-bit samples, so if
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
646 * the signedness differs we have to convert from 8 to 16 bits,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
647 * change the sign and then convert back to 8 bits again. This
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
648 * is not an optimal solution.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
649 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
650 if ( SIGNED(src_in_format) != SIGNED(dst_in_format) )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
651 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
652 src_format |= ( AUDIO_8 | AUDIO_SIGN );
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
653 dst_format |= AUDIO_8;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
654 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
655
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
656 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
657 * !!! FIXME !!!
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
658 *
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
659 * The convertMonoToStereo and convertStereoToMono filters only
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
660 * work with 16-bit samples. So if those are to be applied, we
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
661 * need to convert to 16-bit samples, and then back again.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
662 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
663 if ( src_channels != dst_channels )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
664 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
665 src_format |= AUDIO_8;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
666 dst_format |= AUDIO_8;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
667 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
668
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
669 /*
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
670 * !!! FIXME !!!
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
671 *
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
672 * The rate conversion filters almost certainly only work with
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
673 * 16-bit samples. Yadda, yadda, yadda.
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
674 */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
675 if ( src_rate != dst_rate )
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
676 {
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
677 src_format |= AUDIO_8;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
678 dst_format |= AUDIO_8;
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
679 } /* if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
680 } /* else if */
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
681
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
682 return BuildAudioCVT(Data, src_format, src_channels, src_rate,
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
683 dst_format, dst_channels, dst_rate);
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
684 }
fbbb1f25b944 Cleanups by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 338
diff changeset
685
338
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
686 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
687