17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
231a70ae91Smathue * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
258e0c8248SAndrew Stormont *
268e0c8248SAndrew Stormont * Copyright 2019 RackTop Systems.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <math.h>
337c478bd9Sstevel@tonic-gate #include <AudioHdr.h>
347c478bd9Sstevel@tonic-gate
351a70ae91Smathue #define irint(d) ((int)(d))
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate // Convert a string to lowercase and return an allocated copy of it.
387c478bd9Sstevel@tonic-gate // XXX - There really should be a string-insensitive 8-bit compare routine.
397c478bd9Sstevel@tonic-gate static char *
to_lowercase(char * str)407c478bd9Sstevel@tonic-gate to_lowercase(
417c478bd9Sstevel@tonic-gate char *str)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate unsigned char *oldstr;
447c478bd9Sstevel@tonic-gate unsigned char *newstr;
457c478bd9Sstevel@tonic-gate int i;
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate oldstr = (unsigned char *) str;
487c478bd9Sstevel@tonic-gate newstr = new unsigned char [strlen(str) + 1];
497c478bd9Sstevel@tonic-gate for (i = 0; ; i++) {
507c478bd9Sstevel@tonic-gate if (isupper(oldstr[i]))
517c478bd9Sstevel@tonic-gate newstr[i] = tolower(oldstr[i]);
527c478bd9Sstevel@tonic-gate else
537c478bd9Sstevel@tonic-gate newstr[i] = oldstr[i];
547c478bd9Sstevel@tonic-gate if (oldstr[i] == '\0')
557c478bd9Sstevel@tonic-gate break;
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate return ((char *)newstr);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate // class AudioHdr parsing methods
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate // Return a string containing the sample rate
667c478bd9Sstevel@tonic-gate char *AudioHdr::
RateString() const677c478bd9Sstevel@tonic-gate RateString() const
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate char *str;
707c478bd9Sstevel@tonic-gate int ratek;
717c478bd9Sstevel@tonic-gate int rateh;
727c478bd9Sstevel@tonic-gate int prec;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate str = new char[32];
757c478bd9Sstevel@tonic-gate ratek = sample_rate / 1000;
767c478bd9Sstevel@tonic-gate rateh = sample_rate % 1000;
777c478bd9Sstevel@tonic-gate if (rateh == 0) {
787c478bd9Sstevel@tonic-gate (void) sprintf(str, "%dkHz", ratek);
797c478bd9Sstevel@tonic-gate } else {
807c478bd9Sstevel@tonic-gate // scale down to print minimum digits after the decimal point
817c478bd9Sstevel@tonic-gate prec = 3;
827c478bd9Sstevel@tonic-gate if ((rateh % 10) == 0) {
837c478bd9Sstevel@tonic-gate prec--;
847c478bd9Sstevel@tonic-gate rateh /= 10;
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate if ((rateh % 10) == 0) {
877c478bd9Sstevel@tonic-gate prec--;
887c478bd9Sstevel@tonic-gate rateh /= 10;
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate (void) sprintf(str, "%d.%0*dkHz", ratek, prec, rateh);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate return (str);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate // Return a string containing the number of channels
967c478bd9Sstevel@tonic-gate char *AudioHdr::
ChannelString() const977c478bd9Sstevel@tonic-gate ChannelString() const
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate char *str;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate str = new char[32];
1027c478bd9Sstevel@tonic-gate switch (channels) {
1037c478bd9Sstevel@tonic-gate case 1:
1047c478bd9Sstevel@tonic-gate (void) sprintf(str, "mono");
1057c478bd9Sstevel@tonic-gate break;
1067c478bd9Sstevel@tonic-gate case 2:
1077c478bd9Sstevel@tonic-gate (void) sprintf(str, "stereo");
1087c478bd9Sstevel@tonic-gate break;
1097c478bd9Sstevel@tonic-gate case 4:
1107c478bd9Sstevel@tonic-gate (void) sprintf(str, "quad");
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate default:
1137c478bd9Sstevel@tonic-gate (void) sprintf(str, "%d-channel", channels);
1147c478bd9Sstevel@tonic-gate break;
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate return (str);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate // Return a string containing the encoding
1207c478bd9Sstevel@tonic-gate char *AudioHdr::
EncodingString() const1217c478bd9Sstevel@tonic-gate EncodingString() const
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate char *str;
1247c478bd9Sstevel@tonic-gate Double prec;
1257c478bd9Sstevel@tonic-gate int iprec;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate str = new char[64];
1287c478bd9Sstevel@tonic-gate if ((samples_per_unit == 0) || (bytes_per_unit == 0) ||
1297c478bd9Sstevel@tonic-gate (encoding == NONE)) {
1307c478bd9Sstevel@tonic-gate (void) sprintf(str, "???");
1317c478bd9Sstevel@tonic-gate } else {
1327c478bd9Sstevel@tonic-gate // First encode precision
1337c478bd9Sstevel@tonic-gate iprec = (bytes_per_unit * 8) / samples_per_unit;
1347c478bd9Sstevel@tonic-gate prec = ((Double)bytes_per_unit * 8.) / (Double)samples_per_unit;
1357c478bd9Sstevel@tonic-gate if (prec == (Double) iprec) {
1367c478bd9Sstevel@tonic-gate (void) sprintf(str, "%d-bit ", iprec);
1377c478bd9Sstevel@tonic-gate } else {
1387c478bd9Sstevel@tonic-gate (void) sprintf(str, "%.1f-bit ", double(prec));
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate // Then encode format
1427c478bd9Sstevel@tonic-gate switch (encoding) {
1437c478bd9Sstevel@tonic-gate case ULAW:
1447c478bd9Sstevel@tonic-gate // XXX - See bug 1121000
1457c478bd9Sstevel@tonic-gate // XXX - (void) strcat(str, "�-law");
1467c478bd9Sstevel@tonic-gate (void) strcat(str, "u-law");
1477c478bd9Sstevel@tonic-gate break;
1487c478bd9Sstevel@tonic-gate case ALAW:
1497c478bd9Sstevel@tonic-gate (void) strcat(str, "A-law");
1507c478bd9Sstevel@tonic-gate break;
1517c478bd9Sstevel@tonic-gate case LINEAR:
1527c478bd9Sstevel@tonic-gate (void) strcat(str, "linear");
1537c478bd9Sstevel@tonic-gate break;
1547c478bd9Sstevel@tonic-gate case FLOAT:
1557c478bd9Sstevel@tonic-gate (void) strcat(str, "float");
1567c478bd9Sstevel@tonic-gate break;
1577c478bd9Sstevel@tonic-gate case G721:
1587c478bd9Sstevel@tonic-gate (void) strcat(str, "G.721 ADPCM");
1597c478bd9Sstevel@tonic-gate break;
1607c478bd9Sstevel@tonic-gate case G722:
1617c478bd9Sstevel@tonic-gate (void) strcat(str, "G.722 ADPCM");
1627c478bd9Sstevel@tonic-gate break;
1637c478bd9Sstevel@tonic-gate case G723:
1647c478bd9Sstevel@tonic-gate (void) strcat(str, "G.723 ADPCM");
1657c478bd9Sstevel@tonic-gate break;
1667c478bd9Sstevel@tonic-gate case DVI:
1677c478bd9Sstevel@tonic-gate (void) strcat(str, "DVI ADPCM");
1687c478bd9Sstevel@tonic-gate break;
1697c478bd9Sstevel@tonic-gate default:
1707c478bd9Sstevel@tonic-gate (void) strcat(str, "???");
1717c478bd9Sstevel@tonic-gate break;
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate return (str);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate // Return a string containing the entire audio encoding
1787c478bd9Sstevel@tonic-gate char *AudioHdr::
FormatString() const1797c478bd9Sstevel@tonic-gate FormatString() const
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate char *str;
1827c478bd9Sstevel@tonic-gate char *rate;
1837c478bd9Sstevel@tonic-gate char *chan;
1847c478bd9Sstevel@tonic-gate char *enc;
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate str = new char[4 * 32];
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate enc = EncodingString();
1897c478bd9Sstevel@tonic-gate rate = RateString();
1907c478bd9Sstevel@tonic-gate chan = ChannelString();
1917c478bd9Sstevel@tonic-gate (void) sprintf(str, "%s, %s, %s", enc, rate, chan);
1927c478bd9Sstevel@tonic-gate delete rate;
1937c478bd9Sstevel@tonic-gate delete chan;
1947c478bd9Sstevel@tonic-gate delete enc;
1957c478bd9Sstevel@tonic-gate return (str);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate // Parse a string containing the sample rate
1997c478bd9Sstevel@tonic-gate AudioError AudioHdr::
RateParse(char * str)2007c478bd9Sstevel@tonic-gate RateParse(
2017c478bd9Sstevel@tonic-gate char *str)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate static char *lib_khz = NULL;
2047c478bd9Sstevel@tonic-gate static char *lib_hz = NULL;
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate double r;
2077c478bd9Sstevel@tonic-gate int rate;
2087c478bd9Sstevel@tonic-gate char khzbuf[16];
2097c478bd9Sstevel@tonic-gate char *khz;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate if (str == NULL)
2127c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate // Init i18n string translations
2157c478bd9Sstevel@tonic-gate if (lib_khz == NULL) {
2167c478bd9Sstevel@tonic-gate lib_khz = to_lowercase(_MGET_("khz"));
2177c478bd9Sstevel@tonic-gate lib_hz = to_lowercase(_MGET_("hz"));
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate // Scan for a number followed by an optional khz designator
2217c478bd9Sstevel@tonic-gate switch (sscanf(str, " %lf %15s", &r, khzbuf)) {
2227c478bd9Sstevel@tonic-gate case 2:
2237c478bd9Sstevel@tonic-gate // Process 'khz', if present, and fall through
2247c478bd9Sstevel@tonic-gate khz = to_lowercase(khzbuf);
2257c478bd9Sstevel@tonic-gate if ((strcmp(khz, "khz") == 0) ||
2267c478bd9Sstevel@tonic-gate (strcmp(khz, "khertz") == 0) ||
2277c478bd9Sstevel@tonic-gate (strcmp(khz, "kilohertz") == 0) ||
2287c478bd9Sstevel@tonic-gate (strcmp(khz, "k") == 0) ||
2297c478bd9Sstevel@tonic-gate (strcoll(khz, lib_khz) == 0)) {
2307c478bd9Sstevel@tonic-gate r *= 1000.;
2317c478bd9Sstevel@tonic-gate } else if ((strcmp(khz, "hz") != 0) &&
2327c478bd9Sstevel@tonic-gate (strcmp(khz, "hertz") != 0) &&
2337c478bd9Sstevel@tonic-gate (strcoll(khz, lib_hz) != 0)) {
2347c478bd9Sstevel@tonic-gate delete khz;
2357c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate delete khz;
2388e0c8248SAndrew Stormont /* FALLTHROUGH */
2397c478bd9Sstevel@tonic-gate case 1:
2407c478bd9Sstevel@tonic-gate rate = irint(r);
2417c478bd9Sstevel@tonic-gate break;
2427c478bd9Sstevel@tonic-gate default:
2437c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate // Check for reasonable bounds
2467c478bd9Sstevel@tonic-gate if ((rate <= 0) || (rate > 500000)) {
2477c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate sample_rate = (unsigned int) rate;
2507c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate // Parse a string containing the number of channels
2547c478bd9Sstevel@tonic-gate AudioError AudioHdr::
ChannelParse(char * str)2557c478bd9Sstevel@tonic-gate ChannelParse(
2567c478bd9Sstevel@tonic-gate char *str)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate static char *lib_chan = NULL;
2597c478bd9Sstevel@tonic-gate static char *lib_mono = NULL;
2607c478bd9Sstevel@tonic-gate static char *lib_stereo = NULL;
2617c478bd9Sstevel@tonic-gate char cstrbuf[16];
2627c478bd9Sstevel@tonic-gate char *cstr;
2637c478bd9Sstevel@tonic-gate char xtra[4];
2647c478bd9Sstevel@tonic-gate int chan;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate // Init i18n string translations
2677c478bd9Sstevel@tonic-gate if (lib_chan == NULL) {
2687c478bd9Sstevel@tonic-gate lib_chan = to_lowercase(_MGET_("channel"));
2697c478bd9Sstevel@tonic-gate lib_mono = to_lowercase(_MGET_("mono"));
2707c478bd9Sstevel@tonic-gate lib_stereo = to_lowercase(_MGET_("stereo"));
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate // Parse a number, followed by optional "-channel"
2747c478bd9Sstevel@tonic-gate switch (sscanf(str, " %d %15s", &chan, cstrbuf)) {
2757c478bd9Sstevel@tonic-gate case 2:
2767c478bd9Sstevel@tonic-gate cstr = to_lowercase(cstrbuf);
2777c478bd9Sstevel@tonic-gate if ((strcmp(cstr, "-channel") != 0) &&
2787c478bd9Sstevel@tonic-gate (strcmp(cstr, "-chan") != 0) &&
2797c478bd9Sstevel@tonic-gate (strcoll(cstr, lib_chan) != 0)) {
2807c478bd9Sstevel@tonic-gate delete cstr;
2817c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate delete cstr;
2847c478bd9Sstevel@tonic-gate case 1:
2857c478bd9Sstevel@tonic-gate break;
2867c478bd9Sstevel@tonic-gate default:
2877c478bd9Sstevel@tonic-gate // If no number, look for reasonable keywords
2887c478bd9Sstevel@tonic-gate if (sscanf(str, " %15s %1s", cstrbuf, xtra) != 1) {
2897c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate cstr = to_lowercase(cstrbuf);
2927c478bd9Sstevel@tonic-gate if ((strcmp(cstr, "mono") == 0) ||
2937c478bd9Sstevel@tonic-gate (strcmp(cstr, "monaural") == 0) ||
2947c478bd9Sstevel@tonic-gate (strcoll(cstr, lib_mono) == 0)) {
2957c478bd9Sstevel@tonic-gate chan = 1;
2967c478bd9Sstevel@tonic-gate } else if ((strcmp(cstr, "stereo") == 0) ||
2977c478bd9Sstevel@tonic-gate (strcmp(cstr, "dual") == 0) ||
2987c478bd9Sstevel@tonic-gate (strcoll(cstr, lib_stereo) == 0)) {
2997c478bd9Sstevel@tonic-gate chan = 2;
3007c478bd9Sstevel@tonic-gate } else if ((strcmp(cstr, "quad") == 0) ||
3017c478bd9Sstevel@tonic-gate (strcmp(cstr, "quadrophonic") == 0)) {
3027c478bd9Sstevel@tonic-gate chan = 4;
3037c478bd9Sstevel@tonic-gate } else {
3047c478bd9Sstevel@tonic-gate delete cstr;
3057c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate delete cstr;
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate if ((chan <= 0) || (chan > 256)) {
3107c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate channels = (unsigned int) chan;
3137c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate // Parse a string containing the audio encoding
3177c478bd9Sstevel@tonic-gate AudioError AudioHdr::
EncodingParse(char * str)3187c478bd9Sstevel@tonic-gate EncodingParse(
3197c478bd9Sstevel@tonic-gate char *str)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate static char *lib_bit = NULL;
3227c478bd9Sstevel@tonic-gate static char *lib_ulaw = NULL;
3237c478bd9Sstevel@tonic-gate static char *lib_Alaw = NULL;
3247c478bd9Sstevel@tonic-gate static char *lib_linear = NULL;
3257c478bd9Sstevel@tonic-gate int i;
3267c478bd9Sstevel@tonic-gate char *p;
3277c478bd9Sstevel@tonic-gate char estrbuf[64];
3287c478bd9Sstevel@tonic-gate char *estr;
3297c478bd9Sstevel@tonic-gate char xtrabuf[32];
3307c478bd9Sstevel@tonic-gate char *xtra;
3317c478bd9Sstevel@tonic-gate char *xp;
3327c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
3337c478bd9Sstevel@tonic-gate char *cp;
3347c478bd9Sstevel@tonic-gate double prec;
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate // Init i18n string translations
3377c478bd9Sstevel@tonic-gate if (lib_bit == NULL) {
3387c478bd9Sstevel@tonic-gate lib_bit = to_lowercase(_MGET_("bit"));
3397c478bd9Sstevel@tonic-gate lib_ulaw = to_lowercase(_MGET_("u-law"));
3407c478bd9Sstevel@tonic-gate lib_Alaw = to_lowercase(_MGET_("A-law"));
3417c478bd9Sstevel@tonic-gate lib_linear = to_lowercase(_MGET_("linear8"));
3427c478bd9Sstevel@tonic-gate lib_linear = to_lowercase(_MGET_("linear"));
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate // first copy and remove leading spaces
3467c478bd9Sstevel@tonic-gate (void) strncpy(buf, str, BUFSIZ);
3477c478bd9Sstevel@tonic-gate for (cp = buf; *cp == ' '; cp++)
3487c478bd9Sstevel@tonic-gate continue;
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate // Delimit the precision. If there is one, parse it.
3517c478bd9Sstevel@tonic-gate prec = 0.;
3527c478bd9Sstevel@tonic-gate p = strchr(cp, ' ');
3537c478bd9Sstevel@tonic-gate if (p != NULL) {
3547c478bd9Sstevel@tonic-gate *p++ = '\0';
3557c478bd9Sstevel@tonic-gate i = sscanf(cp, " %lf %15s", &prec, xtrabuf);
3567c478bd9Sstevel@tonic-gate if (i == 0) {
3577c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate if (i == 2) {
3607c478bd9Sstevel@tonic-gate // convert to lowercase and skip leading "-", if any
3617c478bd9Sstevel@tonic-gate xtra = to_lowercase(xtrabuf);
3627c478bd9Sstevel@tonic-gate xp = (xtra[0] == '-') ? &xtra[1] : &xtra[0];
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate if ((strcmp(xp, "bit") != 0) &&
3657c478bd9Sstevel@tonic-gate (strcoll(xp, lib_bit) != 0)) {
3667c478bd9Sstevel@tonic-gate delete xtra;
3677c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate delete xtra;
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate if ((prec <= 0.) || (prec > 512.)) {
3727c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate // Don't be fooled by "8 bit"
3767c478bd9Sstevel@tonic-gate i = sscanf(p, " %15s", xtrabuf);
3777c478bd9Sstevel@tonic-gate if (i == 1) {
3787c478bd9Sstevel@tonic-gate // convert to lowercase and skip leading "-", if any
3797c478bd9Sstevel@tonic-gate xtra = to_lowercase(xtrabuf);
3807c478bd9Sstevel@tonic-gate xp = (xtra[0] == '-') ? &xtra[1] : &xtra[0];
3817c478bd9Sstevel@tonic-gate if ((strcmp(xp, "bit") == 0) ||
3827c478bd9Sstevel@tonic-gate (strcoll(xp, lib_bit) == 0)) {
3837c478bd9Sstevel@tonic-gate xp = strchr(p, ' ');
3847c478bd9Sstevel@tonic-gate if (xp != NULL)
3857c478bd9Sstevel@tonic-gate p = xp;
3867c478bd9Sstevel@tonic-gate else
3877c478bd9Sstevel@tonic-gate p += strlen(xtrabuf);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate delete xtra;
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate } else {
3927c478bd9Sstevel@tonic-gate p = cp;
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate i = sscanf(p, " %31s %31s", estrbuf, xtrabuf);
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate // If "adpcm" appended with a space, concatenate it
3987c478bd9Sstevel@tonic-gate if (i == 2) {
3997c478bd9Sstevel@tonic-gate xtra = to_lowercase(xtrabuf);
4007c478bd9Sstevel@tonic-gate if (strcmp(xtra, "adpcm") == 0) {
4017c478bd9Sstevel@tonic-gate (void) strcat(estrbuf, xtra);
4027c478bd9Sstevel@tonic-gate i = 1;
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate delete xtra;
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate if (i == 1) {
4077c478bd9Sstevel@tonic-gate estr = to_lowercase(estrbuf);
4087c478bd9Sstevel@tonic-gate if ((strcmp(estr, "ulaw") == 0) ||
4097c478bd9Sstevel@tonic-gate (strcmp(estr, "u-law") == 0) ||
4107c478bd9Sstevel@tonic-gate (strcmp(estr, "�law") == 0) ||
4117c478bd9Sstevel@tonic-gate (strcmp(estr, "�-law") == 0) ||
4127c478bd9Sstevel@tonic-gate (strcmp(estr, "mulaw") == 0) ||
4137c478bd9Sstevel@tonic-gate (strcmp(estr, "mu-law") == 0) ||
4147c478bd9Sstevel@tonic-gate (strcoll(estr, lib_ulaw) == 0)) {
4157c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 8.))
4167c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4177c478bd9Sstevel@tonic-gate encoding = ULAW;
4187c478bd9Sstevel@tonic-gate samples_per_unit = 1;
4197c478bd9Sstevel@tonic-gate bytes_per_unit = 1;
4207c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "alaw") == 0) ||
4217c478bd9Sstevel@tonic-gate (strcmp(estr, "a-law") == 0) ||
4227c478bd9Sstevel@tonic-gate (strcoll(estr, lib_Alaw) == 0)) {
4237c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 8.))
4247c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4257c478bd9Sstevel@tonic-gate encoding = ALAW;
4267c478bd9Sstevel@tonic-gate samples_per_unit = 1;
4277c478bd9Sstevel@tonic-gate bytes_per_unit = 1;
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "linear") == 0) ||
4307c478bd9Sstevel@tonic-gate (strcmp(estr, "lin") == 0) ||
4317c478bd9Sstevel@tonic-gate (strcmp(estr, "pcm") == 0) ||
4327c478bd9Sstevel@tonic-gate (strcoll(estr, lib_linear) == 0)) {
4337c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 8.) && (prec != 16.) &&
4347c478bd9Sstevel@tonic-gate (prec != 24.) && (prec != 32.))
4357c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4367c478bd9Sstevel@tonic-gate if (prec == 0.)
4377c478bd9Sstevel@tonic-gate prec = 16.;
4387c478bd9Sstevel@tonic-gate encoding = LINEAR;
4397c478bd9Sstevel@tonic-gate samples_per_unit = 1;
4407c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec / 8.);
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "linear8") == 0) ||
4437c478bd9Sstevel@tonic-gate (strcmp(estr, "lin8") == 0) ||
4447c478bd9Sstevel@tonic-gate (strcmp(estr, "pcm8") == 0)) {
4457c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 8.))
4467c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4477c478bd9Sstevel@tonic-gate prec = 8.;
4487c478bd9Sstevel@tonic-gate encoding = LINEAR;
4497c478bd9Sstevel@tonic-gate samples_per_unit = 1;
4507c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec / 8.);
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "linear16") == 0) ||
4537c478bd9Sstevel@tonic-gate (strcmp(estr, "lin16") == 0) ||
4547c478bd9Sstevel@tonic-gate (strcmp(estr, "pcm16") == 0)) {
4557c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 16.))
4567c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4577c478bd9Sstevel@tonic-gate prec = 16.;
4587c478bd9Sstevel@tonic-gate encoding = LINEAR;
4597c478bd9Sstevel@tonic-gate samples_per_unit = 1;
4607c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec / 8.);
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "linear24") == 0) ||
4637c478bd9Sstevel@tonic-gate (strcmp(estr, "lin24") == 0) ||
4647c478bd9Sstevel@tonic-gate (strcmp(estr, "pcm24") == 0)) {
4657c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 24.))
4667c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4677c478bd9Sstevel@tonic-gate prec = 24.;
4687c478bd9Sstevel@tonic-gate encoding = LINEAR;
4697c478bd9Sstevel@tonic-gate samples_per_unit = 1;
4707c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec / 8.);
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "linear32") == 0) ||
4737c478bd9Sstevel@tonic-gate (strcmp(estr, "lin32") == 0) ||
4747c478bd9Sstevel@tonic-gate (strcmp(estr, "pcm32") == 0)) {
4757c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 32.))
4767c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4777c478bd9Sstevel@tonic-gate prec = 32.;
4787c478bd9Sstevel@tonic-gate encoding = LINEAR;
4797c478bd9Sstevel@tonic-gate samples_per_unit = 1;
4807c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec / 8.);
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "float") == 0) ||
4837c478bd9Sstevel@tonic-gate (strcmp(estr, "floatingpoint") == 0) ||
4847c478bd9Sstevel@tonic-gate (strcmp(estr, "floating-point") == 0)) {
4857c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 32.) && (prec != 64.))
4867c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4877c478bd9Sstevel@tonic-gate if (prec == 0.)
4887c478bd9Sstevel@tonic-gate prec = 64.;
4897c478bd9Sstevel@tonic-gate encoding = FLOAT;
4907c478bd9Sstevel@tonic-gate samples_per_unit = 1;
4917c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec / 8.);
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "float32") == 0) ||
4947c478bd9Sstevel@tonic-gate (strcmp(estr, "floatingpoint32") == 0) ||
4957c478bd9Sstevel@tonic-gate (strcmp(estr, "floating-point32") == 0)) {
4967c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 32.))
4977c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
4987c478bd9Sstevel@tonic-gate prec = 32.;
4997c478bd9Sstevel@tonic-gate encoding = FLOAT;
5007c478bd9Sstevel@tonic-gate samples_per_unit = 1;
5017c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec / 8.);
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "float64") == 0) ||
5047c478bd9Sstevel@tonic-gate (strcmp(estr, "double") == 0) ||
5057c478bd9Sstevel@tonic-gate (strcmp(estr, "floatingpoint64") == 0) ||
5067c478bd9Sstevel@tonic-gate (strcmp(estr, "floating-point64") == 0)) {
5077c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 64.))
5087c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5097c478bd9Sstevel@tonic-gate prec = 64.;
5107c478bd9Sstevel@tonic-gate encoding = FLOAT;
5117c478bd9Sstevel@tonic-gate samples_per_unit = 1;
5127c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec / 8.);
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "g.721") == 0) ||
5157c478bd9Sstevel@tonic-gate (strcmp(estr, "g721") == 0) ||
5167c478bd9Sstevel@tonic-gate (strcmp(estr, "g.721adpcm") == 0) ||
5177c478bd9Sstevel@tonic-gate (strcmp(estr, "g721adpcm") == 0)) {
5187c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 4.))
5197c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5207c478bd9Sstevel@tonic-gate encoding = G721;
5217c478bd9Sstevel@tonic-gate samples_per_unit = 2;
5227c478bd9Sstevel@tonic-gate bytes_per_unit = 1;
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "g.722") == 0) ||
5257c478bd9Sstevel@tonic-gate (strcmp(estr, "g722") == 0) ||
5267c478bd9Sstevel@tonic-gate (strcmp(estr, "g.722adpcm") == 0) ||
5277c478bd9Sstevel@tonic-gate (strcmp(estr, "g722adpcm") == 0)) {
5287c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 8.))
5297c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5307c478bd9Sstevel@tonic-gate encoding = G722;
5317c478bd9Sstevel@tonic-gate samples_per_unit = 1;
5327c478bd9Sstevel@tonic-gate bytes_per_unit = 1;
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "g.723") == 0) ||
5357c478bd9Sstevel@tonic-gate (strcmp(estr, "g723") == 0) ||
5367c478bd9Sstevel@tonic-gate (strcmp(estr, "g.723adpcm") == 0) ||
5377c478bd9Sstevel@tonic-gate (strcmp(estr, "g723adpcm") == 0)) {
5387c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 3.) && (prec != 5.))
5397c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5407c478bd9Sstevel@tonic-gate if (prec == 0.)
5417c478bd9Sstevel@tonic-gate prec = 3.;
5427c478bd9Sstevel@tonic-gate encoding = G723;
5437c478bd9Sstevel@tonic-gate samples_per_unit = 8;
5447c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec);
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "g.723-3") == 0) ||
5477c478bd9Sstevel@tonic-gate (strcmp(estr, "g.723_3") == 0) ||
5487c478bd9Sstevel@tonic-gate (strcmp(estr, "g.723.3") == 0) ||
5497c478bd9Sstevel@tonic-gate (strcmp(estr, "g723-3") == 0) ||
5507c478bd9Sstevel@tonic-gate (strcmp(estr, "g723_3") == 0) ||
5517c478bd9Sstevel@tonic-gate (strcmp(estr, "g723.3") == 0)) {
5527c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 3.))
5537c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5547c478bd9Sstevel@tonic-gate prec = 3.;
5557c478bd9Sstevel@tonic-gate encoding = G723;
5567c478bd9Sstevel@tonic-gate samples_per_unit = 8;
5577c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec);
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "g.723-5") == 0) ||
5607c478bd9Sstevel@tonic-gate (strcmp(estr, "g.723_5") == 0) ||
5617c478bd9Sstevel@tonic-gate (strcmp(estr, "g.723.5") == 0) ||
5627c478bd9Sstevel@tonic-gate (strcmp(estr, "g723-5") == 0) ||
5637c478bd9Sstevel@tonic-gate (strcmp(estr, "g723_5") == 0) ||
5647c478bd9Sstevel@tonic-gate (strcmp(estr, "g723.5") == 0)) {
5657c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 5.))
5667c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5677c478bd9Sstevel@tonic-gate prec = 5.;
5687c478bd9Sstevel@tonic-gate encoding = G723;
5697c478bd9Sstevel@tonic-gate samples_per_unit = 8;
5707c478bd9Sstevel@tonic-gate bytes_per_unit = irint(prec);
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate } else if ((strcmp(estr, "dvi") == 0) ||
5737c478bd9Sstevel@tonic-gate (strcmp(estr, "dviadpcm") == 0)) {
5747c478bd9Sstevel@tonic-gate if ((prec != 0.) && (prec != 4.))
5757c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5767c478bd9Sstevel@tonic-gate encoding = DVI;
5777c478bd9Sstevel@tonic-gate samples_per_unit = 2;
5787c478bd9Sstevel@tonic-gate bytes_per_unit = 1;
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate } else {
5817c478bd9Sstevel@tonic-gate delete estr;
5827c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate delete estr;
5857c478bd9Sstevel@tonic-gate } else {
5867c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate // Parse a string containing the comma-separated audio encoding
5927c478bd9Sstevel@tonic-gate // Format is: "enc, chan, rate"
5937c478bd9Sstevel@tonic-gate // XXX - some countries use comma instead of decimal point
5947c478bd9Sstevel@tonic-gate // so there may be a problem with "44,1 khz"
5957c478bd9Sstevel@tonic-gate AudioError AudioHdr::
FormatParse(char * str)5967c478bd9Sstevel@tonic-gate FormatParse(
5977c478bd9Sstevel@tonic-gate char *str)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate char *pstr;
6007c478bd9Sstevel@tonic-gate char *ptr;
6017c478bd9Sstevel@tonic-gate char *p;
6027c478bd9Sstevel@tonic-gate AudioHdr newhdr;
6037c478bd9Sstevel@tonic-gate AudioError err;
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate pstr = new char[strlen(str) + 1];
6067c478bd9Sstevel@tonic-gate (void) strcpy(pstr, str);
6077c478bd9Sstevel@tonic-gate ptr = pstr;
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate // Delimit and parse the precision string
6107c478bd9Sstevel@tonic-gate p = strchr(ptr, ',');
6117c478bd9Sstevel@tonic-gate if (p == NULL)
6127c478bd9Sstevel@tonic-gate p = strchr(ptr, ' ');
6137c478bd9Sstevel@tonic-gate if (p == NULL) {
6147c478bd9Sstevel@tonic-gate err = AUDIO_ERR_BADARG;
6157c478bd9Sstevel@tonic-gate goto errret;
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate *p++ = '\0';
6187c478bd9Sstevel@tonic-gate err = newhdr.EncodingParse(ptr);
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate // Delimit and parse the sample rate string
6217c478bd9Sstevel@tonic-gate if (!err) {
6227c478bd9Sstevel@tonic-gate ptr = p;
6237c478bd9Sstevel@tonic-gate p = strchr(ptr, ',');
6247c478bd9Sstevel@tonic-gate if (p == NULL)
6257c478bd9Sstevel@tonic-gate p = strchr(ptr, ' ');
6267c478bd9Sstevel@tonic-gate if (p == NULL) {
6277c478bd9Sstevel@tonic-gate err = AUDIO_ERR_BADARG;
6287c478bd9Sstevel@tonic-gate goto errret;
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate *p++ = '\0';
6317c478bd9Sstevel@tonic-gate err = newhdr.RateParse(ptr);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate // Finally, parse the channels string
6357c478bd9Sstevel@tonic-gate if (!err) {
6367c478bd9Sstevel@tonic-gate err = newhdr.ChannelParse(p);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate
6397c478bd9Sstevel@tonic-gate // Validate the resulting header
6407c478bd9Sstevel@tonic-gate if (!err)
6417c478bd9Sstevel@tonic-gate err = newhdr.Validate();
6427c478bd9Sstevel@tonic-gate if (!err)
6437c478bd9Sstevel@tonic-gate *this = newhdr;
6447c478bd9Sstevel@tonic-gate errret:
645*7cad4b84SToomas Soome delete[] pstr;
6467c478bd9Sstevel@tonic-gate return (err);
6477c478bd9Sstevel@tonic-gate }
648