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
33extern "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)
50const double		AUDIO_UNKNOWN_TIME = DBL_MAX;
51
52// Error severity
53enum 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
64enum 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
76enum aud_bool_t {FALSE = 0, TRUE = 1};
77
78class Boolean {
79private:
80	aud_bool_t	value;		// value is TRUE or FALSE
81public:
82	inline Boolean(aud_bool_t x = FALSE): value(x)	// Constructor
83	    { }
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
95enum fileaccess_t {
96    NoAccess = 0, ReadOnly = 1, WriteOnly = 2, ReadWrite = 3,
97    AppendOnly = 6, ReadAppend = 7
98};
99
100class FileAccess {
101private:
102	fileaccess_t	mode;		// combined mode
103public:
104	FileAccess(fileaccess_t x = NoAccess): mode(x) { }	// Constructor
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
120	inline Boolean Readable() const			// TRUE if readable
121	    { return ((int)mode & 1); }
122	inline Boolean Writeable() const		// TRUE if writeable
123	    { return ((int)mode & 2); }
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
130const 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
134class Double {
135private:
136	double	val;
137public:
138	Double(double x = 0.): val(x) { }
139	Double(const Double &x): val(x.val) { }
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
154inline double min(const Double& x, const Double& y) {
155	return (((double)x <  (double)y) ? (double)x : (double)y);
156}
157
158inline double min(const Double& x, double y) {
159	return (((double)x <  (double)y) ? (double)x : (double)y);
160}
161inline double min(double x, const Double& y) {
162	return (((double)x <  (double)y) ? (double)x : (double)y);
163}
164
165inline double max(const Double& x, const Double& y) {
166	return (((double)x >  (double)y) ? (double)x : (double)y);
167}
168inline double max(const Double& x, double y) {
169	return (((double)x >  (double)y) ? (double)x : (double)y);
170}
171inline double max(double x, const Double& y) {
172	return (((double)x >  (double)y) ? (double)x : (double)y);
173}
174
175inline int operator == (const Double &x, const Double &y) {
176	return (fabs((double)x - (double)y) <= AUDIO_MINFLOAT);
177}
178inline int operator == (const Double &x, double y) {
179	return (fabs((double)x - (double)y) <= AUDIO_MINFLOAT);
180}
181inline int operator == (double x, const Double &y) {
182	return (fabs((double)x - (double)y) <= AUDIO_MINFLOAT);
183}
184
185inline int operator != (const Double &x, const Double &y) {
186	return (!(x == y));
187}
188inline int operator != (const Double &x, double y) {
189	return (!(x == y));
190}
191inline int operator != (double x, const Double &y) {
192	return (!(x == y));
193}
194
195inline int operator <= (const Double &x, const Double &y) {
196	return (((double)x < (double)y) || (x == y));
197}
198inline int operator <= (const Double &x, double y) {
199	return (((double)x < (double)y) || (x == y));
200}
201inline int operator <= (double x, const Double &y)
202	{ return (((double)x < (double)y) || (x == y)); }
203
204inline int operator >= (const Double &x, const Double &y)
205	{ return (((double)x > (double)y) || (x == y)); }
206inline int operator >= (const Double &x, double y) {
207	return (((double)x > (double)y) || (x == y));
208}
209inline int operator >= (double x, const Double &y) {
210	return (((double)x > (double)y) || (x == y));
211}
212
213inline int operator < (const Double &x, const Double &y) {
214	return (!(x >= y));
215}
216inline int operator < (const Double &x, double y) {
217	return (!(x >= y));
218}
219inline int operator < (double x, const Double &y) {
220	return (!(x >= y));
221}
222
223inline int operator > (const Double &x, const Double &y) {
224	return (!(x <= y));
225}
226inline int operator > (const Double &x, double y) {
227	return (!(x <= y));
228}
229inline int operator > (double x, const Double &y) {
230	return (!(x <= y));
231}
232
233inline Double& operator += (Double &x, const Double &y) {
234	return (x += (double)y);
235}
236inline double operator += (double &x, const Double &y) {
237	return (x += (double)y);
238}
239inline Double& operator -= (Double &x, const Double &y) {
240	return (x -= (double)y);
241}
242inline double operator -= (double &x, const Double &y) {
243	return (x -= (double)y);
244}
245
246inline int Undefined(const Double &x) {
247	return (x.Undefined());
248}
249inline 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