summaryrefslogtreecommitdiff
path: root/usr/src/cmd/audio/audiotest/audiotest.c
blob: 29dce5feed4ab60eacd1ed77ffcbdc0eba74dbb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
 */
/*
 * This program is a general purpose test facility for audio output.
 * It does not test record.
 *
 * The wavedata.c and wavedata.h files contain the actual samples compressed
 * using the MS ADPCM algorithm.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <sys/soundcard.h>
#include <inttypes.h>
#include <locale.h>

#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
#endif

#define	_(s)	gettext(s)

/*
 * Channel selectors
 */
#define	CH_LEFT		(1 << 0)
#define	CH_RIGHT	(1 << 1)
#define	CH_LREAR4	(1 << 2)	/* quadraphonic */
#define	CH_RREAR4	(1 << 3)	/* quadraphonic */
#define	CH_CENTER	(1 << 2)
#define	CH_LFE		(1 << 3)
#define	CH_LSURR	(1 << 4)
#define	CH_RSURR	(1 << 5)
#define	CH_LREAR	(1 << 6)
#define	CH_RREAR	(1 << 7)
#define	CH_STEREO	(CH_LEFT|CH_RIGHT)
#define	CH_4		(CH_STEREO | CH_LREAR4 | CH_RREAR4)
#define	CH_5		(CH_STEREO | CH_CENTER | CH_LSURR | CH_RSURR)
#define	CH_7		(CH_5 | CH_LREAR | CH_RREAR)

typedef struct chancfg {
	int		mask;
	const char	*name;
	unsigned	flags;
	int16_t		*data;
	int		len;
} chancfg_t;

typedef struct testcfg {
	int		nchan;
	uint32_t	rate;
	chancfg_t	*tests[16];
} testcfg_t;

#define	CFLAG_LFE	0x1	/* lfe channel - not full range */

/*
 * TRANSLATION_NOTE : The following strings are displayed during progress.
 * Its important for alignment that they have the same displayed length.
 */
#define	NM_LEFT		"\t<left> ................"
#define	NM_RIGHT	"\t<right> ..............."
#define	NM_LREAR	"\t<left rear> ..........."
#define	NM_RREAR	"\t<right rear> .........."
#define	NM_LSIDE	"\t<left side> ..........."
#define	NM_RSIDE	"\t<right side> .........."
#define	NM_CENTER	"\t<center> .............."
#define	NM_LFE		"\t<lfe> ................."
#define	NM_STEREO	"\t<stereo> .............."
#define	NM_40		"\t<4.0 surround> ........"
#define	NM_50		"\t<5.0 surround> ........"
#define	NM_70		"\t<7.0 surround> ........"

chancfg_t ch_left = { CH_LEFT, NM_LEFT, 0 };
chancfg_t ch_right = { CH_RIGHT, NM_RIGHT, 0 };
chancfg_t ch_stereo = { CH_STEREO, NM_STEREO, 0 };

chancfg_t ch_center = { CH_CENTER, NM_CENTER, 0 };
chancfg_t ch_lfe = { CH_LFE, NM_LFE, CFLAG_LFE };

chancfg_t ch_lsurr_4 = { (1 << 2), NM_LREAR, 0 };
chancfg_t ch_rsurr_4 = { (1 << 3), NM_RREAR, 0 };
chancfg_t ch_4 = { CH_4, NM_40, 0 };

chancfg_t ch_lsurr_5 = { CH_LSURR, NM_LREAR, 0 };
chancfg_t ch_rsurr_5 = { CH_RSURR, NM_RREAR, 0 };
chancfg_t ch_5 = { CH_5, NM_50, 0 };

chancfg_t ch_lsurr_7 = { CH_LSURR, NM_LSIDE, 0 };
chancfg_t ch_rsurr_7 = { CH_RSURR, NM_RSIDE, 0 };
chancfg_t ch_lrear_7 = { CH_LREAR, NM_LREAR, 0 };
chancfg_t ch_rrear_7 = { CH_RREAR, NM_RREAR, 0 };
chancfg_t ch_7 = { CH_7, NM_70, 0 };

testcfg_t test_stereo = {
	2, 48000, { &ch_left, &ch_right, &ch_stereo, NULL }
};

