1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * This file contains a set of Very Paranoid routines to convert
29*7c478bd9Sstevel@tonic-gate * audio file headers to in-core audio headers and vice versa.
30*7c478bd9Sstevel@tonic-gate *
31*7c478bd9Sstevel@tonic-gate * They are robust enough to handle any random file input without
32*7c478bd9Sstevel@tonic-gate * crashing miserably. Of course, bad audio headers coming from
33*7c478bd9Sstevel@tonic-gate * the calling program can cause significant problems.
34*7c478bd9Sstevel@tonic-gate */
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <memory.h>
38*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h> /* needed for large file error checking */
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
44*7c478bd9Sstevel@tonic-gate #include <libintl.h>
45*7c478bd9Sstevel@tonic-gate #include <math.h>
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate #include <libaudio_impl.h> /* include other audio hdr's */
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate /* Round up to a double boundary */
50*7c478bd9Sstevel@tonic-gate #define ROUND_DBL(x) (((x) + 7) & ~7)
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate #define HEADER_BUFFER 100
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate #define _MGET_(str) (char *)dgettext(TEXT_DOMAIN, str)
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate static int audio_encode_aiff(Audio_hdr *, unsigned char *, unsigned int *);
57*7c478bd9Sstevel@tonic-gate static int audio_encode_au(Audio_hdr *, char *, unsigned int,
58*7c478bd9Sstevel@tonic-gate unsigned char *, unsigned int *);
59*7c478bd9Sstevel@tonic-gate static int audio_encode_wav(Audio_hdr *, unsigned char *, unsigned int *);
60*7c478bd9Sstevel@tonic-gate static double convert_from_ieee_extended(unsigned char *);
61*7c478bd9Sstevel@tonic-gate static void convert_to_ieee_extended(double, unsigned char *);
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate * Write an audio file header to an output stream.
65*7c478bd9Sstevel@tonic-gate *
66*7c478bd9Sstevel@tonic-gate * The file header is encoded from the supplied Audio_hdr structure.
67*7c478bd9Sstevel@tonic-gate * If 'infop' is not NULL, it is the address of a buffer containing 'info'
68*7c478bd9Sstevel@tonic-gate * data. 'ilen' specifies the size of this buffer.
69*7c478bd9Sstevel@tonic-gate * The entire file header will be zero-padded to a double-word boundary.
70*7c478bd9Sstevel@tonic-gate *
71*7c478bd9Sstevel@tonic-gate * Note that the file header is stored on-disk in big-endian format,
72*7c478bd9Sstevel@tonic-gate * regardless of the machine type.
73*7c478bd9Sstevel@tonic-gate *
74*7c478bd9Sstevel@tonic-gate * Note also that the output file descriptor must not have been set up
75*7c478bd9Sstevel@tonic-gate * non-blocking i/o. If non-blocking behavior is desired, set this
76*7c478bd9Sstevel@tonic-gate * flag after writing the file header.
77*7c478bd9Sstevel@tonic-gate */
78*7c478bd9Sstevel@tonic-gate int
audio_write_filehdr(int fd,Audio_hdr * hdrp,int file_type,char * infop,unsigned int ilen)79*7c478bd9Sstevel@tonic-gate audio_write_filehdr(int fd, Audio_hdr *hdrp, int file_type, char *infop,
80*7c478bd9Sstevel@tonic-gate unsigned int ilen)
81*7c478bd9Sstevel@tonic-gate /* file descriptor */
82*7c478bd9Sstevel@tonic-gate /* audio header */
83*7c478bd9Sstevel@tonic-gate /* audio header type */
84*7c478bd9Sstevel@tonic-gate /* info buffer pointer */
85*7c478bd9Sstevel@tonic-gate /* buffer size */
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate int err;
88*7c478bd9Sstevel@tonic-gate unsigned blen;
89*7c478bd9Sstevel@tonic-gate unsigned char *buf; /* temporary buffer */
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate /* create tmp buf for the encoding routines to work with */
92*7c478bd9Sstevel@tonic-gate blen = HEADER_BUFFER + (infop ? ilen : 0) + 4;
93*7c478bd9Sstevel@tonic-gate blen = ROUND_DBL(blen);
94*7c478bd9Sstevel@tonic-gate
95*7c478bd9Sstevel@tonic-gate if (!(buf = (unsigned char *)calloc(1, blen))) {
96*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate switch (file_type) {
100*7c478bd9Sstevel@tonic-gate case FILE_AU:
101*7c478bd9Sstevel@tonic-gate err = audio_encode_au(hdrp, infop, ilen, buf, &blen);
102*7c478bd9Sstevel@tonic-gate break;
103*7c478bd9Sstevel@tonic-gate case FILE_WAV:
104*7c478bd9Sstevel@tonic-gate err = audio_encode_wav(hdrp, buf, &blen);
105*7c478bd9Sstevel@tonic-gate break;
106*7c478bd9Sstevel@tonic-gate case FILE_AIFF:
107*7c478bd9Sstevel@tonic-gate err = audio_encode_aiff(hdrp, buf, &blen);
108*7c478bd9Sstevel@tonic-gate break;
109*7c478bd9Sstevel@tonic-gate default:
110*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILETYPE);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
114*7c478bd9Sstevel@tonic-gate return (err);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate
117*7c478bd9Sstevel@tonic-gate /* Write and free the holding buffer */
118*7c478bd9Sstevel@tonic-gate err = write(fd, (char *)buf, (int)blen);
119*7c478bd9Sstevel@tonic-gate (void) free((char *)buf);
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate if (err != blen)
122*7c478bd9Sstevel@tonic-gate return ((err < 0) ? AUDIO_UNIXERROR : AUDIO_ERR_BADFILEHDR);
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate * Rewrite the aiff header chunk length and the data chunk length fields.
130*7c478bd9Sstevel@tonic-gate */
131*7c478bd9Sstevel@tonic-gate static int
audio_rewrite_aiff_filesize(int fd,unsigned int size,unsigned int channels,unsigned int bytes_per_sample)132*7c478bd9Sstevel@tonic-gate audio_rewrite_aiff_filesize(int fd, unsigned int size, unsigned int channels,
133*7c478bd9Sstevel@tonic-gate unsigned int bytes_per_sample)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate unsigned int offset;
136*7c478bd9Sstevel@tonic-gate unsigned int tmp_uint;
137*7c478bd9Sstevel@tonic-gate unsigned int tmp_uint2;
138*7c478bd9Sstevel@tonic-gate unsigned int total_size;
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate /* first fix aiff_hdr_size */
141*7c478bd9Sstevel@tonic-gate total_size = size + sizeof (aiff_hdr_chunk_t) +
142*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_COMM_CHUNK_SIZE + sizeof (aiff_ssnd_chunk_t);
143*7c478bd9Sstevel@tonic-gate tmp_uint = total_size - (2 * sizeof (int));
144*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &tmp_uint2);
145*7c478bd9Sstevel@tonic-gate offset = sizeof (int);
146*7c478bd9Sstevel@tonic-gate if (lseek(fd, offset, SEEK_SET) < 0) {
147*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate if (write(fd, &tmp_uint2, sizeof (tmp_uint2)) != sizeof (tmp_uint2)) {
150*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate
153*7c478bd9Sstevel@tonic-gate /* fix the frame count */
154*7c478bd9Sstevel@tonic-gate tmp_uint = size / channels / bytes_per_sample;
155*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &tmp_uint2);
156*7c478bd9Sstevel@tonic-gate offset = sizeof (aiff_hdr_chunk_t) + (2 * sizeof (int)) +
157*7c478bd9Sstevel@tonic-gate sizeof (short);
158*7c478bd9Sstevel@tonic-gate if (lseek(fd, offset, SEEK_SET) < 0) {
159*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate if (write(fd, &tmp_uint2, sizeof (tmp_uint2)) != sizeof (tmp_uint2)) {
162*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate /* fix the data size */
166*7c478bd9Sstevel@tonic-gate tmp_uint = size + sizeof (aiff_ssnd_chunk_t) - (2 * sizeof (int));
167*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &tmp_uint2);
168*7c478bd9Sstevel@tonic-gate offset = sizeof (aiff_hdr_chunk_t) + AUDIO_AIFF_COMM_CHUNK_SIZE +
169*7c478bd9Sstevel@tonic-gate sizeof (int);
170*7c478bd9Sstevel@tonic-gate if (lseek(fd, offset, SEEK_SET) < 0) {
171*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate if (write(fd, &tmp_uint2, sizeof (tmp_uint2)) != sizeof (tmp_uint2)) {
174*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
178*7c478bd9Sstevel@tonic-gate
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate * Rewrite the data size field for the .au file format. Rewrite the audio
183*7c478bd9Sstevel@tonic-gate * file header au_data_size field with the supplied value. Otherwise,
184*7c478bd9Sstevel@tonic-gate * return AUDIO_ERR_NOEFFECT.
185*7c478bd9Sstevel@tonic-gate */
186*7c478bd9Sstevel@tonic-gate static int
audio_rewrite_au_filesize(int fd,unsigned int size)187*7c478bd9Sstevel@tonic-gate audio_rewrite_au_filesize(int fd, unsigned int size)
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate au_filehdr_t fhdr;
190*7c478bd9Sstevel@tonic-gate int err;
191*7c478bd9Sstevel@tonic-gate int data;
192*7c478bd9Sstevel@tonic-gate int offset;
193*7c478bd9Sstevel@tonic-gate
194*7c478bd9Sstevel@tonic-gate /* seek to the position of the au_data_size member */
195*7c478bd9Sstevel@tonic-gate offset = (char *)&fhdr.au_data_size - (char *)&fhdr;
196*7c478bd9Sstevel@tonic-gate if (lseek(fd, offset, SEEK_SET) < 0) {
197*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate
200*7c478bd9Sstevel@tonic-gate /* Encode the 32-bit integer header field */
201*7c478bd9Sstevel@tonic-gate AUDIO_AU_HOST2FILE(&size, &data);
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate /* Write the data */
204*7c478bd9Sstevel@tonic-gate err = write(fd, (char *)&data, sizeof (fhdr.au_data_size));
205*7c478bd9Sstevel@tonic-gate if (err != sizeof (fhdr.au_data_size))
206*7c478bd9Sstevel@tonic-gate return ((err < 0) ? AUDIO_UNIXERROR : AUDIO_ERR_BADFILEHDR);
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate /*
213*7c478bd9Sstevel@tonic-gate * Rewrite the riff header chunk length and the data chunk length fields.
214*7c478bd9Sstevel@tonic-gate */
215*7c478bd9Sstevel@tonic-gate static int
audio_rewrite_wav_filesize(int fd,unsigned int size)216*7c478bd9Sstevel@tonic-gate audio_rewrite_wav_filesize(int fd, unsigned int size)
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate wav_filehdr_t fhdr;
219*7c478bd9Sstevel@tonic-gate int calc_size;
220*7c478bd9Sstevel@tonic-gate int err;
221*7c478bd9Sstevel@tonic-gate int data;
222*7c478bd9Sstevel@tonic-gate int offset;
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate /* seek to the position of the riff header chunk length */
225*7c478bd9Sstevel@tonic-gate calc_size = size + sizeof (fhdr) - sizeof (fhdr.wav_riff_ID) -
226*7c478bd9Sstevel@tonic-gate sizeof (fhdr.wav_riff_size);
227*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&calc_size, &data);
228*7c478bd9Sstevel@tonic-gate offset = (char *)&fhdr.wav_riff_size - (char *)&fhdr;
229*7c478bd9Sstevel@tonic-gate if (lseek(fd, offset, SEEK_SET) < 0) {
230*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate
233*7c478bd9Sstevel@tonic-gate /* Write the data */
234*7c478bd9Sstevel@tonic-gate err = write(fd, (char *)&data, sizeof (fhdr.wav_riff_size));
235*7c478bd9Sstevel@tonic-gate if (err != sizeof (fhdr.wav_riff_size))
236*7c478bd9Sstevel@tonic-gate return ((err < 0) ? AUDIO_UNIXERROR : AUDIO_ERR_BADFILEHDR);
237*7c478bd9Sstevel@tonic-gate
238*7c478bd9Sstevel@tonic-gate /* now seek to the position of the data chunk length */
239*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&size, &data);
240*7c478bd9Sstevel@tonic-gate offset = (char *)&fhdr.wav_data_size - (char *)&fhdr;
241*7c478bd9Sstevel@tonic-gate if (lseek(fd, offset, SEEK_SET) < 0) {
242*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate /* Write the data */
246*7c478bd9Sstevel@tonic-gate err = write(fd, (char *)&data, sizeof (fhdr.wav_data_size));
247*7c478bd9Sstevel@tonic-gate if (err != sizeof (fhdr.wav_data_size))
248*7c478bd9Sstevel@tonic-gate return ((err < 0) ? AUDIO_UNIXERROR : AUDIO_ERR_BADFILEHDR);
249*7c478bd9Sstevel@tonic-gate
250*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
251*7c478bd9Sstevel@tonic-gate
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate
254*7c478bd9Sstevel@tonic-gate /*
255*7c478bd9Sstevel@tonic-gate * Rewrite the data size field of an audio header to the output stream if
256*7c478bd9Sstevel@tonic-gate * the output file is capable of seeking.
257*7c478bd9Sstevel@tonic-gate */
258*7c478bd9Sstevel@tonic-gate int
audio_rewrite_filesize(int fd,int file_type,unsigned int size,unsigned int channels,unsigned int bytes_per_sample)259*7c478bd9Sstevel@tonic-gate audio_rewrite_filesize(int fd, int file_type, unsigned int size,
260*7c478bd9Sstevel@tonic-gate unsigned int channels, unsigned int bytes_per_sample)
261*7c478bd9Sstevel@tonic-gate /* file descriptor */
262*7c478bd9Sstevel@tonic-gate /* audio file type */
263*7c478bd9Sstevel@tonic-gate /* new data size */
264*7c478bd9Sstevel@tonic-gate /* number of channels */
265*7c478bd9Sstevel@tonic-gate /* number of bytes per sample */
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate int fcntl_err;
268*7c478bd9Sstevel@tonic-gate
269*7c478bd9Sstevel@tonic-gate /* Can we seek back in this file and write without appending? */
270*7c478bd9Sstevel@tonic-gate fcntl_err = fcntl(fd, F_GETFL, 0);
271*7c478bd9Sstevel@tonic-gate if ((fcntl_err < 0) && ((errno == EOVERFLOW) || (errno == EINVAL))) {
272*7c478bd9Sstevel@tonic-gate /* Large file encountered (probably) */
273*7c478bd9Sstevel@tonic-gate perror("fcntl");
274*7c478bd9Sstevel@tonic-gate exit(1);
275*7c478bd9Sstevel@tonic-gate } else if ((lseek(fd, (off_t)0, SEEK_SET) < 0) ||
276*7c478bd9Sstevel@tonic-gate (fcntl_err & FAPPEND)) {
277*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT);
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate
280*7c478bd9Sstevel@tonic-gate switch (file_type) {
281*7c478bd9Sstevel@tonic-gate case FILE_AU:
282*7c478bd9Sstevel@tonic-gate return (audio_rewrite_au_filesize(fd, size));
283*7c478bd9Sstevel@tonic-gate case FILE_WAV:
284*7c478bd9Sstevel@tonic-gate return (audio_rewrite_wav_filesize(fd, size));
285*7c478bd9Sstevel@tonic-gate case FILE_AIFF:
286*7c478bd9Sstevel@tonic-gate return (audio_rewrite_aiff_filesize(fd, size, channels,
287*7c478bd9Sstevel@tonic-gate bytes_per_sample));
288*7c478bd9Sstevel@tonic-gate default:
289*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILETYPE);
290*7c478bd9Sstevel@tonic-gate }
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate
293*7c478bd9Sstevel@tonic-gate
294*7c478bd9Sstevel@tonic-gate /*
295*7c478bd9Sstevel@tonic-gate * Decode an audio file header from an input stream.
296*7c478bd9Sstevel@tonic-gate *
297*7c478bd9Sstevel@tonic-gate * The file header is decoded into the supplied Audio_hdr structure, regardless
298*7c478bd9Sstevel@tonic-gate * of the file format. Thus .wav and .aiff files look like .au files once the
299*7c478bd9Sstevel@tonic-gate * header is decoded.
300*7c478bd9Sstevel@tonic-gate *
301*7c478bd9Sstevel@tonic-gate * If 'infop' is not NULL, it is the address of a buffer to which the
302*7c478bd9Sstevel@tonic-gate * 'info' portion of the file header will be copied. 'ilen' specifies
303*7c478bd9Sstevel@tonic-gate * the maximum number of bytes to copy. The buffer will be NULL-terminated,
304*7c478bd9Sstevel@tonic-gate * even if it means over-writing the last byte.
305*7c478bd9Sstevel@tonic-gate *
306*7c478bd9Sstevel@tonic-gate * Note that the .au file header is stored on-disk in big-endian format,
307*7c478bd9Sstevel@tonic-gate * regardless of the machine type. This may not have been true if
308*7c478bd9Sstevel@tonic-gate * the file was written on a non-Sun machine. For now, such
309*7c478bd9Sstevel@tonic-gate * files will appear invalid.
310*7c478bd9Sstevel@tonic-gate *
311*7c478bd9Sstevel@tonic-gate * Note also that the input file descriptor must not have been set up
312*7c478bd9Sstevel@tonic-gate * non-blocking i/o. If non-blocking behavior is desired, set this
313*7c478bd9Sstevel@tonic-gate * flag after reading the file header.
314*7c478bd9Sstevel@tonic-gate */
315*7c478bd9Sstevel@tonic-gate int
audio_read_filehdr(int fd,Audio_hdr * hdrp,int * file_type,char * infop,unsigned int ilen)316*7c478bd9Sstevel@tonic-gate audio_read_filehdr(int fd, Audio_hdr *hdrp, int *file_type, char *infop,
317*7c478bd9Sstevel@tonic-gate unsigned int ilen)
318*7c478bd9Sstevel@tonic-gate /* input file descriptor */
319*7c478bd9Sstevel@tonic-gate /* output audio header */
320*7c478bd9Sstevel@tonic-gate /* audio file type */
321*7c478bd9Sstevel@tonic-gate /* info buffer pointer */
322*7c478bd9Sstevel@tonic-gate /* buffer size */
323*7c478bd9Sstevel@tonic-gate {
324*7c478bd9Sstevel@tonic-gate int err;
325*7c478bd9Sstevel@tonic-gate int dsize;
326*7c478bd9Sstevel@tonic-gate int isize;
327*7c478bd9Sstevel@tonic-gate unsigned resid;
328*7c478bd9Sstevel@tonic-gate unsigned char buf[HEADER_BUFFER];
329*7c478bd9Sstevel@tonic-gate struct stat st;
330*7c478bd9Sstevel@tonic-gate
331*7c478bd9Sstevel@tonic-gate /* decode the file header and fill in the hdrp structure */
332*7c478bd9Sstevel@tonic-gate if ((err = audio_decode_filehdr(fd, buf, file_type, hdrp, &isize)) !=
333*7c478bd9Sstevel@tonic-gate AUDIO_SUCCESS) {
334*7c478bd9Sstevel@tonic-gate goto checkerror;
335*7c478bd9Sstevel@tonic-gate }
336*7c478bd9Sstevel@tonic-gate
337*7c478bd9Sstevel@tonic-gate /* Stat the file, to determine if it is a regular file. */
338*7c478bd9Sstevel@tonic-gate err = fstat(fd, &st);
339*7c478bd9Sstevel@tonic-gate if (err < 0) {
340*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate
343*7c478bd9Sstevel@tonic-gate /*
344*7c478bd9Sstevel@tonic-gate * If au_data_size is not indeterminate (i.e., this isn't a pipe),
345*7c478bd9Sstevel@tonic-gate * try to validate the au_offset and au_data_size.
346*7c478bd9Sstevel@tonic-gate */
347*7c478bd9Sstevel@tonic-gate if (*file_type == FILE_AU && hdrp->data_size != AUDIO_UNKNOWN_SIZE) {
348*7c478bd9Sstevel@tonic-gate /* Only trust the size for regular files */
349*7c478bd9Sstevel@tonic-gate if (S_ISREG(st.st_mode)) {
350*7c478bd9Sstevel@tonic-gate dsize = isize + hdrp->data_size + sizeof (au_filehdr_t);
351*7c478bd9Sstevel@tonic-gate if (st.st_size < dsize) {
352*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
353*7c478bd9Sstevel@tonic-gate _MGET_("Warning: More audio data "
354*7c478bd9Sstevel@tonic-gate "than the file header specifies\n"));
355*7c478bd9Sstevel@tonic-gate } else if (st.st_size > dsize) {
356*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
357*7c478bd9Sstevel@tonic-gate _MGET_("Warning: Less audio data "
358*7c478bd9Sstevel@tonic-gate "than the file header specifies\n"));
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate
363*7c478bd9Sstevel@tonic-gate resid = isize;
364*7c478bd9Sstevel@tonic-gate /*
365*7c478bd9Sstevel@tonic-gate * Deal with extra header data.
366*7c478bd9Sstevel@tonic-gate */
367*7c478bd9Sstevel@tonic-gate if ((infop != NULL) && (ilen != 0)) {
368*7c478bd9Sstevel@tonic-gate /*
369*7c478bd9Sstevel@tonic-gate * If infop is non-NULL, try to read in the info data
370*7c478bd9Sstevel@tonic-gate */
371*7c478bd9Sstevel@tonic-gate if (isize > ilen)
372*7c478bd9Sstevel@tonic-gate isize = ilen;
373*7c478bd9Sstevel@tonic-gate err = read(fd, infop, (int)isize);
374*7c478bd9Sstevel@tonic-gate if (err != isize)
375*7c478bd9Sstevel@tonic-gate goto checkerror;
376*7c478bd9Sstevel@tonic-gate
377*7c478bd9Sstevel@tonic-gate /* Zero any residual bytes in the text buffer */
378*7c478bd9Sstevel@tonic-gate if (isize < ilen)
379*7c478bd9Sstevel@tonic-gate (void) memset(&infop[isize], '\0',
380*7c478bd9Sstevel@tonic-gate (int)(ilen - isize));
381*7c478bd9Sstevel@tonic-gate else
382*7c478bd9Sstevel@tonic-gate infop[ilen - 1] = '\0'; /* zero-terminate */
383*7c478bd9Sstevel@tonic-gate
384*7c478bd9Sstevel@tonic-gate resid -= err; /* subtract the amount read */
385*7c478bd9Sstevel@tonic-gate }
386*7c478bd9Sstevel@tonic-gate
387*7c478bd9Sstevel@tonic-gate /*
388*7c478bd9Sstevel@tonic-gate * If we truncated the info, seek or read data until info size
389*7c478bd9Sstevel@tonic-gate * is satisfied. If regular file, seek nearly to end and check
390*7c478bd9Sstevel@tonic-gate * for eof.
391*7c478bd9Sstevel@tonic-gate */
392*7c478bd9Sstevel@tonic-gate if (resid != 0) {
393*7c478bd9Sstevel@tonic-gate if (S_ISREG(st.st_mode)) {
394*7c478bd9Sstevel@tonic-gate err = lseek(fd, (off_t)(resid - 1), SEEK_CUR);
395*7c478bd9Sstevel@tonic-gate if ((err < 0) ||
396*7c478bd9Sstevel@tonic-gate ((err = read(fd, (char *)buf, 1)) != 1))
397*7c478bd9Sstevel@tonic-gate goto checkerror;
398*7c478bd9Sstevel@tonic-gate } else while (resid != 0) {
399*7c478bd9Sstevel@tonic-gate char junk[8192]; /* temporary buffer */
400*7c478bd9Sstevel@tonic-gate
401*7c478bd9Sstevel@tonic-gate isize = (resid > sizeof (junk)) ?
402*7c478bd9Sstevel@tonic-gate sizeof (junk) : resid;
403*7c478bd9Sstevel@tonic-gate err = read(fd, junk, isize);
404*7c478bd9Sstevel@tonic-gate if (err != isize)
405*7c478bd9Sstevel@tonic-gate goto checkerror;
406*7c478bd9Sstevel@tonic-gate resid -= err;
407*7c478bd9Sstevel@tonic-gate }
408*7c478bd9Sstevel@tonic-gate }
409*7c478bd9Sstevel@tonic-gate
410*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
411*7c478bd9Sstevel@tonic-gate
412*7c478bd9Sstevel@tonic-gate checkerror:
413*7c478bd9Sstevel@tonic-gate if ((err < 0) && (errno == EOVERFLOW)) {
414*7c478bd9Sstevel@tonic-gate perror("read");
415*7c478bd9Sstevel@tonic-gate exit(1);
416*7c478bd9Sstevel@tonic-gate } else {
417*7c478bd9Sstevel@tonic-gate return ((err < 0) ? AUDIO_UNIXERROR : AUDIO_ERR_BADFILEHDR);
418*7c478bd9Sstevel@tonic-gate }
419*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate
422*7c478bd9Sstevel@tonic-gate /*
423*7c478bd9Sstevel@tonic-gate * Return TRUE if the named file is an audio file. Else, return FALSE.
424*7c478bd9Sstevel@tonic-gate */
425*7c478bd9Sstevel@tonic-gate int
audio_isaudiofile(char * name)426*7c478bd9Sstevel@tonic-gate audio_isaudiofile(char *name)
427*7c478bd9Sstevel@tonic-gate {
428*7c478bd9Sstevel@tonic-gate int fd;
429*7c478bd9Sstevel@tonic-gate int err;
430*7c478bd9Sstevel@tonic-gate int file_type; /* ignored */
431*7c478bd9Sstevel@tonic-gate int isize;
432*7c478bd9Sstevel@tonic-gate Audio_hdr hdr;
433*7c478bd9Sstevel@tonic-gate unsigned char buf[sizeof (au_filehdr_t)];
434*7c478bd9Sstevel@tonic-gate
435*7c478bd9Sstevel@tonic-gate /* Open the file (set O_NONBLOCK in case the name refers to a device) */
436*7c478bd9Sstevel@tonic-gate fd = open(name, O_RDONLY | O_NONBLOCK);
437*7c478bd9Sstevel@tonic-gate if (fd < 0) {
438*7c478bd9Sstevel@tonic-gate if (errno == EOVERFLOW) {
439*7c478bd9Sstevel@tonic-gate perror("open");
440*7c478bd9Sstevel@tonic-gate exit(1);
441*7c478bd9Sstevel@tonic-gate } else {
442*7c478bd9Sstevel@tonic-gate return (FALSE);
443*7c478bd9Sstevel@tonic-gate }
444*7c478bd9Sstevel@tonic-gate }
445*7c478bd9Sstevel@tonic-gate
446*7c478bd9Sstevel@tonic-gate /* Read the header (but not the text info). */
447*7c478bd9Sstevel@tonic-gate err = read(fd, (char *)buf, sizeof (buf));
448*7c478bd9Sstevel@tonic-gate if (err < 0) {
449*7c478bd9Sstevel@tonic-gate if (errno == EOVERFLOW) {
450*7c478bd9Sstevel@tonic-gate perror("open");
451*7c478bd9Sstevel@tonic-gate exit(1);
452*7c478bd9Sstevel@tonic-gate } else {
453*7c478bd9Sstevel@tonic-gate return (FALSE);
454*7c478bd9Sstevel@tonic-gate }
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate (void) close(fd);
457*7c478bd9Sstevel@tonic-gate
458*7c478bd9Sstevel@tonic-gate if ((err == sizeof (buf)) &&
459*7c478bd9Sstevel@tonic-gate (audio_decode_filehdr(fd, buf, &file_type, &hdr, &isize) ==
460*7c478bd9Sstevel@tonic-gate AUDIO_SUCCESS)) {
461*7c478bd9Sstevel@tonic-gate return (hdr.encoding);
462*7c478bd9Sstevel@tonic-gate } else {
463*7c478bd9Sstevel@tonic-gate return (FALSE);
464*7c478bd9Sstevel@tonic-gate }
465*7c478bd9Sstevel@tonic-gate }
466*7c478bd9Sstevel@tonic-gate
467*7c478bd9Sstevel@tonic-gate /*
468*7c478bd9Sstevel@tonic-gate * audio_endian()
469*7c478bd9Sstevel@tonic-gate *
470*7c478bd9Sstevel@tonic-gate * This routine tests the magic number at the head of a buffer
471*7c478bd9Sstevel@tonic-gate * containing the file header. The first thing in the header
472*7c478bd9Sstevel@tonic-gate * should be the magic number.
473*7c478bd9Sstevel@tonic-gate */
474*7c478bd9Sstevel@tonic-gate static int
audio_endian(unsigned char * buf,int * file_type)475*7c478bd9Sstevel@tonic-gate audio_endian(unsigned char *buf, int *file_type)
476*7c478bd9Sstevel@tonic-gate {
477*7c478bd9Sstevel@tonic-gate unsigned int magic1;
478*7c478bd9Sstevel@tonic-gate unsigned int magic2;
479*7c478bd9Sstevel@tonic-gate
480*7c478bd9Sstevel@tonic-gate /* put the buffer into an int that is aligned properly */
481*7c478bd9Sstevel@tonic-gate (void) memcpy(&magic1, buf, sizeof (magic1));
482*7c478bd9Sstevel@tonic-gate
483*7c478bd9Sstevel@tonic-gate magic2 = magic1;
484*7c478bd9Sstevel@tonic-gate SWABI(magic2);
485*7c478bd9Sstevel@tonic-gate
486*7c478bd9Sstevel@tonic-gate if (magic1 == AUDIO_AU_FILE_MAGIC || magic2 == AUDIO_AU_FILE_MAGIC) {
487*7c478bd9Sstevel@tonic-gate *file_type = FILE_AU;
488*7c478bd9Sstevel@tonic-gate return (AUDIO_ENDIAN_BIG);
489*7c478bd9Sstevel@tonic-gate } else if (magic1 == AUDIO_WAV_RIFF_ID || magic2 == AUDIO_WAV_RIFF_ID) {
490*7c478bd9Sstevel@tonic-gate *file_type = FILE_WAV;
491*7c478bd9Sstevel@tonic-gate return (AUDIO_ENDIAN_SMALL);
492*7c478bd9Sstevel@tonic-gate } else if (magic1 == AUDIO_AIFF_HDR_CHUNK_ID ||
493*7c478bd9Sstevel@tonic-gate magic2 == AUDIO_AIFF_HDR_CHUNK_ID) {
494*7c478bd9Sstevel@tonic-gate *file_type = FILE_AIFF;
495*7c478bd9Sstevel@tonic-gate return (AUDIO_ENDIAN_BIG);
496*7c478bd9Sstevel@tonic-gate }
497*7c478bd9Sstevel@tonic-gate
498*7c478bd9Sstevel@tonic-gate return (AUDIO_ENDIAN_UNKNOWN);
499*7c478bd9Sstevel@tonic-gate }
500*7c478bd9Sstevel@tonic-gate
501*7c478bd9Sstevel@tonic-gate /*
502*7c478bd9Sstevel@tonic-gate * Decode an aiff file header. Unlike .au and .wav, we have to process
503*7c478bd9Sstevel@tonic-gate * by chunk.
504*7c478bd9Sstevel@tonic-gate */
505*7c478bd9Sstevel@tonic-gate static int
decode_aiff(int fd,unsigned char * buf,Audio_hdr * hdrp,int * isize)506*7c478bd9Sstevel@tonic-gate decode_aiff(int fd, unsigned char *buf, Audio_hdr *hdrp, int *isize)
507*7c478bd9Sstevel@tonic-gate {
508*7c478bd9Sstevel@tonic-gate aiff_hdr_chunk_t hdr_chunk;
509*7c478bd9Sstevel@tonic-gate aiff_comm_chunk_t comm_chunk;
510*7c478bd9Sstevel@tonic-gate aiff_ssnd_chunk_t ssnd_chunk;
511*7c478bd9Sstevel@tonic-gate uint32_t ID;
512*7c478bd9Sstevel@tonic-gate uint32_t size;
513*7c478bd9Sstevel@tonic-gate uint32_t tmp;
514*7c478bd9Sstevel@tonic-gate int data_type;
515*7c478bd9Sstevel@tonic-gate int hdr_sizes;
516*7c478bd9Sstevel@tonic-gate int sr;
517*7c478bd9Sstevel@tonic-gate short bits_per_sample;
518*7c478bd9Sstevel@tonic-gate short channels;
519*7c478bd9Sstevel@tonic-gate
520*7c478bd9Sstevel@tonic-gate /* we've read in 4 bytes, read in the rest of the wav header */
521*7c478bd9Sstevel@tonic-gate size = sizeof (hdr_chunk) - sizeof (hdr_chunk.aiff_hdr_ID);
522*7c478bd9Sstevel@tonic-gate
523*7c478bd9Sstevel@tonic-gate /* read in the rest of the header */
524*7c478bd9Sstevel@tonic-gate if (read(fd, &hdr_chunk.aiff_hdr_size, size) != size) {
525*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
526*7c478bd9Sstevel@tonic-gate }
527*7c478bd9Sstevel@tonic-gate
528*7c478bd9Sstevel@tonic-gate /* see which kind of audio file we have */
529*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_FILE2HOST_INT(&hdr_chunk.aiff_hdr_data_type, &data_type);
530*7c478bd9Sstevel@tonic-gate if (data_type != AUDIO_AIFF_HDR_FORM_AIFF) {
531*7c478bd9Sstevel@tonic-gate /* we can't play this version of a .aiff file */
532*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
533*7c478bd9Sstevel@tonic-gate }
534*7c478bd9Sstevel@tonic-gate
535*7c478bd9Sstevel@tonic-gate hdr_sizes = sizeof (hdr_chunk);
536*7c478bd9Sstevel@tonic-gate
537*7c478bd9Sstevel@tonic-gate /*
538*7c478bd9Sstevel@tonic-gate * We don't know what the chunk order will be, so read each, getting
539*7c478bd9Sstevel@tonic-gate * the data we need from each. Eventually we'll get to the end of
540*7c478bd9Sstevel@tonic-gate * the file, in which case we should have all of the info on the
541*7c478bd9Sstevel@tonic-gate * file that we need. We then lseek() back to the data to play.
542*7c478bd9Sstevel@tonic-gate *
543*7c478bd9Sstevel@tonic-gate * We start each loop by reading the chunk ID.
544*7c478bd9Sstevel@tonic-gate */
545*7c478bd9Sstevel@tonic-gate while (read(fd, &tmp, sizeof (tmp)) == sizeof (tmp)) {
546*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_FILE2HOST_INT(&tmp, &ID);
547*7c478bd9Sstevel@tonic-gate switch (ID) {
548*7c478bd9Sstevel@tonic-gate case AUDIO_AIFF_COMM_ID:
549*7c478bd9Sstevel@tonic-gate /* read in the rest of the COMM chunk */
550*7c478bd9Sstevel@tonic-gate size = AUDIO_AIFF_COMM_CHUNK_SIZE -
551*7c478bd9Sstevel@tonic-gate sizeof (comm_chunk.aiff_comm_ID);
552*7c478bd9Sstevel@tonic-gate if (read(fd, &comm_chunk.aiff_comm_size, size) !=
553*7c478bd9Sstevel@tonic-gate size) {
554*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
555*7c478bd9Sstevel@tonic-gate }
556*7c478bd9Sstevel@tonic-gate
557*7c478bd9Sstevel@tonic-gate sr = convert_from_ieee_extended(
558*7c478bd9Sstevel@tonic-gate comm_chunk.aiff_comm_sample_rate);
559*7c478bd9Sstevel@tonic-gate
560*7c478bd9Sstevel@tonic-gate hdr_sizes += AUDIO_AIFF_COMM_CHUNK_SIZE;
561*7c478bd9Sstevel@tonic-gate
562*7c478bd9Sstevel@tonic-gate break;
563*7c478bd9Sstevel@tonic-gate case AUDIO_AIFF_SSND_ID:
564*7c478bd9Sstevel@tonic-gate /* read in the rest of the INST chunk */
565*7c478bd9Sstevel@tonic-gate size = sizeof (ssnd_chunk) -
566*7c478bd9Sstevel@tonic-gate sizeof (ssnd_chunk.aiff_ssnd_ID);
567*7c478bd9Sstevel@tonic-gate if (read(fd, &ssnd_chunk.aiff_ssnd_size, size) !=
568*7c478bd9Sstevel@tonic-gate size) {
569*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
570*7c478bd9Sstevel@tonic-gate }
571*7c478bd9Sstevel@tonic-gate
572*7c478bd9Sstevel@tonic-gate /*
573*7c478bd9Sstevel@tonic-gate * This has to be the last chunk because the audio data
574*7c478bd9Sstevel@tonic-gate * follows. So we should have all we need to tell the
575*7c478bd9Sstevel@tonic-gate * app the format information.
576*7c478bd9Sstevel@tonic-gate */
577*7c478bd9Sstevel@tonic-gate hdrp->sample_rate = sr;
578*7c478bd9Sstevel@tonic-gate
579*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_FILE2HOST_SHORT(
580*7c478bd9Sstevel@tonic-gate &comm_chunk.aiff_comm_channels,
581*7c478bd9Sstevel@tonic-gate &channels);
582*7c478bd9Sstevel@tonic-gate /* use channels to convert from short to int */
583*7c478bd9Sstevel@tonic-gate hdrp->channels = channels;
584*7c478bd9Sstevel@tonic-gate
585*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_FILE2HOST_SHORT(
586*7c478bd9Sstevel@tonic-gate &comm_chunk.aiff_comm_sample_size,
587*7c478bd9Sstevel@tonic-gate &bits_per_sample);
588*7c478bd9Sstevel@tonic-gate switch (bits_per_sample) {
589*7c478bd9Sstevel@tonic-gate case AUDIO_AIFF_COMM_8_BIT_SAMPLE_SIZE:
590*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_AU_ENCODING_LINEAR_8;
591*7c478bd9Sstevel@tonic-gate break;
592*7c478bd9Sstevel@tonic-gate case AUDIO_AIFF_COMM_16_BIT_SAMPLE_SIZE:
593*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_AU_ENCODING_LINEAR_16;
594*7c478bd9Sstevel@tonic-gate break;
595*7c478bd9Sstevel@tonic-gate default:
596*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
597*7c478bd9Sstevel@tonic-gate }
598*7c478bd9Sstevel@tonic-gate
599*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_FILE2HOST_INT(&ssnd_chunk.aiff_ssnd_size,
600*7c478bd9Sstevel@tonic-gate &size);
601*7c478bd9Sstevel@tonic-gate size -= sizeof (ssnd_chunk.aiff_ssnd_offset) +
602*7c478bd9Sstevel@tonic-gate sizeof (ssnd_chunk.aiff_ssnd_block_size);
603*7c478bd9Sstevel@tonic-gate hdrp->data_size = size;
604*7c478bd9Sstevel@tonic-gate
605*7c478bd9Sstevel@tonic-gate hdr_sizes += sizeof (ssnd_chunk);
606*7c478bd9Sstevel@tonic-gate
607*7c478bd9Sstevel@tonic-gate *isize = hdr_sizes - sizeof (au_filehdr_t);
608*7c478bd9Sstevel@tonic-gate
609*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
610*7c478bd9Sstevel@tonic-gate default:
611*7c478bd9Sstevel@tonic-gate /*
612*7c478bd9Sstevel@tonic-gate * Unknown chunk. Read the size, which is right after
613*7c478bd9Sstevel@tonic-gate * the ID. Then seek past it to get to the next chunk.
614*7c478bd9Sstevel@tonic-gate */
615*7c478bd9Sstevel@tonic-gate if (read(fd, &size, sizeof (size)) != sizeof (size)) {
616*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
617*7c478bd9Sstevel@tonic-gate }
618*7c478bd9Sstevel@tonic-gate
619*7c478bd9Sstevel@tonic-gate if (lseek(fd, size, SEEK_CUR) < 0) {
620*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
621*7c478bd9Sstevel@tonic-gate }
622*7c478bd9Sstevel@tonic-gate break;
623*7c478bd9Sstevel@tonic-gate }
624*7c478bd9Sstevel@tonic-gate }
625*7c478bd9Sstevel@tonic-gate
626*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
627*7c478bd9Sstevel@tonic-gate
628*7c478bd9Sstevel@tonic-gate } /* decode_aiff() */
629*7c478bd9Sstevel@tonic-gate
630*7c478bd9Sstevel@tonic-gate /*
631*7c478bd9Sstevel@tonic-gate * Decode an au file header.
632*7c478bd9Sstevel@tonic-gate */
633*7c478bd9Sstevel@tonic-gate static int
decode_au(int fd,unsigned char * buf,Audio_hdr * hdrp,int * isize,boolean_t read_info)634*7c478bd9Sstevel@tonic-gate decode_au(int fd, unsigned char *buf, Audio_hdr *hdrp, int *isize,
635*7c478bd9Sstevel@tonic-gate boolean_t read_info)
636*7c478bd9Sstevel@tonic-gate {
637*7c478bd9Sstevel@tonic-gate au_filehdr_t fhdr;
638*7c478bd9Sstevel@tonic-gate int offset;
639*7c478bd9Sstevel@tonic-gate int size;
640*7c478bd9Sstevel@tonic-gate
641*7c478bd9Sstevel@tonic-gate if (read_info) {
642*7c478bd9Sstevel@tonic-gate /* read in the rest of the au header */
643*7c478bd9Sstevel@tonic-gate size = sizeof (fhdr) - sizeof (int);
644*7c478bd9Sstevel@tonic-gate (void) lseek(fd, (off_t)4, SEEK_SET);
645*7c478bd9Sstevel@tonic-gate if (read(fd, &buf[sizeof (int)], size) != size) {
646*7c478bd9Sstevel@tonic-gate
647*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
648*7c478bd9Sstevel@tonic-gate }
649*7c478bd9Sstevel@tonic-gate }
650*7c478bd9Sstevel@tonic-gate
651*7c478bd9Sstevel@tonic-gate /* put the buffer into a structure that is aligned properly */
652*7c478bd9Sstevel@tonic-gate (void) memcpy(&fhdr, buf, sizeof (fhdr));
653*7c478bd9Sstevel@tonic-gate
654*7c478bd9Sstevel@tonic-gate /* Decode the 32-bit integer header fields. */
655*7c478bd9Sstevel@tonic-gate AUDIO_AU_FILE2HOST(&fhdr.au_offset, &offset);
656*7c478bd9Sstevel@tonic-gate AUDIO_AU_FILE2HOST(&fhdr.au_data_size, &hdrp->data_size);
657*7c478bd9Sstevel@tonic-gate AUDIO_AU_FILE2HOST(&fhdr.au_encoding, &hdrp->encoding);
658*7c478bd9Sstevel@tonic-gate AUDIO_AU_FILE2HOST(&fhdr.au_sample_rate, &hdrp->sample_rate);
659*7c478bd9Sstevel@tonic-gate AUDIO_AU_FILE2HOST(&fhdr.au_channels, &hdrp->channels);
660*7c478bd9Sstevel@tonic-gate
661*7c478bd9Sstevel@tonic-gate /* Set the info field size (ie, number of bytes left before data). */
662*7c478bd9Sstevel@tonic-gate *isize = offset - sizeof (au_filehdr_t);
663*7c478bd9Sstevel@tonic-gate
664*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
665*7c478bd9Sstevel@tonic-gate
666*7c478bd9Sstevel@tonic-gate } /* decode_au() */
667*7c478bd9Sstevel@tonic-gate
668*7c478bd9Sstevel@tonic-gate /*
669*7c478bd9Sstevel@tonic-gate * Decode a wav file header.
670*7c478bd9Sstevel@tonic-gate *
671*7c478bd9Sstevel@tonic-gate * .wav files are stored on-disk in little-endian format.
672*7c478bd9Sstevel@tonic-gate */
673*7c478bd9Sstevel@tonic-gate static int
decode_wav(int fd,unsigned char * buf,Audio_hdr * hdrp,int * isize)674*7c478bd9Sstevel@tonic-gate decode_wav(int fd, unsigned char *buf, Audio_hdr *hdrp, int *isize)
675*7c478bd9Sstevel@tonic-gate {
676*7c478bd9Sstevel@tonic-gate wav_filehdr_t fhdr;
677*7c478bd9Sstevel@tonic-gate uint32_t ID;
678*7c478bd9Sstevel@tonic-gate uint32_t size;
679*7c478bd9Sstevel@tonic-gate short bits_per_sample;
680*7c478bd9Sstevel@tonic-gate short encoding;
681*7c478bd9Sstevel@tonic-gate
682*7c478bd9Sstevel@tonic-gate /* we've read in 4 bytes, read in the rest of the wav header */
683*7c478bd9Sstevel@tonic-gate size = sizeof (fhdr) - sizeof (int);
684*7c478bd9Sstevel@tonic-gate
685*7c478bd9Sstevel@tonic-gate /* read in the rest of the header */
686*7c478bd9Sstevel@tonic-gate if (read(fd, &buf[sizeof (int)], size) != size) {
687*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR);
688*7c478bd9Sstevel@tonic-gate }
689*7c478bd9Sstevel@tonic-gate
690*7c478bd9Sstevel@tonic-gate /* put the buffer into a structure that is aligned properly */
691*7c478bd9Sstevel@tonic-gate (void) memcpy(&fhdr, buf, sizeof (fhdr));
692*7c478bd9Sstevel@tonic-gate
693*7c478bd9Sstevel@tonic-gate /* make sure we have the correct RIFF type */
694*7c478bd9Sstevel@tonic-gate AUDIO_WAV_FILE2HOST_INT(&fhdr.wav_type_ID, &ID);
695*7c478bd9Sstevel@tonic-gate if (ID != AUDIO_WAV_TYPE_ID) {
696*7c478bd9Sstevel@tonic-gate /* not a wave file */
697*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
698*7c478bd9Sstevel@tonic-gate }
699*7c478bd9Sstevel@tonic-gate
700*7c478bd9Sstevel@tonic-gate /* decode the fields */
701*7c478bd9Sstevel@tonic-gate AUDIO_WAV_FILE2HOST_INT(&fhdr.wav_fmt_ID, &ID);
702*7c478bd9Sstevel@tonic-gate if (ID != AUDIO_WAV_FORMAT_ID) {
703*7c478bd9Sstevel@tonic-gate /* mangled format */
704*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
705*7c478bd9Sstevel@tonic-gate }
706*7c478bd9Sstevel@tonic-gate
707*7c478bd9Sstevel@tonic-gate AUDIO_WAV_FILE2HOST_SHORT(&fhdr.wav_fmt_encoding, &encoding);
708*7c478bd9Sstevel@tonic-gate AUDIO_WAV_FILE2HOST_SHORT(&fhdr.wav_fmt_channels, &hdrp->channels);
709*7c478bd9Sstevel@tonic-gate AUDIO_WAV_FILE2HOST_INT(&fhdr.wav_fmt_sample_rate, &hdrp->sample_rate);
710*7c478bd9Sstevel@tonic-gate AUDIO_WAV_FILE2HOST_SHORT(&fhdr.wav_fmt_bits_per_sample,
711*7c478bd9Sstevel@tonic-gate &bits_per_sample);
712*7c478bd9Sstevel@tonic-gate
713*7c478bd9Sstevel@tonic-gate /* convert .wav encodings to .au encodings */
714*7c478bd9Sstevel@tonic-gate switch (encoding) {
715*7c478bd9Sstevel@tonic-gate case AUDIO_WAV_FMT_ENCODING_PCM:
716*7c478bd9Sstevel@tonic-gate switch (bits_per_sample) {
717*7c478bd9Sstevel@tonic-gate case AUDIO_WAV_FMT_BITS_PER_SAMPLE_8_BITS:
718*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_AU_ENCODING_LINEAR_8;
719*7c478bd9Sstevel@tonic-gate break;
720*7c478bd9Sstevel@tonic-gate case AUDIO_WAV_FMT_BITS_PER_SAMPLE_16_BITS:
721*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_AU_ENCODING_LINEAR_16;
722*7c478bd9Sstevel@tonic-gate break;
723*7c478bd9Sstevel@tonic-gate default:
724*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
725*7c478bd9Sstevel@tonic-gate }
726*7c478bd9Sstevel@tonic-gate break;
727*7c478bd9Sstevel@tonic-gate case AUDIO_WAV_FMT_ENCODING_ALAW:
728*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_AU_ENCODING_ALAW;
729*7c478bd9Sstevel@tonic-gate break;
730*7c478bd9Sstevel@tonic-gate case AUDIO_WAV_FMT_ENCODING_MULAW:
731*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_AU_ENCODING_ULAW;
732*7c478bd9Sstevel@tonic-gate break;
733*7c478bd9Sstevel@tonic-gate default:
734*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
735*7c478bd9Sstevel@tonic-gate }
736*7c478bd9Sstevel@tonic-gate
737*7c478bd9Sstevel@tonic-gate AUDIO_WAV_FILE2HOST_INT(&fhdr.wav_data_size, &hdrp->data_size);
738*7c478bd9Sstevel@tonic-gate
739*7c478bd9Sstevel@tonic-gate *isize = sizeof (wav_filehdr_t) - sizeof (au_filehdr_t);
740*7c478bd9Sstevel@tonic-gate
741*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
742*7c478bd9Sstevel@tonic-gate
743*7c478bd9Sstevel@tonic-gate } /* decode_wav() */
744*7c478bd9Sstevel@tonic-gate
745*7c478bd9Sstevel@tonic-gate /*
746*7c478bd9Sstevel@tonic-gate * Try to decode buffer containing an audio file header into an audio header.
747*7c478bd9Sstevel@tonic-gate */
748*7c478bd9Sstevel@tonic-gate int
audio_decode_filehdr(int fd,unsigned char * buf,int * file_type,Audio_hdr * hdrp,int * isize)749*7c478bd9Sstevel@tonic-gate audio_decode_filehdr(int fd, unsigned char *buf, int *file_type,
750*7c478bd9Sstevel@tonic-gate Audio_hdr *hdrp, int *isize)
751*7c478bd9Sstevel@tonic-gate /* file descriptor */
752*7c478bd9Sstevel@tonic-gate /* buffer address */
753*7c478bd9Sstevel@tonic-gate /* audio file type */
754*7c478bd9Sstevel@tonic-gate /* output audio header */
755*7c478bd9Sstevel@tonic-gate /* output size of info */
756*7c478bd9Sstevel@tonic-gate {
757*7c478bd9Sstevel@tonic-gate int err;
758*7c478bd9Sstevel@tonic-gate struct stat fd_stat;
759*7c478bd9Sstevel@tonic-gate boolean_t read_info;
760*7c478bd9Sstevel@tonic-gate
761*7c478bd9Sstevel@tonic-gate /* Test for .au first */
762*7c478bd9Sstevel@tonic-gate hdrp->endian = audio_endian(buf, file_type);
763*7c478bd9Sstevel@tonic-gate
764*7c478bd9Sstevel@tonic-gate /*
765*7c478bd9Sstevel@tonic-gate * When cat'ing a file, audioconvert will read the whole header
766*7c478bd9Sstevel@tonic-gate * trying to figure out the file. audioplay however, does not.
767*7c478bd9Sstevel@tonic-gate * Hence we check if this is a pipe and do not attempt to read
768*7c478bd9Sstevel@tonic-gate * any more header info if the file type is already known.
769*7c478bd9Sstevel@tonic-gate * Otherwise we overwrite the header data already in the buffer.
770*7c478bd9Sstevel@tonic-gate */
771*7c478bd9Sstevel@tonic-gate if (fstat(fd, &fd_stat) < 0) {
772*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
773*7c478bd9Sstevel@tonic-gate }
774*7c478bd9Sstevel@tonic-gate if (S_ISFIFO(fd_stat.st_mode) && (*file_type == FILE_AU)) {
775*7c478bd9Sstevel@tonic-gate read_info = B_FALSE;
776*7c478bd9Sstevel@tonic-gate } else {
777*7c478bd9Sstevel@tonic-gate /*
778*7c478bd9Sstevel@tonic-gate * Not an au file, or file type unknown. Reread the header's
779*7c478bd9Sstevel@tonic-gate * magic number. Fortunately this is always an int.
780*7c478bd9Sstevel@tonic-gate */
781*7c478bd9Sstevel@tonic-gate (void) lseek(fd, (off_t)0, SEEK_SET);
782*7c478bd9Sstevel@tonic-gate err = read(fd, (char *)buf, sizeof (int));
783*7c478bd9Sstevel@tonic-gate read_info = B_TRUE;
784*7c478bd9Sstevel@tonic-gate
785*7c478bd9Sstevel@tonic-gate /* test the magic number to determine the endian */
786*7c478bd9Sstevel@tonic-gate if ((hdrp->endian = audio_endian(buf, file_type)) ==
787*7c478bd9Sstevel@tonic-gate AUDIO_ENDIAN_UNKNOWN) {
788*7c478bd9Sstevel@tonic-gate
789*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
790*7c478bd9Sstevel@tonic-gate }
791*7c478bd9Sstevel@tonic-gate }
792*7c478bd9Sstevel@tonic-gate
793*7c478bd9Sstevel@tonic-gate /* decode the different file types, putting the data into hdrp */
794*7c478bd9Sstevel@tonic-gate switch (*file_type) {
795*7c478bd9Sstevel@tonic-gate case FILE_AU:
796*7c478bd9Sstevel@tonic-gate if ((err = decode_au(fd, buf, hdrp, isize, read_info)) !=
797*7c478bd9Sstevel@tonic-gate AUDIO_SUCCESS) {
798*7c478bd9Sstevel@tonic-gate return (err);
799*7c478bd9Sstevel@tonic-gate }
800*7c478bd9Sstevel@tonic-gate break;
801*7c478bd9Sstevel@tonic-gate case FILE_WAV:
802*7c478bd9Sstevel@tonic-gate if ((err = decode_wav(fd, buf, hdrp, isize)) != AUDIO_SUCCESS) {
803*7c478bd9Sstevel@tonic-gate return (err);
804*7c478bd9Sstevel@tonic-gate }
805*7c478bd9Sstevel@tonic-gate break;
806*7c478bd9Sstevel@tonic-gate case FILE_AIFF:
807*7c478bd9Sstevel@tonic-gate if ((err = decode_aiff(fd, buf, hdrp, isize)) !=
808*7c478bd9Sstevel@tonic-gate AUDIO_SUCCESS) {
809*7c478bd9Sstevel@tonic-gate return (err);
810*7c478bd9Sstevel@tonic-gate }
811*7c478bd9Sstevel@tonic-gate break;
812*7c478bd9Sstevel@tonic-gate default:
813*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
814*7c478bd9Sstevel@tonic-gate }
815*7c478bd9Sstevel@tonic-gate
816*7c478bd9Sstevel@tonic-gate /* Convert from file format info to audio format info */
817*7c478bd9Sstevel@tonic-gate switch (hdrp->encoding) {
818*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_ULAW:
819*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_ULAW;
820*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 1;
821*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1;
822*7c478bd9Sstevel@tonic-gate break;
823*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_ALAW:
824*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_ALAW;
825*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 1;
826*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1;
827*7c478bd9Sstevel@tonic-gate break;
828*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_LINEAR_8:
829*7c478bd9Sstevel@tonic-gate if (*file_type == FILE_WAV) {
830*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_LINEAR8;
831*7c478bd9Sstevel@tonic-gate } else {
832*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_LINEAR;
833*7c478bd9Sstevel@tonic-gate }
834*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 1;
835*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1;
836*7c478bd9Sstevel@tonic-gate break;
837*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_LINEAR_16:
838*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_LINEAR;
839*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 2;
840*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1;
841*7c478bd9Sstevel@tonic-gate break;
842*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_LINEAR_24:
843*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_LINEAR;
844*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 3;
845*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1;
846*7c478bd9Sstevel@tonic-gate break;
847*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_LINEAR_32:
848*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_LINEAR;
849*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 4;
850*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1;
851*7c478bd9Sstevel@tonic-gate break;
852*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_FLOAT:
853*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_FLOAT;
854*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 4;
855*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1;
856*7c478bd9Sstevel@tonic-gate break;
857*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_DOUBLE:
858*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_FLOAT;
859*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 8;
860*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1;
861*7c478bd9Sstevel@tonic-gate break;
862*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_ADPCM_G721:
863*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_G721;
864*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 1;
865*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 2;
866*7c478bd9Sstevel@tonic-gate break;
867*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_ADPCM_G723_3:
868*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_G723;
869*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 3;
870*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 8;
871*7c478bd9Sstevel@tonic-gate break;
872*7c478bd9Sstevel@tonic-gate case AUDIO_AU_ENCODING_ADPCM_G723_5:
873*7c478bd9Sstevel@tonic-gate hdrp->encoding = AUDIO_ENCODING_G723;
874*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = 5;
875*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 8;
876*7c478bd9Sstevel@tonic-gate break;
877*7c478bd9Sstevel@tonic-gate
878*7c478bd9Sstevel@tonic-gate default:
879*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADFILEHDR);
880*7c478bd9Sstevel@tonic-gate }
881*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
882*7c478bd9Sstevel@tonic-gate }
883*7c478bd9Sstevel@tonic-gate
884*7c478bd9Sstevel@tonic-gate /*
885*7c478bd9Sstevel@tonic-gate * Encode a .aiff file header from the supplied Audio_hdr structure and
886*7c478bd9Sstevel@tonic-gate * store in the supplied char* buffer. blen is the size of the buffer to
887*7c478bd9Sstevel@tonic-gate * store the header in. Unlike .au and .wav we can't cast to a data structure.
888*7c478bd9Sstevel@tonic-gate * We have to build it one chunk at a time.
889*7c478bd9Sstevel@tonic-gate *
890*7c478bd9Sstevel@tonic-gate * NOTE: .aiff doesn't support unsigned 8-bit linear PCM.
891*7c478bd9Sstevel@tonic-gate */
892*7c478bd9Sstevel@tonic-gate static int
audio_encode_aiff(Audio_hdr * hdrp,unsigned char * buf,unsigned int * blen)893*7c478bd9Sstevel@tonic-gate audio_encode_aiff(Audio_hdr *hdrp, unsigned char *buf, unsigned int *blen)
894*7c478bd9Sstevel@tonic-gate /* audio header */
895*7c478bd9Sstevel@tonic-gate /* output buffer */
896*7c478bd9Sstevel@tonic-gate /* output buffer size */
897*7c478bd9Sstevel@tonic-gate {
898*7c478bd9Sstevel@tonic-gate aiff_comm_chunk_t comm_chunk;
899*7c478bd9Sstevel@tonic-gate aiff_hdr_chunk_t hdr_chunk;
900*7c478bd9Sstevel@tonic-gate aiff_ssnd_chunk_t ssnd_chunk;
901*7c478bd9Sstevel@tonic-gate uint32_t tmp_uint;
902*7c478bd9Sstevel@tonic-gate uint32_t tmp_uint2;
903*7c478bd9Sstevel@tonic-gate int buf_size = 0;
904*7c478bd9Sstevel@tonic-gate uint16_t tmp_ushort;
905*7c478bd9Sstevel@tonic-gate
906*7c478bd9Sstevel@tonic-gate /* the only encoding we support for .aiff is signed linear PCM */
907*7c478bd9Sstevel@tonic-gate if (hdrp->encoding != AUDIO_ENCODING_LINEAR) {
908*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_ENCODING);
909*7c478bd9Sstevel@tonic-gate }
910*7c478bd9Sstevel@tonic-gate
911*7c478bd9Sstevel@tonic-gate /* build the header chunk */
912*7c478bd9Sstevel@tonic-gate tmp_uint = AUDIO_AIFF_HDR_CHUNK_ID;
913*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &hdr_chunk.aiff_hdr_ID);
914*7c478bd9Sstevel@tonic-gate /* needs to be fixed when closed */
915*7c478bd9Sstevel@tonic-gate tmp_uint = AUDIO_AIFF_UNKNOWN_SIZE;
916*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &hdr_chunk.aiff_hdr_size);
917*7c478bd9Sstevel@tonic-gate tmp_uint = AUDIO_AIFF_HDR_FORM_AIFF;
918*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &hdr_chunk.aiff_hdr_data_type);
919*7c478bd9Sstevel@tonic-gate (void) memcpy(&buf[buf_size], &hdr_chunk, sizeof (hdr_chunk));
920*7c478bd9Sstevel@tonic-gate buf_size += sizeof (hdr_chunk);
921*7c478bd9Sstevel@tonic-gate
922*7c478bd9Sstevel@tonic-gate /* build the COMM chunk */
923*7c478bd9Sstevel@tonic-gate tmp_uint = AUDIO_AIFF_COMM_ID;
924*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &comm_chunk.aiff_comm_ID);
925*7c478bd9Sstevel@tonic-gate tmp_uint = AUDIO_AIFF_COMM_SIZE;
926*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &comm_chunk.aiff_comm_size);
927*7c478bd9Sstevel@tonic-gate tmp_ushort = hdrp->channels;
928*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_SHORT(&tmp_ushort, &comm_chunk.aiff_comm_channels);
929*7c478bd9Sstevel@tonic-gate /* needs to be fixed when closed */
930*7c478bd9Sstevel@tonic-gate tmp_uint = AUDIO_AIFF_UNKNOWN_SIZE;
931*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &tmp_uint2);
932*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_COMM_INT2FRAMES(comm_chunk.aiff_comm_frames, tmp_uint2);
933*7c478bd9Sstevel@tonic-gate tmp_ushort = hdrp->bytes_per_unit * 8;
934*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_SHORT(&tmp_ushort,
935*7c478bd9Sstevel@tonic-gate &comm_chunk.aiff_comm_sample_size);
936*7c478bd9Sstevel@tonic-gate convert_to_ieee_extended((double)hdrp->sample_rate,
937*7c478bd9Sstevel@tonic-gate comm_chunk.aiff_comm_sample_rate);
938*7c478bd9Sstevel@tonic-gate (void) memcpy(&buf[buf_size], &comm_chunk, AUDIO_AIFF_COMM_CHUNK_SIZE);
939*7c478bd9Sstevel@tonic-gate buf_size += AUDIO_AIFF_COMM_CHUNK_SIZE;
940*7c478bd9Sstevel@tonic-gate
941*7c478bd9Sstevel@tonic-gate /* build the SSND chunk */
942*7c478bd9Sstevel@tonic-gate tmp_uint = AUDIO_AIFF_SSND_ID;
943*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &ssnd_chunk.aiff_ssnd_ID);
944*7c478bd9Sstevel@tonic-gate /* needs to be fixed when closed */
945*7c478bd9Sstevel@tonic-gate tmp_uint = AUDIO_AIFF_UNKNOWN_SIZE;
946*7c478bd9Sstevel@tonic-gate AUDIO_AIFF_HOST2FILE_INT(&tmp_uint, &ssnd_chunk.aiff_ssnd_size);
947*7c478bd9Sstevel@tonic-gate ssnd_chunk.aiff_ssnd_offset = 0;
948*7c478bd9Sstevel@tonic-gate ssnd_chunk.aiff_ssnd_block_size = 0;
949*7c478bd9Sstevel@tonic-gate (void) memcpy(&buf[buf_size], &ssnd_chunk, sizeof (ssnd_chunk));
950*7c478bd9Sstevel@tonic-gate buf_size += sizeof (ssnd_chunk);
951*7c478bd9Sstevel@tonic-gate
952*7c478bd9Sstevel@tonic-gate *blen = buf_size;
953*7c478bd9Sstevel@tonic-gate
954*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
955*7c478bd9Sstevel@tonic-gate
956*7c478bd9Sstevel@tonic-gate } /* audio_encode_aiff() */
957*7c478bd9Sstevel@tonic-gate
958*7c478bd9Sstevel@tonic-gate /*
959*7c478bd9Sstevel@tonic-gate * Encode a .au file header from the supplied Audio_hdr structure and
960*7c478bd9Sstevel@tonic-gate * store in the supplied char* buffer. blen is the size of the buffer to
961*7c478bd9Sstevel@tonic-gate * store the header in. If 'infop' is not NULL, it is the address of a
962*7c478bd9Sstevel@tonic-gate * buffer containing 'info' data. 'ilen' specifies the size of this buffer.
963*7c478bd9Sstevel@tonic-gate * The entire file header will be zero-padded to a double-word boundary.
964*7c478bd9Sstevel@tonic-gate *
965*7c478bd9Sstevel@tonic-gate * NOTE: .au doesn't support unsigned 8-bit linear PCM.
966*7c478bd9Sstevel@tonic-gate */
967*7c478bd9Sstevel@tonic-gate static int
audio_encode_au(Audio_hdr * hdrp,char * infop,unsigned int ilen,unsigned char * buf,unsigned int * blen)968*7c478bd9Sstevel@tonic-gate audio_encode_au(Audio_hdr *hdrp, char *infop, unsigned int ilen,
969*7c478bd9Sstevel@tonic-gate unsigned char *buf, unsigned int *blen)
970*7c478bd9Sstevel@tonic-gate /* audio header */
971*7c478bd9Sstevel@tonic-gate /* info buffer pointer */
972*7c478bd9Sstevel@tonic-gate /* info buffer size */
973*7c478bd9Sstevel@tonic-gate /* output buffer */
974*7c478bd9Sstevel@tonic-gate /* output buffer size */
975*7c478bd9Sstevel@tonic-gate {
976*7c478bd9Sstevel@tonic-gate au_filehdr_t fhdr;
977*7c478bd9Sstevel@tonic-gate int encoding;
978*7c478bd9Sstevel@tonic-gate int hdrsize;
979*7c478bd9Sstevel@tonic-gate int magic;
980*7c478bd9Sstevel@tonic-gate int offset;
981*7c478bd9Sstevel@tonic-gate
982*7c478bd9Sstevel@tonic-gate /*
983*7c478bd9Sstevel@tonic-gate * Set the size of the real header (hdr size + info size).
984*7c478bd9Sstevel@tonic-gate * If no supplied info, make sure a minimum size is accounted for.
985*7c478bd9Sstevel@tonic-gate * Also, round the whole thing up to double-word alignment.
986*7c478bd9Sstevel@tonic-gate */
987*7c478bd9Sstevel@tonic-gate if ((infop == NULL) || (ilen == 0)) {
988*7c478bd9Sstevel@tonic-gate infop = NULL;
989*7c478bd9Sstevel@tonic-gate ilen = 4;
990*7c478bd9Sstevel@tonic-gate }
991*7c478bd9Sstevel@tonic-gate hdrsize = sizeof (fhdr) + ilen;
992*7c478bd9Sstevel@tonic-gate offset = ROUND_DBL(hdrsize);
993*7c478bd9Sstevel@tonic-gate
994*7c478bd9Sstevel@tonic-gate /* Check the data encoding. */
995*7c478bd9Sstevel@tonic-gate switch (hdrp->encoding) {
996*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_LINEAR8:
997*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_ENCODING); /* we don't support ulinear */
998*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_ULAW:
999*7c478bd9Sstevel@tonic-gate if (hdrp->samples_per_unit != 1)
1000*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1001*7c478bd9Sstevel@tonic-gate
1002*7c478bd9Sstevel@tonic-gate switch (hdrp->bytes_per_unit) {
1003*7c478bd9Sstevel@tonic-gate case 1:
1004*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_ULAW;
1005*7c478bd9Sstevel@tonic-gate break;
1006*7c478bd9Sstevel@tonic-gate default:
1007*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1008*7c478bd9Sstevel@tonic-gate }
1009*7c478bd9Sstevel@tonic-gate break;
1010*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_ALAW:
1011*7c478bd9Sstevel@tonic-gate if (hdrp->samples_per_unit != 1)
1012*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1013*7c478bd9Sstevel@tonic-gate
1014*7c478bd9Sstevel@tonic-gate switch (hdrp->bytes_per_unit) {
1015*7c478bd9Sstevel@tonic-gate case 1:
1016*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_ALAW;
1017*7c478bd9Sstevel@tonic-gate break;
1018*7c478bd9Sstevel@tonic-gate default:
1019*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1020*7c478bd9Sstevel@tonic-gate }
1021*7c478bd9Sstevel@tonic-gate break;
1022*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_LINEAR:
1023*7c478bd9Sstevel@tonic-gate if (hdrp->samples_per_unit != 1)
1024*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1025*7c478bd9Sstevel@tonic-gate
1026*7c478bd9Sstevel@tonic-gate switch (hdrp->bytes_per_unit) {
1027*7c478bd9Sstevel@tonic-gate case 1:
1028*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_LINEAR_8;
1029*7c478bd9Sstevel@tonic-gate break;
1030*7c478bd9Sstevel@tonic-gate case 2:
1031*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_LINEAR_16;
1032*7c478bd9Sstevel@tonic-gate break;
1033*7c478bd9Sstevel@tonic-gate case 3:
1034*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_LINEAR_24;
1035*7c478bd9Sstevel@tonic-gate break;
1036*7c478bd9Sstevel@tonic-gate case 4:
1037*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_LINEAR_32;
1038*7c478bd9Sstevel@tonic-gate break;
1039*7c478bd9Sstevel@tonic-gate default:
1040*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1041*7c478bd9Sstevel@tonic-gate }
1042*7c478bd9Sstevel@tonic-gate break;
1043*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_FLOAT:
1044*7c478bd9Sstevel@tonic-gate if (hdrp->samples_per_unit != 1)
1045*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1046*7c478bd9Sstevel@tonic-gate
1047*7c478bd9Sstevel@tonic-gate switch (hdrp->bytes_per_unit) {
1048*7c478bd9Sstevel@tonic-gate case 4:
1049*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_FLOAT;
1050*7c478bd9Sstevel@tonic-gate break;
1051*7c478bd9Sstevel@tonic-gate case 8:
1052*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_DOUBLE;
1053*7c478bd9Sstevel@tonic-gate break;
1054*7c478bd9Sstevel@tonic-gate default:
1055*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1056*7c478bd9Sstevel@tonic-gate }
1057*7c478bd9Sstevel@tonic-gate break;
1058*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_G721:
1059*7c478bd9Sstevel@tonic-gate if (hdrp->bytes_per_unit != 1)
1060*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1061*7c478bd9Sstevel@tonic-gate else if (hdrp->samples_per_unit != 2)
1062*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1063*7c478bd9Sstevel@tonic-gate else
1064*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_ADPCM_G721;
1065*7c478bd9Sstevel@tonic-gate break;
1066*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_G723:
1067*7c478bd9Sstevel@tonic-gate if (hdrp->samples_per_unit != 8)
1068*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1069*7c478bd9Sstevel@tonic-gate else if (hdrp->bytes_per_unit == 3)
1070*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_ADPCM_G723_3;
1071*7c478bd9Sstevel@tonic-gate else if (hdrp->bytes_per_unit == 5)
1072*7c478bd9Sstevel@tonic-gate encoding = AUDIO_AU_ENCODING_ADPCM_G723_5;
1073*7c478bd9Sstevel@tonic-gate else
1074*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1075*7c478bd9Sstevel@tonic-gate break;
1076*7c478bd9Sstevel@tonic-gate default:
1077*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADHDR);
1078*7c478bd9Sstevel@tonic-gate }
1079*7c478bd9Sstevel@tonic-gate
1080*7c478bd9Sstevel@tonic-gate /* copy the fhdr into the supplied buffer - make sure it'll fit */
1081*7c478bd9Sstevel@tonic-gate if (*blen < offset) {
1082*7c478bd9Sstevel@tonic-gate /* XXX - is this apropriate? */
1083*7c478bd9Sstevel@tonic-gate return (AUDIO_EOF);
1084*7c478bd9Sstevel@tonic-gate }
1085*7c478bd9Sstevel@tonic-gate
1086*7c478bd9Sstevel@tonic-gate /* reset blen to actual size of hdr data */
1087*7c478bd9Sstevel@tonic-gate *blen = (unsigned)offset;
1088*7c478bd9Sstevel@tonic-gate
1089*7c478bd9Sstevel@tonic-gate magic = AUDIO_AU_FILE_MAGIC; /* set the magic number */
1090*7c478bd9Sstevel@tonic-gate
1091*7c478bd9Sstevel@tonic-gate /* Encode the audio header structure. */
1092*7c478bd9Sstevel@tonic-gate AUDIO_AU_HOST2FILE(&magic, &fhdr.au_magic);
1093*7c478bd9Sstevel@tonic-gate AUDIO_AU_HOST2FILE(&offset, &fhdr.au_offset);
1094*7c478bd9Sstevel@tonic-gate AUDIO_AU_HOST2FILE(&hdrp->data_size, &fhdr.au_data_size);
1095*7c478bd9Sstevel@tonic-gate AUDIO_AU_HOST2FILE(&encoding, &fhdr.au_encoding);
1096*7c478bd9Sstevel@tonic-gate AUDIO_AU_HOST2FILE(&hdrp->sample_rate, &fhdr.au_sample_rate);
1097*7c478bd9Sstevel@tonic-gate AUDIO_AU_HOST2FILE(&hdrp->channels, &fhdr.au_channels);
1098*7c478bd9Sstevel@tonic-gate
1099*7c478bd9Sstevel@tonic-gate /* Copy to the buffer */
1100*7c478bd9Sstevel@tonic-gate (void) memcpy(buf, &fhdr, sizeof (fhdr));
1101*7c478bd9Sstevel@tonic-gate
1102*7c478bd9Sstevel@tonic-gate /* Copy the info data, if present */
1103*7c478bd9Sstevel@tonic-gate if (infop != NULL) {
1104*7c478bd9Sstevel@tonic-gate (void) memcpy(&buf[sizeof (fhdr)], infop, (int)ilen);
1105*7c478bd9Sstevel@tonic-gate buf += ilen;
1106*7c478bd9Sstevel@tonic-gate }
1107*7c478bd9Sstevel@tonic-gate
1108*7c478bd9Sstevel@tonic-gate if (offset > hdrsize) {
1109*7c478bd9Sstevel@tonic-gate (void) memset(&buf[hdrsize], '\0', (size_t)(offset - hdrsize));
1110*7c478bd9Sstevel@tonic-gate }
1111*7c478bd9Sstevel@tonic-gate
1112*7c478bd9Sstevel@tonic-gate /* buf now has the data, just return ... */
1113*7c478bd9Sstevel@tonic-gate
1114*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
1115*7c478bd9Sstevel@tonic-gate
1116*7c478bd9Sstevel@tonic-gate } /* audio_encode_au() */
1117*7c478bd9Sstevel@tonic-gate
1118*7c478bd9Sstevel@tonic-gate /*
1119*7c478bd9Sstevel@tonic-gate * Encode a .wav file header from the supplied Audio_hdr structure and
1120*7c478bd9Sstevel@tonic-gate * store in the supplied char* buffer. blen is the size of the buffer to
1121*7c478bd9Sstevel@tonic-gate * store the header in. .wav doesn't support an information string like
1122*7c478bd9Sstevel@tonic-gate * .au does.
1123*7c478bd9Sstevel@tonic-gate *
1124*7c478bd9Sstevel@tonic-gate * NOTE: .wav only supports a few encoding methods.
1125*7c478bd9Sstevel@tonic-gate */
1126*7c478bd9Sstevel@tonic-gate static int
audio_encode_wav(Audio_hdr * hdrp,unsigned char * buf,unsigned int * blen)1127*7c478bd9Sstevel@tonic-gate audio_encode_wav(Audio_hdr *hdrp, unsigned char *buf, unsigned int *blen)
1128*7c478bd9Sstevel@tonic-gate /* audio header */
1129*7c478bd9Sstevel@tonic-gate /* output buffer */
1130*7c478bd9Sstevel@tonic-gate /* output buffer size */
1131*7c478bd9Sstevel@tonic-gate {
1132*7c478bd9Sstevel@tonic-gate wav_filehdr_t fhdr;
1133*7c478bd9Sstevel@tonic-gate int bytes_per_second;
1134*7c478bd9Sstevel@tonic-gate int bytes_per_sample;
1135*7c478bd9Sstevel@tonic-gate int bits_per_sample;
1136*7c478bd9Sstevel@tonic-gate int id;
1137*7c478bd9Sstevel@tonic-gate int length;
1138*7c478bd9Sstevel@tonic-gate int type;
1139*7c478bd9Sstevel@tonic-gate short encoding;
1140*7c478bd9Sstevel@tonic-gate
1141*7c478bd9Sstevel@tonic-gate /* make sure we've got valid encoding and precision settings for .wav */
1142*7c478bd9Sstevel@tonic-gate switch (hdrp->encoding) {
1143*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_LINEAR8:
1144*7c478bd9Sstevel@tonic-gate if (hdrp->bytes_per_unit != 1) {
1145*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_ENCODING);
1146*7c478bd9Sstevel@tonic-gate }
1147*7c478bd9Sstevel@tonic-gate encoding = AUDIO_WAV_FMT_ENCODING_PCM;
1148*7c478bd9Sstevel@tonic-gate break;
1149*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_ULAW:
1150*7c478bd9Sstevel@tonic-gate if (hdrp->bytes_per_unit != 1) {
1151*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_ENCODING);
1152*7c478bd9Sstevel@tonic-gate }
1153*7c478bd9Sstevel@tonic-gate encoding = AUDIO_WAV_FMT_ENCODING_MULAW;
1154*7c478bd9Sstevel@tonic-gate break;
1155*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_ALAW:
1156*7c478bd9Sstevel@tonic-gate if (hdrp->bytes_per_unit != 1) {
1157*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_ENCODING);
1158*7c478bd9Sstevel@tonic-gate }
1159*7c478bd9Sstevel@tonic-gate encoding = AUDIO_WAV_FMT_ENCODING_ALAW;
1160*7c478bd9Sstevel@tonic-gate break;
1161*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_LINEAR:
1162*7c478bd9Sstevel@tonic-gate if (hdrp->bytes_per_unit != 2) {
1163*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_ENCODING);
1164*7c478bd9Sstevel@tonic-gate }
1165*7c478bd9Sstevel@tonic-gate encoding = AUDIO_WAV_FMT_ENCODING_PCM;
1166*7c478bd9Sstevel@tonic-gate break;
1167*7c478bd9Sstevel@tonic-gate default:
1168*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_ENCODING);
1169*7c478bd9Sstevel@tonic-gate }
1170*7c478bd9Sstevel@tonic-gate
1171*7c478bd9Sstevel@tonic-gate /* fill in the riff chunk */
1172*7c478bd9Sstevel@tonic-gate id = AUDIO_WAV_RIFF_ID;
1173*7c478bd9Sstevel@tonic-gate length = AUDIO_WAV_UNKNOWN_SIZE;
1174*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&id, &fhdr.wav_riff_ID);
1175*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&length, &fhdr.wav_riff_size);
1176*7c478bd9Sstevel@tonic-gate
1177*7c478bd9Sstevel@tonic-gate /* fill in the type chunk */
1178*7c478bd9Sstevel@tonic-gate type = AUDIO_WAV_TYPE_ID;
1179*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&type, &fhdr.wav_type_ID);
1180*7c478bd9Sstevel@tonic-gate
1181*7c478bd9Sstevel@tonic-gate
1182*7c478bd9Sstevel@tonic-gate /* fill in the format chunk */
1183*7c478bd9Sstevel@tonic-gate id = AUDIO_WAV_FORMAT_ID;
1184*7c478bd9Sstevel@tonic-gate length = AUDIO_WAV_FORMAT_SIZE;
1185*7c478bd9Sstevel@tonic-gate bytes_per_second = hdrp->sample_rate * hdrp->channels *
1186*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit;
1187*7c478bd9Sstevel@tonic-gate bytes_per_sample = hdrp->channels * hdrp->bytes_per_unit;
1188*7c478bd9Sstevel@tonic-gate bits_per_sample = hdrp->bytes_per_unit * 8;
1189*7c478bd9Sstevel@tonic-gate
1190*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&id, &fhdr.wav_fmt_ID);
1191*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&length, &fhdr.wav_fmt_size);
1192*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_SHORT(&encoding, &fhdr.wav_fmt_encoding);
1193*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_SHORT(&hdrp->channels, &fhdr.wav_fmt_channels);
1194*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&hdrp->sample_rate, &fhdr.wav_fmt_sample_rate);
1195*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&bytes_per_second,
1196*7c478bd9Sstevel@tonic-gate &fhdr.wav_fmt_bytes_per_second);
1197*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_SHORT(&bytes_per_sample,
1198*7c478bd9Sstevel@tonic-gate &fhdr.wav_fmt_bytes_per_sample);
1199*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_SHORT(&bits_per_sample,
1200*7c478bd9Sstevel@tonic-gate &fhdr.wav_fmt_bits_per_sample);
1201*7c478bd9Sstevel@tonic-gate
1202*7c478bd9Sstevel@tonic-gate /* fill in the data chunk */
1203*7c478bd9Sstevel@tonic-gate id = AUDIO_WAV_DATA_ID_LC;
1204*7c478bd9Sstevel@tonic-gate length = AUDIO_WAV_UNKNOWN_SIZE;
1205*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&id, &fhdr.wav_data_ID);
1206*7c478bd9Sstevel@tonic-gate AUDIO_WAV_HOST2FILE_INT(&length, &fhdr.wav_data_size);
1207*7c478bd9Sstevel@tonic-gate
1208*7c478bd9Sstevel@tonic-gate *blen = sizeof (fhdr);
1209*7c478bd9Sstevel@tonic-gate
1210*7c478bd9Sstevel@tonic-gate /* copy to the buffer */
1211*7c478bd9Sstevel@tonic-gate (void) memcpy(buf, &fhdr, sizeof (fhdr));
1212*7c478bd9Sstevel@tonic-gate
1213*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
1214*7c478bd9Sstevel@tonic-gate
1215*7c478bd9Sstevel@tonic-gate } /* audio_encode_wav() */
1216*7c478bd9Sstevel@tonic-gate
1217*7c478bd9Sstevel@tonic-gate /*
1218*7c478bd9Sstevel@tonic-gate * Utility routine used to convert 10 byte IEEE extended float into
1219*7c478bd9Sstevel@tonic-gate * a regular double. Raw data arrives in an unsigned char array. Because
1220*7c478bd9Sstevel@tonic-gate * this is for sample rate, which is always positive, we don't worry
1221*7c478bd9Sstevel@tonic-gate * about the sign.
1222*7c478bd9Sstevel@tonic-gate */
1223*7c478bd9Sstevel@tonic-gate static double
convert_from_ieee_extended(unsigned char * data)1224*7c478bd9Sstevel@tonic-gate convert_from_ieee_extended(unsigned char *data)
1225*7c478bd9Sstevel@tonic-gate {
1226*7c478bd9Sstevel@tonic-gate double value = 0.0;
1227*7c478bd9Sstevel@tonic-gate unsigned long high_mantissa;
1228*7c478bd9Sstevel@tonic-gate unsigned long low_mantissa;
1229*7c478bd9Sstevel@tonic-gate int exponent;
1230*7c478bd9Sstevel@tonic-gate
1231*7c478bd9Sstevel@tonic-gate /* first 2 bytes are the exponent */
1232*7c478bd9Sstevel@tonic-gate exponent = ((data[0] & 0x7f) << 8) | data[1];
1233*7c478bd9Sstevel@tonic-gate
1234*7c478bd9Sstevel@tonic-gate high_mantissa = ((unsigned long)data[2] << 24) |
1235*7c478bd9Sstevel@tonic-gate ((unsigned long)data[3] << 16) |
1236*7c478bd9Sstevel@tonic-gate ((unsigned long)data[4] << 8) |
1237*7c478bd9Sstevel@tonic-gate (unsigned long)data[5];
1238*7c478bd9Sstevel@tonic-gate low_mantissa = ((unsigned long)data[6] << 24) |
1239*7c478bd9Sstevel@tonic-gate ((unsigned long)data[7] << 16) |
1240*7c478bd9Sstevel@tonic-gate ((unsigned long)data[8] << 8) |
1241*7c478bd9Sstevel@tonic-gate (unsigned long)data[9];
1242*7c478bd9Sstevel@tonic-gate
1243*7c478bd9Sstevel@tonic-gate /* convert exponent and mantissas into a real double */
1244*7c478bd9Sstevel@tonic-gate if (exponent == 0 && high_mantissa == 0 && low_mantissa == 0) {
1245*7c478bd9Sstevel@tonic-gate /* everything is 0, so we're done */
1246*7c478bd9Sstevel@tonic-gate value = 0.0;
1247*7c478bd9Sstevel@tonic-gate } else {
1248*7c478bd9Sstevel@tonic-gate if (exponent == 0x7fff) { /* infinity */
1249*7c478bd9Sstevel@tonic-gate value = MAXFLOAT;
1250*7c478bd9Sstevel@tonic-gate } else {
1251*7c478bd9Sstevel@tonic-gate /* convert exponent from being unsigned to signed */
1252*7c478bd9Sstevel@tonic-gate exponent -= 0x3fff;
1253*7c478bd9Sstevel@tonic-gate
1254*7c478bd9Sstevel@tonic-gate exponent -= 31;
1255*7c478bd9Sstevel@tonic-gate value = ldexp((double)high_mantissa, exponent);
1256*7c478bd9Sstevel@tonic-gate
1257*7c478bd9Sstevel@tonic-gate exponent -= 32;
1258*7c478bd9Sstevel@tonic-gate value += ldexp((double)low_mantissa, exponent);
1259*7c478bd9Sstevel@tonic-gate }
1260*7c478bd9Sstevel@tonic-gate }
1261*7c478bd9Sstevel@tonic-gate
1262*7c478bd9Sstevel@tonic-gate return (value);
1263*7c478bd9Sstevel@tonic-gate
1264*7c478bd9Sstevel@tonic-gate }
1265*7c478bd9Sstevel@tonic-gate
1266*7c478bd9Sstevel@tonic-gate /*
1267*7c478bd9Sstevel@tonic-gate * Utility routine to convert a double into 10 byte IEEE extended floating
1268*7c478bd9Sstevel@tonic-gate * point. The new number is placed into the unsigned char array. This is a
1269*7c478bd9Sstevel@tonic-gate * very brain dead convesion routine. It only supports integers, but then
1270*7c478bd9Sstevel@tonic-gate * that should be all we need for sample rate.
1271*7c478bd9Sstevel@tonic-gate */
1272*7c478bd9Sstevel@tonic-gate static void
convert_to_ieee_extended(double value,unsigned char * data)1273*7c478bd9Sstevel@tonic-gate convert_to_ieee_extended(double value, unsigned char *data)
1274*7c478bd9Sstevel@tonic-gate {
1275*7c478bd9Sstevel@tonic-gate double fmantissa;
1276*7c478bd9Sstevel@tonic-gate int exponent;
1277*7c478bd9Sstevel@tonic-gate int mantissa;
1278*7c478bd9Sstevel@tonic-gate
1279*7c478bd9Sstevel@tonic-gate exponent = 16398;
1280*7c478bd9Sstevel@tonic-gate fmantissa = value;
1281*7c478bd9Sstevel@tonic-gate
1282*7c478bd9Sstevel@tonic-gate while (fmantissa < 44000) {
1283*7c478bd9Sstevel@tonic-gate fmantissa *= 2;
1284*7c478bd9Sstevel@tonic-gate exponent--;
1285*7c478bd9Sstevel@tonic-gate }
1286*7c478bd9Sstevel@tonic-gate
1287*7c478bd9Sstevel@tonic-gate mantissa = (int)fmantissa << 16;
1288*7c478bd9Sstevel@tonic-gate
1289*7c478bd9Sstevel@tonic-gate data[0] = exponent >> 8;
1290*7c478bd9Sstevel@tonic-gate data[1] = exponent;
1291*7c478bd9Sstevel@tonic-gate data[2] = mantissa >> 24;
1292*7c478bd9Sstevel@tonic-gate data[3] = mantissa >> 16;
1293*7c478bd9Sstevel@tonic-gate data[4] = mantissa >> 8;
1294*7c478bd9Sstevel@tonic-gate data[5] = mantissa;
1295*7c478bd9Sstevel@tonic-gate data[6] = 0;
1296*7c478bd9Sstevel@tonic-gate data[7] = 0;
1297*7c478bd9Sstevel@tonic-gate data[8] = 0;
1298*7c478bd9Sstevel@tonic-gate data[9] = 0;
1299*7c478bd9Sstevel@tonic-gate
1300*7c478bd9Sstevel@tonic-gate }
1301