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