8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / audio / utilities / AudioHdr.cc
blob0758bbc62edd6b24bcbdb0fcf1c76784814728d8
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <AudioHdr.h>
31 // class AudioHdr basic methods
33 // This routine uses the byteorder network utilities to tell whether the
34 // current process uses network byte order or not.
35 AudioEndian AudioHdr::localByteOrder() const
37 short sTestHost;
38 short sTestNetwork;
39 static AudioEndian ae = UNDEFINED_ENDIAN;
41 if (ae == UNDEFINED_ENDIAN) {
42 sTestHost = MAXSHORT;
43 sTestNetwork = htons(sTestHost);
44 if (sTestNetwork != sTestHost) {
45 ae = LITTLE_ENDIAN;
46 } else {
47 ae = BIG_ENDIAN;
50 return (ae);
53 // Clear a header structure
54 void AudioHdr::
55 Clear()
57 sample_rate = 0;
58 samples_per_unit = 0;
59 bytes_per_unit = 0;
60 channels = 0;
61 encoding = NONE;
64 // Return error code (TRUE) if header is inconsistent or unrecognizable
65 // XXX - how do we support extensions?
66 AudioError AudioHdr::
67 Validate() const
69 // Check for uninitialized fields
70 if ((bytes_per_unit < 1) || (samples_per_unit < 1) ||
71 (sample_rate < 1) || (channels < 1))
72 return (AUDIO_ERR_BADHDR);
74 switch (encoding) {
75 case NONE:
76 return (AUDIO_ERR_BADHDR);
78 case LINEAR:
79 if (bytes_per_unit > 4)
80 return (AUDIO_ERR_PRECISION);
81 if (samples_per_unit != 1)
82 return (AUDIO_ERR_HDRINVAL);
83 break;
85 case FLOAT:
86 if ((bytes_per_unit != 4) && (bytes_per_unit != 8))
87 return (AUDIO_ERR_PRECISION);
88 if (samples_per_unit != 1)
89 return (AUDIO_ERR_HDRINVAL);
90 break;
92 case ULAW:
93 case ALAW:
94 case G722:
95 if (bytes_per_unit != 1)
96 return (AUDIO_ERR_PRECISION);
97 if (samples_per_unit != 1)
98 return (AUDIO_ERR_HDRINVAL);
99 break;
101 case G721:
102 case DVI:
103 // G.721 is a 4-bit encoding
104 if ((bytes_per_unit != 1) || (samples_per_unit != 2))
105 return (AUDIO_ERR_PRECISION);
106 break;
108 case G723:
109 // G.723 has 3-bit and 5-bit flavors
110 // 5-bit is currently unsupported
111 if ((bytes_per_unit != 3) || (samples_per_unit != 8))
112 return (AUDIO_ERR_PRECISION);
113 break;
115 return (AUDIO_SUCCESS);
119 // Convert a byte count into a floating-point time value, in seconds,
120 // using the encoding specified in the audio header.
121 Double AudioHdr::
122 Bytes_to_Time(
123 off_t cnt) const // byte count
125 if ((cnt == AUDIO_UNKNOWN_SIZE) || (Validate() != AUDIO_SUCCESS))
126 return (AUDIO_UNKNOWN_TIME);
128 // round off to nearest sample frame!
129 cnt -= (cnt % (bytes_per_unit * channels));
131 return (Double) ((double)cnt /
132 ((double)(channels * bytes_per_unit * sample_rate) /
133 (double)samples_per_unit));
136 // Convert a floating-point time value, in seconds, to a byte count for
137 // the audio encoding in the audio header. Make sure that the byte count
138 // or offset does not span a sample frame.
139 off_t AudioHdr::
140 Time_to_Bytes(
141 Double sec) const // time, in seconds
143 off_t offset;
145 if (Undefined(sec) || (Validate() != AUDIO_SUCCESS))
146 return (AUDIO_UNKNOWN_SIZE);
148 offset = (off_t)(0.5 + (sec *
149 ((double)(channels * bytes_per_unit * sample_rate) /
150 (double)samples_per_unit)));
152 // Round down to the start of the nearest sample frame
153 offset -= (offset % (bytes_per_unit * channels));
154 return (offset);
157 // Round a byte count down to a sample frame boundary.
158 off_t AudioHdr::
159 Bytes_to_Bytes(
160 off_t& cnt) const
162 if (Validate() != AUDIO_SUCCESS)
163 return (AUDIO_UNKNOWN_SIZE);
165 // Round down to the start of the nearest sample frame
166 cnt -= (cnt % (bytes_per_unit * channels));
167 return (cnt);
170 // Round a byte count down to a sample frame boundary.
171 size_t AudioHdr::
172 Bytes_to_Bytes(
173 size_t& cnt) const
175 if (Validate() != AUDIO_SUCCESS)
176 return (AUDIO_UNKNOWN_SIZE);
178 // Round down to the start of the nearest sample frame
179 cnt -= (cnt % (bytes_per_unit * channels));
180 return (cnt);
183 // Convert a count of sample frames into a floating-point time value,
184 // in seconds, using the encoding specified in the audio header.
185 Double AudioHdr::
186 Samples_to_Time(
187 unsigned long cnt) const // sample frame count
189 if ((cnt == AUDIO_UNKNOWN_SIZE) || (Validate() != AUDIO_SUCCESS))
190 return (AUDIO_UNKNOWN_TIME);
192 return ((Double)(((double)cnt * (double)samples_per_unit) /
193 (double)sample_rate));
196 // Convert a floating-point time value, in seconds, to a count of sample frames
197 // for the audio encoding in the audio header.
198 unsigned long AudioHdr::
199 Time_to_Samples(
200 Double sec) const // time, in seconds
202 if (Undefined(sec) || (Validate() != AUDIO_SUCCESS))
203 return (AUDIO_UNKNOWN_SIZE);
205 // Round down to sample frame boundary
206 return ((unsigned long) (AUDIO_MINFLOAT +
207 (((double)sec * (double)sample_rate) / (double)samples_per_unit)));
210 // Return the number of bytes in a sample frame for the audio encoding.
211 unsigned int AudioHdr::
212 FrameLength() const
214 return (bytes_per_unit * channels);