xref: /titanic_41/usr/src/cmd/audio/utilities/hdr_misc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 1989-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Miscellaneous audio-related operations.
31  */
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36 
37 #include <libaudio_impl.h>
38 #include <audio_errno.h>
39 #include <audio_hdr.h>
40 
41 /*
42  * Convert a byte count into a floating-point time value, in seconds,
43  * using the encoding specified in the given audio header structure.
44  * Note that the byte count is not the same as the offset in an audio file,
45  * since the size of the audio file header is not taken into account.
46  */
47 double
audio_bytes_to_secs(Audio_hdr * hp,unsigned int cnt)48 audio_bytes_to_secs(Audio_hdr *hp, unsigned int cnt)
49 {
50 	return ((double)cnt /
51 	    ((double)(hp->channels * hp->bytes_per_unit * hp->sample_rate) /
52 	    (double)hp->samples_per_unit));
53 }
54 
55 /*
56  * Convert a floating-point time value, in seconds, to a byte count for
57  * the audio encoding in the given audio header.  Note that the byte count
58  * is not the same as the offset in an audio file, since the size of the
59  * audio file header is not taken into account.
60  */
61 unsigned
audio_secs_to_bytes(Audio_hdr * hp,double sec)62 audio_secs_to_bytes(Audio_hdr *hp, double sec)
63 {
64 	unsigned	offset;
65 
66 	offset = (unsigned)(0.5 + (sec *
67 	    ((double)(hp->channels * hp->bytes_per_unit * hp->sample_rate) /
68 	    (double)hp->samples_per_unit)));
69 
70 	/* Round down to the start of the nearest sample frame */
71 	offset -= (offset % (hp->bytes_per_unit * hp->channels));
72 	return (offset);
73 }
74 
75 /*
76  * Convert an ASCII time value (hh:mm:ss.dd) into floating-point seconds.
77  * Returns value if successfully converted.  Otherwise, returns HUGE_VAL.
78  *
79  * XXX - currently allows the ridiculous construct:  5.3E3:-47.3E-1:17.3
80  */
81 double
audio_str_to_secs(char * str)82 audio_str_to_secs(char *str)
83 {
84 	double	val;
85 	char	*str2;
86 
87 	val = strtod(str, &str2);	/* get first numeric field */
88 	if (str2 == str)
89 		return (HUGE_VAL);
90 
91 	if (*str2 == ':') {		/* that was hours (or minutes) */
92 		val *= 60.;
93 		str = str2 + 1;
94 		val += strtod(str, &str2);	/* another field is required */
95 		if (str2 == str)
96 			return (HUGE_VAL);
97 	}
98 
99 	if (*str2 == ':') {		/* converted hours and minutes */
100 		val *= 60.;
101 		str = str2 + 1;
102 		val += strtod(str, &str2);	/* another field is required */
103 		if (str2 == str)
104 			return (HUGE_VAL);
105 	}
106 
107 	if (*str2 != '\0')
108 		return (HUGE_VAL);
109 	return (val);
110 }
111 
112 /*
113  * Convert floating-point seconds into an ASCII time value (hh:mm:ss.dd).
114  *
115  * HUGE_VAL is converted to 0:00.  'Precision' specifies the maximum
116  * number of digits after the decimal point (-1 allows the max).
117  *
118  * Store the resulting string in the specified buffer (must be at least
119  * AUDIO_MAX_TIMEVAL bytes long).  The string address is returned.
120  */
121 char *
audio_secs_to_str(double sec,char * str,int precision)122 audio_secs_to_str(double sec, char *str, int precision)
123 {
124 	char		*p;
125 	unsigned	ovflow;
126 	int		hours;
127 	double		x;
128 	char		buf[64];
129 
130 	if (sec == HUGE_VAL) {
131 		(void) strcpy(str, "0:00");
132 		return (str);
133 	}
134 
135 	/* Limit precision arg to reasonable value */
136 	if ((precision > 10) || (precision < 0))
137 		precision = 10;
138 
139 	/* If negative, write a minus sign and get on with it. */
140 	p = str;
141 	if (sec < 0.) {
142 		sec = -sec;
143 
144 		/* Round off within precision to avoid -.01 printing as -0:00 */
145 		(void) sprintf(buf, "%.*f", precision, sec);
146 		(void) sscanf(buf, "%lf", &sec);
147 		if (sec > 0.)
148 			*p++ = '-';
149 	}
150 
151 	/* Round off within precision to avoid 1:59.999 printing as 1:60.00 */
152 	x = fmod(sec, 60.);
153 	sec -= x;
154 	(void) sprintf(buf, "%.*f", precision, x);
155 	(void) sscanf(buf, "%lf", &x);
156 	sec += x;
157 
158 	if (sec >= 60.) {
159 		/* Extract minutes */
160 		ovflow = ((unsigned)sec) / 60;
161 		sec -= (double)(ovflow * 60);
162 		hours = (ovflow >= 60);
163 		if (hours) {
164 			/* convert hours */
165 			(void) sprintf(p, "%d:", ovflow / 60);
166 			p = &p[strlen(p)];
167 			ovflow %= 60;
168 		}
169 		/* convert minutes (use two digits if hours printed) */
170 		(void) sprintf(p, "%0*d:", (hours ? 2 : 1), ovflow);
171 		p = &p[strlen(p)];
172 	} else {
173 		*p++ = '0';
174 		*p++ = ':';
175 	}
176 
177 	if (sec < 10.)
178 		*p++ = '0';
179 	(void) sprintf(p, "%.*f", precision, sec);
180 	return (str);
181 }
182 
183 /*
184  * Compare the encoding fields of two audio headers.
185  * Return 0 if they are the same, 1 if they are the same except for
186  * sample rate, else -1.
187  */
188 int
audio_cmp_hdr(Audio_hdr * h1,Audio_hdr * h2)189 audio_cmp_hdr(Audio_hdr *h1, Audio_hdr *h2)
190 {
191 	if ((h1->encoding != h2->encoding) ||
192 	    (h1->bytes_per_unit != h2->bytes_per_unit) ||
193 	    (h1->channels != h2->channels) ||
194 	    (h1->samples_per_unit != h2->samples_per_unit))
195 		return (-1);
196 
197 	if (h1->sample_rate != h2->sample_rate)
198 		return (1);
199 
200 	return (0);
201 }
202 
203 /*
204  * Interpret the encoding information in the specified header
205  * and return an appropriate string in the supplied buffer.
206  * The buffer should contain at least AUDIO_MAX_ENCODE_INFO bytes.
207  * The returned string is something like:
208  *	"stereo 16-bit linear PCM @ 44.1kHz"
209  *
210  * Returns AUDIO_ERR_BADHDR if the header cannot be interpreted.
211  */
212 int
audio_enc_to_str(Audio_hdr * hdrp,char * str)213 audio_enc_to_str(Audio_hdr *hdrp, char *str)
214 {
215 	char		*chan;
216 	char		*prec;
217 	char		*enc;
218 	char		cbuf[AUDIO_MAX_ENCODE_INFO];
219 	char		pbuf[AUDIO_MAX_ENCODE_INFO];
220 	char		sbuf[AUDIO_MAX_ENCODE_INFO];
221 	int		err;
222 
223 	err = AUDIO_SUCCESS;
224 
225 	switch (hdrp->channels) {
226 	case 0:
227 		chan = "(zero channels?)";
228 		err = AUDIO_ERR_BADHDR;
229 		break;
230 	case 1:
231 		chan = "mono"; break;
232 	case 2:
233 		chan = "stereo"; break;
234 	case 4:
235 		chan = "quad"; break;
236 	default:
237 		chan = pbuf;
238 		(void) sprintf(cbuf, "%u-channel", hdrp->channels); break;
239 	}
240 
241 	switch (hdrp->encoding) {
242 	case AUDIO_ENCODING_ULAW:
243 		enc = "u-law";
244 		goto pcm;
245 	case AUDIO_ENCODING_ALAW:
246 		enc = "A-law";
247 		goto pcm;
248 	case AUDIO_ENCODING_LINEAR:
249 		enc = "linear PCM";
250 		goto pcm;
251 	case AUDIO_ENCODING_FLOAT:
252 		enc = "floating-point";
253 pcm:
254 		if (hdrp->samples_per_unit != 1)
255 			goto unknown;
256 		prec = pbuf;
257 		(void) sprintf(pbuf, "%u-bit", hdrp->bytes_per_unit * 8);
258 		break;
259 
260 	default:
261 unknown:
262 		err = AUDIO_ERR_ENCODING;
263 		enc = "(unknown encoding?)";
264 		if (hdrp->samples_per_unit != 0) {
265 			prec = pbuf;
266 			(void) sprintf(pbuf, "%f-bit",
267 			    (double)(hdrp->bytes_per_unit * 8) /
268 			    (double)hdrp->samples_per_unit);
269 		} else {
270 			prec = "(unknown precision?)";
271 			err = AUDIO_ERR_BADHDR;
272 		}
273 	}
274 
275 	(void) sprintf(sbuf, "%.3fkHz", ((double)hdrp->sample_rate / 1000.));
276 	(void) sprintf(str, "%s %s %s @ %s", chan, prec, enc, sbuf);
277 	return (err);
278 }
279