main.c revision 1.13
1/* $NetBSD: main.c,v 1.13 2019/08/24 05:51:06 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) 76{ 77 struct audiofmt *f; 78 int j; 79 80 assert(adev != NULL); 81 82 printf("[%c] %s @ %s: ", 83 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_maxunit(); 163 for (i = 0; i <= n; i++) { 164 adev = audiodev_get(i); 165 if (adev) 166 print_audiodev(adev); 167 } 168 } else if (strcmp(argv[1], "list") == 0 && argc == 3) { 169 errno = 0; 170 i = strtoul(argv[2], NULL, 10); 171 if (errno) 172 usage(argv[0]); 173 /* NOTREACHED */ 174 adev = audiodev_get(i); 175 if (adev == NULL) { 176 fprintf(stderr, "no such device\n"); 177 return EXIT_FAILURE; 178 } 179 print_audiodev(adev); 180 } else if (strcmp(argv[1], "default") == 0 && argc == 3) { 181 if (*argv[2] < '0' || *argv[2] > '9') 182 usage(argv[0]); 183 /* NOTREACHED */ 184 errno = 0; 185 i = strtoul(argv[2], NULL, 10); 186 if (errno) 187 usage(argv[0]); 188 /* NOTREACHED */ 189 adev = audiodev_get(i); 190 if (adev == NULL) { 191 fprintf(stderr, "no such device\n"); 192 return EXIT_FAILURE; 193 } 194 printf("setting default audio device to %s\n", adev->xname); 195 if (audiodev_set_default(adev) == -1) { 196 perror("couldn't set default device"); 197 return EXIT_FAILURE; 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(argv[0]); 204 /* NOTREACHED */ 205 errno = 0; 206 i = strtoul(argv[2], NULL, 10); 207 if (errno) 208 usage(argv[0]); 209 /* NOTREACHED */ 210 adev = audiodev_get(i); 211 if (adev == NULL) { 212 fprintf(stderr, "no such device\n"); 213 return EXIT_FAILURE; 214 } 215 216 mode = 0; 217 for (j = 0; j < strlen(argv[3]); j++) { 218 if (argv[3][j] == 'p') 219 mode |= AUMODE_PLAY; 220 else if (argv[3][j] == 'r') 221 mode |= AUMODE_RECORD; 222 else 223 usage(argv[0]); 224 } 225 if (mode == 0) 226 usage(argv[0]); 227 /* NOTREACHED */ 228 enc = argv[4]; 229 prec = strtoul(argv[5], NULL, 10); 230 if (errno) 231 usage(argv[0]); 232 errno = 0; 233 ch = strtoul(argv[6], NULL, 10); 234 if (errno) 235 usage(argv[0]); 236 /* NOTREACHED */ 237 errno = 0; 238 freq = strtoul(argv[7], NULL, 10); 239 if (errno) 240 usage(argv[0]); 241 /* NOTREACHED */ 242 243 if (audiodev_set_param(adev, mode, enc, prec, ch, freq) == -1) { 244 perror("couldn't set parameter"); 245 return EXIT_FAILURE; 246 } 247 } else if (strcmp(argv[1], "test") == 0 && argc == 3) { 248 if (*argv[2] < '0' || *argv[2] > '9') 249 usage(argv[0]); 250 /* NOTREACHED */ 251 errno = 0; 252 i = strtoul(argv[2], NULL, 10); 253 if (errno) 254 usage(argv[0]); 255 /* NOTREACHED */ 256 adev = audiodev_get(i); 257 if (adev == NULL) { 258 fprintf(stderr, "no such device\n"); 259 return EXIT_FAILURE; 260 } 261 print_audiodev(adev); 262 if (audiodev_test(adev) == -1) 263 return EXIT_FAILURE; 264 } else 265 usage(argv[0]); 266 /* NOTREACHED */ 267 268 return EXIT_SUCCESS; 269} 270