2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)audiosize.c 1.19 04/03/01 Copyright 1998-2004 J. Schilling */
15 * Copyright (c) 1998-2004 J. Schilling
17 * First .vaw implementation made by Dave Platt <dplatt@iq.nc.com>
18 * Current .wav implementation with additional help from Heiko Eißfeld.
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2
23 * as published by the Free Software Foundation.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License along with
31 * this program; see the file COPYING. If not, write to the Free Software
32 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 #include <usal/usalcmd.h>
56 #define SUN_AU_MAGIC ".snd"
57 #define SUN_AU_UNKNOWN_LEN ((Uint)~0)
58 #define SUN_AU_ULAW8 1 /* American ISDN Telephonie */
59 #define SUN_AU_LINEAR8 2 /* Linear PCM 8 bit/channel */
60 #define SUN_AU_LINEAR16 3 /* Linear PCM 16 bit/channel */
61 #define SUN_AU_LINEAR24 4 /* Linear PCM 24 bit/channel */
62 #define SUN_AU_LINEAR32 5 /* Linear PCM 32 bit/channel */
63 #define SUN_AU_FLOAT 6 /* 32 bit IEEE floatingpoint */
64 #define SUN_AU_DOUBLE 7 /* 64 bit IEEE floatingpoint */
65 #define SUN_AU_G721 23 /* 4 bit CCITT G.721 ADPCM */
66 #define SUN_AU_G722 24 /* CCITT G.722 ADPCM */
67 #define SUN_AU_G723_3 25 /* 3 bit CCITT G.723 ADPCM */
68 #define SUN_AU_G723_5 26 /* 5 bit CCITT G.723 ADPCM */
69 #define SUN_AU_ALAW8 27 /* International ISDN Tel. */
84 Uchar av_byte_rate[4];
86 Uchar bits_per_sample[2];
89 #define WAV_RIFF_MAGIC "RIFF" /* Magic for file format */
90 #define WAV_WAVE_MAGIC "WAVE" /* Magic for Waveform Audio */
91 #define WAV_FMT_MAGIC "fmt " /* Start of Waveform format */
92 #define WAV_DATA_MAGIC "data" /* Start of data chunk */
93 #define WAV_FORMAT_PCM 0x0001 /* Linear PCM format */
94 #define WAV_FORMAT_ULAW 0x0101 /* American ISDN Telephonie */
95 #define WAV_FORMAT_ALAW 0x0102 /* International ISDN Tel. */
96 #define WAV_FORMAT_ADPCM 0x0103 /* ADPCM format */
98 #define le_a_to_u_short(a) ((unsigned short) \
99 ((((unsigned char *)a)[0] & 0xFF) | \
100 (((unsigned char *)a)[1] << 8 & 0xFF00)))
103 #define le_a_to_u_long(a) ((unsigned long) \
104 ((((unsigned char *)a)[0] & 0xFF) | \
105 (((unsigned char *)a)[1] << 8 & 0xFF00) | \
106 (((unsigned char *)a)[2] << 16 & 0xFF0000) | \
107 (((unsigned char *)a)[3] << 24 & 0xFF000000UL)))
109 #define le_a_to_u_long(a) ((unsigned long) \
110 ((((unsigned char *)a)[0] & 0xFF) | \
111 (((unsigned char *)a)[1] << 8 & 0xFF00) | \
112 (((unsigned char *)a)[2] << 16 & 0xFF0000) | \
113 (((unsigned char *)a)[3] << 24 & 0xFF000000)))
116 BOOL is_auname(const char *name);
118 BOOL is_wavname(const char *name);
119 off_t wavsize(int f);
122 is_auname(const char *name)
126 if ((p = strrchr(name, '.')) == NULL)
128 return (streql(p, ".au"));
132 * Read Sun audio header, leave file seek pointer past auheader.
142 long ret = AU_BAD_HEADER;
145 * First check if a bad guy tries to call ausize()
146 * with an unappropriate file descriptor.
147 * return -1 in this case.
151 if (fstat(f, &sb) < 0)
153 mode = sb.st_mode & S_IFMT;
154 if (!S_ISREG(mode) && !S_ISBLK(mode) && !S_ISCHR(mode))
157 if (read(f, &hdr, sizeof (hdr)) != sizeof (hdr))
160 if (strncmp((char *)hdr.magic, SUN_AU_MAGIC, 4) != 0)
165 val = a_to_u_4_byte(hdr.encoding);
166 if (val != SUN_AU_LINEAR16)
169 val = a_to_u_4_byte(hdr.channels);
173 val = a_to_u_4_byte(hdr.sample_rate);
177 size = (off_t)a_to_u_4_byte(hdr.hdr_size);
178 if (size < (off_t)sizeof (hdr) || size > 512)
180 lseek(f, size, SEEK_SET);
183 * Most .au files don't seem to honor the data_size field,
184 * so we use the whole file size without the header.
186 size = sb.st_size - size;
190 lseek(f, (off_t)0L, SEEK_SET);
195 is_wavname(const char *name)
199 if ((p = strrchr(name, '.')) == NULL)
201 return (streql(p, ".wav") || streql(p, ".WAV"));
205 * Read WAV header, leave file seek pointer past WAV header.
218 long ret = AU_BAD_HEADER;
221 * First check if a bad guy tries to call wavsize()
222 * with an unappropriate file descriptor.
223 * return -1 in this case.
228 if (fstat(f, &sb) < 0)
230 mode = sb.st_mode & S_IFMT;
231 if (!S_ISREG(mode) && !S_ISBLK(mode) && !S_ISCHR(mode))
238 if (read(f, &chunk, sizeof (chunk)) != sizeof (chunk))
240 size = (off_t)le_a_to_u_long(chunk.cksize);
242 if (strncmp((char *)chunk.ckid, WAV_RIFF_MAGIC, 4) == 0) {
244 * We found (first) RIFF header. Check if a WAVE
245 * magic follows. Set up size to be able to skip
248 if (read(f, &riff, sizeof (riff)) != sizeof (riff))
250 if (strncmp((char *)riff.wave, WAV_WAVE_MAGIC, 4) != 0)
252 size = (off_t)sizeof (riff);
254 } else if (strncmp((char *)chunk.ckid, WAV_FMT_MAGIC, 4) == 0) {
256 * We found WAVE "fmt " header. Check size (if it is
257 * valid for a WAVE file) and coding whether it is
260 if (size < (off_t)sizeof (fmt)) goto err;
261 if (sizeof (fmt) != read(f, &fmt, sizeof (fmt))) goto err;
262 if (le_a_to_u_short(fmt.channels) != 2 ||
263 le_a_to_u_long(fmt.sample_rate) != 44100 ||
264 le_a_to_u_short(fmt.bits_per_sample) != 16) {
270 } else if (strncmp((char *)chunk.ckid, WAV_DATA_MAGIC, 4) == 0) {
272 * We found WAVE "data" header. This contains the
273 * size value of the audio part.
279 if ((cursor + size + sizeof (chunk)) > sb.st_size)
280 size = sb.st_size - (cursor + sizeof (chunk));
283 cursor += size + sizeof (chunk);
284 lseek(f, cursor, SEEK_SET); /* Skip over current chunk */
287 lseek(f, (off_t)0L, SEEK_SET);