summaryrefslogtreecommitdiff
path: root/test/chmap.c
blob: 8035a1b61d79175e0b1782a3d56db6efec300498 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * 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"
	       "  -f format     PCM format\n"
	       "  -c channels   Channels\n"
	       "  -r rate       Sample rate\n");
}

static const char * const chname[] = {
	"Unknown",
	"FL", "FR", "RL", "RR", "FC", "LFE", "SL", "SR", "RC",
	"FLC", "FRC", "RLC", "RRC", "FLW", "FRW", "FLH",
	"FCH", "FCH", "FRH", "TC",
	"N/A",
};

#define ARRAY_SIZE(x)	(sizeof(x) / sizeof(x[0]))

static void print_channels(const snd_pcm_chmap_t *map)
{
	unsigned int i;

	printf("  ");
	for (i = 0; i < map->channels; i++) {
		unsigned int c = map->pos[i];
		unsigned int p = c & SND_CHMAP_POSITION_MASK;
		if (c & SND_CHMAP_DRIVER_SPEC)
			printf(" %d", p);
		else if (p >= ARRAY_SIZE(chname))
			printf(" Ch%d", p);
		else
			printf(" %s", chname[p]);
		if (c & SND_CHMAP_PHASE_INVERSE)
			printf("[INV]");
	}
	printf("\n");
}

static int to_channel(const char *name)
{
	unsigned int i;

	if (isdigit(*name))
		return atoi(name);
	for (i = 0; i < ARRAY_SIZE(chname); i++)
		if (!strcmp(chname[i], name))
			return i;
	return SND_CHMAP_UNKNOWN;
}

static const char *chmap_type(int type)
{
	switch (type) {
	case SND_CHMAP_NONE:
		return "None";
	case SND_CHMAP_FIXED:
		return "Fixed";
	case SND_CHMAP_VAR:
		return "Variable";
	case SND_CHMAP_PAIRED:
		return "Paired";
	default:
		return "Unknown";
	}
}

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", chmap_type(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(&params);
	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++)
		map->pos[i] = to_channel(arg[i]);
	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;
}