audiotest.c revision 68c47f65208790c466e5e484f2293d3baed71c6a
/*
* 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
* 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* 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/soundcard.h>
#include <inttypes.h>
#include <locale.h>
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
#define _(s) gettext(s)
/*
* Channel selectors
*/
#define CH_LEFT (1 << 0)
typedef struct chancfg {
int mask;
const char *name;
unsigned flags;
int len;
} chancfg_t;
typedef struct testcfg {
int nchan;
} testcfg_t;
/*
* 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> ........"
testcfg_t test_stereo = {
};
};
};
};
/*
* 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).
*/
static int mixerfd;
static int num_devices_tested = 0;
static short *sample_buf;
void
{
int nsamples;
int i;
perror("malloc");
exit(-1);
}
int ch;
int samp;
src = sample_buf;
perror("malloc");
exit(-1);
}
*src : 0;
dst++;
}
src += 2;
}
} else {
/* Skip LFE for now */
}
}
}
/*
* 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
{
float ratio;
unsigned long t;
int sample_rate;
int delay;
long long total_bytes = 0;
int i;
caps = 0;
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;
(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.
*/
(void) printf(_("The device doesn't support %d channels\n"),
return (-2);
}
/*
* Set up the sample rate.
*/
tmp = SAMPLE_RATE;
perror("SNDCTL_DSP_SPEED");
return (-3);
}
sample_rate = tmp;
if (sample_rate != SAMPLE_RATE) {
(void) printf(_("The device doesn't support %d Hz\n"),
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) printf(_("SKIPPED\n"));
continue;
}
(void) printf(_("ERROR: %s\n"),
return (-3);
}
(void) printf(_("OK\n"));
}
delay = 0;
/*
* Perform the time computations using milliseconds.
*/
t *= 1000;
total_bytes -= delay;
total_bytes *= 1000;
total_bytes /= t;
(void) printf(_("\t<measured sample rate %8.2f Hz (%4.2f%%)>\n"),
return (1);
}
static int
find_num_devices(void)
{
/*
* Find out the number of available audio devices by calling
* SNDCTL_SYSINFO.
*/
_("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"),
(void) printf(_("Platform: %s %s %s %s\n"),
}
/*
* The test_device() routine checks certain information about the device
* and calls testdsp() to play the test sound.
*/
int
{
int code;
int fd;
if (fd == -1) {
return (-1);
}
perror("SNDCTL_AUDIOINFO");
return (-1);
}
(void) printf(_("\n*** Scanning sound adapter #%d ***\n"),
(void) printf(_(" - Device not present - Skipping\n"));
return (0);
}
(void) printf(_(" - Skipping input only device\n"));
return (0);
}
(void) printf(_(" - Performing audio playback test... "));
if (code < 0) {
return (code);
}
return (code == 1);
}
void
describe_error(int err)
{
switch (err) {
case ENODEV:
_("The device file was found in /dev but\n"
"the driver was not loaded.\n"));
break;
case ENXIO:
_("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:
_("Your system cannot allocate memory for the device\n"
"buffers. Reboot your machine and try again.\n"));
break;
case ENOENT:
_("The device file is missing from /dev.\n"));
break;
case EBUSY:
_("The device is busy. There is some other application\n"
"using it.\n"));
break;
default:
break;
}
}
int
{
int t, i;
int maxdev;
int flags = 0;
int status = 0;
int errors = 0;
int numdev;
extern int optind;
(void) textdomain(TEXT_DOMAIN);
tcfg = &test_stereo;
/*
* Simple command line switch handling.
*/
switch (i) {
case 'l':
break;
case '2':
tcfg = &test_stereo;
break;
case '4':
break;
case '5':
break;
case '7':
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"
" -l Loop test\n"), argv[0]);
exit(-1);
}
}
/*
* Open the mixer device used for calling SNDCTL_SYSINFO and
* SNDCTL_AUDIOINFO.
*/
exit(-1);
}
/*
* 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);
}
do {
char *dn;
int rv;
status = 0;
if (numdev > 0) {
for (t = 0; t < numdev; t++) {
if (rv < 0) {
errors++;
} else if (rv) {
status++;
}
}
} else {
for (t = 0; t < maxdev; t++) {
&ainfo) == -1) {
perror("SNDCTL_AUDIOINFO");
status++;
continue;
}
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"));
return (status);
}