Debian Codegraph @

code/client/snd_wavelet.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "snd_local.h"
24
25 #define C0 0.4829629131445341
26 #define C1 0.8365163037378079
27 #define C2 0.2241438680420134
28 #define C3 -0.1294095225512604
29
30 void daub4(float b[], unsigned long n, int isign)
31 {
32 float wksp[4097] = { 0.0f };
33 #define a(x) b[(x)-1] // numerical recipies so a[1] = b[0]
34
35 unsigned long nh,nh1,i,j;
36
37 if (n < 4) return;
38
39 nh1=(nh=n >> 1)+1;
40 if (isign >= 0) {
41 for (i=1,j=1;j<=n-3;j+=2,i++) {
42 wksp[i] = C0*a(j)+C1*a(j+1)+C2*a(j+2)+C3*a(j+3);
43 wksp[i+nh] = C3*a(j)-C2*a(j+1)+C1*a(j+2)-C0*a(j+3);
44 }
45 wksp[i ] = C0*a(n-1)+C1*a(n)+C2*a(1)+C3*a(2);
46 wksp[i+nh] = C3*a(n-1)-C2*a(n)+C1*a(1)-C0*a(2);
47 } else {
48 wksp[1] = C2*a(nh)+C1*a(n)+C0*a(1)+C3*a(nh1);
49 wksp[2] = C3*a(nh)-C0*a(n)+C1*a(1)-C2*a(nh1);
50 for (i=1,j=3;i<nh;i++) {
51 wksp[j++] = C2*a(i)+C1*a(i+nh)+C0*a(i+1)+C3*a(i+nh1);
52 wksp[j++] = C3*a(i)-C0*a(i+nh)+C1*a(i+1)-C2*a(i+nh1);
53 }
54 }
55 for (i=1;i<=n;i++) {
56 a(i)=wksp[i];
57 }
58 #undef a
59 }
60
61 void wt1(float a[], unsigned long n, int isign)
62 {
63 unsigned long nn;
64 int inverseStartLength = n/4;
65 if (n < inverseStartLength) return;
66 if (isign >= 0) {
67 for (nn=n;nn>=inverseStartLength;nn>>=1) daub4(a,nn,isign);
68 } else {
69 for (nn=inverseStartLength;nn<=n;nn<<=1) daub4(a,nn,isign);
70 }
71 }
72
73 /* The number of bits required by each value */
74 static unsigned char numBits[] = {
75 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
76 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
77 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
78 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
79 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
80 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
81 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
82 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
83 };
84
85 byte MuLawEncode(short s) {
86 unsigned long adjusted;
87 byte sign, exponent, mantissa;
88
89 sign = (s<0)?0:0x80;
90
91 if (s<0) s=-s;
92 adjusted = (long)s << (16-sizeof(short)*8);
93 adjusted += 128L + 4L;
94 if (adjusted > 32767) adjusted = 32767;
95 exponent = numBits[(adjusted>>7)&0xff] - 1;
96 mantissa = (adjusted>>(exponent+3))&0xf;
97 return ~(sign | (exponent<<4) | mantissa);
98 }
99
100 short MuLawDecode(byte uLaw) {
101 signed long adjusted;
102 byte exponent, mantissa;
103
104 uLaw = ~uLaw;
105 exponent = (uLaw>>4) & 0x7;
106 mantissa = (uLaw&0xf) + 16;
107 adjusted = (mantissa << (exponent +3)) - 128 - 4;
108
109 return (uLaw & 0x80)? adjusted : -adjusted;
110 }
111
112 short mulawToShort[256];
113 static qboolean madeTable = qfalse;
114
115 static int NXStreamCount;
116
117 void NXPutc(NXStream *stream, char out) {
118 stream[NXStreamCount++] = out;
119 }
120
121
122 void encodeWavelet( sfx_t *sfx, short *packets) {
123 float wksp[4097] = {0}, temp;
124 int i, samples, size;
125 sndBuffer *newchunk, *chunk;
126 byte *out;
127
128 if (!madeTable) {
129 for (i=0;i<256;i++) {
130 mulawToShort[i] = (float)MuLawDecode((byte)i);
131 }
132 madeTable = qtrue;
133 }
134 chunk = NULL;
135
136 samples = sfx->soundLength;
137 while(samples>0) {
138 size = samples;
139 if (size>(SND_CHUNK_SIZE*2)) {
140 size = (SND_CHUNK_SIZE*2);
141 }
142
143 if (size<4) {
144 size = 4;
145 }
146
147 newchunk = SND_malloc();
148 if (sfx->soundData == NULL) {
149 sfx->soundData = newchunk;
150 } else if (chunk != NULL) {
151 chunk->next = newchunk;
152 }
153 chunk = newchunk;
154 for(i=0; i<size; i++) {
155 wksp[i] = *packets;
156 packets++;
157 }
158 wt1(wksp, size, 1);
159 out = (byte *)chunk->sndChunk;
160
161 for(i=0;i<size;i++) {
162 temp = wksp[i];
163 if (temp > 32767) temp = 32767; else if (temp<-32768) temp = -32768;
164 out[i] = MuLawEncode((short)temp);
165 }
166
167 chunk->size = size;
168 samples -= size;
169 }
170 }
171
172 void decodeWavelet(sndBuffer *chunk, short *to) {
173 float wksp[4097] = {0};
174 int i;
175 byte *out;
176
177 int size = chunk->size;
178
179 out = (byte *)chunk->sndChunk;
180 for(i=0;i<size;i++) {
181 wksp[i] = mulawToShort[out[i]];
182 }
183
184 wt1(wksp, size, -1);
185
186 if (!to) return;
187
188 for(i=0; i<size; i++) {
189 to[i] = wksp[i];
190 }
191 }
192
193
194 void encodeMuLaw( sfx_t *sfx, short *packets) {
195 int i, samples, size, grade, poop;
196 sndBuffer *newchunk, *chunk;
197 byte *out;
198
199 if (!madeTable) {
200 for (i=0;i<256;i++) {
201 mulawToShort[i] = (float)MuLawDecode((byte)i);
202 }
203 madeTable = qtrue;
204 }
205
206 chunk = NULL;
207 samples = sfx->soundLength;
208 grade = 0;
209
210 while(samples>0) {
211 size = samples;
212 if (size>(SND_CHUNK_SIZE*2)) {
213 size = (SND_CHUNK_SIZE*2);
214 }
215
216 newchunk = SND_malloc();
217 if (sfx->soundData == NULL) {
218 sfx->soundData = newchunk;
219 } else if (chunk != NULL) {
220 chunk->next = newchunk;
221 }
222 chunk = newchunk;
223 out = (byte *)chunk->sndChunk;
224 for(i=0; i<size; i++) {
225 poop = packets[0]+grade;
226 if (poop>32767) {
227 poop = 32767;
228 } else if (poop<-32768) {
229 poop = -32768;
230 }
231 out[i] = MuLawEncode((short)poop);
232 grade = poop - mulawToShort[out[i]];
233 packets++;
234 }
235 chunk->size = size;
236 samples -= size;
237 }
238 }
239
240 void decodeMuLaw(sndBuffer *chunk, short *to) {
241 int i;
242 byte *out;
243
244 int size = chunk->size;
245
246 out = (byte *)chunk->sndChunk;
247 for(i=0;i<size;i++) {
248 to[i] = mulawToShort[out[i]];
249 }
250 }
251
252