428
|
1 /* miscplay.c - general routines related to playing sounds
|
|
2 **
|
|
3 ** Copyright (C) 1995,96 by Markus Gutschke (gutschk@math.uni-muenster.de)
|
|
4 ** This was sawed out from version 1.3 of linuxplay.c by
|
|
5 ** Robert Bihlmeyer <robbe@orcus.priv.at>.
|
|
6 **
|
|
7 ** Parts of this code were inspired by sunplay.c, which is copyright 1989 by
|
|
8 ** Jef Poskanzer and 1991,92 by Jamie Zawinski; c.f. sunplay.c for further
|
|
9 ** information.
|
|
10 **
|
|
11 ** Permission to use, copy, modify, and distribute this software and its
|
|
12 ** documentation for any purpose and without fee is hereby granted, provided
|
|
13 ** that the above copyright notice appear in all copies and that both that
|
|
14 ** copyright notice and this permission notice appear in supporting
|
|
15 ** documentation. This software is provided "as is" without express or
|
|
16 ** implied warranty.
|
|
17 */
|
|
18
|
563
|
19 /* Synched up with: Not in FSF. */
|
|
20
|
|
21 /* This file Mule-ized by Ben Wing, 5-15-01. */
|
|
22
|
428
|
23 #include <config.h>
|
563
|
24 #include "lisp.h"
|
428
|
25
|
|
26 #include "miscplay.h"
|
563
|
27 #include "sound.h"
|
|
28
|
428
|
29 #include "syssignal.h"
|
|
30 #include "sysfile.h"
|
|
31
|
|
32 #ifdef __GNUC__
|
|
33 #define UNUSED(x) ((void)(x))
|
|
34 #else
|
|
35 #define UNUSED(x)
|
|
36 #endif
|
|
37
|
|
38 /* Maintain global variable for keeping parser state information; this struct
|
|
39 is set to zero before the first invocation of the parser. The use of a
|
|
40 global variable prevents multiple concurrent executions of this code, but
|
|
41 this does not happen anyways... */
|
|
42 enum wvState
|
|
43 { wvMain,
|
|
44 wvSubchunk,
|
|
45 wvOutOfBlock,
|
|
46 wvSkipChunk,
|
|
47 wvSoundChunk,
|
|
48 wvFatal,
|
|
49 wvFatalNotify
|
|
50 };
|
|
51
|
|
52 static union {
|
|
53 struct {
|
|
54 int align;
|
|
55 enum wvState state;
|
|
56 size_t left;
|
563
|
57 UChar_Binary leftover[HEADERSZ];
|
428
|
58 signed long chunklength;
|
|
59 } wave;
|
|
60 struct {
|
|
61 int align;
|
|
62 int isdata;
|
|
63 int skipping;
|
|
64 size_t left;
|
563
|
65 UChar_Binary leftover[HEADERSZ];
|
428
|
66 } audio;
|
|
67 } parsestate;
|
|
68
|
|
69 /* Use a global buffer as scratch-pad for possible conversions of the
|
|
70 sampling format */
|
563
|
71 UChar_Binary miscplay_sndbuf[SNDBUFSZ];
|
428
|
72
|
|
73 /* Initialize global parser state information to zero */
|
|
74 void reset_parsestate()
|
|
75 {
|
|
76 memset(&parsestate,0,sizeof(parsestate));
|
|
77 }
|
|
78
|
|
79 /* Verify that we could fully parse the entire soundfile; this is needed
|
|
80 only for files in WAVE format */
|
|
81 int parse_wave_complete()
|
|
82 {
|
|
83 if (parsestate.wave.state != wvOutOfBlock &&
|
|
84 parsestate.wave.state != wvFatal) {
|
563
|
85 sound_warn("Unexpected end of WAVE file");
|
428
|
86 return 0;
|
|
87 } else
|
|
88 return 1;
|
|
89 }
|
|
90
|
|
91 /* There is no special treatment required for parsing raw data files; we
|
|
92 assume that these files contain data in 8bit unsigned format that
|
|
93 has been sampled at 8kHz; there is no extra header */
|
|
94 static size_t parseraw(void **data,size_t *sz,void **outbuf)
|
|
95 {
|
|
96 int rc = *sz;
|
|
97
|
|
98 *outbuf = *data;
|
|
99 *sz = 0;
|
|
100 return(rc);
|
|
101 }
|
|
102
|
|
103 /* Currently we cannot cope with files in VOC format; if you really need
|
|
104 to play these files, they should be converted by using SOX */
|
|
105 static size_t parsevoc(void **data,size_t *sz,void **outbuf)
|
|
106 {
|
|
107 UNUSED(data);
|
|
108 UNUSED(sz);
|
|
109 UNUSED(outbuf);
|
|
110 return(0);
|
|
111 }
|
|
112
|
|
113 /* We need to perform some look-ahead in order to parse files in WAVE format;
|
|
114 this might require re-partioning of the data segments if headers cross the
|
|
115 boundaries between two read operations. This is done in a two-step way:
|
|
116 first we request a certain amount of bytes... */
|
570
|
117 static int waverequire(void **data,size_t *sz,size_t rq)
|
428
|
118 {
|
|
119 int rc = 1;
|
|
120
|
|
121 if (rq > HEADERSZ) {
|
563
|
122 sound_warn("Header size exceeded while parsing WAVE file");
|
428
|
123 parsestate.wave.state = wvFatal;
|
|
124 *sz = 0;
|
|
125 return(0); }
|
|
126 if ((rq -= parsestate.wave.left) <= 0)
|
|
127 return(rc);
|
|
128 if (rq > *sz) {rq = *sz; rc = 0;}
|
|
129 memcpy(parsestate.wave.leftover+parsestate.wave.left,
|
|
130 *data,rq);
|
|
131 parsestate.wave.left += rq;
|
563
|
132 (*(UChar_Binary **)data) += rq;
|
428
|
133 *sz -= rq;
|
|
134 return(rc);
|
|
135 }
|
|
136
|
|
137 /* ...and next we remove this many bytes from the buffer */
|
442
|
138 static inline void waveremove(size_t rq)
|
428
|
139 {
|
|
140 if (parsestate.wave.left <= rq)
|
|
141 parsestate.wave.left = 0;
|
|
142 else {
|
|
143 parsestate.wave.left -= rq;
|
|
144 memmove(parsestate.wave.leftover,
|
|
145 parsestate.wave.leftover+rq,
|
|
146 parsestate.wave.left); }
|
|
147 return;
|
|
148 }
|
|
149
|
|
150 /* Sound files in WAVE format can contain an arbitrary amount of tagged
|
|
151 chunks; this requires quite some effort for parsing the data */
|
|
152 static size_t parsewave(void **data,size_t *sz,void **outbuf)
|
|
153 {
|
|
154 for (;;)
|
|
155 switch (parsestate.wave.state) {
|
|
156 case wvMain:
|
|
157 if (!waverequire(data,sz,20))
|
|
158 return(0);
|
|
159 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
|
|
160 parsestate.wave.chunklength = parsestate.wave.leftover[16] +
|
|
161 256*(parsestate.wave.leftover[17] +
|
|
162 256*(parsestate.wave.leftover[18] +
|
|
163 256*parsestate.wave.leftover[19]));
|
|
164 waveremove(20);
|
|
165 parsestate.wave.state = wvSubchunk;
|
|
166 break;
|
|
167 case wvSubchunk:
|
|
168 if (!waverequire(data,sz,parsestate.wave.chunklength))
|
|
169 return(0);
|
|
170 parsestate.wave.align = parsestate.wave.chunklength < 14 ? 1
|
|
171 : parsestate.wave.leftover[12];
|
|
172 if (parsestate.wave.align != 1 &&
|
|
173 parsestate.wave.align != 2 &&
|
|
174 parsestate.wave.align != 4) {
|
563
|
175 sound_warn("Illegal datawidth detected while parsing WAVE file");
|
428
|
176 parsestate.wave.state = wvFatal; }
|
|
177 else
|
|
178 parsestate.wave.state = wvOutOfBlock;
|
|
179 waveremove(parsestate.wave.chunklength);
|
|
180 break;
|
|
181 case wvOutOfBlock:
|
|
182 if (!waverequire(data,sz,8))
|
|
183 return(0);
|
|
184 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
|
|
185 parsestate.wave.chunklength = parsestate.wave.leftover[4] +
|
|
186 256*(parsestate.wave.leftover[5] +
|
|
187 256*(parsestate.wave.leftover[6] +
|
|
188 256*(parsestate.wave.leftover[7] & 0x7F)));
|
|
189 if (memcmp(parsestate.wave.leftover,"data",4))
|
|
190 parsestate.wave.state = wvSkipChunk;
|
|
191 else
|
|
192 parsestate.wave.state = wvSoundChunk;
|
|
193 waveremove(8);
|
|
194 break;
|
|
195 case wvSkipChunk:
|
|
196 if (parsestate.wave.chunklength > 0 && *sz > 0 &&
|
|
197 (signed long)*sz < (signed long)parsestate.wave.chunklength) {
|
|
198 parsestate.wave.chunklength -= *sz;
|
|
199 *sz = 0; }
|
|
200 else {
|
|
201 if (parsestate.wave.chunklength > 0 && *sz > 0) {
|
|
202 *sz -= parsestate.wave.chunklength;
|
563
|
203 (*(UChar_Binary **)data) += parsestate.wave.chunklength; }
|
428
|
204 parsestate.wave.state = wvOutOfBlock; }
|
|
205 break;
|
|
206 case wvSoundChunk: {
|
|
207 size_t count,rq;
|
|
208 if (parsestate.wave.left) { /* handle leftover bytes from last
|
|
209 alignment operation */
|
|
210 count = parsestate.wave.left;
|
|
211 rq = HEADERSZ-count;
|
|
212 if (rq > (size_t) parsestate.wave.chunklength)
|
|
213 rq = parsestate.wave.chunklength;
|
|
214 if (!waverequire(data,sz,rq)) {
|
|
215 parsestate.wave.chunklength -= parsestate.wave.left - count;
|
|
216 return(0); }
|
|
217 parsestate.wave.chunklength -= rq;
|
|
218 *outbuf = parsestate.wave.leftover;
|
|
219 parsestate.wave.left = 0;
|
|
220 return(rq); }
|
|
221 if (*sz >= (size_t) parsestate.wave.chunklength) {
|
|
222 count = parsestate.wave.chunklength;
|
|
223 rq = 0; }
|
|
224 else {
|
|
225 count = *sz;
|
|
226 count -= rq = count % parsestate.wave.align; }
|
|
227 *outbuf = *data;
|
563
|
228 (*(UChar_Binary **)data) += count;
|
428
|
229 *sz -= count;
|
|
230 if ((parsestate.wave.chunklength -= count) < parsestate.wave.align) {
|
|
231 parsestate.wave.state = wvOutOfBlock;
|
|
232 /* Some broken software (e.g. SOX) attaches junk to the end of a sound
|
|
233 chunk; so, let's ignore this... */
|
|
234 if (parsestate.wave.chunklength)
|
|
235 parsestate.wave.state = wvSkipChunk; }
|
|
236 else if (rq)
|
|
237 /* align data length to a multiple of datasize; keep additional data
|
|
238 in "leftover" buffer --- this is necessary to ensure proper
|
|
239 functioning of the sndcnv... routines */
|
|
240 waverequire(data,sz,rq);
|
|
241 return(count); }
|
|
242 case wvFatalNotify:
|
563
|
243 sound_warn("Irrecoverable error while parsing WAVE file");
|
428
|
244 parsestate.wave.state = wvFatal;
|
|
245 break;
|
|
246 case wvFatal:
|
|
247 default:
|
|
248 *sz = 0;
|
|
249 return(0); }
|
|
250 }
|
|
251
|
|
252 /* Strip the header from files in Sun/DEC audio format; this requires some
|
|
253 extra processing as the header can be an arbitrary size and it might
|
|
254 result in alignment errors for subsequent conversions --- thus we do
|
|
255 some buffering, where needed */
|
|
256 static size_t parsesundecaudio(void **data,size_t *sz,void **outbuf)
|
|
257 {
|
|
258 /* There is data left over from the last invocation of this function; join
|
|
259 it with the new data and return a sound chunk that is as big as a
|
|
260 single entry */
|
|
261 if (parsestate.audio.left) {
|
|
262 if (parsestate.audio.left + *sz > (size_t) parsestate.audio.align) {
|
|
263 int count;
|
|
264 memmove(parsestate.audio.leftover + parsestate.audio.left,
|
|
265 *data,
|
|
266 count = parsestate.audio.align - parsestate.audio.left);
|
|
267 *outbuf = parsestate.audio.leftover;
|
|
268 *sz -= count;
|
563
|
269 *data = (*(Char_Binary **)data) + count;
|
428
|
270 parsestate.audio.left = 0;
|
|
271 return(parsestate.audio.align); }
|
|
272 else {
|
|
273 /* We need even more data in order to get one complete single entry! */
|
|
274 memmove(parsestate.audio.leftover + parsestate.audio.left,
|
|
275 *data,
|
|
276 *sz);
|
563
|
277 *data = (*(Char_Binary **)data) + *sz;
|
428
|
278 parsestate.audio.left += *sz;
|
|
279 *sz = 0;
|
|
280 return(0); } }
|
|
281
|
|
282 /* This is the main sound chunk, strip of any extra data that does not fit
|
|
283 the alignment requirements and move these bytes into the leftover buffer*/
|
|
284 if (parsestate.audio.isdata) {
|
|
285 int rc = *sz;
|
|
286 *outbuf = *data;
|
|
287 if ((parsestate.audio.left = rc % parsestate.audio.align) != 0) {
|
|
288 memmove(parsestate.audio.leftover,
|
563
|
289 (Char_Binary *)*outbuf + rc - parsestate.audio.left,
|
428
|
290 parsestate.audio.left);
|
|
291 rc -= parsestate.audio.left; }
|
|
292 *sz = 0;
|
|
293 return(rc); }
|
|
294
|
|
295 /* This is the first invocation of this function; we need to parse the
|
|
296 header information and determine how many bytes we need to skip until
|
|
297 the start of the sound chunk */
|
|
298 if (!parsestate.audio.skipping) {
|
563
|
299 UChar_Binary *header = (UChar_Binary *) *data;
|
428
|
300 if (*sz < 8) {
|
563
|
301 sound_warn("Irrecoverable error while parsing Sun/DEC audio file");
|
428
|
302 return(0); }
|
|
303 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
|
|
304 if (header[3]) { /* Sun audio (big endian) */
|
|
305 parsestate.audio.align = ((header[15] > 2)+1)*header[23];
|
|
306 parsestate.audio.skipping = header[7]+256*(header[6]+256*
|
|
307 (header[5]+256*header[4])); }
|
|
308 else { /* DEC audio (little endian) */
|
|
309 parsestate.audio.align = ((header[12] > 2)+1)*header[20];
|
|
310 parsestate.audio.skipping = header[4]+256*(header[5]+256*
|
|
311 (header[6]+256*header[7])); }}
|
|
312
|
|
313 /* We are skipping extra data that has been attached to header; most usually
|
|
314 this will be just a comment, such as the original filename and/or the
|
|
315 creation date. Make sure that we do not return less than one single sound
|
|
316 sample entry to the caller; if this happens, rather decide to move those
|
|
317 few bytes into the leftover buffer and deal with it later */
|
|
318 if (*sz >= (size_t) parsestate.audio.skipping) {
|
|
319 /* Skip just the header information and return the sound chunk */
|
|
320 int rc = *sz - parsestate.audio.skipping;
|
563
|
321 *outbuf = (Char_Binary *)*data + parsestate.audio.skipping;
|
428
|
322 if ((parsestate.audio.left = rc % parsestate.audio.align) != 0) {
|
|
323 memmove(parsestate.audio.leftover,
|
563
|
324 (Char_Binary *)*outbuf + rc - parsestate.audio.left,
|
428
|
325 parsestate.audio.left);
|
|
326 rc -= parsestate.audio.left; }
|
|
327 *sz = 0;
|
|
328 parsestate.audio.skipping = 0;
|
|
329 parsestate.audio.isdata++;
|
|
330 return(rc); }
|
|
331 else {
|
|
332 /* Skip everything */
|
|
333 parsestate.audio.skipping -= *sz;
|
|
334 return(0); }
|
|
335 }
|
|
336
|
|
337 /* If the soundcard could not be set to natively support the data format, we
|
|
338 try to do some limited on-the-fly conversion to a different format; if
|
|
339 no conversion is needed, though, we can output directly */
|
|
340 size_t sndcnvnop(void **data,size_t *sz,void **outbuf)
|
|
341 {
|
|
342 int rc = *sz;
|
|
343
|
|
344 *outbuf = *data;
|
|
345 *sz = 0;
|
|
346 return(rc);
|
|
347 }
|
|
348
|
|
349 /* Convert 8 bit unsigned stereo data to 8 bit unsigned mono data */
|
|
350 size_t sndcnv8U_2mono(void **data,size_t *sz,void **outbuf)
|
|
351 {
|
563
|
352 REGISTER UChar_Binary *src;
|
|
353 REGISTER UChar_Binary *dest;
|
428
|
354 int rc,count;
|
|
355
|
|
356 count = *sz / 2;
|
|
357 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
|
|
358 else *sz = 0;
|
|
359 rc = count;
|
563
|
360 src = (UChar_Binary *) *data;
|
428
|
361 *outbuf =
|
|
362 dest = miscplay_sndbuf;
|
|
363 while (count--)
|
452
|
364 {
|
563
|
365 *dest++ = (UChar_Binary)(((int)*(src) +
|
452
|
366 (int)*(src+1)) / 2);
|
|
367 src += 2;
|
|
368 }
|
428
|
369 *data = src;
|
|
370 return(rc);
|
|
371 }
|
|
372
|
|
373 /* Convert 8 bit signed stereo data to 8 bit signed mono data */
|
|
374 size_t sndcnv8S_2mono(void **data,size_t *sz,void **outbuf)
|
|
375 {
|
563
|
376 REGISTER UChar_Binary *src;
|
|
377 REGISTER UChar_Binary *dest;
|
428
|
378 int rc, count;
|
|
379
|
|
380 count = *sz / 2;
|
|
381 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
|
|
382 else *sz = 0;
|
|
383 rc = count;
|
563
|
384 src = (UChar_Binary *) *data;
|
428
|
385 *outbuf =
|
|
386 dest = miscplay_sndbuf;
|
|
387 while (count--)
|
444
|
388 {
|
593
|
389 *dest++ = (UChar_Binary)(((int)*((SChar_Binary *)(src)) +
|
|
390 (int)*((SChar_Binary *)(src+1))) / 2);
|
444
|
391 src += 2;
|
|
392 }
|
428
|
393 *data = src;
|
|
394 return(rc);
|
|
395 }
|
|
396
|
|
397 /* Convert 8 bit signed stereo data to 8 bit unsigned mono data */
|
|
398 size_t sndcnv2monounsigned(void **data,size_t *sz,void **outbuf)
|
|
399 {
|
563
|
400 REGISTER UChar_Binary *src;
|
|
401 REGISTER UChar_Binary *dest;
|
428
|
402 int rc,count;
|
|
403
|
|
404 count = *sz / 2;
|
|
405 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
|
|
406 else *sz = 0;
|
|
407 rc = count;
|
563
|
408 src = (UChar_Binary *) *data;
|
428
|
409 *outbuf =
|
|
410 dest = miscplay_sndbuf;
|
|
411 while (count--)
|
444
|
412 {
|
593
|
413 *dest++ = (UChar_Binary)(((int)*((SChar_Binary *)(src)) +
|
|
414 (int)*((SChar_Binary *)(src+1))) / 2) ^ 0x80;
|
444
|
415 src += 2;
|
|
416 }
|
428
|
417 *data = src;
|
|
418 return(rc);
|
|
419 }
|
|
420
|
|
421 /* Convert 8 bit signed mono data to 8 bit unsigned mono data */
|
|
422 size_t sndcnv2unsigned(void **data,size_t *sz,void **outbuf)
|
|
423 {
|
563
|
424 REGISTER UChar_Binary *src;
|
|
425 REGISTER UChar_Binary *dest;
|
428
|
426 int rc,count;
|
|
427
|
|
428 count = *sz;
|
|
429 if (count > SNDBUFSZ) { *sz -= SNDBUFSZ; count = SNDBUFSZ; }
|
|
430 else *sz = 0;
|
|
431 rc = count;
|
563
|
432 src = (UChar_Binary *) *data;
|
428
|
433 *outbuf =
|
|
434 dest = miscplay_sndbuf;
|
|
435 while (count--)
|
|
436 *dest++ = *(src)++ ^ 0x80;
|
|
437 *data = src;
|
|
438 return(rc);
|
|
439 }
|
|
440
|
|
441 /* Convert a number in the range -32768..32767 to an 8 bit ulaw encoded
|
|
442 number --- I hope, I got this conversion right :-) */
|
593
|
443 static inline SChar_Binary int2ulaw(int i)
|
428
|
444 {
|
|
445 /* Lookup table for fast calculation of number of bits that need shifting*/
|
|
446 static short int t_bits[128] = {
|
|
447 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,
|
|
448 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,
|
|
449 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,
|
|
450 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};
|
|
451 REGISTER int bits,logi;
|
|
452
|
|
453 /* unrolling this condition (hopefully) improves execution speed */
|
|
454 if (i < 0) {
|
|
455 if ((i = (132-i)) > 0x7FFF) i = 0x7FFF;
|
|
456 logi = (i >> ((bits = t_bits[i/256])+4));
|
|
457 return((bits << 4 | logi) ^ 0x7F); }
|
|
458 else {
|
|
459 if ((i = 132+i) > 0x7FFF) i = 0x7FFF;
|
|
460 logi = (i >> ((bits = t_bits[i/256])+4));
|
|
461 return(~(bits << 4 | logi)); }
|
|
462 }
|
|
463
|
|
464 /* Convert from 8 bit ulaw mono to 8 bit linear mono */
|
|
465 size_t sndcnvULaw_2linear(void **data,size_t *sz,void **outbuf)
|
|
466 {
|
|
467 /* conversion table stolen from Linux's ulaw.h */
|
563
|
468 static UChar_Binary ulaw_dsp[] = {
|
428
|
469 3, 7, 11, 15, 19, 23, 27, 31,
|
|
470 35, 39, 43, 47, 51, 55, 59, 63,
|
|
471 66, 68, 70, 72, 74, 76, 78, 80,
|
|
472 82, 84, 86, 88, 90, 92, 94, 96,
|
|
473 98, 99, 100, 101, 102, 103, 104, 105,
|
|
474 106, 107, 108, 109, 110, 111, 112, 113,
|
|
475 113, 114, 114, 115, 115, 116, 116, 117,
|
|
476 117, 118, 118, 119, 119, 120, 120, 121,
|
|
477 121, 121, 122, 122, 122, 122, 123, 123,
|
|
478 123, 123, 124, 124, 124, 124, 125, 125,
|
|
479 125, 125, 125, 125, 126, 126, 126, 126,
|
|
480 126, 126, 126, 126, 127, 127, 127, 127,
|
|
481 127, 127, 127, 127, 127, 127, 127, 127,
|
|
482 128, 128, 128, 128, 128, 128, 128, 128,
|
|
483 128, 128, 128, 128, 128, 128, 128, 128,
|
|
484 128, 128, 128, 128, 128, 128, 128, 128,
|
|
485 253, 249, 245, 241, 237, 233, 229, 225,
|
|
486 221, 217, 213, 209, 205, 201, 197, 193,
|
|
487 190, 188, 186, 184, 182, 180, 178, 176,
|
|
488 174, 172, 170, 168, 166, 164, 162, 160,
|
|
489 158, 157, 156, 155, 154, 153, 152, 151,
|
|
490 150, 149, 148, 147, 146, 145, 144, 143,
|
|
491 143, 142, 142, 141, 141, 140, 140, 139,
|
|
492 139, 138, 138, 137, 137, 136, 136, 135,
|
|
493 135, 135, 134, 134, 134, 134, 133, 133,
|
|
494 133, 133, 132, 132, 132, 132, 131, 131,
|
|
495 131, 131, 131, 131, 130, 130, 130, 130,
|
|
496 130, 130, 130, 130, 129, 129, 129, 129,
|
|
497 129, 129, 129, 129, 129, 129, 129, 129,
|
|
498 128, 128, 128, 128, 128, 128, 128, 128,
|
|
499 128, 128, 128, 128, 128, 128, 128, 128,
|
|
500 128, 128, 128, 128, 128, 128, 128, 128,
|
|
501 };
|
563
|
502 UChar_Binary *p=(UChar_Binary *)*data;
|
428
|
503
|
|
504 *outbuf = *data;
|
|
505 while ((*sz)--)
|
444
|
506 {
|
|
507 *p = ulaw_dsp[*p];
|
|
508 p++;
|
|
509 }
|
428
|
510 *sz = 0;
|
|
511 *data = p;
|
563
|
512 return p - (UChar_Binary *)*outbuf;
|
428
|
513 }
|
|
514
|
|
515 /* Convert 8 bit ulaw stereo data to 8 bit ulaw mono data */
|
|
516 size_t sndcnvULaw_2mono(void **data,size_t *sz,void **outbuf)
|
|
517 {
|
|
518
|
|
519 static short int ulaw2int[256] = {
|
|
520 /* Precomputed lookup table for conversion from ulaw to 15 bit signed */
|
|
521 -16062,-15550,-15038,-14526,-14014,-13502,-12990,-12478,
|
|
522 -11966,-11454,-10942,-10430, -9918, -9406, -8894, -8382,
|
|
523 -7998, -7742, -7486, -7230, -6974, -6718, -6462, -6206,
|
|
524 -5950, -5694, -5438, -5182, -4926, -4670, -4414, -4158,
|
|
525 -3966, -3838, -3710, -3582, -3454, -3326, -3198, -3070,
|
|
526 -2942, -2814, -2686, -2558, -2430, -2302, -2174, -2046,
|
|
527 -1950, -1886, -1822, -1758, -1694, -1630, -1566, -1502,
|
|
528 -1438, -1374, -1310, -1246, -1182, -1118, -1054, -990,
|
|
529 -942, -910, -878, -846, -814, -782, -750, -718,
|
|
530 -686, -654, -622, -590, -558, -526, -494, -462,
|
|
531 -438, -422, -406, -390, -374, -358, -342, -326,
|
|
532 -310, -294, -278, -262, -246, -230, -214, -198,
|
|
533 -186, -178, -170, -162, -154, -146, -138, -130,
|
|
534 -122, -114, -106, -98, -90, -82, -74, -66,
|
|
535 -60, -56, -52, -48, -44, -40, -36, -32,
|
|
536 -28, -24, -20, -16, -12, -8, -4, +0,
|
|
537 +16062,+15550,+15038,+14526,+14014,+13502,+12990,+12478,
|
|
538 +11966,+11454,+10942,+10430, +9918, +9406, +8894, +8382,
|
|
539 +7998, +7742, +7486, +7230, +6974, +6718, +6462, +6206,
|
|
540 +5950, +5694, +5438, +5182, +4926, +4670, +4414, +4158,
|
|
541 +3966, +3838, +3710, +3582, +3454, +3326, +3198, +3070,
|
|
542 +2942, +2814, +2686, +2558, +2430, +2302, +2174, +2046,
|
|
543 +1950, +1886, +1822, +1758, +1694, +1630, +1566, +1502,
|
|
544 +1438, +1374, +1310, +1246, +1182, +1118, +1054, +990,
|
|
545 +942, +910, +878, +846, +814, +782, +750, +718,
|
|
546 +686, +654, +622, +590, +558, +526, +494, +462,
|
|
547 +438, +422, +406, +390, +374, +358, +342, +326,
|
|
548 +310, +294, +278, +262, +246, +230, +214, +198,
|
|
549 +186, +178, +170, +162, +154, +146, +138, +130,
|
|
550 +122, +114, +106, +98, +90, +82, +74, +66,
|
|
551 +60, +56, +52, +48, +44, +40, +36, +32,
|
|
552 +28, +24, +20, +16, +12, +8, +4, +0};
|
|
553
|
563
|
554 REGISTER UChar_Binary *src;
|
|
555 REGISTER UChar_Binary *dest;
|
428
|
556 int rc,count;
|
|
557
|
|
558 count = *sz / 2;
|
|
559 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
|
|
560 else *sz = 0;
|
|
561 rc = count;
|
563
|
562 src = (UChar_Binary *) *data;
|
428
|
563 *outbuf =
|
|
564 dest = miscplay_sndbuf;
|
|
565 while (count--)
|
444
|
566 {
|
|
567 /* it is not possible to directly interpolate between two ulaw encoded
|
|
568 data bytes, thus we need to convert to linear format first and later
|
|
569 we convert back to ulaw format */
|
|
570 *dest++ = int2ulaw(ulaw2int[*src] +
|
|
571 ulaw2int[*(src+1)]);
|
|
572 src += 2;
|
|
573 }
|
428
|
574 *data = src;
|
|
575 return(rc);
|
|
576 }
|
|
577
|
|
578 size_t sndcnv16swap(void **data,size_t *sz,void **outbuf)
|
|
579 {
|
|
580 size_t cnt = *sz / 2;
|
|
581 unsigned short *p;
|
|
582
|
|
583 *outbuf = *data;
|
|
584 p = (unsigned short *) *outbuf;
|
444
|
585 while (cnt--)
|
|
586 {
|
|
587 *p = ((*p & 0x00ff) << 8) | (*p >> 8);
|
|
588 p++;
|
|
589 }
|
428
|
590 *data = p;
|
|
591 cnt = *sz;
|
|
592 *sz = 0;
|
|
593 return cnt;
|
|
594 }
|
|
595
|
|
596 /* Convert 16 bit little endian signed stereo data to 16 bit little endian
|
|
597 signed mono data */
|
|
598 size_t sndcnv16_2monoLE(void **data,size_t *sz,void **outbuf)
|
|
599 {
|
563
|
600 REGISTER UChar_Binary *src;
|
|
601 REGISTER UChar_Binary *dest;
|
428
|
602 int rc,count;
|
|
603 signed short i;
|
|
604
|
|
605 count = *sz / 2;
|
|
606 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
|
|
607 else *sz = 0;
|
|
608 rc = count;
|
563
|
609 src = (UChar_Binary *) *data;
|
428
|
610 *outbuf =
|
|
611 dest = miscplay_sndbuf;
|
|
612 for (count /= 2; count--; ) {
|
|
613 i = ((int)(src[0]) +
|
|
614 256*(int)(src[1]) +
|
|
615 (int)(src[2]) +
|
|
616 256*(int)(src[3])) / 2;
|
|
617 src += 4;
|
563
|
618 *dest++ = (UChar_Binary)(i & 0xFF);
|
|
619 *dest++ = (UChar_Binary)((i / 256) & 0xFF); }
|
428
|
620 *data = src;
|
|
621 return(rc);
|
|
622 }
|
|
623
|
|
624 /* Convert 16 bit big endian signed stereo data to 16 bit big endian
|
|
625 signed mono data */
|
|
626 size_t sndcnv16_2monoBE(void **data,size_t *sz,void **outbuf)
|
|
627 {
|
563
|
628 REGISTER UChar_Binary *src;
|
|
629 REGISTER UChar_Binary *dest;
|
428
|
630 int rc,count;
|
|
631 signed short i;
|
|
632
|
|
633 count = *sz / 2;
|
|
634 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
|
|
635 else *sz = 0;
|
|
636 rc = count;
|
563
|
637 src = (UChar_Binary *) *data;
|
428
|
638 *outbuf =
|
|
639 dest = miscplay_sndbuf;
|
|
640 for (count /= 2; count--; ) {
|
|
641 i = ((int)(src[1]) +
|
|
642 256*(int)(src[0]) +
|
|
643 (int)(src[3]) +
|
|
644 256*(int)(src[2])) / 2;
|
|
645 src += 4;
|
563
|
646 *dest++ = (UChar_Binary)((i / 256) & 0xFF);
|
|
647 *dest++ = (UChar_Binary)(i & 0xFF); }
|
428
|
648 *data = src;
|
|
649 return(rc);
|
|
650 }
|
|
651
|
|
652 /* Convert 16 bit little endian signed data to 8 bit unsigned data */
|
|
653 size_t sndcnv2byteLE(void **data,size_t *sz,void **outbuf)
|
|
654 {
|
563
|
655 REGISTER UChar_Binary *src;
|
|
656 REGISTER UChar_Binary *dest;
|
428
|
657 int rc,count;
|
|
658
|
|
659 count = *sz / 2;
|
|
660 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
|
|
661 else *sz = 0;
|
|
662 rc = count;
|
563
|
663 src = (UChar_Binary *) *data;
|
428
|
664 *outbuf =
|
|
665 dest = miscplay_sndbuf;
|
|
666 while (count--) {
|
593
|
667 *dest++ = (UChar_Binary)(((SChar_Binary *)src)[1] ^ (SChar_Binary)0x80);
|
428
|
668 src += 2;
|
|
669 }
|
|
670 *data = src;
|
|
671 return(rc);
|
|
672 }
|
|
673
|
|
674 /* Convert 16 bit big endian signed data to 8 bit unsigned data */
|
|
675 size_t sndcnv2byteBE(void **data,size_t *sz,void **outbuf)
|
|
676 {
|
563
|
677 REGISTER UChar_Binary *src;
|
|
678 REGISTER UChar_Binary *dest;
|
428
|
679 int rc,count;
|
|
680
|
|
681 count = *sz / 2;
|
|
682 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
|
|
683 else *sz = 0;
|
|
684 rc = count;
|
563
|
685 src = (UChar_Binary *) *data;
|
428
|
686 *outbuf =
|
|
687 dest = miscplay_sndbuf;
|
|
688 while (count--) {
|
593
|
689 *dest++ = (UChar_Binary)(((SChar_Binary *)src)[0] ^ (SChar_Binary)0x80);
|
428
|
690 src += 2;
|
|
691 }
|
|
692 *data = src;
|
|
693 return(rc);
|
|
694 }
|
|
695
|
|
696 /* Convert 16 bit little endian signed stereo data to 8 bit unsigned
|
|
697 mono data */
|
|
698 size_t sndcnv2monobyteLE(void **data,size_t *sz,void **outbuf)
|
|
699 {
|
563
|
700 REGISTER UChar_Binary *src;
|
|
701 REGISTER UChar_Binary *dest;
|
428
|
702 int rc,count;
|
|
703
|
|
704 count = *sz / 4;
|
|
705 if (count > SNDBUFSZ) { *sz -= 4*SNDBUFSZ; count = SNDBUFSZ; }
|
|
706 else *sz = 0;
|
|
707 rc = count;
|
563
|
708 src = (UChar_Binary *) *data;
|
428
|
709 *outbuf =
|
|
710 dest = miscplay_sndbuf;
|
|
711 while (count--) {
|
593
|
712 *dest++ = (UChar_Binary)(((int)((SChar_Binary *)src)[1] +
|
|
713 (int)((SChar_Binary *)src)[3]) / 2 ^ 0x80);
|
428
|
714 src += 4;
|
|
715 }
|
|
716 *data = src;
|
|
717 return(rc);
|
|
718 }
|
|
719
|
|
720 /* Convert 16 bit big endian signed stereo data to 8 bit unsigned
|
|
721 mono data */
|
|
722 size_t sndcnv2monobyteBE(void **data,size_t *sz,void **outbuf)
|
|
723 {
|
563
|
724 REGISTER UChar_Binary *src;
|
|
725 REGISTER UChar_Binary *dest;
|
428
|
726 int rc,count;
|
|
727
|
|
728 count = *sz / 4;
|
|
729 if (count > SNDBUFSZ) { *sz -= 4*SNDBUFSZ; count = SNDBUFSZ; }
|
|
730 else *sz = 0;
|
|
731 rc = count;
|
563
|
732 src = (UChar_Binary *) *data;
|
428
|
733 *outbuf =
|
|
734 dest = miscplay_sndbuf;
|
|
735 while (count--) {
|
593
|
736 *dest++ = (UChar_Binary)(((int)((SChar_Binary *)src)[0] +
|
|
737 (int)((SChar_Binary *)src)[2]) / 2 ^ 0x80);
|
428
|
738 src += 4;
|
|
739 }
|
|
740 *data = src;
|
|
741 return(rc);
|
|
742 }
|
|
743
|
|
744 /* Look at the header of the sound file and try to determine the format;
|
|
745 we can recognize files in VOC, WAVE, and, Sun/DEC-audio format--- everything
|
|
746 else is assumed to be raw 8 bit unsigned data sampled at 8kHz */
|
563
|
747 fmtType analyze_format(UChar_Binary *format,int *fmt,int *speed,
|
428
|
748 int *tracks,
|
|
749 size_t (**parsesndfile)(void **,size_t *sz,
|
|
750 void **))
|
|
751 {
|
|
752 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
|
|
753 if (!memcmp(format,"Creative Voice File\x1A\x1A\x00",22) &&
|
|
754 (format[22]+256*format[23]) ==
|
|
755 ((0x1233-format[24]-256*format[25])&0xFFFF)) { /* VOC */
|
|
756 *fmt = AFMT_U8;
|
|
757 *speed = 8000;
|
|
758 *tracks = 2;
|
|
759 *parsesndfile = parsevoc;
|
|
760 return(fmtVoc); }
|
|
761 else if (!memcmp(format,"RIFF",4) &&
|
|
762 !memcmp(format+8,"WAVEfmt ",8)) { /* WAVE */
|
|
763 if (memcmp(format+20,"\001\000\001"/* PCM mono */,4) &&
|
|
764 memcmp(format+20,"\001\000\002"/* PCM stereo */,4))
|
|
765 return(fmtIllegal);
|
|
766 *fmt = (format[32]/(*tracks = format[22])) == 1 ?
|
|
767 AFMT_U8 : AFMT_S16_LE;
|
|
768 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
|
|
769 *speed = format[24]+256*(format[25]+256*
|
|
770 (format[26]+256*format[27]));
|
|
771 *parsesndfile = parsewave;
|
|
772 return(fmtWave); }
|
|
773 else if (!memcmp(format,".snd",4)) { /* Sun Audio (big endian) */
|
|
774 if (format[7]+256*(format[6]+256*(format[5]+256*format[4])) < 24) {
|
|
775 *fmt = AFMT_MU_LAW;
|
|
776 *speed = 8000;
|
|
777 *tracks = 1;
|
|
778 *parsesndfile = parsesundecaudio;
|
|
779 return(fmtSunAudio); }
|
|
780 if (!memcmp(format+12,"\000\000\000\001",4)) *fmt = AFMT_MU_LAW;
|
|
781 else if (!memcmp(format+12,"\000\000\000\002",4)) *fmt = AFMT_S8;
|
|
782 else if (!memcmp(format+12,"\000\000\000\003",4)) *fmt = AFMT_S16_BE;
|
|
783 else return(fmtIllegal);
|
|
784 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
|
|
785 *speed = format[19]+256*(format[18]+256*
|
|
786 (format[17]+256*format[16]));
|
|
787 *tracks = format[23];
|
|
788 *parsesndfile = parsesundecaudio;
|
|
789 return(fmtSunAudio); }
|
|
790 else if (!memcmp(format,".sd",4)) { /* DEC Audio (little endian) */
|
|
791 if (format[4]+256*(format[5]+256*(format[6]+256*format[7])) < 24) {
|
|
792 *fmt = AFMT_MU_LAW;
|
|
793 *speed = 8000;
|
|
794 *tracks = 1;
|
|
795 *parsesndfile = parsesundecaudio;
|
|
796 return(fmtSunAudio); }
|
|
797 if (!memcmp(format+12,"\001\000\000",4)) *fmt = AFMT_MU_LAW;
|
|
798 else if (!memcmp(format+12,"\002\000\000",4)) *fmt = AFMT_S8;
|
|
799 else if (!memcmp(format+12,"\003\000\000",4)) *fmt = AFMT_S16_LE;
|
|
800 else return(fmtIllegal);
|
|
801 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
|
|
802 *speed = format[16]+256*(format[17]+256*
|
|
803 (format[18]+256*format[19]));
|
|
804 *tracks = format[20];
|
|
805 *parsesndfile = parsesundecaudio;
|
|
806 return(fmtSunAudio); }
|
|
807 else {
|
|
808 *fmt = AFMT_U8;
|
|
809 *speed = 8000;
|
|
810 *tracks = 1;
|
|
811 *parsesndfile = parseraw;
|
|
812 return(fmtRaw); }
|
|
813 }
|