testcfg_t test_quad = {
	4, 48000, { &ch_left, &ch_right, &ch_stereo,
	&ch_lsurr_4, &ch_rsurr_4, &ch_4, NULL }
};

testcfg_t test_51 = {
	6, 48000, { &ch_left, &ch_right, &ch_stereo,
	&ch_lsurr_5, &ch_rsurr_5, &ch_center, &ch_lfe, &ch_5, NULL }
};

testcfg_t test_71 = {
	8, 48000, { &ch_left, &ch_right, &ch_stereo,
	&ch_lsurr_7, &ch_rsurr_7, &ch_lrear_7, &ch_rrear_7,
	&ch_center, &ch_lfe, &ch_7, NULL }
};

/*
 * uncompress_wave() is defined in wavedata.c. It expands the audio
 * samples stored in wavedata.h and returns the lenghth of the
 * uncompressed version in bytes.
 *
 * The uncompressed wave data format is 16 bit (native) stereo
 * recorded at 48000 Hz.
 */
extern int uncompress_wave(short *outbuf);

static int data_len;

#define	MAXDEVICE   64
extern void describe_error(int);

#define	SAMPLE_RATE 48000

/*
 * Operating mode flags (set from the command line).
 */
#define	TF_LOOP		0x00000010	/* Loop until interrupted */

static int mixerfd;
static int num_devices_tested = 0;

static short *sample_buf;

void
prepare(testcfg_t *tcfg)
{
	int	nsamples;
	int	i, j;
	chancfg_t	*ccfg;
	if ((sample_buf = malloc(2000000)) == NULL) {
		perror("malloc");
		exit(-1);
	}

	data_len = uncompress_wave(sample_buf);
	nsamples = (data_len / sizeof (int16_t)) / 2;

	for (i = 0; (ccfg = tcfg->tests[i]) != NULL; i++) {
		int16_t		*src, *dst;
		int		ch;
		int		samp;
		int		rate_multiple;

		src = sample_buf;
		rate_multiple = tcfg->rate / 48000;

		if (ccfg->flags != CFLAG_LFE) {
			ccfg->len = nsamples * tcfg->nchan *
			    sizeof (int16_t) * rate_multiple;
			ccfg->data = malloc(ccfg->len);
			if ((dst = ccfg->data) == NULL) {
				perror("malloc");
				exit(-1);
			}
			for (samp = 0; samp < nsamples; samp++) {
				for (ch = 0; ch < tcfg->nchan; ch++) {
					for (j = 0; j < rate_multiple; j++) {
						*dst = ((1U << ch) & ccfg->mask)
						    ? *src : 0;
						dst++;
					}
				}
				src += 2;
			}
		} else {
			/* Skip LFE for now */
			ccfg->len = 0;
		}
	}
}

/*
 * The testdsp() routine checks the capabilities of a given audio device number
 * (parameter n) and decides if the test sound needs to be played.
 */

