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