xref: /onnv-gate/usr/src/cmd/audio/include/aiff.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*
28*0Sstevel@tonic-gate  * This header file defines the .aiff audio file format.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #ifndef _AIFF_H
32*0Sstevel@tonic-gate #define	_AIFF_H
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #ifdef	__cplusplus
39*0Sstevel@tonic-gate extern "C" {
40*0Sstevel@tonic-gate #endif
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * Define the on-disk audio file header for the aiff file format.
44*0Sstevel@tonic-gate  * By definition .aiff files are big endian. Macros are provided
45*0Sstevel@tonic-gate  * to make the conversion easier.
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  * As many file formats, .aiff is composed of "chunks" of data grouped
48*0Sstevel@tonic-gate  * together. The aiff specification states that chunks may be in any
49*0Sstevel@tonic-gate  * order. Thus it is not possible to create a condensed header structure
50*0Sstevel@tonic-gate  * as is possible with .aif or .wav.
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  * The first chunk is always a FORM chunk. All other chunks have the
53*0Sstevel@tonic-gate  * following form:
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  *	Chunk ID
56*0Sstevel@tonic-gate  *	Chunk Data Size
57*0Sstevel@tonic-gate  *	Data
58*0Sstevel@tonic-gate  *
59*0Sstevel@tonic-gate  * AIFF files must have FORM, COMM, and SSND chunks. All other chunks
60*0Sstevel@tonic-gate  * can be ignored. When a chunk with an unknown ID is found then the
61*0Sstevel@tonic-gate  * application should read the next integer to get the size and then
62*0Sstevel@tonic-gate  * seek past the unknown chunk to the next chunk.
63*0Sstevel@tonic-gate  *
64*0Sstevel@tonic-gate  * When building a .aiff header the size of the data isn't always known.
65*0Sstevel@tonic-gate  * The following define is used for that situation.
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate #define	AUDIO_AIFF_UNKNOWN_SIZE		(~0)
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate struct aiff_hdr_chunk {
70*0Sstevel@tonic-gate 	uint32_t	aiff_hdr_ID;		/* initial chunk ID */
71*0Sstevel@tonic-gate 	uint32_t	aiff_hdr_size;		/* file_size - aiff_hdr_chunk */
72*0Sstevel@tonic-gate 	uint32_t	aiff_hdr_data_type;	/* file data type */
73*0Sstevel@tonic-gate };
74*0Sstevel@tonic-gate typedef struct aiff_hdr_chunk aiff_hdr_chunk_t;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate /* define for aiff_hdr_chunk.aiff_hdr_ID */
77*0Sstevel@tonic-gate #define	AUDIO_AIFF_HDR_CHUNK_ID	((uint32_t)0x464f524d)	/* 'FORM' */
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /* define for audio form type */
80*0Sstevel@tonic-gate #define	AUDIO_AIFF_HDR_FORM_AIFF	((uint32_t)0x41494646)	/* 'AIFF' */
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate  * The COMMon chunk definitions. Due to an unfortunate layout, the integer
84*0Sstevel@tonic-gate  * aiff_comm_frames is not on a 4 byte boundary, which most compilers pad to
85*0Sstevel@tonic-gate  * put back onto an integer boundary. Thus it is implemented as 4 chars which
86*0Sstevel@tonic-gate  * gets around this. There are convenience macros to aid in getting and setting
87*0Sstevel@tonic-gate  * the value. Also, some compilers will pad the end of the data structure to
88*0Sstevel@tonic-gate  * place it on a 4 byte boundary, thus sizeof (aiff_comm_chunk_t) is off by
89*0Sstevel@tonic-gate  * 2 bytes. Use AIFF_COMM_CHUNK_SIZE instead.
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_SR_SIZE		10
92*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_CHUNK_SIZE	26
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate struct aiff_comm_chunk {
95*0Sstevel@tonic-gate 	uint32_t	aiff_comm_ID;		/* chunk ID */
96*0Sstevel@tonic-gate 	uint32_t	aiff_comm_size;		/* size without _ID and _size */
97*0Sstevel@tonic-gate 	uint16_t	aiff_comm_channels;	/* number of channels */
98*0Sstevel@tonic-gate 	uint8_t		aiff_comm_frames[4];	/* sample frames */
99*0Sstevel@tonic-gate 	int16_t		aiff_comm_sample_size;	/* bits in each sample */
100*0Sstevel@tonic-gate 	uint8_t		aiff_comm_sample_rate[AUDIO_AIFF_COMM_SR_SIZE];
101*0Sstevel@tonic-gate 						/* SR in float */
102*0Sstevel@tonic-gate };
103*0Sstevel@tonic-gate typedef struct aiff_comm_chunk aiff_comm_chunk_t;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /* define for aiff_comm_chunk.aiff_comm_ID */
106*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_ID		((uint32_t)0x434f4d4d)	/* 'COMM' */
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate /* define for aiff_comm_chunk.aiff_comm_size */
109*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_SIZE		18
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /* define for aiff_comm_chunk.aiff_comm_channels */
112*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_CHANNELS_MONO		1
113*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_CHANNELS_STEREO		2
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate /* defines to get and set the frame count */
116*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_FRAMES2INT(X)					\
117*0Sstevel@tonic-gate 	(((X)[0] << 24) | ((X)[1] << 16) | ((X)[2] << 8) | (X)[3])
118*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_INT2FRAMES(X, D)				\
119*0Sstevel@tonic-gate 	(X)[0] = (D) >> 24; (X)[1] = (D) >> 16; (X)[2] = (D) >> 8; (X)[3] = (D);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /* define for aiff_comm_chunk.aiff_comm_sample_size */
122*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_8_BIT_SAMPLE_SIZE	8
123*0Sstevel@tonic-gate #define	AUDIO_AIFF_COMM_16_BIT_SAMPLE_SIZE	16
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate /*
127*0Sstevel@tonic-gate  * The SSND chunk definitions. Sound data immediately follows this data
128*0Sstevel@tonic-gate  * structure. Use aiff_ssnd_block_size to move past the data. The size of
129*0Sstevel@tonic-gate  * audio is aiff_ssnd_size - 8.
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate struct aiff_ssnd_chunk {
132*0Sstevel@tonic-gate 	uint32_t	aiff_ssnd_ID;		/* chunk ID */
133*0Sstevel@tonic-gate 	uint32_t	aiff_ssnd_size;		/* size without _id and _size */
134*0Sstevel@tonic-gate 	uint32_t	aiff_ssnd_offset;	/* offset to frame beginning */
135*0Sstevel@tonic-gate 	uint32_t	aiff_ssnd_block_size;	/* block size */
136*0Sstevel@tonic-gate };
137*0Sstevel@tonic-gate typedef struct aiff_ssnd_chunk aiff_ssnd_chunk_t;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /* define for aiff_ssnd_chunk.aiff_ssnd_ID */
140*0Sstevel@tonic-gate #define	AUDIO_AIFF_SSND_ID		((uint32_t)0x53534e44)	/* 'SSND' */
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate /* byte swapping macros */
144*0Sstevel@tonic-gate #if defined(__sparc)				/* big endian */
145*0Sstevel@tonic-gate #define	AUDIO_AIFF_FILE2HOST_INT(from, to)				\
146*0Sstevel@tonic-gate 		*((int *)(to)) = *((int *)(from))
147*0Sstevel@tonic-gate #define	AUDIO_AIFF_FILE2HOST_SHORT(from, to)				\
148*0Sstevel@tonic-gate 		*((short *)(to)) = *((short *)(from))
149*0Sstevel@tonic-gate #define	AUDIO_AIFF_HOST2FILE_INT(from, to)				\
150*0Sstevel@tonic-gate 		*((int *)(to)) = *((int *)(from))
151*0Sstevel@tonic-gate #define	AUDIO_AIFF_HOST2FILE_SHORT(from, to)				\
152*0Sstevel@tonic-gate 		*((short *)(to)) = *((short *)(from))
153*0Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)	/* little endian */
154*0Sstevel@tonic-gate #define	AUDIO_AIFF_FILE2HOST_INT(from, to)				\
155*0Sstevel@tonic-gate 		(*to) = ((((*from) >> 24) & 0xff) | (((*from) & 0xff) << 24) | \
156*0Sstevel@tonic-gate 		(((*from) >> 8) & 0xff00) | (((*from) & 0xff00) << 8))
157*0Sstevel@tonic-gate #define	AUDIO_AIFF_FILE2HOST_SHORT(from, to)				\
158*0Sstevel@tonic-gate 		(*to) = ((((*from) >> 8) & 0xff) | (((*from) & 0xff) << 8))
159*0Sstevel@tonic-gate #define	AUDIO_AIFF_HOST2FILE_INT(from, to)				\
160*0Sstevel@tonic-gate 		AUDIO_AIFF_FILE2HOST_INT((from), (to))
161*0Sstevel@tonic-gate #define	AUDIO_AIFF_HOST2FILE_SHORT(from, to)				\
162*0Sstevel@tonic-gate 		AUDIO_AIFF_FILE2HOST_SHORT((from), (to))
163*0Sstevel@tonic-gate #else
164*0Sstevel@tonic-gate #error unknown machine type;
165*0Sstevel@tonic-gate #endif	/* byte swapping */
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate #ifdef	__cplusplus
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate #endif
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate #endif /* _AIFF_H */
173