summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Wright <jason@cvs.openbsd.org>2007-04-25 15:27:55 +0000
committerJason Wright <jason@cvs.openbsd.org>2007-04-25 15:27:55 +0000
commit819a78728db9eafb602cdfc225453828db2ebeeb (patch)
tree1eb67a39e17e55e2140b660d3ab27603ebeac587
parentddc11f5123c057e730ab87812cb978b141ba517a (diff)
- add ability to specify a tone to be played (defaults to 440hz)
- stop using atoi ... use strtod/strtol - fixing timing stuff for alaw/8
-rw-r--r--regress/sys/dev/audio/autest.19
-rw-r--r--regress/sys/dev/audio/autest.c78
2 files changed, 74 insertions, 13 deletions
diff --git a/regress/sys/dev/audio/autest.1 b/regress/sys/dev/audio/autest.1
index 3ec6a53829b..5d7aae0b838 100644
--- a/regress/sys/dev/audio/autest.1
+++ b/regress/sys/dev/audio/autest.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: autest.1,v 1.8 2005/09/27 18:11:02 jmc Exp $
+.\" $OpenBSD: autest.1,v 1.9 2007/04/25 15:27:54 jason Exp $
.\"
.\" Copyright (c) 2002 Jason L. Wright (jason@thought.net)
.\" All rights reserved.
@@ -34,14 +34,15 @@
.Nm autest
.Op Fl f Ar device
.Op Fl r Ar rate
+.Op Fl t Ar tone
.Sh DESCRIPTION
The
.Nm
utility opens an
.Xr audio 4
device and iterates through all of the encodings supported by the device,
-playing a 440Hz tone in the proper format.
-The tone should sound almost identical in each of the formats.
+playing a tone in the proper format.
+The tone should sound identical in each of the formats.
.Pp
The options are as follows:
.Bl -tag -width Ds
@@ -59,6 +60,8 @@ to test.
It will request the audio subsystem to play that Hz;
however the audio device may return a different speed.
This can be useful to test different speeds, e.g. 8000, 44100, 48000.
+.It Fl t Ar tone
+Specify the tone to be played (default 440Hz).
.El
.Pp
.Nm
diff --git a/regress/sys/dev/audio/autest.c b/regress/sys/dev/audio/autest.c
index 171b99dbcb6..fd2a31f9856 100644
--- a/regress/sys/dev/audio/autest.c
+++ b/regress/sys/dev/audio/autest.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: autest.c,v 1.11 2005/09/27 02:53:43 drahn Exp $ */
+/* $OpenBSD: autest.c,v 1.12 2007/04/25 15:27:54 jason Exp $ */
/*
* Copyright (c) 2002 Jason L. Wright (jason@thought.net)
@@ -37,6 +37,7 @@
#include <math.h>
#include <unistd.h>
#include <errno.h>
+#include <limits.h>
/* XXX ADPCM is currently pretty broken... diagnosis and fix welcome */
#undef USE_ADPCM
@@ -69,13 +70,50 @@ void enc_adpcm_8(int, audio_encoding_t *, int, int);
void audio_wait(int);
void check_srate(struct ausrate *);
void mark_time(struct timeval *);
+int get_int(const char *, int *);
+int get_double(const char *, double *);
#define PLAYFREQ 440.0
#define PLAYSECS 2
+double playfreq = PLAYFREQ;
#define DEFAULT_DEV "/dev/sound"
int
+get_double(const char *buf, double *d)
+{
+ char *ep;
+ long dd;
+
+ errno = 0;
+ dd = strtod(buf, &ep);
+ if (buf[0] == '\0' || *ep != '\0')
+ return (-1);
+ if (errno == ERANGE && (dd == -HUGE_VAL || dd == HUGE_VAL))
+ return (-1);
+ *d = dd;
+ return (0);
+}
+
+int
+get_int(const char *buf, int *i)
+{
+ char *ep;
+ long lv;
+
+ errno = 0;
+ lv = strtol(buf, &ep, 10);
+ if (buf[0] == '\0' || *ep != '\0')
+ return (-1);
+ if (errno == ERANGE && (lv == LONG_MAX || lv == LONG_MIN))
+ return (-1);
+ if (lv < INT_MIN || lv > INT_MAX)
+ return (-1);
+ *i = lv;
+ return (0);
+}
+
+int
main(int argc, char **argv)
{
audio_info_t ainfo;
@@ -83,13 +121,24 @@ main(int argc, char **argv)
int fd, i, c;
int rate = 8000;
- while ((c = getopt(argc, argv, "f:r:")) != -1) {
+ while ((c = getopt(argc, argv, "f:r:t:")) != -1) {
switch (c) {
case 'f':
fname = optarg;
break;
case 'r':
- rate = atoi(optarg);
+ if (get_int(optarg, &rate) || rate <= 0) {
+ fprintf(stderr, "%s bad rate %s\n",
+ argv[0], optarg);
+ return (1);
+ }
+ break;
+ case 't':
+ if (get_double(optarg, &playfreq) || playfreq <= 0.0) {
+ fprintf(stderr, "%s bad freq %s\n",
+ argv[0], optarg);
+ return (1);
+ }
break;
case '?':
default:
@@ -314,7 +363,7 @@ enc_ulinear_8(int fd, audio_encoding_t *enc, int chans, int rate)
u_int8_t v;
d = 127.0 * sinf(((float)i / (float)inf.play.sample_rate) *
- (2 * M_PI * PLAYFREQ));
+ (2 * M_PI * playfreq));
d = rintf(d + 127.0);
v = d;
@@ -375,7 +424,7 @@ enc_slinear_8(int fd, audio_encoding_t *enc, int chans, int rate)
int8_t v;
d = 127.0 * sinf(((float)i / (float)inf.play.sample_rate) *
- (2 * M_PI * PLAYFREQ));
+ (2 * M_PI * playfreq));
d = rintf(d);
v = d;
@@ -436,7 +485,7 @@ enc_slinear_16(int fd, audio_encoding_t *enc, int chans, int order, int rate)
int16_t v;
d = 32767.0 * sinf(((float)i / (float)inf.play.sample_rate) *
- (2 * M_PI * PLAYFREQ));
+ (2 * M_PI * playfreq));
d = rintf(d);
v = d;
@@ -506,7 +555,7 @@ enc_ulinear_16(int fd, audio_encoding_t *enc, int chans, int order, int rate)
u_int16_t v;
d = 32767.0 * sinf(((float)i / (float)inf.play.sample_rate) *
- (2 * M_PI * PLAYFREQ));
+ (2 * M_PI * playfreq));
d = rintf(d + 32767.0);
v = d;
@@ -580,7 +629,7 @@ enc_adpcm_8(int fd, audio_encoding_t *enc, int chans, int rate)
float d;
d = 32767.0 * sinf(((float)i / (float)inf.play.sample_rate) *
- (2 * M_PI * PLAYFREQ));
+ (2 * M_PI * playfreq));
samples[i] = rintf(d);
}
@@ -657,7 +706,7 @@ enc_ulaw_8(int fd, audio_encoding_t *enc, int chans, int rate)
float x;
x = 32765.0 * sinf(((float)i / (float)inf.play.sample_rate) *
- (2 * M_PI * PLAYFREQ));
+ (2 * M_PI * playfreq));
samples[i] = x;
}
@@ -687,6 +736,7 @@ void
enc_alaw_8(int fd, audio_encoding_t *enc, int chans, int rate)
{
audio_info_t inf;
+ struct ausrate rt;
int16_t *samples = NULL;
int i, j;
u_int8_t *outbuf = NULL, *p;
@@ -706,6 +756,10 @@ enc_alaw_8(int fd, audio_encoding_t *enc, int chans, int rate)
printf("[getinfo: %s]", strerror(errno));
goto out;
}
+ rt.r_rate = inf.play.sample_rate;
+ rt.s_rate = inf.play.sample_rate;
+ rt.bps = 1* chans;
+ rt.bytes = inf.play.sample_rate * chans * PLAYSECS;
samples = (int16_t *)calloc(inf.play.sample_rate, sizeof(*samples));
if (samples == NULL) {
@@ -723,7 +777,7 @@ enc_alaw_8(int fd, audio_encoding_t *enc, int chans, int rate)
float x;
x = 32767.0 * sinf(((float)i / (float)inf.play.sample_rate) *
- (2 * M_PI * PLAYFREQ));
+ (2 * M_PI * playfreq));
samples[i] = x;
}
@@ -734,10 +788,14 @@ enc_alaw_8(int fd, audio_encoding_t *enc, int chans, int rate)
}
}
+ mark_time(&rt.tv_begin);
for (i = 0; i < PLAYSECS; i++) {
write(fd, outbuf, inf.play.sample_rate * chans);
}
audio_wait(fd);
+ mark_time(&rt.tv_end);
+ check_srate(&rt);
+
out:
if (samples != NULL)