Home | History | Annotate | Line # | Download | only in audiocfg
main.c revision 1.7.26.1
      1  1.7.26.1  christos /* $NetBSD: main.c,v 1.7.26.1 2019/06/10 22:10:17 christos Exp $ */
      2       1.1       mrg 
      3       1.1       mrg /*
      4       1.1       mrg  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5       1.1       mrg  * All rights reserved.
      6       1.1       mrg  *
      7       1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8       1.1       mrg  * modification, are permitted provided that the following conditions
      9       1.1       mrg  * are met:
     10       1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11       1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12       1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15       1.1       mrg  *
     16       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1       mrg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1       mrg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1       mrg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1       mrg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1       mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1       mrg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1       mrg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1       mrg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1       mrg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1       mrg  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1       mrg  */
     28       1.1       mrg 
     29       1.6  jmcneill #include <assert.h>
     30       1.1       mrg #include <errno.h>
     31       1.1       mrg #include <fcntl.h>
     32       1.1       mrg #include <limits.h>
     33       1.1       mrg #include <stdio.h>
     34       1.1       mrg #include <stdlib.h>
     35       1.1       mrg #include <unistd.h>
     36       1.1       mrg 
     37       1.1       mrg #include "audiodev.h"
     38       1.1       mrg #include "drvctl.h"
     39       1.1       mrg 
     40       1.7     joerg __dead static void
     41       1.1       mrg usage(const char *p)
     42       1.1       mrg {
     43  1.7.26.1  christos 	fprintf(stderr, "usage: %s list [<index>]\n", p);
     44       1.3       wiz 	fprintf(stderr, "       %s default <index>\n", p);
     45  1.7.26.1  christos 	fprintf(stderr, "       %s set  <index> [p|r] <enc> <prec> <ch> <freq>\n",
     46  1.7.26.1  christos 	    p);
     47       1.3       wiz 	fprintf(stderr, "       %s test <index>\n", p);
     48       1.1       mrg 	exit(EXIT_FAILURE);
     49       1.1       mrg }
     50       1.1       mrg 
     51  1.7.26.1  christos const char *encoding_names[] = {
     52  1.7.26.1  christos 	"none",
     53  1.7.26.1  christos 	AudioEmulaw,
     54  1.7.26.1  christos 	AudioEalaw,
     55  1.7.26.1  christos 	"pcm16",
     56  1.7.26.1  christos 	"pcm8",
     57  1.7.26.1  christos 	AudioEadpcm,
     58  1.7.26.1  christos 	AudioEslinear_le,
     59  1.7.26.1  christos 	AudioEslinear_be,
     60  1.7.26.1  christos 	AudioEulinear_le,
     61  1.7.26.1  christos 	AudioEulinear_be,
     62  1.7.26.1  christos 	AudioEslinear,
     63  1.7.26.1  christos 	AudioEulinear,
     64  1.7.26.1  christos 	AudioEmpeg_l1_stream,
     65  1.7.26.1  christos 	AudioEmpeg_l1_packets,
     66  1.7.26.1  christos 	AudioEmpeg_l1_system,
     67  1.7.26.1  christos 	AudioEmpeg_l2_stream,
     68  1.7.26.1  christos 	AudioEmpeg_l2_packets,
     69  1.7.26.1  christos 	AudioEmpeg_l2_system,
     70  1.7.26.1  christos 	AudioEac3,
     71  1.7.26.1  christos };
     72  1.7.26.1  christos u_int encoding_max = __arraycount(encoding_names);
     73  1.7.26.1  christos 
     74       1.6  jmcneill static void
     75       1.6  jmcneill print_audiodev(struct audiodev *adev, int i)
     76       1.6  jmcneill {
     77  1.7.26.1  christos 	struct audiofmt *f;
     78  1.7.26.1  christos 	int j;
     79  1.7.26.1  christos 
     80       1.6  jmcneill 	assert(adev != NULL);
     81       1.6  jmcneill 
     82       1.6  jmcneill 	printf("%u: [%c] %s @ %s: ",
     83       1.6  jmcneill 	    i, adev->defaultdev ? '*' : ' ',
     84       1.6  jmcneill 	    adev->xname, adev->pxname);
     85       1.6  jmcneill 	printf("%s", adev->audio_device.name);
     86       1.6  jmcneill 	if (strlen(adev->audio_device.version) > 0)
     87       1.6  jmcneill 		printf(" %s", adev->audio_device.version);
     88  1.7.26.1  christos 	printf("\n");
     89  1.7.26.1  christos 	printf("       playback: ");
     90  1.7.26.1  christos 	if ((adev->info.mode & AUMODE_PLAY))
     91  1.7.26.1  christos 		printf("%uch, %uHz\n",
     92  1.7.26.1  christos 		    adev->info.play.channels, adev->info.play.sample_rate);
     93  1.7.26.1  christos 	else
     94  1.7.26.1  christos 		printf("unavailable\n");
     95  1.7.26.1  christos 	printf("       record:   ");
     96  1.7.26.1  christos 	if ((adev->info.mode & AUMODE_RECORD))
     97  1.7.26.1  christos 		printf("%uch, %uHz\n",
     98  1.7.26.1  christos 		    adev->info.record.channels, adev->info.record.sample_rate);
     99  1.7.26.1  christos 	else
    100  1.7.26.1  christos 		printf("unavailable\n");
    101  1.7.26.1  christos 
    102  1.7.26.1  christos 	TAILQ_FOREACH(f, &adev->formats, next) {
    103  1.7.26.1  christos 		printf("       ");
    104  1.7.26.1  christos 		if (f->fmt.priority < 0)
    105  1.7.26.1  christos 			printf("(  ) ");
    106  1.7.26.1  christos 		else if ((f->fmt.mode & (AUMODE_PLAY | AUMODE_RECORD))
    107  1.7.26.1  christos 		    == (AUMODE_PLAY | AUMODE_RECORD))
    108  1.7.26.1  christos 			printf("(PR) ");
    109  1.7.26.1  christos 		else if ((f->fmt.mode & AUMODE_PLAY))
    110  1.7.26.1  christos 			printf("(P-) ");
    111  1.7.26.1  christos 		else if ((f->fmt.mode & AUMODE_RECORD))
    112  1.7.26.1  christos 			printf("(-R) ");
    113  1.7.26.1  christos 
    114  1.7.26.1  christos 		if (f->fmt.encoding < encoding_max)
    115  1.7.26.1  christos 			printf("%s", encoding_names[f->fmt.encoding]);
    116  1.7.26.1  christos 		else
    117  1.7.26.1  christos 			printf("unknown_encoding_%d", f->fmt.encoding);
    118  1.7.26.1  christos 		printf(" %d/%d, %dch, ",
    119  1.7.26.1  christos 		    f->fmt.validbits,
    120  1.7.26.1  christos 		    f->fmt.precision,
    121  1.7.26.1  christos 		    f->fmt.channels);
    122  1.7.26.1  christos 		if (f->fmt.frequency_type == 0) {
    123  1.7.26.1  christos 			printf("%d-%dHz",
    124  1.7.26.1  christos 			    f->fmt.frequency[0],
    125  1.7.26.1  christos 			    f->fmt.frequency[1]);
    126  1.7.26.1  christos 		} else {
    127  1.7.26.1  christos 			for (j = 0; j < (int)f->fmt.frequency_type; j++) {
    128  1.7.26.1  christos 				printf("%s%d",
    129  1.7.26.1  christos 				    (j == 0) ? "{ " : ", ",
    130  1.7.26.1  christos 				    f->fmt.frequency[j]);
    131  1.7.26.1  christos 			}
    132  1.7.26.1  christos 			printf(" }");
    133  1.7.26.1  christos 		}
    134  1.7.26.1  christos 		printf("\n");
    135  1.7.26.1  christos 	}
    136       1.6  jmcneill }
    137       1.6  jmcneill 
    138       1.1       mrg int
    139       1.1       mrg main(int argc, char *argv[])
    140       1.1       mrg {
    141       1.1       mrg 	struct audiodev *adev;
    142       1.1       mrg 	unsigned int n, i;
    143  1.7.26.1  christos 	unsigned int j;
    144  1.7.26.1  christos 	const char *enc;
    145  1.7.26.1  christos 	unsigned int prec;
    146  1.7.26.1  christos 	unsigned int ch;
    147  1.7.26.1  christos 	unsigned int freq;
    148  1.7.26.1  christos 	int mode;
    149       1.1       mrg 
    150       1.1       mrg 	if (audiodev_refresh() == -1)
    151       1.1       mrg 		return EXIT_FAILURE;
    152       1.1       mrg 
    153       1.2  jmcneill 	if (argc < 2)
    154       1.2  jmcneill 		usage(argv[0]);
    155       1.2  jmcneill 		/* NOTREACHED */
    156       1.2  jmcneill 
    157  1.7.26.1  christos 	if (strcmp(argv[1], "list") == 0 && argc == 2) {
    158       1.1       mrg 		n = audiodev_count();
    159       1.6  jmcneill 		for (i = 0; i < n; i++)
    160       1.6  jmcneill 			print_audiodev(audiodev_get(i), i);
    161  1.7.26.1  christos 	} else if (strcmp(argv[1], "list") == 0 && argc == 3) {
    162  1.7.26.1  christos 		errno = 0;
    163  1.7.26.1  christos 		i = strtoul(argv[2], NULL, 10);
    164  1.7.26.1  christos 		if (errno)
    165  1.7.26.1  christos 			usage(argv[0]);
    166  1.7.26.1  christos 			/* NOTREACHED */
    167  1.7.26.1  christos 		print_audiodev(audiodev_get(i), i);
    168       1.2  jmcneill 	} else if (strcmp(argv[1], "default") == 0 && argc == 3) {
    169       1.2  jmcneill 		if (*argv[2] < '0' || *argv[2] > '9')
    170       1.1       mrg 			usage(argv[0]);
    171       1.1       mrg 			/* NOTREACHED */
    172       1.1       mrg 		errno = 0;
    173       1.2  jmcneill 		i = strtoul(argv[2], NULL, 10);
    174       1.1       mrg 		if (errno)
    175       1.1       mrg 			usage(argv[0]);
    176       1.1       mrg 			/* NOTREACHED */
    177       1.1       mrg 		adev = audiodev_get(i);
    178       1.1       mrg 		if (adev == NULL) {
    179       1.1       mrg 			fprintf(stderr, "no such device\n");
    180       1.1       mrg 			return EXIT_FAILURE;
    181       1.1       mrg 		}
    182       1.1       mrg 		printf("setting default audio device to %s\n", adev->xname);
    183       1.1       mrg 		if (audiodev_set_default(adev) == -1) {
    184       1.1       mrg 			perror("couldn't set default device");
    185       1.1       mrg 			return EXIT_FAILURE;
    186       1.1       mrg 		}
    187  1.7.26.1  christos 	} else if (strcmp(argv[1], "set") == 0 && argc == 8) {
    188  1.7.26.1  christos 		/* XXX bad commandline... */
    189  1.7.26.1  christos 		/* audiocfg set <index> [p|r] <enc> <prec> <ch> <freq> */
    190  1.7.26.1  christos 		if (*argv[2] < '0' || *argv[2] > '9')
    191  1.7.26.1  christos 			usage(argv[0]);
    192  1.7.26.1  christos 			/* NOTREACHED */
    193  1.7.26.1  christos 		errno = 0;
    194  1.7.26.1  christos 		i = strtoul(argv[2], NULL, 10);
    195  1.7.26.1  christos 		if (errno)
    196  1.7.26.1  christos 			usage(argv[0]);
    197  1.7.26.1  christos 			/* NOTREACHED */
    198  1.7.26.1  christos 		adev = audiodev_get(i);
    199  1.7.26.1  christos 		if (adev == NULL) {
    200  1.7.26.1  christos 			fprintf(stderr, "no such device\n");
    201  1.7.26.1  christos 			return EXIT_FAILURE;
    202  1.7.26.1  christos 		}
    203  1.7.26.1  christos 
    204  1.7.26.1  christos 		mode = 0;
    205  1.7.26.1  christos 		for (j = 0; j < strlen(argv[3]); j++) {
    206  1.7.26.1  christos 			if (argv[3][j] == 'p')
    207  1.7.26.1  christos 				mode |= AUMODE_PLAY;
    208  1.7.26.1  christos 			else if (argv[3][j] == 'r')
    209  1.7.26.1  christos 				mode |= AUMODE_RECORD;
    210  1.7.26.1  christos 			else
    211  1.7.26.1  christos 				usage(argv[0]);
    212  1.7.26.1  christos 		}
    213  1.7.26.1  christos 		enc = argv[4];
    214  1.7.26.1  christos 		prec = strtoul(argv[5], NULL, 10);
    215  1.7.26.1  christos 		if (errno)
    216  1.7.26.1  christos 			usage(argv[0]);
    217  1.7.26.1  christos 		errno = 0;
    218  1.7.26.1  christos 		ch = strtoul(argv[6], NULL, 10);
    219  1.7.26.1  christos 		if (errno)
    220  1.7.26.1  christos 			usage(argv[0]);
    221  1.7.26.1  christos 			/* NOTREACHED */
    222  1.7.26.1  christos 		errno = 0;
    223  1.7.26.1  christos 		freq = strtoul(argv[7], NULL, 10);
    224  1.7.26.1  christos 		if (errno)
    225  1.7.26.1  christos 			usage(argv[0]);
    226  1.7.26.1  christos 			/* NOTREACHED */
    227  1.7.26.1  christos 
    228  1.7.26.1  christos 		if (audiodev_set_param(adev, mode, enc, prec, ch, freq) == -1) {
    229  1.7.26.1  christos 			perror("couldn't set parameter");
    230  1.7.26.1  christos 			return EXIT_FAILURE;
    231  1.7.26.1  christos 		}
    232       1.2  jmcneill 	} else if (strcmp(argv[1], "test") == 0 && argc == 3) {
    233       1.2  jmcneill 		if (*argv[2] < '0' || *argv[2] > '9')
    234       1.2  jmcneill 			usage(argv[0]);
    235       1.2  jmcneill 			/* NOTREACHED */
    236       1.2  jmcneill 		errno = 0;
    237       1.2  jmcneill 		i = strtoul(argv[2], NULL, 10);
    238       1.2  jmcneill 		if (errno)
    239       1.2  jmcneill 			usage(argv[0]);
    240       1.2  jmcneill 			/* NOTREACHED */
    241       1.2  jmcneill 		adev = audiodev_get(i);
    242       1.2  jmcneill 		if (adev == NULL) {
    243       1.2  jmcneill 			fprintf(stderr, "no such device\n");
    244       1.2  jmcneill 			return EXIT_FAILURE;
    245       1.2  jmcneill 		}
    246       1.6  jmcneill 		print_audiodev(adev, i);
    247  1.7.26.1  christos 		for (i = 0; i < adev->info.play.channels; i++) {
    248       1.6  jmcneill 			printf("  testing channel %d...", i);
    249       1.6  jmcneill 			fflush(stdout);
    250       1.6  jmcneill 			if (audiodev_test(adev, 1 << i) == -1)
    251       1.6  jmcneill 				return EXIT_FAILURE;
    252       1.6  jmcneill 			printf(" done\n");
    253       1.2  jmcneill 		}
    254       1.2  jmcneill 	} else
    255       1.1       mrg 		usage(argv[0]);
    256       1.1       mrg 		/* NOTREACHED */
    257       1.1       mrg 
    258       1.1       mrg 	return EXIT_SUCCESS;
    259       1.1       mrg }
    260