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