4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (C) 4Front Technologies 1996-2008.
24 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
27 * This program is a general purpose test facility for audio output.
28 * It does not test record.
30 * The wavedata.c and wavedata.h files contain the actual samples compressed
31 * using the MS ADPCM algorithm.
42 #include <sys/ioctl.h>
43 #include <sys/utsname.h>
44 #include <sys/soundcard.h>
48 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
49 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
52 #define _(s) gettext(s)
57 #define CH_LEFT (1 << 0)
58 #define CH_RIGHT (1 << 1)
59 #define CH_LREAR4 (1 << 2) /* quadraphonic */
60 #define CH_RREAR4 (1 << 3) /* quadraphonic */
61 #define CH_CENTER (1 << 2)
62 #define CH_LFE (1 << 3)
63 #define CH_LSURR (1 << 4)
64 #define CH_RSURR (1 << 5)
65 #define CH_LREAR (1 << 6)
66 #define CH_RREAR (1 << 7)
67 #define CH_STEREO (CH_LEFT|CH_RIGHT)
68 #define CH_4 (CH_STEREO | CH_LREAR4 | CH_RREAR4)
69 #define CH_5 (CH_STEREO | CH_CENTER | CH_LSURR | CH_RSURR)
70 #define CH_7 (CH_5 | CH_LREAR | CH_RREAR)
72 typedef struct chancfg
{
80 typedef struct testcfg
{
86 #define CFLAG_LFE 0x1 /* lfe channel - not full range */
89 * TRANSLATION_NOTE : The following strings are displayed during progress.
90 * Its important for alignment that they have the same displayed length.
92 #define NM_LEFT "\t<left> ................"
93 #define NM_RIGHT "\t<right> ..............."
94 #define NM_LREAR "\t<left rear> ..........."
95 #define NM_RREAR "\t<right rear> .........."
96 #define NM_LSIDE "\t<left side> ..........."
97 #define NM_RSIDE "\t<right side> .........."
98 #define NM_CENTER "\t<center> .............."
99 #define NM_LFE "\t<lfe> ................."
100 #define NM_STEREO "\t<stereo> .............."
101 #define NM_40 "\t<4.0 surround> ........"
102 #define NM_50 "\t<5.0 surround> ........"
103 #define NM_70 "\t<7.0 surround> ........"
105 chancfg_t ch_left
= { CH_LEFT
, NM_LEFT
, 0 };
106 chancfg_t ch_right
= { CH_RIGHT
, NM_RIGHT
, 0 };
107 chancfg_t ch_stereo
= { CH_STEREO
, NM_STEREO
, 0 };
109 chancfg_t ch_center
= { CH_CENTER
, NM_CENTER
, 0 };
110 chancfg_t ch_lfe
= { CH_LFE
, NM_LFE
, CFLAG_LFE
};
112 chancfg_t ch_lsurr_4
= { (1 << 2), NM_LREAR
, 0 };
113 chancfg_t ch_rsurr_4
= { (1 << 3), NM_RREAR
, 0 };
114 chancfg_t ch_4
= { CH_4
, NM_40
, 0 };
116 chancfg_t ch_lsurr_5
= { CH_LSURR
, NM_LREAR
, 0 };
117 chancfg_t ch_rsurr_5
= { CH_RSURR
, NM_RREAR
, 0 };
118 chancfg_t ch_5
= { CH_5
, NM_50
, 0 };
120 chancfg_t ch_lsurr_7
= { CH_LSURR
, NM_LSIDE
, 0 };
121 chancfg_t ch_rsurr_7
= { CH_RSURR
, NM_RSIDE
, 0 };
122 chancfg_t ch_lrear_7
= { CH_LREAR
, NM_LREAR
, 0 };
123 chancfg_t ch_rrear_7
= { CH_RREAR
, NM_RREAR
, 0 };
124 chancfg_t ch_7
= { CH_7
, NM_70
, 0 };
126 testcfg_t test_stereo
= {
127 2, 48000, { &ch_left
, &ch_right
, &ch_stereo
, NULL
}
130 testcfg_t test_quad
= {
131 4, 48000, { &ch_left
, &ch_right
, &ch_stereo
,
132 &ch_lsurr_4
, &ch_rsurr_4
, &ch_4
, NULL
}
135 testcfg_t test_51
= {
136 6, 48000, { &ch_left
, &ch_right
, &ch_stereo
,
137 &ch_lsurr_5
, &ch_rsurr_5
, &ch_center
, &ch_lfe
, &ch_5
, NULL
}
140 testcfg_t test_71
= {
141 8, 48000, { &ch_left
, &ch_right
, &ch_stereo
,
142 &ch_lsurr_7
, &ch_rsurr_7
, &ch_lrear_7
, &ch_rrear_7
,
143 &ch_center
, &ch_lfe
, &ch_7
, NULL
}
147 * uncompress_wave() is defined in wavedata.c. It expands the audio
148 * samples stored in wavedata.h and returns the lenghth of the
149 * uncompressed version in bytes.
151 * The uncompressed wave data format is 16 bit (native) stereo
152 * recorded at 48000 Hz.
154 extern int uncompress_wave(short *outbuf
);
159 extern void describe_error(int);
161 #define SAMPLE_RATE 48000
164 * Operating mode flags (set from the command line).
166 #define TF_LOOP 0x00000010 /* Loop until interrupted */
169 static int num_devices_tested
= 0;
171 static short *sample_buf
;
174 prepare(testcfg_t
*tcfg
)
179 if ((sample_buf
= malloc(2000000)) == NULL
) {
184 data_len
= uncompress_wave(sample_buf
);
185 nsamples
= (data_len
/ sizeof (int16_t)) / 2;
187 for (i
= 0; (ccfg
= tcfg
->tests
[i
]) != NULL
; i
++) {
194 rate_multiple
= tcfg
->rate
/ 48000;
196 if (ccfg
->flags
!= CFLAG_LFE
) {
197 ccfg
->len
= nsamples
* tcfg
->nchan
*
198 sizeof (int16_t) * rate_multiple
;
199 ccfg
->data
= malloc(ccfg
->len
);
200 if ((dst
= ccfg
->data
) == NULL
) {
204 for (samp
= 0; samp
< nsamples
; samp
++) {
205 for (ch
= 0; ch
< tcfg
->nchan
; ch
++) {
206 for (j
= 0; j
< rate_multiple
; j
++) {
207 *dst
= ((1U << ch
) & ccfg
->mask
)
215 /* Skip LFE for now */
222 * The testdsp() routine checks the capabilities of a given audio device number
223 * (parameter n) and decides if the test sound needs to be played.
228 testdsp(int hd
, int flags
, testcfg_t
*tcfg
)
231 struct timeval t1
, t2
;
235 long long total_bytes
= 0;
236 unsigned int tmp
, caps
;
241 if (ioctl(hd
, SNDCTL_DSP_GETCAPS
, &caps
) == -1) {
242 perror("SNDCTL_DSP_GETCAPS");
247 * Setup the sample format. Since OSS will support AFMT_S16_NE
248 * regardless of the device we do not need to support any
252 if (ioctl(hd
, SNDCTL_DSP_SETFMT
, &tmp
) == -1 || tmp
!= AFMT_S16_NE
) {
253 (void) printf(_("Device doesn't support native 16-bit PCM\n"));
258 * Setup the device for channels. Once again we can simply
259 * assume that stereo will always work before OSS takes care
260 * of this by emulation if necessary.
263 if (ioctl(hd
, SNDCTL_DSP_CHANNELS
, &tmp
) == -1 || tmp
!= tcfg
->nchan
) {
264 (void) printf(_("The device doesn't support %d channels\n"),
270 * Set up the sample rate.
273 if (ioctl(hd
, SNDCTL_DSP_SPEED
, &tmp
) == -1) {
274 perror("SNDCTL_DSP_SPEED");
279 if (sample_rate
!= tcfg
->rate
) {
280 (void) printf(_("The device doesn't support %d Hz\n"),
287 * This program will measure the real sampling rate by
288 * computing the total time required to play the sample.
290 * This is not terribly presice with short test sounds but it
291 * can be used to detect if the sampling rate badly
292 * wrong. Errors of few percents is more likely to be caused
293 * by poor accuracy of the system clock rather than problems
294 * with the sampling rate.
296 (void) gettimeofday(&t1
, NULL
);
298 for (i
= 0; (ccfg
= tcfg
->tests
[i
]) != NULL
; i
++) {
299 (void) fputs(_(ccfg
->name
), stdout
);
300 (void) fflush(stdout
);
301 if (ccfg
->flags
& CFLAG_LFE
) {
302 (void) printf(_("SKIPPED\n"));
306 if (write(hd
, ccfg
->data
, ccfg
->len
) < 0) {
307 (void) printf(_("ERROR: %s\n"),
311 (void) printf(_("OK\n"));
312 total_bytes
+= ccfg
->len
;
315 (void) gettimeofday(&t2
, NULL
);
317 (void) ioctl(hd
, SNDCTL_DSP_GETODELAY
, &delay
); /* Ignore errors */
320 * Perform the time computations using milliseconds.
323 t
= t2
.tv_sec
- t1
.tv_sec
;
326 t
+= t2
.tv_usec
/ 1000;
327 t
-= t1
.tv_usec
/ 1000;
329 total_bytes
-= delay
;
333 total_bytes
/= (tcfg
->nchan
* sizeof (int16_t));
335 ratio
= ((float)total_bytes
/ (float)sample_rate
) * 100.0;
336 (void) printf(_("\t<measured sample rate %8.2f Hz (%4.2f%%)>\n"),
337 (float)sample_rate
* ratio
/ 100.0, ratio
- 100.0);
338 num_devices_tested
++;
344 find_num_devices(void)
349 * Find out the number of available audio devices by calling
353 if (ioctl(mixerfd
, SNDCTL_SYSINFO
, &info
) == -1) {
354 if (errno
== ENXIO
) {
355 (void) fprintf(stderr
,
356 _("No supported sound hardware detected.\n"));
359 perror("SNDCTL_SYSINFO");
360 (void) printf(_("Cannot get system information.\n"));
364 (void) printf(_("Sound subsystem and version: %s %s (0x%08X)\n"),
365 info
.product
, info
.version
, info
.versionnum
);
367 if (uname(&un
) != -1)
368 (void) printf(_("Platform: %s %s %s %s\n"),
369 un
.sysname
, un
.release
, un
.version
, un
.machine
);
371 return (info
.numaudios
);
375 * The test_device() routine checks certain information about the device
376 * and calls testdsp() to play the test sound.
380 test_device(char *dn
, int flags
, testcfg_t
*tcfg
)
386 fd
= open(dn
, O_WRONLY
, 0);
391 describe_error(errno
);
396 if (ioctl(fd
, SNDCTL_AUDIOINFO
, &ainfo
) == -1) {
397 perror("SNDCTL_AUDIOINFO");
402 (void) printf(_("\n*** Scanning sound adapter #%d ***\n"),
405 (void) printf(_("%s (audio engine %d): %s\n"), ainfo
.devnode
, ainfo
.dev
,
408 if (!ainfo
.enabled
) {
409 (void) printf(_(" - Device not present - Skipping\n"));
414 if (!(ainfo
.caps
& PCM_CAP_OUTPUT
)) {
415 (void) printf(_(" - Skipping input only device\n"));
420 (void) printf(_(" - Performing audio playback test... "));
421 (void) fflush(stdout
);
423 code
= testdsp(fd
, flags
, tcfg
);
433 describe_error(int err
)
437 (void) fprintf(stderr
,
438 _("The device file was found in /dev but\n"
439 "the driver was not loaded.\n"));
443 (void) fprintf(stderr
,
444 _("There are no sound devices available.\n"
445 "The most likely reason is that the device you have\n"
446 "is malfunctioning or it's not supported.\n"
447 "It's also possible that you are trying to use the wrong "
452 (void) fprintf(stderr
,
453 _("Your system cannot allocate memory for the device\n"
454 "buffers. Reboot your machine and try again.\n"));
458 (void) fprintf(stderr
,
459 _("The device file is missing from /dev.\n"));
464 (void) fprintf(stderr
,
465 _("The device is busy. There is some other application\n"
475 main(int argc
, char *argv
[])
486 (void) setlocale(LC_ALL
, "");
487 (void) textdomain(TEXT_DOMAIN
);
492 * Simple command line switch handling.
495 while ((i
= getopt(argc
, argv
, "l2457r:")) != EOF
) {
513 tcfg
->rate
= atoi(optarg
);
516 (void) printf(_("Usage: %s [options...] [device]\n"
518 " -4 Quadraphonic 4.0 test\n"
519 " -5 Surround 5.1 test\n"
520 " -7 Surround 7.1 test\n"
521 " -r Sample Rate (48000|96000|192000)\n"
522 " -l Loop test\n"), argv
[0]);
528 * Open the mixer device used for calling SNDCTL_SYSINFO and
531 if ((mixerfd
= open("/dev/mixer", O_RDWR
, 0)) == -1) {
533 perror("/dev/mixer");
535 describe_error(errno
);
539 prepare(tcfg
); /* Prepare the wave data */
542 * Enumerate all devices and play the test sounds.
544 maxdev
= find_num_devices();
546 (void) printf(_("\n*** No audio hardware available ***\n"));
550 numdev
= (argc
- optind
);
558 for (t
= 0; t
< numdev
; t
++) {
559 dn
= argv
[optind
+ t
];
560 rv
= test_device(dn
, flags
, tcfg
);
568 for (t
= 0; t
< maxdev
; t
++) {
570 if (ioctl(mixerfd
, SNDCTL_AUDIOINFO
,
572 perror("SNDCTL_AUDIOINFO");
577 rv
= test_device(dn
, flags
, tcfg
);
587 (void) printf(_("\n*** All tests completed OK ***\n"));
589 (void) printf(_("\n*** Errors were detected ***\n"));
591 } while (status
&& (flags
& TF_LOOP
));
593 (void) close(mixerfd
);