/*ARGSUSED*/
int
testdsp(int hd, int flags, testcfg_t *tcfg)
{
	float ratio;
	struct timeval t1, t2;
	unsigned long t;
	int sample_rate;
	int delay;
	long long total_bytes = 0;
	unsigned int tmp, caps;
	int i;
	chancfg_t *ccfg;

	caps = 0;
	if (ioctl(hd, SNDCTL_DSP_GETCAPS, &caps) == -1) {
		perror("SNDCTL_DSP_GETCAPS");
		return (-1);
	}

	/*
	 * Setup the sample format. Since OSS will support AFMT_S16_NE
	 * regardless of the device we do not need to support any
	 * other formats.
	 */
	tmp = AFMT_S16_NE;
	if (ioctl(hd, SNDCTL_DSP_SETFMT, &tmp) == -1 || tmp != AFMT_S16_NE) {
		(void) printf(_("Device doesn't support native 16-bit PCM\n"));
		return (-1);
	}

	/*
	 * Setup the device for channels. Once again we can simply
	 * assume that stereo will always work before OSS takes care
	 * of this by emulation if necessary.
	 */
	tmp = tcfg->nchan;
	if (ioctl(hd, SNDCTL_DSP_CHANNELS, &tmp) == -1 || tmp != tcfg->nchan) {
		(void) printf(_("The device doesn't support %d channels\n"),
		    tcfg->nchan);
		return (-2);
	}

	/*
	 * Set up the sample rate.
	 */
	tmp = tcfg->rate;
	if (ioctl(hd, SNDCTL_DSP_SPEED, &tmp) == -1) {
		perror("SNDCTL_DSP_SPEED");
		return (-3);
	}

	sample_rate = tmp;
	if (sample_rate != tcfg->rate) {
		(void) printf(_("The device doesn't support %d Hz\n"),
		    tcfg->rate);
		return (-3);
	}
	(void) printf("\n");

	/*
	 * This program will measure the real sampling rate by
	 * computing the total time required to play the sample.
	 *
	 * This is not terribly presice with short test sounds but it
	 * can be used to detect if the sampling rate badly
	 * wrong. Errors of few percents is more likely to be caused
	 * by poor accuracy of the system clock rather than problems
	 * with the sampling rate.
	 */
	(void) gettimeofday(&t1, NULL);

	for (i = 0; (ccfg = tcfg->tests[i]) != NULL; i++) {
		(void) fputs(_(ccfg->name), stdout);
		(void) fflush(stdout);
		if (ccfg->flags & CFLAG_LFE) {
			(void) printf(_("SKIPPED\n"));
			continue;
		}

		if (write(hd, ccfg->data, ccfg->len) < 0) {
			(void) printf(_("ERROR: %s\n"),
			    strerror(errno));
			return (-3);
		}
		(void) printf(_("OK\n"));
		total_bytes += ccfg->len;
	}

	(void) gettimeofday(&t2, NULL);
	delay = 0;
	(void) ioctl(hd, SNDCTL_DSP_GETODELAY, &delay);	/* Ignore errors */

	/*
	 * Perform the time computations using milliseconds.
	 */

	t = t2.tv_sec - t1.tv_sec;
	t *= 1000;

	t += t2.tv_usec / 1000;
	t -= t1.tv_usec / 1000;

	total_bytes -= delay;
	total_bytes *= 1000;

	total_bytes /= t;
	total_bytes /= (tcfg->nchan * sizeof (int16_t));

	ratio = ((float)total_bytes / (float)sample_rate) * 100.0;
	(void) printf(_("\t<measured sample rate %8.2f Hz (%4.2f%%)>\n"),
	    (float)sample_rate * ratio / 100.0, ratio - 100.0);
	num_devices_tested++;

	return (1);
}

static int
find_num_devices(void)
{
	oss_sysinfo info;
	struct utsname un;
	/*
	 * Find out the number of available audio devices by calling
	 * SNDCTL_SYSINFO.
	 */

	if (ioctl(mixerfd, SNDCTL_SYSINFO, &info) == -1) {
		if (errno == ENXIO) {
			(void) fprintf(stderr,
			    _("No supported sound hardware detected.\n"));
			exit(-1);
		} else {
			perror("SNDCTL_SYSINFO");
			(void) printf(_("Cannot get system information.\n"));
			exit(-1);
		}
	}
	(void) printf(_("Sound subsystem and version: %s %s (0x%08X)\n"),
	    info.product, info.version, info.versionnum);

	if (uname(&un) != -1)
		(void) printf(_("Platform: %s %s %s %s\n"),
		    un.sysname, un.release, un.version, un.machine);

	return (info.numaudios);
}

/*
 * The test_device() routine checks certain information about the device
 * and calls testdsp() to play the test sound.
 */

int
test_device(char *dn, int flags, testcfg_t *tcfg)
{
	oss_audioinfo ainfo;
	int code;
	int fd;

	fd = open(dn, O_WRONLY, 0);
	if (fd == -1) {
		int err = errno;
		perror(dn);
		errno = err;
		describe_error(errno);
		return (-1);
	}

	ainfo.dev = -1;
	if (ioctl(fd, SNDCTL_AUDIOINFO, &ainfo) == -1) {
		perror("SNDCTL_AUDIOINFO");
		(void) close(fd);
		return (-1);
	}

	(void) printf(_("\n*** Scanning sound adapter #%d ***\n"),
	    ainfo.card_number);

	(void) printf(_("%s (audio engine %d): %s\n"), ainfo.devnode, ainfo.dev,
	    ainfo.name);

	if (!ainfo.enabled) {
		(void) printf(_("  - Device not present - Skipping\n"));
		(void) close(fd);
		return (0);
	}

	if (!(ainfo.caps & PCM_CAP_OUTPUT)) {
		(void) printf(_("  - Skipping input only device\n"));
		(void) close(fd);
		return (0);
	}

	(void) printf(_("  - Performing audio playback test... "));
	(void) fflush(stdout);

	code = testdsp(fd, flags, tcfg);
	(void) close(fd);
	if (code < 0) {
		return (code);
	}

	return (code == 1);
}

