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