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