void
describe_error(int err)
{
	switch (err) {
	case ENODEV:
		(void) fprintf(stderr,
		    _("The device file was found in /dev but\n"
		    "the driver was not loaded.\n"));
		break;

	case ENXIO:
		(void) fprintf(stderr,
		    _("There are no sound devices available.\n"
		    "The most likely reason is that the device you have\n"
		    "is malfunctioning or it's not supported.\n"
		    "It's also possible that you are trying to use the wrong "
		    "device file.\n"));
		break;

	case ENOSPC:
		(void) fprintf(stderr,
		    _("Your system cannot allocate memory for the device\n"
		    "buffers. Reboot your machine and try again.\n"));
		break;

	case ENOENT:
		(void) fprintf(stderr,
		    _("The device file is missing from /dev.\n"));
		break;


	case EBUSY:
		(void) fprintf(stderr,
		    _("The device is busy. There is some other application\n"
		    "using it.\n"));
		break;

	default:
		break;
	}
}

int
main(int argc, char *argv[])
{
	int t, i;
	int maxdev;
	int flags = 0;
	int status = 0;
	int errors = 0;
	int numdev;
	extern int optind;
	testcfg_t	*tcfg;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	tcfg = &test_stereo;

	/*
	 * Simple command line switch handling.
	 */

	while ((i = getopt(argc, argv, "l2457r:")) != EOF) {
		switch (i) {
		case 'l':
			flags |= TF_LOOP;
			break;
		case '2':
			tcfg = &test_stereo;
			break;
		case '4':
			tcfg = &test_quad;
			break;
		case '5':
			tcfg = &test_51;
			break;
		case '7':
			tcfg = &test_71;
			break;
		case 'r':
			tcfg->rate = atoi(optarg);
			break;
		default:
			(void) printf(_("Usage: %s [options...] [device]\n"
			    "	-2	Stereo test\n"
			    "	-4	Quadraphonic 4.0 test\n"
			    "	-5	Surround 5.1 test\n"
			    "	-7	Surround 7.1 test\n"
			    "	-r	Sample Rate (48000|96000|192000)\n"
			    "	-l	Loop test\n"), argv[0]);
			exit(-1);
		}
	}

	/*
	 * Open the mixer device used for calling SNDCTL_SYSINFO and
	 * SNDCTL_AUDIOINFO.
	 */
	if ((mixerfd = open("/dev/mixer", O_RDWR, 0)) == -1) {
		int err = errno;
		perror("/dev/mixer");
		errno = err;
		describe_error(errno);
		exit(-1);
	}

	prepare(tcfg);			/* Prepare the wave data */

	/*
	 * Enumerate all devices and play the test sounds.
	 */
	maxdev = find_num_devices();
	if (maxdev < 1) {
		(void) printf(_("\n*** No audio hardware available ***\n"));
		exit(-1);
	}

	numdev = (argc - optind);
	do {
		char *dn;
		oss_audioinfo	ainfo;
		int rv;

		status = 0;
		if (numdev > 0) {
			for (t = 0; t < numdev; t++) {
				dn = argv[optind + t];
				rv = test_device(dn, flags, tcfg);
				if (rv < 0) {
					errors++;
				} else if (rv) {
					status++;
				}
			}
		} else {
			for (t = 0; t < maxdev; t++) {
				ainfo.dev = t;
				if (ioctl(mixerfd, SNDCTL_AUDIOINFO,
				    &ainfo) == -1) {
					perror("SNDCTL_AUDIOINFO");
					status++;
					continue;
				}
				dn = ainfo.devnode;
				rv = test_device(dn, flags, tcfg);
				if (rv < 0) {
					errors++;
				} else if (rv) {
					status++;
				}
			}
		}

		if (errors == 0)
			(void) printf(_("\n*** All tests completed OK ***\n"));
		else
			(void) printf(_("\n*** Errors were detected ***\n"));

	} while (status && (flags & TF_LOOP));

	(void) close(mixerfd);

	return (status);
}