xref: /onnv-gate/usr/src/cmd/audio/include/AudioHdr.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef _MULTIMEDIA_AUDIOHDR_H
28*0Sstevel@tonic-gate #define	_MULTIMEDIA_AUDIOHDR_H
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #ifdef NO_EXTERN_C
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #ifdef __cplusplus
35*0Sstevel@tonic-gate extern "C" {
36*0Sstevel@tonic-gate #endif
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #endif /* NO_EXTERN_C */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <AudioTypes.h>
41*0Sstevel@tonic-gate #include <AudioError.h>
42*0Sstevel@tonic-gate #include <audio_hdr.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate // Required for the use of MAXSHORT
45*0Sstevel@tonic-gate #include <values.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate // Required by htons() and other network services.
48*0Sstevel@tonic-gate #include <sys/types.h>
49*0Sstevel@tonic-gate #include <netinet/in.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate // Define an in-core audio data header.
52*0Sstevel@tonic-gate //
53*0Sstevel@tonic-gate // This is different than the on-disk file header.
54*0Sstevel@tonic-gate //
55*0Sstevel@tonic-gate //
56*0Sstevel@tonic-gate // The audio header contains the following fields:
57*0Sstevel@tonic-gate //
58*0Sstevel@tonic-gate //	sample_rate		Number of samples per second (per channel).
59*0Sstevel@tonic-gate //
60*0Sstevel@tonic-gate //	samples_per_unit	This field describes the number of samples
61*0Sstevel@tonic-gate //				  represented by each sample unit (which, by
62*0Sstevel@tonic-gate //				  definition, are aligned on byte boundaries).
63*0Sstevel@tonic-gate //				  Audio samples may be stored individually
64*0Sstevel@tonic-gate //				  or, in the case of compressed formats
65*0Sstevel@tonic-gate //				  (e.g., ADPCM), grouped in algorithm-
66*0Sstevel@tonic-gate //				  specific ways.  If the data is bit-packed,
67*0Sstevel@tonic-gate //				  this field tells the number of samples
68*0Sstevel@tonic-gate //				  needed to get to a byte boundary.
69*0Sstevel@tonic-gate //
70*0Sstevel@tonic-gate //	bytes_per_unit		Number of bytes stored for each sample unit
71*0Sstevel@tonic-gate //
72*0Sstevel@tonic-gate //	channels		Number of interleaved sample units.
73*0Sstevel@tonic-gate //				   For any given time quantum, the set
74*0Sstevel@tonic-gate //				   consisting of 'channels' sample units
75*0Sstevel@tonic-gate //				   is called a sample frame.  Seeks in
76*0Sstevel@tonic-gate //				   the data should be aligned to the start
77*0Sstevel@tonic-gate //				   of the nearest sample frame.
78*0Sstevel@tonic-gate //
79*0Sstevel@tonic-gate //	encoding		Data encoding format.
80*0Sstevel@tonic-gate //
81*0Sstevel@tonic-gate //
82*0Sstevel@tonic-gate // The first four values are used to compute the byte offset given a
83*0Sstevel@tonic-gate // particular time, and vice versa.  Specifically:
84*0Sstevel@tonic-gate //
85*0Sstevel@tonic-gate //	seconds = offset / C
86*0Sstevel@tonic-gate //	offset = seconds * C
87*0Sstevel@tonic-gate // where:
88*0Sstevel@tonic-gate //	C = (channels * bytes_per_unit * sample_rate) / samples_per_unit
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate // Define the possible encoding types.
92*0Sstevel@tonic-gate // XXX - As long as audio_hdr.h exists, these values should match the
93*0Sstevel@tonic-gate //	 corresponding fields in audio_hdr.h since the cast operator
94*0Sstevel@tonic-gate //	 copies them blindly.  This implies that the values should
95*0Sstevel@tonic-gate //	 match any of the encodings that appear in <sys/audioio.h> also.
96*0Sstevel@tonic-gate // XXX - How can encoding types be added dynamically?
97*0Sstevel@tonic-gate enum AudioEncoding {
98*0Sstevel@tonic-gate 	NONE = 0,	// no encoding type set
99*0Sstevel@tonic-gate 	ULAW = 1,	// ISDN u-law
100*0Sstevel@tonic-gate 	ALAW = 2,	// ISDN A-law
101*0Sstevel@tonic-gate 	LINEAR = 3,	// PCM 2's-complement (0-center)
102*0Sstevel@tonic-gate 	FLOAT = 100,	// IEEE float (-1. <-> +1.)
103*0Sstevel@tonic-gate 	G721 = 101,	// CCITT G.721 ADPCM
104*0Sstevel@tonic-gate 	G722 = 102,	// CCITT G.722 ADPCM
105*0Sstevel@tonic-gate 	G723 = 103,	// CCITT G.723 ADPCM
106*0Sstevel@tonic-gate 	DVI = 104 	// DVI ADPCM
107*0Sstevel@tonic-gate };
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate // The byte order of the data.  This is only applicable if the data
110*0Sstevel@tonic-gate // is 16-bit.  All variables of this type will have the prefix "endian".
111*0Sstevel@tonic-gate enum AudioEndian {
112*0Sstevel@tonic-gate 	BIG_ENDIAN = 0,	// Sun and network byte order
113*0Sstevel@tonic-gate 	LITTLE_ENDIAN = 1,  // Intel byte order
114*0Sstevel@tonic-gate 	SWITCH_ENDIAN = 2,  // Flag to switch to the opposite endian, used
115*0Sstevel@tonic-gate 			    // by coerceEndian().
116*0Sstevel@tonic-gate 	UNDEFINED_ENDIAN = -1
117*0Sstevel@tonic-gate };
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate // Define a public data header structure.
120*0Sstevel@tonic-gate // Clients must be able to modify multiple fields inbetween validity checking.
121*0Sstevel@tonic-gate class AudioHdr {
122*0Sstevel@tonic-gate public:
123*0Sstevel@tonic-gate 	unsigned int	sample_rate;		// samples per second
124*0Sstevel@tonic-gate 	unsigned int	samples_per_unit;	// samples per unit
125*0Sstevel@tonic-gate 	unsigned int	bytes_per_unit;		// bytes per sample unit
126*0Sstevel@tonic-gate 	unsigned int	channels;		// # of interleaved channels
127*0Sstevel@tonic-gate 	AudioEncoding	encoding;		// data encoding format
128*0Sstevel@tonic-gate 	AudioEndian	endian;			// byte order
129*0Sstevel@tonic-gate 
AudioHdr()130*0Sstevel@tonic-gate 	AudioHdr():					// Constructor
131*0Sstevel@tonic-gate 	    sample_rate(0), samples_per_unit(0), bytes_per_unit(0),
132*0Sstevel@tonic-gate 	    channels(0), encoding(NONE)
133*0Sstevel@tonic-gate 	    {
134*0Sstevel@tonic-gate 		// The default for files is BIG, but this is
135*0Sstevel@tonic-gate 		// set in the AudioUnixfile class.
136*0Sstevel@tonic-gate 		endian = localByteOrder();
137*0Sstevel@tonic-gate 	    }
138*0Sstevel@tonic-gate 
AudioHdr(Audio_hdr hdr)139*0Sstevel@tonic-gate 	AudioHdr(Audio_hdr hdr):		// Constructor from C struct
140*0Sstevel@tonic-gate 	    sample_rate(hdr.sample_rate),
141*0Sstevel@tonic-gate 	    samples_per_unit(hdr.samples_per_unit),
142*0Sstevel@tonic-gate 	    bytes_per_unit(hdr.bytes_per_unit),
143*0Sstevel@tonic-gate 	    channels(hdr.channels),
144*0Sstevel@tonic-gate 	    encoding((AudioEncoding)hdr.encoding)
145*0Sstevel@tonic-gate 	    {
146*0Sstevel@tonic-gate 		// The default for files is BIG, but this is
147*0Sstevel@tonic-gate 		// set in the AudioUnixfile class.
148*0Sstevel@tonic-gate 		endian = localByteOrder();
149*0Sstevel@tonic-gate 	    }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	// Determines the local byte order, otherwise know as the endian
152*0Sstevel@tonic-gate 	// nature of the current machine.
153*0Sstevel@tonic-gate 	AudioEndian localByteOrder() const;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	virtual void Clear();				// Init header
156*0Sstevel@tonic-gate 	virtual AudioError Validate() const;		// Check hdr validity
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	// Conversion between time (in seconds) and byte offsets
159*0Sstevel@tonic-gate 	virtual Double Bytes_to_Time(off_t cnt) const;
160*0Sstevel@tonic-gate 	virtual off_t Time_to_Bytes(Double sec) const;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	// Round down a byte count to a sample frame boundary
163*0Sstevel@tonic-gate 	virtual off_t Bytes_to_Bytes(off_t& cnt) const;
164*0Sstevel@tonic-gate 	virtual size_t Bytes_to_Bytes(size_t& cnt) const;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	// Conversion between time (in seconds) and sample frames
167*0Sstevel@tonic-gate 	virtual Double Samples_to_Time(unsigned long cnt) const;
168*0Sstevel@tonic-gate 	virtual unsigned long Time_to_Samples(Double sec) const;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	// Return the number of bytes in a sample frame for the audio encoding.
171*0Sstevel@tonic-gate 	virtual unsigned int FrameLength() const;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	// Return some meaningful strings.  The returned char pointers
174*0Sstevel@tonic-gate 	// must be deleted when the caller is through with them.
175*0Sstevel@tonic-gate 	virtual char *RateString() const;	// eg "44.1kHz"
176*0Sstevel@tonic-gate 	virtual char *ChannelString() const;	// eg "stereo"
177*0Sstevel@tonic-gate 	virtual char *EncodingString() const;	// eg "3-bit G.723"
178*0Sstevel@tonic-gate 	virtual char *FormatString() const;	// eg "4-bit G.721, 8 kHz, mono"
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	// Parse strings and set corresponding header fields.
181*0Sstevel@tonic-gate 	virtual AudioError RateParse(char *);
182*0Sstevel@tonic-gate 	virtual AudioError ChannelParse(char *);
183*0Sstevel@tonic-gate 	virtual AudioError EncodingParse(char *);
184*0Sstevel@tonic-gate 	virtual AudioError FormatParse(char *);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	// for casting to C Audio_hdr struct
Audio_hdr()187*0Sstevel@tonic-gate 	operator Audio_hdr() {
188*0Sstevel@tonic-gate 		Audio_hdr hdr;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 		hdr.sample_rate = sample_rate;
191*0Sstevel@tonic-gate 		hdr.samples_per_unit = samples_per_unit;
192*0Sstevel@tonic-gate 		hdr.bytes_per_unit = bytes_per_unit;
193*0Sstevel@tonic-gate 		hdr.channels = channels;
194*0Sstevel@tonic-gate 		hdr.encoding = encoding;
195*0Sstevel@tonic-gate 		hdr.endian = endian;
196*0Sstevel@tonic-gate 		return (hdr);
197*0Sstevel@tonic-gate 	};
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	// compare two AudioHdr objects
200*0Sstevel@tonic-gate 	int operator == (const AudioHdr& tst)
201*0Sstevel@tonic-gate 	{
202*0Sstevel@tonic-gate 		return ((sample_rate == tst.sample_rate) &&
203*0Sstevel@tonic-gate 		    (samples_per_unit == tst.samples_per_unit) &&
204*0Sstevel@tonic-gate 		    (bytes_per_unit == tst.bytes_per_unit) &&
205*0Sstevel@tonic-gate 		    (channels == tst.channels) &&
206*0Sstevel@tonic-gate // Audioconvert uses this method to see if a conversion should take
207*0Sstevel@tonic-gate // place, but doesn't know how to convert between endian formats.
208*0Sstevel@tonic-gate // This makes it ignore endian differences.
209*0Sstevel@tonic-gate //		    (endian = tst.endian) &&
210*0Sstevel@tonic-gate 		    (encoding == tst.encoding));
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 	int operator != (const AudioHdr& tst)
213*0Sstevel@tonic-gate 	{
214*0Sstevel@tonic-gate 		return (! (*this == tst));
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate };
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate #ifdef NO_EXTERN_C
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate #ifdef __cplusplus
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate #endif
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate #endif /* NO_EXTERN_C */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate #endif /* !_MULTIMEDIA_AUDIOHDR_H */
227