4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/core-util.h>
34 #include "channelmap.h"
36 const char *const table[] = {
37 [PA_CHANNEL_POSITION_MONO] = "mono",
39 [PA_CHANNEL_POSITION_FRONT_CENTER] = "front-center",
40 [PA_CHANNEL_POSITION_FRONT_LEFT] = "front-left",
41 [PA_CHANNEL_POSITION_FRONT_RIGHT] = "front-right",
43 [PA_CHANNEL_POSITION_REAR_CENTER] = "rear-center",
44 [PA_CHANNEL_POSITION_REAR_LEFT] = "rear-left",
45 [PA_CHANNEL_POSITION_REAR_RIGHT] = "rear-right",
47 [PA_CHANNEL_POSITION_LFE] = "lfe",
49 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "front-left-of-center",
50 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "front-right-of-center",
52 [PA_CHANNEL_POSITION_SIDE_LEFT] = "side-left",
53 [PA_CHANNEL_POSITION_SIDE_RIGHT] = "side-right",
55 [PA_CHANNEL_POSITION_AUX0] = "aux0",
56 [PA_CHANNEL_POSITION_AUX1] = "aux1",
57 [PA_CHANNEL_POSITION_AUX2] = "aux2",
58 [PA_CHANNEL_POSITION_AUX3] = "aux3",
59 [PA_CHANNEL_POSITION_AUX4] = "aux4",
60 [PA_CHANNEL_POSITION_AUX5] = "aux5",
61 [PA_CHANNEL_POSITION_AUX6] = "aux6",
62 [PA_CHANNEL_POSITION_AUX7] = "aux7",
63 [PA_CHANNEL_POSITION_AUX8] = "aux8",
64 [PA_CHANNEL_POSITION_AUX9] = "aux9",
65 [PA_CHANNEL_POSITION_AUX10] = "aux10",
66 [PA_CHANNEL_POSITION_AUX11] = "aux11",
67 [PA_CHANNEL_POSITION_AUX12] = "aux12",
68 [PA_CHANNEL_POSITION_AUX13] = "aux13",
69 [PA_CHANNEL_POSITION_AUX14] = "aux14",
70 [PA_CHANNEL_POSITION_AUX15] = "aux15",
71 [PA_CHANNEL_POSITION_AUX16] = "aux16",
72 [PA_CHANNEL_POSITION_AUX17] = "aux17",
73 [PA_CHANNEL_POSITION_AUX18] = "aux18",
74 [PA_CHANNEL_POSITION_AUX19] = "aux19",
75 [PA_CHANNEL_POSITION_AUX20] = "aux20",
76 [PA_CHANNEL_POSITION_AUX21] = "aux21",
77 [PA_CHANNEL_POSITION_AUX22] = "aux22",
78 [PA_CHANNEL_POSITION_AUX23] = "aux23",
79 [PA_CHANNEL_POSITION_AUX24] = "aux24",
80 [PA_CHANNEL_POSITION_AUX25] = "aux25",
81 [PA_CHANNEL_POSITION_AUX26] = "aux26",
82 [PA_CHANNEL_POSITION_AUX27] = "aux27",
83 [PA_CHANNEL_POSITION_AUX28] = "aux28",
84 [PA_CHANNEL_POSITION_AUX29] = "aux29",
85 [PA_CHANNEL_POSITION_AUX30] = "aux30",
86 [PA_CHANNEL_POSITION_AUX31] = "aux31",
88 [PA_CHANNEL_POSITION_TOP_CENTER] = "top-center",
90 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "top-front-left",
91 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "top-front-right",
92 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "top-front-center",
94 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "top-rear-left",
95 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "top-rear-right",
96 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "top-rear-center"
99 pa_channel_map* pa_channel_map_init(pa_channel_map *m) {
105 for (c = 0; c < PA_CHANNELS_MAX; c++)
106 m->map[c] = PA_CHANNEL_POSITION_INVALID;
111 pa_channel_map* pa_channel_map_init_mono(pa_channel_map *m) {
114 pa_channel_map_init(m);
117 m->map[0] = PA_CHANNEL_POSITION_MONO;
121 pa_channel_map* pa_channel_map_init_stereo(pa_channel_map *m) {
124 pa_channel_map_init(m);
127 m->map[0] = PA_CHANNEL_POSITION_LEFT;
128 m->map[1] = PA_CHANNEL_POSITION_RIGHT;
132 pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
134 assert(channels > 0);
135 assert(channels <= PA_CHANNELS_MAX);
137 pa_channel_map_init(m);
139 m->channels = channels;
142 case PA_CHANNEL_MAP_AIFF:
144 /* This is somewhat compatible with RFC3551 */
148 m->map[0] = PA_CHANNEL_POSITION_MONO;
152 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
153 m->map[1] = PA_CHANNEL_POSITION_SIDE_LEFT;
154 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
155 m->map[3] = PA_CHANNEL_POSITION_FRONT_RIGHT;
156 m->map[4] = PA_CHANNEL_POSITION_SIDE_RIGHT;
157 m->map[5] = PA_CHANNEL_POSITION_LFE;
161 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
162 m->map[3] = PA_CHANNEL_POSITION_REAR_LEFT;
163 m->map[4] = PA_CHANNEL_POSITION_REAR_RIGHT;
167 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
168 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
172 m->map[0] = PA_CHANNEL_POSITION_LEFT;
173 m->map[1] = PA_CHANNEL_POSITION_RIGHT;
174 m->map[2] = PA_CHANNEL_POSITION_CENTER;
178 m->map[0] = PA_CHANNEL_POSITION_LEFT;
179 m->map[1] = PA_CHANNEL_POSITION_CENTER;
180 m->map[2] = PA_CHANNEL_POSITION_RIGHT;
181 m->map[3] = PA_CHANNEL_POSITION_LFE;
188 case PA_CHANNEL_MAP_ALSA:
192 m->map[0] = PA_CHANNEL_POSITION_MONO;
196 m->map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
197 m->map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
201 m->map[5] = PA_CHANNEL_POSITION_LFE;
205 m->map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
209 m->map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
210 m->map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
214 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
215 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
222 case PA_CHANNEL_MAP_AUX: {
225 if (channels >= PA_CHANNELS_MAX)
228 for (i = 0; i < channels; i++)
229 m->map[i] = PA_CHANNEL_POSITION_AUX0 + i;
234 case PA_CHANNEL_MAP_WAVEEX:
238 m->map[0] = PA_CHANNEL_POSITION_MONO;
242 m->map[15] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
243 m->map[16] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
244 m->map[17] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
248 m->map[12] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
249 m->map[13] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
250 m->map[14] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
254 m->map[11] = PA_CHANNEL_POSITION_TOP_CENTER;
258 m->map[9] = PA_CHANNEL_POSITION_SIDE_LEFT;
259 m->map[10] = PA_CHANNEL_POSITION_SIDE_RIGHT;
263 m->map[8] = PA_CHANNEL_POSITION_REAR_CENTER;
267 m->map[6] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
268 m->map[7] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
272 m->map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
273 m->map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
277 m->map[3] = PA_CHANNEL_POSITION_LFE;
281 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
285 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
286 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
293 case PA_CHANNEL_MAP_OSS:
297 m->map[0] = PA_CHANNEL_POSITION_MONO;
301 m->map[6] = PA_CHANNEL_POSITION_REAR_LEFT;
302 m->map[7] = PA_CHANNEL_POSITION_REAR_RIGHT;
306 m->map[4] = PA_CHANNEL_POSITION_SIDE_LEFT;
307 m->map[5] = PA_CHANNEL_POSITION_SIDE_RIGHT;
311 m->map[3] = PA_CHANNEL_POSITION_LFE;
315 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
319 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
320 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
334 const char* pa_channel_position_to_string(pa_channel_position_t pos) {
336 if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
342 int pa_channel_map_equal(const pa_channel_map *a, const pa_channel_map *b) {
348 if (a->channels != b->channels)
351 for (c = 0; c < a->channels; c++)
352 if (a->map[c] != b->map[c])
358 char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) {
369 for (channel = 0; channel < map->channels && l > 1; channel++) {
370 l -= snprintf(e, l, "%s%s",
372 pa_channel_position_to_string(map->map[channel]));
381 pa_channel_map *pa_channel_map_parse(pa_channel_map *rmap, const char *s) {
389 memset(&map, 0, sizeof(map));
391 if (strcmp(s, "stereo") == 0) {
393 map.map[0] = PA_CHANNEL_POSITION_LEFT;
394 map.map[1] = PA_CHANNEL_POSITION_RIGHT;
401 while ((p = pa_split(s, ",", &state))) {
403 if (map.channels >= PA_CHANNELS_MAX) {
408 /* Some special aliases */
409 if (strcmp(p, "left") == 0)
410 map.map[map.channels++] = PA_CHANNEL_POSITION_LEFT;
411 else if (strcmp(p, "right") == 0)
412 map.map[map.channels++] = PA_CHANNEL_POSITION_RIGHT;
413 else if (strcmp(p, "center") == 0)
414 map.map[map.channels++] = PA_CHANNEL_POSITION_CENTER;
415 else if (strcmp(p, "subwoofer") == 0)
416 map.map[map.channels++] = PA_CHANNEL_POSITION_SUBWOOFER;
418 pa_channel_position_t i;
420 for (i = 0; i < PA_CHANNEL_POSITION_MAX; i++)
421 if (strcmp(p, table[i]) == 0) {
422 map.map[map.channels++] = i;
426 if (i >= PA_CHANNEL_POSITION_MAX) {
437 if (!pa_channel_map_valid(&map))
444 int pa_channel_map_valid(const pa_channel_map *map) {
449 if (map->channels <= 0 || map->channels > PA_CHANNELS_MAX)
452 for (c = 0; c < map->channels; c++) {
454 if (map->map[c] < 0 ||map->map[c] >= PA_CHANNEL_POSITION_MAX)