#include <stdio.h>
#include <string.h>
#include <math.h>
#include <libaudio_impl.h>
#include <audio_errno.h>
#include <audio_hdr.h>
double
audio_bytes_to_secs(Audio_hdr *hp, unsigned int cnt)
{
return ((double)cnt /
((double)(hp->channels * hp->bytes_per_unit * hp->sample_rate) /
(double)hp->samples_per_unit));
}
unsigned
audio_secs_to_bytes(Audio_hdr *hp, double sec)
{
unsigned offset;
offset = (unsigned)(0.5 + (sec *
((double)(hp->channels * hp->bytes_per_unit * hp->sample_rate) /
(double)hp->samples_per_unit)));
offset -= (offset % (hp->bytes_per_unit * hp->channels));
return (offset);
}
double
audio_str_to_secs(char *str)
{
double val;
char *str2;
val = strtod(str, &str2);
if (str2 == str)
return (HUGE_VAL);
if (*str2 == ':') {
val *= 60.;
str = str2 + 1;
val += strtod(str, &str2);
if (str2 == str)
return (HUGE_VAL);
}
if (*str2 == ':') {
val *= 60.;
str = str2 + 1;
val += strtod(str, &str2);
if (str2 == str)
return (HUGE_VAL);
}
if (*str2 != '\0')
return (HUGE_VAL);
return (val);
}
char *
audio_secs_to_str(double sec, char *str, int precision)
{
char *p;
unsigned ovflow;
int hours;
double x;
char buf[64];
if (sec == HUGE_VAL) {
(void) strcpy(str, "0:00");
return (str);
}
if ((precision > 10) || (precision < 0))
precision = 10;
p = str;
if (sec < 0.) {
sec = -sec;
(void) sprintf(buf, "%.*f", precision, sec);
(void) sscanf(buf, "%lf", &sec);
if (sec > 0.)
*p++ = '-';
}
x = fmod(sec, 60.);
sec -= x;
(void) sprintf(buf, "%.*f", precision, x);
(void) sscanf(buf, "%lf", &x);
sec += x;
if (sec >= 60.) {
ovflow = ((unsigned)sec) / 60;
sec -= (double)(ovflow * 60);
hours = (ovflow >= 60);
if (hours) {
(void) sprintf(p, "%d:", ovflow / 60);
p = &p[strlen(p)];
ovflow %= 60;
}
(void) sprintf(p, "%0*d:", (hours ? 2 : 1), ovflow);
p = &p[strlen(p)];
} else {
*p++ = '0';
*p++ = ':';
}
if (sec < 10.)
*p++ = '0';
(void) sprintf(p, "%.*f", precision, sec);
return (str);
}
int
audio_cmp_hdr(Audio_hdr *h1, Audio_hdr *h2)
{
if ((h1->encoding != h2->encoding) ||
(h1->bytes_per_unit != h2->bytes_per_unit) ||
(h1->channels != h2->channels) ||
(h1->samples_per_unit != h2->samples_per_unit))
return (-1);
if (h1->sample_rate != h2->sample_rate)
return (1);
return (0);
}
int
audio_enc_to_str(Audio_hdr *hdrp, char *str)
{
char *chan;
char *prec;
char *enc;
char cbuf[AUDIO_MAX_ENCODE_INFO];
char pbuf[AUDIO_MAX_ENCODE_INFO];
char sbuf[AUDIO_MAX_ENCODE_INFO];
int err;
err = AUDIO_SUCCESS;
switch (hdrp->channels) {
case 0:
chan = "(zero channels?)";
err = AUDIO_ERR_BADHDR;
break;
case 1:
chan = "mono"; break;
case 2:
chan = "stereo"; break;
case 4:
chan = "quad"; break;
default:
chan = pbuf;
(void) sprintf(cbuf, "%u-channel", hdrp->channels); break;
}
switch (hdrp->encoding) {
case AUDIO_ENCODING_ULAW:
enc = "u-law";
goto pcm;
case AUDIO_ENCODING_ALAW:
enc = "A-law";
goto pcm;
case AUDIO_ENCODING_LINEAR:
enc = "linear PCM";
goto pcm;
case AUDIO_ENCODING_FLOAT:
enc = "floating-point";
pcm:
if (hdrp->samples_per_unit != 1)
goto unknown;
prec = pbuf;
(void) sprintf(pbuf, "%u-bit", hdrp->bytes_per_unit * 8);
break;
default:
unknown:
err = AUDIO_ERR_ENCODING;
enc = "(unknown encoding?)";
if (hdrp->samples_per_unit != 0) {
prec = pbuf;
(void) sprintf(pbuf, "%f-bit",
(double)(hdrp->bytes_per_unit * 8) /
(double)hdrp->samples_per_unit);
} else {
prec = "(unknown precision?)";
err = AUDIO_ERR_BADHDR;
}
}
(void) sprintf(sbuf, "%.3fkHz", ((double)hdrp->sample_rate / 1000.));
(void) sprintf(str, "%s %s %s @ %s", chan, prec, enc, sbuf);
return (err);
}