annotate alt_audio_convert.c @ 340:5a72981b8cba

Added optional, experimental audio conversion routines by Frank Ranostaj.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 20 May 2002 16:21:58 +0000
parents 7b9a0f3f030e
children fbbb1f25b944
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 int DECLSPEC Sound_ConvertAudio( Sound_AudioCVT *Data )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 int expand8BitTo16Bit( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 int swapBytes( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 int cut16BitTo8Bit( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132 int changeSigned( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142 int convertStereoToMono( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 int convertMonoToStereo( AdapterC Data, int length )
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;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 short* buffer = Data.buffer-2;
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 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 buffer[i] = buffer [i+1] = buffer[i/2];
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 return length*2;
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 int minus5dB( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 int doubleRateStereo( AdapterC Data, int length )
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
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 int doubleRateMono( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 int halfRateStereo( AdapterC Data, int length )
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
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 int halfRateMono( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 int varRateStereo( AdapterC Data, int length )
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
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 int varRateMono( AdapterC Data, int length )
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225 Fraction findFraction( float Value )
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
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
269 float sinc( float x )
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
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275 void calculateVarFilter( short* dst, float Ratio, float phase, float scale )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
276 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
277 const unsigned short KaiserWindow7[]= {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
278 22930, 16292, 14648, 14288, 14470, 14945, 15608, 16404,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
279 17304, 18289, 19347, 20467, 21644, 22872, 24145, 25460,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
280 26812, 28198, 29612, 31052, 32513, 33991, 35482, 36983,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
281 38487, 39993, 41494, 42986, 44466, 45928, 47368, 48782,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
282 50165, 51513, 52821, 54086, 55302, 56466, 57575, 58624,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
283 59610, 60529, 61379, 62156, 62858, 63483, 64027, 64490,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284 64870, 65165, 65375, 65498, 65535, 65484, 65347, 65124,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285 64815, 64422, 63946, 63389, 62753, 62039, 61251, 60391 };
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286 int i;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287 float w;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 const float fg = -.018 + .5 / Ratio;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289 const float omega = 2 * M_PI * fg;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290 phase -= 63;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 for( i = 0; i < 64; i++)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 w = scale * ( KaiserWindow7[i] * ( i + 1 ));
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 dst[i] = w * sinc( omega * (i+phase) );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 dst[127-i] = w * sinc( omega * (127-i+phase) );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 }
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 typedef struct{
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300 float scale;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 int incr;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 } VarFilterMode;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304 const VarFilterMode Up = { 0.0211952, 0 };
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
305 const VarFilterMode Down = { 0.0364733, 2 };
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
306
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 void setupVarFilter( VarFilter* filter,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309 float Ratio, VarFilterMode Direction )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 int i,n,d;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312 Fraction IRatio;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 float phase;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 IRatio = findFraction( Ratio );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316 if ( (1/Ratio) < Ratio )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317 Ratio = 1/Ratio;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319 n = IRatio.numerator;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
320 d = IRatio.denominator;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
321 filter->pos_mod = n;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
322
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
323 for( i = 0; i < d; i++ )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
324 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 if( phase >= n )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
327 phase -= d;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
328 filter->incr[i] = Direction.incr;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
329 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
330 else
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
331 filter->incr[i] = 1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333 calculateVarFilter( filter->c[i], Ratio, phase/(float)n,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 Direction.scale );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 phase += d;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336 }
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 int createRateConverter( Sound_AudioCVT *Data, int filter_index,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 int SrcRate, int DestRate, int Channel )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342 int VarPos = 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
343 int Mono = 2 - Channel;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 float Ratio = DestRate;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345 if( SrcRate < 1 || SrcRate > 1<<18 ||
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
346 DestRate < 1 || DestRate > 1<<18 ) return -1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
347 if( SrcRate == DestRate ) return filter_index;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348 Ratio /= SrcRate;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
349
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350 if( Ratio > 1.0)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 VarPos = filter_index++;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352 else
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353 Data->adapter[filter_index++] = minus5dB;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
354
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
355 while( Ratio > 64.0/31.0)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
356 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
357 Data->adapter[filter_index++] =
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
358 Mono ? doubleRateMono : doubleRateStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
359 Ratio /= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
360 Data->len_mult *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
361 Data->add *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362 Data->add += _fsize;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365 while( Ratio < 31.0/64.0 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
366 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
367 Data->adapter[filter_index++] =
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
368 Mono ? halfRateMono : halfRateStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
369 Ratio *= 2;
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
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
372 if( Ratio > 1.0 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
373 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
374 setupVarFilter( &Data->filter, Ratio, Up );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
375 Data->adapter[VarPos] =
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
376 Mono ? varRateMono : varRateStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
377 Data->len_mult *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378 Data->add *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
379 Data->add += _fsize;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
380 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
381 else
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
382 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
383 setupVarFilter( &Data->filter, Ratio, Down );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
384 Data->adapter[filter_index++] =
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
385 Mono ? varRateMono : varRateStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
386 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
387 return 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
388 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
389
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
390 int DECLSPEC Sound_BuildAudioCVT(Sound_AudioCVT *Data,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
391 Uint16 src_format, Uint8 src_channels, int src_rate,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
392 Uint16 dst_format, Uint8 dst_channels, int dst_rate)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
393 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
394 int filter_index = 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
396 if( Data == NULL ) return -1;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
397 Data->len_mult = 1.0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
398 Data->add = 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
399
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
400 /* Check channels */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
401 if( src_channels < 1 || src_channels > 2 ||
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
402 dst_channels < 1 || dst_channels > 2 ) goto error_exit;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
403
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
404 /* First filter: Size/Endian conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
405 switch( src_format & AUDIO_FORMAT)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
406 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
407 case AUDIO_8:
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
408 Data->adapter[filter_index++] = expand8BitTo16Bit;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
409 Data->len_mult *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
410 break;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
411 case AUDIO_16WRONG:
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
412 Data->adapter[filter_index++] = swapBytes;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
414
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
415 /* Second adapter: Sign conversion -- unsigned/signed */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
416 if( src_format & AUDIO_SIGN )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
417 Data->adapter[filter_index++] = changeSigned;
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 /* Third adapter: Stereo->Mono conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
420 if( src_channels == 2 && dst_channels == 1 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
421 Data->adapter[filter_index++] = convertStereoToMono;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
422
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
423 /* Do rate conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
424 if( src_channels == 2 && dst_channels == 2 )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
425 filter_index = createRateConverter( Data, filter_index,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
426 src_rate, dst_rate, 2 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
427 else
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
428 filter_index = createRateConverter( Data, filter_index,
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
429 src_rate, dst_rate, 1 );
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
430
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
431 if( filter_index < 0 ) goto error_exit; /* propagate error */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
432
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
433 /* adapter: Mono->Stereo conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
434 if( src_channels == 1 && dst_channels == 2 ){
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
435 Data->adapter[filter_index++] = convertMonoToStereo;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
436 Data->add *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
437 Data->len_mult *= 2;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
438 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
439
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
440 /* adapter: final Sign conversion -- unsigned/signed */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
441 if( dst_format & AUDIO_SIGN )
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
442 Data->adapter[filter_index++] = changeSigned;
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 /* final adapter: Size/Endian conversion */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
445 switch( dst_format & AUDIO_FORMAT)
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
446 {
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
447 case AUDIO_8:
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
448 Data->adapter[filter_index++] = cut16BitTo8Bit;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
449 break;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
450 case AUDIO_16WRONG:
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
451 Data->adapter[filter_index++] = swapBytes;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
452 }
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
453 /* Set up the filter information */
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
454 Data->adapter[filter_index] = NULL;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
455 Data->needed = (filter_index > 0);
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
456 return 0;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
457
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
458 error_exit:
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
459 Data->adapter[0] = NULL;
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
460 return -1;
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 /*-------------------------------------------------------------------------*/
7b9a0f3f030e Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
463