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