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