2 This file is part of PulseAudio.
4 Copyright 2005-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/macro.h>
35 #include "channelmap.h"
37 const char *const table[PA_CHANNEL_POSITION_MAX] = {
38 [PA_CHANNEL_POSITION_MONO] = "mono",
40 [PA_CHANNEL_POSITION_FRONT_CENTER] = "front-center",
41 [PA_CHANNEL_POSITION_FRONT_LEFT] = "front-left",
42 [PA_CHANNEL_POSITION_FRONT_RIGHT] = "front-right",
44 [PA_CHANNEL_POSITION_REAR_CENTER] = "rear-center",
45 [PA_CHANNEL_POSITION_REAR_LEFT] = "rear-left",
46 [PA_CHANNEL_POSITION_REAR_RIGHT] = "rear-right",
48 [PA_CHANNEL_POSITION_LFE] = "lfe",
50 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "front-left-of-center",
51 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "front-right-of-center",
53 [PA_CHANNEL_POSITION_SIDE_LEFT] = "side-left",
54 [PA_CHANNEL_POSITION_SIDE_RIGHT] = "side-right",
56 [PA_CHANNEL_POSITION_AUX0] = "aux0",
57 [PA_CHANNEL_POSITION_AUX1] = "aux1",
58 [PA_CHANNEL_POSITION_AUX2] = "aux2",
59 [PA_CHANNEL_POSITION_AUX3] = "aux3",
60 [PA_CHANNEL_POSITION_AUX4] = "aux4",
61 [PA_CHANNEL_POSITION_AUX5] = "aux5",
62 [PA_CHANNEL_POSITION_AUX6] = "aux6",
63 [PA_CHANNEL_POSITION_AUX7] = "aux7",
64 [PA_CHANNEL_POSITION_AUX8] = "aux8",
65 [PA_CHANNEL_POSITION_AUX9] = "aux9",
66 [PA_CHANNEL_POSITION_AUX10] = "aux10",
67 [PA_CHANNEL_POSITION_AUX11] = "aux11",
68 [PA_CHANNEL_POSITION_AUX12] = "aux12",
69 [PA_CHANNEL_POSITION_AUX13] = "aux13",
70 [PA_CHANNEL_POSITION_AUX14] = "aux14",
71 [PA_CHANNEL_POSITION_AUX15] = "aux15",
72 [PA_CHANNEL_POSITION_AUX16] = "aux16",
73 [PA_CHANNEL_POSITION_AUX17] = "aux17",
74 [PA_CHANNEL_POSITION_AUX18] = "aux18",
75 [PA_CHANNEL_POSITION_AUX19] = "aux19",
76 [PA_CHANNEL_POSITION_AUX20] = "aux20",
77 [PA_CHANNEL_POSITION_AUX21] = "aux21",
78 [PA_CHANNEL_POSITION_AUX22] = "aux22",
79 [PA_CHANNEL_POSITION_AUX23] = "aux23",
80 [PA_CHANNEL_POSITION_AUX24] = "aux24",
81 [PA_CHANNEL_POSITION_AUX25] = "aux25",
82 [PA_CHANNEL_POSITION_AUX26] = "aux26",
83 [PA_CHANNEL_POSITION_AUX27] = "aux27",
84 [PA_CHANNEL_POSITION_AUX28] = "aux28",
85 [PA_CHANNEL_POSITION_AUX29] = "aux29",
86 [PA_CHANNEL_POSITION_AUX30] = "aux30",
87 [PA_CHANNEL_POSITION_AUX31] = "aux31",
89 [PA_CHANNEL_POSITION_TOP_CENTER] = "top-center",
91 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "top-front-center",
92 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "top-front-left",
93 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "top-front-right",
95 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "top-rear-center",
96 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "top-rear-left",
97 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "top-rear-right"
100 const char *const pretty_table[PA_CHANNEL_POSITION_MAX] = {
101 [PA_CHANNEL_POSITION_MONO] = "Mono",
103 [PA_CHANNEL_POSITION_FRONT_CENTER] = "Front Center",
104 [PA_CHANNEL_POSITION_FRONT_LEFT] = "Front Left",
105 [PA_CHANNEL_POSITION_FRONT_RIGHT] = "Front Right",
107 [PA_CHANNEL_POSITION_REAR_CENTER] = "Rear Center",
108 [PA_CHANNEL_POSITION_REAR_LEFT] = "Rear Left",
109 [PA_CHANNEL_POSITION_REAR_RIGHT] = "Rear Right",
111 [PA_CHANNEL_POSITION_LFE] = "Low Frequency Emmiter",
113 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "Front Left-of-center",
114 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "Front Right-of-center",
116 [PA_CHANNEL_POSITION_SIDE_LEFT] = "Side Left",
117 [PA_CHANNEL_POSITION_SIDE_RIGHT] = "Side Right",
119 [PA_CHANNEL_POSITION_AUX0] = "Auxiliary 0",
120 [PA_CHANNEL_POSITION_AUX1] = "Auxiliary 1",
121 [PA_CHANNEL_POSITION_AUX2] = "Auxiliary 2",
122 [PA_CHANNEL_POSITION_AUX3] = "Auxiliary 3",
123 [PA_CHANNEL_POSITION_AUX4] = "Auxiliary 4",
124 [PA_CHANNEL_POSITION_AUX5] = "Auxiliary 5",
125 [PA_CHANNEL_POSITION_AUX6] = "Auxiliary 6",
126 [PA_CHANNEL_POSITION_AUX7] = "Auxiliary 7",
127 [PA_CHANNEL_POSITION_AUX8] = "Auxiliary 8",
128 [PA_CHANNEL_POSITION_AUX9] = "Auxiliary 9",
129 [PA_CHANNEL_POSITION_AUX10] = "Auxiliary 10",
130 [PA_CHANNEL_POSITION_AUX11] = "Auxiliary 11",
131 [PA_CHANNEL_POSITION_AUX12] = "Auxiliary 12",
132 [PA_CHANNEL_POSITION_AUX13] = "Auxiliary 13",
133 [PA_CHANNEL_POSITION_AUX14] = "Auxiliary 14",
134 [PA_CHANNEL_POSITION_AUX15] = "Auxiliary 15",
135 [PA_CHANNEL_POSITION_AUX16] = "Auxiliary 16",
136 [PA_CHANNEL_POSITION_AUX17] = "Auxiliary 17",
137 [PA_CHANNEL_POSITION_AUX18] = "Auxiliary 18",
138 [PA_CHANNEL_POSITION_AUX19] = "Auxiliary 19",
139 [PA_CHANNEL_POSITION_AUX20] = "Auxiliary 20",
140 [PA_CHANNEL_POSITION_AUX21] = "Auxiliary 21",
141 [PA_CHANNEL_POSITION_AUX22] = "Auxiliary 22",
142 [PA_CHANNEL_POSITION_AUX23] = "Auxiliary 23",
143 [PA_CHANNEL_POSITION_AUX24] = "Auxiliary 24",
144 [PA_CHANNEL_POSITION_AUX25] = "Auxiliary 25",
145 [PA_CHANNEL_POSITION_AUX26] = "Auxiliary 26",
146 [PA_CHANNEL_POSITION_AUX27] = "Auxiliary 27",
147 [PA_CHANNEL_POSITION_AUX28] = "Auxiliary 28",
148 [PA_CHANNEL_POSITION_AUX29] = "Auxiliary 29",
149 [PA_CHANNEL_POSITION_AUX30] = "Auxiliary 30",
150 [PA_CHANNEL_POSITION_AUX31] = "Auxiliary 31",
152 [PA_CHANNEL_POSITION_TOP_CENTER] = "Top Center",
154 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "Top Front Center",
155 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "Top Front Left",
156 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "Top Front Right",
158 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "Top Rear Center",
159 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "Top Rear left",
160 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "Top Rear Right"
163 pa_channel_map* pa_channel_map_init(pa_channel_map *m) {
169 for (c = 0; c < PA_CHANNELS_MAX; c++)
170 m->map[c] = PA_CHANNEL_POSITION_INVALID;
175 pa_channel_map* pa_channel_map_init_mono(pa_channel_map *m) {
178 pa_channel_map_init(m);
181 m->map[0] = PA_CHANNEL_POSITION_MONO;
185 pa_channel_map* pa_channel_map_init_stereo(pa_channel_map *m) {
188 pa_channel_map_init(m);
191 m->map[0] = PA_CHANNEL_POSITION_LEFT;
192 m->map[1] = PA_CHANNEL_POSITION_RIGHT;
196 pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
198 pa_assert(channels > 0);
199 pa_assert(channels <= PA_CHANNELS_MAX);
201 pa_channel_map_init(m);
203 m->channels = channels;
206 case PA_CHANNEL_MAP_AIFF:
208 /* This is somewhat compatible with RFC3551 */
212 m->map[0] = PA_CHANNEL_POSITION_MONO;
216 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
217 m->map[1] = PA_CHANNEL_POSITION_SIDE_LEFT;
218 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
219 m->map[3] = PA_CHANNEL_POSITION_FRONT_RIGHT;
220 m->map[4] = PA_CHANNEL_POSITION_SIDE_RIGHT;
221 m->map[5] = PA_CHANNEL_POSITION_LFE;
225 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
226 m->map[3] = PA_CHANNEL_POSITION_REAR_LEFT;
227 m->map[4] = PA_CHANNEL_POSITION_REAR_RIGHT;
231 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
232 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
236 m->map[0] = PA_CHANNEL_POSITION_LEFT;
237 m->map[1] = PA_CHANNEL_POSITION_RIGHT;
238 m->map[2] = PA_CHANNEL_POSITION_CENTER;
242 m->map[0] = PA_CHANNEL_POSITION_LEFT;
243 m->map[1] = PA_CHANNEL_POSITION_CENTER;
244 m->map[2] = PA_CHANNEL_POSITION_RIGHT;
245 m->map[3] = PA_CHANNEL_POSITION_LFE;
252 case PA_CHANNEL_MAP_ALSA:
256 m->map[0] = PA_CHANNEL_POSITION_MONO;
260 m->map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
261 m->map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
265 m->map[5] = PA_CHANNEL_POSITION_LFE;
269 m->map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
273 m->map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
274 m->map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
278 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
279 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
286 case PA_CHANNEL_MAP_AUX: {
289 if (channels >= PA_CHANNELS_MAX)
292 for (i = 0; i < channels; i++)
293 m->map[i] = PA_CHANNEL_POSITION_AUX0 + i;
298 case PA_CHANNEL_MAP_WAVEEX:
302 m->map[0] = PA_CHANNEL_POSITION_MONO;
306 m->map[15] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
307 m->map[16] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
308 m->map[17] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
312 m->map[12] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
313 m->map[13] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
314 m->map[14] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
318 m->map[11] = PA_CHANNEL_POSITION_TOP_CENTER;
322 m->map[9] = PA_CHANNEL_POSITION_SIDE_LEFT;
323 m->map[10] = PA_CHANNEL_POSITION_SIDE_RIGHT;
327 m->map[8] = PA_CHANNEL_POSITION_REAR_CENTER;
331 m->map[6] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
332 m->map[7] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
336 m->map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
337 m->map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
341 m->map[3] = PA_CHANNEL_POSITION_LFE;
345 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
349 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
350 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
357 case PA_CHANNEL_MAP_OSS:
361 m->map[0] = PA_CHANNEL_POSITION_MONO;
365 m->map[6] = PA_CHANNEL_POSITION_REAR_LEFT;
366 m->map[7] = PA_CHANNEL_POSITION_REAR_RIGHT;
370 m->map[4] = PA_CHANNEL_POSITION_SIDE_LEFT;
371 m->map[5] = PA_CHANNEL_POSITION_SIDE_RIGHT;
375 m->map[3] = PA_CHANNEL_POSITION_LFE;
379 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
383 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
384 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
397 pa_channel_map* pa_channel_map_init_extend(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
401 pa_assert(channels > 0);
402 pa_assert(channels <= PA_CHANNELS_MAX);
404 pa_channel_map_init(m);
406 for (c = channels; c > 0; c--) {
408 if (pa_channel_map_init_auto(m, c, def)) {
411 for (; c < channels; c++) {
413 m->map[c] = PA_CHANNEL_POSITION_AUX0 + i;
417 m->channels = channels;
426 const char* pa_channel_position_to_string(pa_channel_position_t pos) {
428 if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
434 const char* pa_channel_position_to_pretty_string(pa_channel_position_t pos) {
435 if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
438 return pretty_table[pos];
441 int pa_channel_map_equal(const pa_channel_map *a, const pa_channel_map *b) {
447 if (a->channels != b->channels)
450 for (c = 0; c < a->channels; c++)
451 if (a->map[c] != b->map[c])
457 char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) {
468 for (channel = 0; channel < map->channels && l > 1; channel++) {
469 l -= pa_snprintf(e, l, "%s%s",
471 pa_channel_position_to_string(map->map[channel]));
480 pa_channel_map *pa_channel_map_parse(pa_channel_map *rmap, const char *s) {
488 memset(&map, 0, sizeof(map));
490 if (strcmp(s, "stereo") == 0) {
492 map.map[0] = PA_CHANNEL_POSITION_LEFT;
493 map.map[1] = PA_CHANNEL_POSITION_RIGHT;
500 while ((p = pa_split(s, ",", &state))) {
502 if (map.channels >= PA_CHANNELS_MAX) {
507 /* Some special aliases */
508 if (strcmp(p, "left") == 0)
509 map.map[map.channels++] = PA_CHANNEL_POSITION_LEFT;
510 else if (strcmp(p, "right") == 0)
511 map.map[map.channels++] = PA_CHANNEL_POSITION_RIGHT;
512 else if (strcmp(p, "center") == 0)
513 map.map[map.channels++] = PA_CHANNEL_POSITION_CENTER;
514 else if (strcmp(p, "subwoofer") == 0)
515 map.map[map.channels++] = PA_CHANNEL_POSITION_SUBWOOFER;
517 pa_channel_position_t i;
519 for (i = 0; i < PA_CHANNEL_POSITION_MAX; i++)
520 if (strcmp(p, table[i]) == 0) {
521 map.map[map.channels++] = i;
525 if (i >= PA_CHANNEL_POSITION_MAX) {
536 if (!pa_channel_map_valid(&map))
543 int pa_channel_map_valid(const pa_channel_map *map) {
548 if (map->channels <= 0 || map->channels > PA_CHANNELS_MAX)
551 for (c = 0; c < map->channels; c++) {
553 if (map->map[c] < 0 ||map->map[c] >= PA_CHANNEL_POSITION_MAX)