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