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