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