1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
* channel mapping API test program
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include "../include/asoundlib.h"
static void usage(void)
{
printf("usage: chmap [options] query\n"
" chmap [options] get\n"
" chmap [options] set CH0 CH1 CH2...\n"
"options:\n"
" -D device Specify PCM device to handle\n"
" -s stream Specify PCM stream direction (playback/capture)\n"
" -f format PCM format\n"
" -c channels Channels\n"
" -r rate Sample rate\n");
}
static void print_channels(const snd_pcm_chmap_t *map)
{
char tmp[128];
if (snd_pcm_chmap_print(map, sizeof(tmp), tmp) > 0)
printf(" %s\n", tmp);
}
static int query_chmaps(snd_pcm_t *pcm)
{
snd_pcm_chmap_query_t **maps = snd_pcm_query_chmaps(pcm);
snd_pcm_chmap_query_t **p, *v;
if (!maps) {
printf("Cannot query maps\n");
return 1;
}
for (p = maps; (v = *p) != NULL; p++) {
printf("Type = %s, Channels = %d\n",
snd_pcm_chmap_type_name(v->type),
v->map.channels);
print_channels(&v->map);
}
snd_pcm_free_chmaps(maps);
return 0;
}
static int setup_pcm(snd_pcm_t *pcm, int format, int channels, int rate)
{
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
if (snd_pcm_hw_params_any(pcm, params) < 0) {
printf("Cannot init hw_params\n");
return -1;
}
if (format != SND_PCM_FORMAT_UNKNOWN) {
if (snd_pcm_hw_params_set_format(pcm, params, format) < 0) {
printf("Cannot set format %s\n",
snd_pcm_format_name(format));
return -1;
}
}
if (channels > 0) {
if (snd_pcm_hw_params_set_channels(pcm, params, channels) < 0) {
printf("Cannot set channels %d\n", channels);
return -1;
}
}
if (rate > 0) {
if (snd_pcm_hw_params_set_rate_near(pcm, params, (unsigned int *)&rate, 0) < 0) {
printf("Cannot set rate %d\n", rate);
return -1;
}
}
if (snd_pcm_hw_params(pcm, params) < 0) {
printf("Cannot set hw_params\n");
return -1;
}
return 0;
}
static int get_chmap(snd_pcm_t *pcm, int format, int channels, int rate)
{
snd_pcm_chmap_t *map;
if (setup_pcm(pcm, format, channels, rate))
return 1;
map = snd_pcm_get_chmap(pcm);
if (!map) {
printf("Cannot get chmap\n");
return 1;
}
printf("Channels = %d\n", map->channels);
print_channels(map);
free(map);
return 0;
}
static int set_chmap(snd_pcm_t *pcm, int format, int channels, int rate,
int nargs, char **arg)
{
int i;
snd_pcm_chmap_t *map;
if (channels && channels != nargs) {
printf("Inconsistent channels %d vs %d\n", channels, nargs);
return 1;
}
if (!channels) {
if (!nargs) {
printf("No channels are given\n");
return 1;
}
channels = nargs;
}
if (setup_pcm(pcm, format, channels, rate))
return 1;
map = malloc(sizeof(int) * (channels + 1));
if (!map) {
printf("cannot malloc\n");
return 1;
}
map->channels = channels;
for (i = 0; i < channels; i++) {
int val = snd_pcm_chmap_from_string(arg[i]);
if (val < 0)
val = SND_CHMAP_UNKNOWN;
map->pos[i] = val;
}
if (snd_pcm_set_chmap(pcm, map) < 0) {
printf("Cannot set chmap\n");
return 1;
}
free(map);
map = snd_pcm_get_chmap(pcm);
if (!map) {
printf("Cannot get chmap\n");
return 1;
}
printf("Get channels = %d\n", map->channels);
print_channels(map);
free(map);
return 0;
}
int main(int argc, char **argv)
{
char *device = NULL;
int stream = SND_PCM_STREAM_PLAYBACK;
int format = SND_PCM_FORMAT_UNKNOWN;
int channels = 0;
int rate = 0;
snd_pcm_t *pcm;
int c;
while ((c = getopt(argc, argv, "D:s:f:c:r:")) != -1) {
switch (c) {
case 'D':
device = optarg;
break;
case 's':
if (*optarg == 'c' || *optarg == 'C')
stream = SND_PCM_STREAM_CAPTURE;
else
stream = SND_PCM_STREAM_PLAYBACK;
break;
case 'f':
format = snd_pcm_format_value(optarg);
break;
case 'c':
channels = atoi(optarg);
break;
case 'r':
rate = atoi(optarg);
break;
default:
usage();
return 1;
}
}
if (argc <= optind) {
usage();
return 1;
}
if (!device) {
printf("No device is specified\n");
return 1;
}
if (snd_pcm_open(&pcm, device, stream, SND_PCM_NONBLOCK) < 0) {
printf("Cannot open PCM stream %s for %s\n", device,
snd_pcm_stream_name(stream));
return 1;
}
switch (*argv[optind]) {
case 'q':
return query_chmaps(pcm);
case 'g':
return get_chmap(pcm, format, channels, rate);
case 's':
return set_chmap(pcm, format, channels, rate,
argc - optind - 1, argv + optind + 1);
}
usage();
return 1;
}
|