wav.c revision 1.1 1 /*
2 * Copyright (c) 2002 Matthew R. Green
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * WAV support for the audio tools; thanks go to the sox utility for
31 * clearing up issues with WAV files.
32 */
33
34 #include <sys/types.h>
35 #include <sys/audioio.h>
36 #include <sys/ioctl.h>
37 #include <sys/time.h>
38
39 #include <ctype.h>
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "libaudio.h"
46
47 struct {
48 int wenc;
49 char *wname;
50 } wavencs[] = {
51 { WAVE_FORMAT_UNKNOWN, "Microsoft Official Unknown" },
52 { WAVE_FORMAT_PCM, "Microsoft PCM" },
53 { WAVE_FORMAT_ADPCM, "Microsoft ADPCM" },
54 { WAVE_FORMAT_ALAW, "Microsoft A-law" },
55 { WAVE_FORMAT_MULAW, "Microsoft U-law" },
56 { WAVE_FORMAT_OKI_ADPCM,"OKI ADPCM" },
57 { WAVE_FORMAT_DIGISTD, "Digistd format" },
58 { WAVE_FORMAT_DIGIFIX, "Digifix format" },
59 { -1, "?Unknown?" },
60 };
61
62 char *
63 wav_enc_from_val(encoding)
64 int encoding;
65 {
66 int i;
67
68 for (i = 0; wavencs[i].wenc != -1; i++)
69 if (wavencs[i].wenc == encoding)
70 break;
71 return (wavencs[i].wname);
72 }
73
74 /*
75 * sample header is:
76 *
77 * RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
78 *
79 */
80 /*
81 * WAV format helpers
82 */
83 /*
84 * find a .wav header, etc. returns header length on success
85 */
86 ssize_t
87 audio_wav_parse_hdr(hdr, sz, enc, prec, sample, channels, datasize)
88 void *hdr;
89 size_t sz;
90 int *enc;
91 int *prec;
92 int *sample;
93 int *channels;
94 size_t *datasize;
95 {
96 char *where = hdr, *owhere;
97 wav_audioheaderpart part;
98 wav_audioheaderfmt fmt;
99 char *end = (((char *)hdr) + sz);
100 int newenc, newprec;
101
102 if (sz < 32)
103 return (AUDIO_ENOENT);
104
105 if (strncmp(where, "RIFF", 4))
106 return (AUDIO_ENOENT);
107 where += 8;
108 if (strncmp(where, "WAVE", 4))
109 return (AUDIO_ENOENT);
110 where += 4;
111
112 do {
113 memcpy(&part, where, sizeof part);
114 owhere = where;
115 where += getle32(part.len) + 8;
116 } while (where < end && strncmp(part.name, "fmt ", 4));
117
118 /* too short ? */
119 if (where + sizeof fmt > end)
120 return (AUDIO_ESHORTHDR);
121
122 memcpy(&fmt, (owhere + 8), sizeof fmt);
123
124 #if 0
125 printf("fmt header is:\n\t%d\ttag\n\t%d\tchannels\n\t%d\tsample rate\n\t%d\tavg_bps\n\t%d\talignment\n\t%d\tbits per sample\n", getle16(fmt.tag), getle16(fmt.channels), getle32(fmt.sample_rate), getle32(fmt.avg_bps), getle16(fmt.alignment), getle16(fmt.bits_per_sample));
126 #endif
127
128 switch (getle16(fmt.tag)) {
129 case WAVE_FORMAT_UNKNOWN:
130 case WAVE_FORMAT_ADPCM:
131 case WAVE_FORMAT_OKI_ADPCM:
132 case WAVE_FORMAT_DIGISTD:
133 case WAVE_FORMAT_DIGIFIX:
134 case IBM_FORMAT_MULAW:
135 case IBM_FORMAT_ALAW:
136 case IBM_FORMAT_ADPCM:
137 default:
138 return (AUDIO_EWAVUNSUPP);
139
140 case WAVE_FORMAT_PCM:
141 switch (getle16(fmt.bits_per_sample)) {
142 case 8:
143 newprec = 8;
144 break;
145 case 16:
146 newprec = 16;
147 break;
148 case 24:
149 newprec = 24;
150 break;
151 case 32:
152 newprec = 32;
153 break;
154 default:
155 return (AUDIO_EWAVBADPCM);
156 }
157 if (newprec == 8)
158 newenc = AUDIO_ENCODING_ULINEAR_LE;
159 else
160 newenc = AUDIO_ENCODING_SLINEAR_LE;
161 break;
162 case WAVE_FORMAT_ALAW:
163 newenc = AUDIO_ENCODING_ALAW;
164 newprec = 8;
165 break;
166 case WAVE_FORMAT_MULAW:
167 newenc = AUDIO_ENCODING_ULAW;
168 newprec = 8;
169 break;
170 }
171
172 do {
173 memcpy(&part, where, sizeof part);
174 #if 0
175 printf("part `%c%c%c%c' len = %d\n", part.name[0], part.name[1], part.name[2], part.name[3], getle32(part.len));
176 #endif
177 owhere = where;
178 where += (getle32(part.len) + 8);
179 } while ((char *)where < end && strncmp(part.name, "data", 4));
180
181 if ((where - getle32(part.len)) <= end) {
182 if (channels)
183 *channels = getle16(fmt.channels);
184 if (sample)
185 *sample = getle32(fmt.sample_rate);
186 if (enc)
187 *enc = newenc;
188 if (prec)
189 *prec = newprec;
190 if (datasize)
191 *datasize = (size_t)getle32(part.len);
192 return (owhere - (char *)hdr + 8);
193 }
194 return (AUDIO_EWAVNODATA);
195 }
196