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 1992-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _MULTIMEDIA_AUDIO_HDR_H
28 #define	_MULTIMEDIA_AUDIO_HDR_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /*
35  * Define an in-core audio data header.
36  *
37  * This is different that the on-disk file header.
38  * The fields defined here are preliminary at best.
39  */
40 
41 /*
42  * The audio header contains the following fields:
43  *
44  *      endian                  Byte order of 16-bit or greater PCM,
45  *                                and possibly floating point data.
46  *
47  *	sample_rate		Number of samples per second (per channel).
48  *
49  *	samples_per_unit	This field describes the number of samples
50  *				  represented by each sample unit (which, by
51  *				  definition, are aligned on byte boundaries).
52  *				  Audio samples may be stored individually
53  *				  or, in the case of compressed formats
54  *				  (e.g., ADPCM), grouped in algorithm-
55  *				  specific ways.  If the data is bit-packed,
56  *				  this field tells the number of samples
57  *				  needed to get to a byte boundary.
58  *
59  *	bytes_per_unit		Number of bytes stored for each sample unit
60  *
61  *	channels		Number of interleaved sample units.
62  *				   For any given time quantum, the set
63  *				   consisting of 'channels' sample units
64  *				   is called a sample frame.  Seeks in
65  *				   the data should be aligned to the start
66  *				   of the nearest sample frame.
67  *
68  *	encoding		Data encoding format.
69  *
70  *	data_size		Number of bytes in the data.
71  *				   This value is advisory only, and may
72  *				   be set to the value AUDIO_UNKNOWN_SIZE
73  *				   if the data size is unknown (for
74  *				   instance, if the data is being
75  *				   recorded or generated and piped
76  *				   to another process).
77  *
78  * The first four values are used to compute the byte offset given a
79  * particular time, and vice versa.  Specifically:
80  *
81  *	seconds = offset / C
82  *	offset = seconds * C
83  * where:
84  *	C = (channels * bytes_per_unit * sample_rate) / samples_per_unit
85  *
86  *
87  */
88 typedef struct {
89 	unsigned	sample_rate;		/* samples per second */
90 	unsigned	samples_per_unit;	/* samples per unit */
91 	unsigned	bytes_per_unit;		/* bytes per sample unit */
92 	unsigned	channels;		/* # of interleaved channels */
93 	unsigned	encoding;		/* data encoding format */
94 	unsigned	endian;			/* byte order */
95 	unsigned	data_size;		/* length of data (optional) */
96 } Audio_hdr;
97 
98 /*
99  * Define the possible encoding types.
100  * Note that the names that overlap the encodings in <sun/audioio.h>
101  * must have the same values.
102  */
103 #define	AUDIO_ENCODING_NONE	(0)	/* No encoding specified ... */
104 #define	AUDIO_ENCODING_ULAW	(1)	/* ISDN u-law */
105 #define	AUDIO_ENCODING_ALAW	(2)	/* ISDN A-law */
106 #define	AUDIO_ENCODING_LINEAR	(3)	/* PCM 2's-complement (0-center) */
107 #define	AUDIO_ENCODING_FLOAT	(100)	/* IEEE float (-1. <-> +1.) */
108 #define	AUDIO_ENCODING_G721	(101)	/* CCITT g.721 ADPCM */
109 #define	AUDIO_ENCODING_G722	(102)	/* CCITT g.722 ADPCM */
110 #define	AUDIO_ENCODING_G723	(103)	/* CCITT g.723 ADPCM */
111 #define	AUDIO_ENCODING_DVI	(104)	/* DVI ADPCM */
112 
113 /*
114  * Define the possible endian types.
115  */
116 #define	AUDIO_ENDIAN_BIG 0	/* SPARC, 68000, etc. */
117 #define	AUDIO_ENDIAN_SMALL 1	/* Intel */
118 #define	AUDIO_ENDIAN_UNKNOWN 2	/* Unknown endian */
119 
120 /* Value used for indeterminate size (e.g., data passed through a pipe) */
121 #define	AUDIO_UNKNOWN_SIZE	((unsigned)(~0))
122 
123 
124 /* Define conversion macros for integer<->floating-point conversions */
125 
126 /* double to 8,16,32-bit linear */
127 #define	audio_d2c(X)		((X) >= 1. ? 127 : (X) <= -1. ? -127 :	\
128 				    (char)(rint((X) * 127.)))
129 #define	audio_d2s(X)		((X) >= 1. ? 32767 : (X) <= -1. ? -32767 :\
130 				    (short)(rint((X) * 32767.)))
131 #define	audio_d2l(X)		((X) >= 1. ? 2147483647 : (X) <= -1. ?	\
132 				    -2147483647 :			\
133 				    (long)(rint((X) * 2147483647.)))
134 
135 /* 8,16,32-bit linear to double */
136 #define	audio_c2d(X)		(((unsigned char)(X)) == 0x80 ? -1. :	\
137 				    ((double)((char)(X))) / 127.)
138 #define	audio_s2d(X)		(((unsigned short)(X)) == 0x8000 ? -1. :\
139 				    ((double)((short)(X))) / 32767.)
140 #define	audio_l2d(X)		(((unsigned long)(X)) == 0x80000000 ? -1. :\
141 				    ((double)((long)(X))) / 2147483647.)
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif /* !_MULTIMEDIA_AUDIO_HDR_H */
148