xref: /illumos-gate/usr/src/cmd/audio/include/AudioTypes.h (revision 164ffd8939460c53fad1cb0c2a860ad54badd3e1)
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 (c) 1992-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ifndef _MULTIMEDIA_AUDIOTYPES_H
28 #define	_MULTIMEDIA_AUDIOTYPES_H
29 
30 #ifdef NO_EXTERN_C
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #endif /* NO_EXTERN_C */
37 
38 #include <math.h>
39 #include <stdlib.h>
40 #include <float.h>
41 #include <limits.h>
42 #include <fcntl.h>
43 #include <sys/types.h>
44 
45 #include <audio_hdr.h>
46 
47 // Types used in the audio API
48 
49 // Values used for indeterminate size (e.g., data passed through a pipe)
50 const double		AUDIO_UNKNOWN_TIME = DBL_MAX;
51 
52 // Error severity
53 enum AudioSeverity {
54 	InitMessage,			// debugging message from constructor
55 	InitFatal,			// fatal error from constructor
56 	Message,			// debugging message
57 	Warning,			// non-fatal error
58 	Error,				// potentially severe error
59 	Consistency,			// internal consistency warning
60 	Fatal				// fatal internal error
61 };
62 
63 // Used in SetPosition methods
64 enum Whence { Absolute = 0, Relative = 1, Relative_eof = 2};
65 
66 // XXX - classes that ought to be defined elsewhere
67 
68 // A Boolean 'primitive type' with values TRUE and FALSE
69 // undefine these in case they're defined elsewhere
70 #undef TRUE
71 #undef FALSE
72 
73 // use bool_t 'cause boolean_t is already used under 5.0
74 // Since 4/93 can't use bool_t cause rpc/types.h typedefs it
75 // so use aud_bool_t
76 enum aud_bool_t {FALSE = 0, TRUE = 1};
77 
78 class Boolean {
79 private:
80 	aud_bool_t	value;		// value is TRUE or FALSE
81 public:
value(x)82 	inline Boolean(aud_bool_t x = FALSE): value(x)	// Constructor
83 	    { }
Boolean(int x)84 	inline Boolean(int x)				// Constructor from int
85 	    { value = (x == 0) ? FALSE : TRUE; }
86 	inline Boolean operator=(int x)			// Assignment from int
87 	    { return (value = (x == 0) ? FALSE : TRUE); }
88 	inline operator int()				// Cast to integer
89 	    { return ((value == TRUE) ? 1 : 0); }
90 	inline Boolean operator!()			// Logical not
91 	    { return ((value == TRUE) ? FALSE : TRUE); }
92 };
93 
94 // A 'primitive type' for file access modes
95 enum fileaccess_t {
96     NoAccess = 0, ReadOnly = 1, WriteOnly = 2, ReadWrite = 3,
97     AppendOnly = 6, ReadAppend = 7
98 };
99 
100 class FileAccess {
101 private:
102 	fileaccess_t	mode;		// combined mode
103 public:
mode(x)104 	FileAccess(fileaccess_t x = NoAccess): mode(x) { }	// Constructor
fileaccess_t()105 	inline operator fileaccess_t()			// Cast to enum
106 	    { return (mode); }
107 	inline operator int() {				// Cast to integer
108 	    switch (mode) {
109 	    case ReadOnly: return (O_RDONLY);
110 	    case WriteOnly: return (O_WRONLY);
111 	    case ReadWrite: return (O_RDWR);
112 	    case AppendOnly: return (O_WRONLY | O_APPEND);
113 	    case ReadAppend: return (O_RDWR | O_APPEND);
114 	    case NoAccess:
115 	    default:
116 		return (-1);
117 	    }
118 	}
119 	// These tests depend on the actual enum values
Readable()120 	inline Boolean Readable() const			// TRUE if readable
121 	    { return ((int)mode & 1); }
Writeable()122 	inline Boolean Writeable() const		// TRUE if writeable
123 	    { return ((int)mode & 2); }
Append()124 	inline Boolean Append() const			// TRUE if append only
125 	    { return ((int)mode & 4); }
126 };
127 
128 
129 // Define a small number corresponding to minor floating-point bit errors
130 const double		AUDIO_MINFLOAT = .00000001;
131 
132 // Define a 'double' class that allows some leeway in magnitude checking
133 // to try to correct for small errors due to floating-point imprecision
134 class Double {
135 private:
136 	double	val;
137 public:
val(x)138 	Double(double x = 0.): val(x) { }
Double(const Double & x)139 	Double(const Double &x): val(x.val) { }
Undefined()140 	inline int Undefined() const
141 	    { return (val == AUDIO_UNKNOWN_TIME); }
142 	inline operator double() const
143 	    { return (val); }
144 	inline Double& operator += (double y)
145 	    { val += y; return (*this); }
146 	inline Double& operator -= (double y)
147 	    { val -= y; return (*this); }
148 	Double& operator=(const Double&) = default;
149 };
150 
151 // inline double fabs(double x)
152 //    { return ((x >= 0.) ? x : -x); }
153 
min(const Double & x,const Double & y)154 inline double min(const Double& x, const Double& y) {
155 	return (((double)x <  (double)y) ? (double)x : (double)y);
156 }
157 
min(const Double & x,double y)158 inline double min(const Double& x, double y) {
159 	return (((double)x <  (double)y) ? (double)x : (double)y);
160 }
min(double x,const Double & y)161 inline double min(double x, const Double& y) {
162 	return (((double)x <  (double)y) ? (double)x : (double)y);
163 }
164 
max(const Double & x,const Double & y)165 inline double max(const Double& x, const Double& y) {
166 	return (((double)x >  (double)y) ? (double)x : (double)y);
167 }
max(const Double & x,double y)168 inline double max(const Double& x, double y) {
169 	return (((double)x >  (double)y) ? (double)x : (double)y);
170 }
max(double x,const Double & y)171 inline double max(double x, const Double& y) {
172 	return (((double)x >  (double)y) ? (double)x : (double)y);
173 }
174 
175 inline int operator == (const Double &x, const Double &y) {
176 	return (fabs((double)x - (double)y) <= AUDIO_MINFLOAT);
177 }
178 inline int operator == (const Double &x, double y) {
179 	return (fabs((double)x - (double)y) <= AUDIO_MINFLOAT);
180 }
181 inline int operator == (double x, const Double &y) {
182 	return (fabs((double)x - (double)y) <= AUDIO_MINFLOAT);
183 }
184 
185 inline int operator != (const Double &x, const Double &y) {
186 	return (!(x == y));
187 }
188 inline int operator != (const Double &x, double y) {
189 	return (!(x == y));
190 }
191 inline int operator != (double x, const Double &y) {
192 	return (!(x == y));
193 }
194 
195 inline int operator <= (const Double &x, const Double &y) {
196 	return (((double)x < (double)y) || (x == y));
197 }
198 inline int operator <= (const Double &x, double y) {
199 	return (((double)x < (double)y) || (x == y));
200 }
201 inline int operator <= (double x, const Double &y)
202 	{ return (((double)x < (double)y) || (x == y)); }
203 
204 inline int operator >= (const Double &x, const Double &y)
205 	{ return (((double)x > (double)y) || (x == y)); }
206 inline int operator >= (const Double &x, double y) {
207 	return (((double)x > (double)y) || (x == y));
208 }
209 inline int operator >= (double x, const Double &y) {
210 	return (((double)x > (double)y) || (x == y));
211 }
212 
213 inline int operator < (const Double &x, const Double &y) {
214 	return (!(x >= y));
215 }
216 inline int operator < (const Double &x, double y) {
217 	return (!(x >= y));
218 }
219 inline int operator < (double x, const Double &y) {
220 	return (!(x >= y));
221 }
222 
223 inline int operator > (const Double &x, const Double &y) {
224 	return (!(x <= y));
225 }
226 inline int operator > (const Double &x, double y) {
227 	return (!(x <= y));
228 }
229 inline int operator > (double x, const Double &y) {
230 	return (!(x <= y));
231 }
232 
233 inline Double& operator += (Double &x, const Double &y) {
234 	return (x += (double)y);
235 }
236 inline double operator += (double &x, const Double &y) {
237 	return (x += (double)y);
238 }
239 inline Double& operator -= (Double &x, const Double &y) {
240 	return (x -= (double)y);
241 }
242 inline double operator -= (double &x, const Double &y) {
243 	return (x -= (double)y);
244 }
245 
Undefined(const Double & x)246 inline int Undefined(const Double &x) {
247 	return (x.Undefined());
248 }
Undefined(double x)249 inline int Undefined(double x) {
250 	return (x == AUDIO_UNKNOWN_TIME);
251 }
252 
253 #ifdef NO_EXTERN_C
254 
255 #ifdef __cplusplus
256 }
257 #endif
258 
259 #endif /* NO_EXTERN_C */
260 
261 #endif /* !_MULTIMEDIA_AUDIOTYPES_H */
262