annotate src/miscplay.c @ 398:74fd4e045ea6 r21-2-29

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