1 /* $NetBSD: main.c,v 1.16 2019/08/24 07:39:42 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 <err.h> 31 #include <errno.h> 32 #include <fcntl.h> 33 #include <limits.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <unistd.h> 37 38 #include "audiodev.h" 39 #include "drvctl.h" 40 41 __dead static void 42 usage(void) 43 { 44 const char *p = getprogname(); 45 46 fprintf(stderr, "usage: %s list [<index>]\n", p); 47 fprintf(stderr, " %s default <index>\n", p); 48 fprintf(stderr, " %s set <index> [p|r] <enc> <prec> <ch> <freq>\n", 49 p); 50 fprintf(stderr, " %s test <index>\n", p); 51 exit(EXIT_FAILURE); 52 } 53 54 const char *encoding_names[] = { 55 "none", 56 AudioEmulaw, 57 AudioEalaw, 58 "pcm16", 59 "pcm8", 60 AudioEadpcm, 61 AudioEslinear_le, 62 AudioEslinear_be, 63 AudioEulinear_le, 64 AudioEulinear_be, 65 AudioEslinear, 66 AudioEulinear, 67 AudioEmpeg_l1_stream, 68 AudioEmpeg_l1_packets, 69 AudioEmpeg_l1_system, 70 AudioEmpeg_l2_stream, 71 AudioEmpeg_l2_packets, 72 AudioEmpeg_l2_system, 73 AudioEac3, 74 }; 75 u_int encoding_max = __arraycount(encoding_names); 76 77 static void 78 print_audiodev(struct audiodev *adev, int i) 79 { 80 struct audiofmt *f; 81 int j; 82 83 assert(adev != NULL); 84 85 printf("%u: [%c] %s @ %s: ", 86 i, adev->defaultdev ? '*' : ' ', 87 adev->xname, adev->pxname); 88 printf("%s", adev->audio_device.name); 89 if (strlen(adev->audio_device.version) > 0) 90 printf(" %s", adev->audio_device.version); 91 printf("\n"); 92 printf(" playback: "); 93 if ((adev->hwinfo.mode & AUMODE_PLAY)) { 94 printf("%uch, %uHz\n", 95 adev->hwinfo.play.channels, 96 adev->hwinfo.play.sample_rate); 97 } else { 98 printf("unavailable\n"); 99 } 100 printf(" record: "); 101 if ((adev->hwinfo.mode & AUMODE_RECORD)) { 102 printf("%uch, %uHz\n", 103 adev->hwinfo.record.channels, 104 adev->hwinfo.record.sample_rate); 105 } else { 106 printf("unavailable\n"); 107 } 108 109 TAILQ_FOREACH(f, &adev->formats, next) { 110 printf(" "); 111 if (f->fmt.priority < 0) 112 printf("( ) "); 113 else if ((f->fmt.mode & (AUMODE_PLAY | AUMODE_RECORD)) 114 == (AUMODE_PLAY | AUMODE_RECORD)) 115 printf("(PR) "); 116 else if ((f->fmt.mode & AUMODE_PLAY)) 117 printf("(P-) "); 118 else if ((f->fmt.mode & AUMODE_RECORD)) 119 printf("(-R) "); 120 121 if (f->fmt.encoding < encoding_max) 122 printf("%s", encoding_names[f->fmt.encoding]); 123 else 124 printf("unknown_encoding_%d", f->fmt.encoding); 125 printf(" %d/%d, %dch, ", 126 f->fmt.validbits, 127 f->fmt.precision, 128 f->fmt.channels); 129 if (f->fmt.frequency_type == 0) { 130 printf("%d-%dHz", 131 f->fmt.frequency[0], 132 f->fmt.frequency[1]); 133 } else { 134 for (j = 0; j < (int)f->fmt.frequency_type; j++) { 135 printf("%s%d", 136 (j == 0) ? "{ " : ", ", 137 f->fmt.frequency[j]); 138 } 139 printf(" }"); 140 } 141 printf("\n"); 142 } 143 } 144 145 int 146 main(int argc, char *argv[]) 147 { 148 struct audiodev *adev; 149 unsigned int n, i; 150 unsigned int j; 151 const char *enc; 152 unsigned int prec; 153 unsigned int ch; 154 unsigned int freq; 155 int mode; 156 157 if (audiodev_refresh() == -1) 158 return EXIT_FAILURE; 159 160 if (argc < 2) 161 usage(); 162 /* NOTREACHED */ 163 164 if (strcmp(argv[1], "list") == 0 && argc == 2) { 165 n = audiodev_count(); 166 for (i = 0; i < n; i++) 167 print_audiodev(audiodev_get(i), i); 168 } else if (strcmp(argv[1], "list") == 0 && argc == 3) { 169 if (*argv[2] < '0' || *argv[2] > '9') 170 usage(); 171 /* NOTREACHED */ 172 errno = 0; 173 i = strtoul(argv[2], NULL, 10); 174 if (errno) 175 usage(); 176 /* NOTREACHED */ 177 adev = audiodev_get(i); 178 if (adev == NULL) { 179 errx(EXIT_FAILURE, "no such device"); 180 } 181 print_audiodev(adev, i); 182 } else if (strcmp(argv[1], "default") == 0 && argc == 3) { 183 if (*argv[2] < '0' || *argv[2] > '9') 184 usage(); 185 /* NOTREACHED */ 186 errno = 0; 187 i = strtoul(argv[2], NULL, 10); 188 if (errno) 189 usage(); 190 /* NOTREACHED */ 191 adev = audiodev_get(i); 192 if (adev == NULL) { 193 errx(EXIT_FAILURE, "no such device"); 194 } 195 printf("setting default audio device to %s\n", adev->xname); 196 if (audiodev_set_default(adev) == -1) { 197 errx(EXIT_FAILURE, "couldn't set default device"); 198 } 199 } else if (strcmp(argv[1], "set") == 0 && argc == 8) { 200 /* XXX bad commandline... */ 201 /* audiocfg set <index> [p|r] <enc> <prec> <ch> <freq> */ 202 if (*argv[2] < '0' || *argv[2] > '9') 203 usage(); 204 /* NOTREACHED */ 205 errno = 0; 206 i = strtoul(argv[2], NULL, 10); 207 if (errno) 208 usage(); 209 /* NOTREACHED */ 210 adev = audiodev_get(i); 211 if (adev == NULL) { 212 errx(EXIT_FAILURE, "no such device"); 213 } 214 215 mode = 0; 216 for (j = 0; j < strlen(argv[3]); j++) { 217 if (argv[3][j] == 'p') 218 mode |= AUMODE_PLAY; 219 else if (argv[3][j] == 'r') 220 mode |= AUMODE_RECORD; 221 else 222 usage(); 223 } 224 if (mode == 0) 225 usage(); 226 /* NOTREACHED */ 227 enc = argv[4]; 228 prec = strtoul(argv[5], NULL, 10); 229 if (errno) 230 usage(); 231 errno = 0; 232 ch = strtoul(argv[6], NULL, 10); 233 if (errno) 234 usage(); 235 /* NOTREACHED */ 236 errno = 0; 237 freq = strtoul(argv[7], NULL, 10); 238 if (errno) 239 usage(); 240 /* NOTREACHED */ 241 242 if (audiodev_set_param(adev, mode, enc, prec, ch, freq) == -1) { 243 errx(EXIT_FAILURE, "couldn't set parameter"); 244 } 245 } else if (strcmp(argv[1], "test") == 0 && argc == 3) { 246 if (*argv[2] < '0' || *argv[2] > '9') 247 usage(); 248 /* NOTREACHED */ 249 errno = 0; 250 i = strtoul(argv[2], NULL, 10); 251 if (errno) 252 usage(); 253 /* NOTREACHED */ 254 adev = audiodev_get(i); 255 if (adev == NULL) { 256 errx(EXIT_FAILURE, "no such device"); 257 } 258 print_audiodev(adev, i); 259 if (audiodev_test(adev) == -1) 260 return EXIT_FAILURE; 261 } else 262 usage(); 263 /* NOTREACHED */ 264 265 return EXIT_SUCCESS; 266 } 267