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