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