4 This file is part of PulseAudio.
6 Copyright 2005-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/xmalloc.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
37 #include "channelmap.h"
39 const char *const table[] = {
40 [PA_CHANNEL_POSITION_MONO] = "mono",
42 [PA_CHANNEL_POSITION_FRONT_CENTER] = "front-center",
43 [PA_CHANNEL_POSITION_FRONT_LEFT] = "front-left",
44 [PA_CHANNEL_POSITION_FRONT_RIGHT] = "front-right",
46 [PA_CHANNEL_POSITION_REAR_CENTER] = "rear-center",
47 [PA_CHANNEL_POSITION_REAR_LEFT] = "rear-left",
48 [PA_CHANNEL_POSITION_REAR_RIGHT] = "rear-right",
50 [PA_CHANNEL_POSITION_LFE] = "lfe",
52 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "front-left-of-center",
53 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "front-right-of-center",
55 [PA_CHANNEL_POSITION_SIDE_LEFT] = "side-left",
56 [PA_CHANNEL_POSITION_SIDE_RIGHT] = "side-right",
58 [PA_CHANNEL_POSITION_AUX0] = "aux0",
59 [PA_CHANNEL_POSITION_AUX1] = "aux1",
60 [PA_CHANNEL_POSITION_AUX2] = "aux2",
61 [PA_CHANNEL_POSITION_AUX3] = "aux3",
62 [PA_CHANNEL_POSITION_AUX4] = "aux4",
63 [PA_CHANNEL_POSITION_AUX5] = "aux5",
64 [PA_CHANNEL_POSITION_AUX6] = "aux6",
65 [PA_CHANNEL_POSITION_AUX7] = "aux7",
66 [PA_CHANNEL_POSITION_AUX8] = "aux8",
67 [PA_CHANNEL_POSITION_AUX9] = "aux9",
68 [PA_CHANNEL_POSITION_AUX10] = "aux10",
69 [PA_CHANNEL_POSITION_AUX11] = "aux11",
70 [PA_CHANNEL_POSITION_AUX12] = "aux12",
71 [PA_CHANNEL_POSITION_AUX13] = "aux13",
72 [PA_CHANNEL_POSITION_AUX14] = "aux14",
73 [PA_CHANNEL_POSITION_AUX15] = "aux15",
74 [PA_CHANNEL_POSITION_AUX16] = "aux16",
75 [PA_CHANNEL_POSITION_AUX17] = "aux17",
76 [PA_CHANNEL_POSITION_AUX18] = "aux18",
77 [PA_CHANNEL_POSITION_AUX19] = "aux19",
78 [PA_CHANNEL_POSITION_AUX20] = "aux20",
79 [PA_CHANNEL_POSITION_AUX21] = "aux21",
80 [PA_CHANNEL_POSITION_AUX22] = "aux22",
81 [PA_CHANNEL_POSITION_AUX23] = "aux23",
82 [PA_CHANNEL_POSITION_AUX24] = "aux24",
83 [PA_CHANNEL_POSITION_AUX25] = "aux25",
84 [PA_CHANNEL_POSITION_AUX26] = "aux26",
85 [PA_CHANNEL_POSITION_AUX27] = "aux27",
86 [PA_CHANNEL_POSITION_AUX28] = "aux28",
87 [PA_CHANNEL_POSITION_AUX29] = "aux29",
88 [PA_CHANNEL_POSITION_AUX30] = "aux30",
89 [PA_CHANNEL_POSITION_AUX31] = "aux31",
91 [PA_CHANNEL_POSITION_TOP_CENTER] = "top-center",
93 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "top-front-center",
94 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "top-front-left",
95 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "top-front-right",
97 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "top-rear-center",
98 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "top-rear-left",
99 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "top-rear-right"
102 const char *const pretty_table[] = {
103 [PA_CHANNEL_POSITION_MONO] = "Mono",
105 [PA_CHANNEL_POSITION_FRONT_CENTER] = "Front Center",
106 [PA_CHANNEL_POSITION_FRONT_LEFT] = "Front Left",
107 [PA_CHANNEL_POSITION_FRONT_RIGHT] = "Front Right",
109 [PA_CHANNEL_POSITION_REAR_CENTER] = "Rear Center",
110 [PA_CHANNEL_POSITION_REAR_LEFT] = "Rear Left",
111 [PA_CHANNEL_POSITION_REAR_RIGHT] = "Rear Right",
113 [PA_CHANNEL_POSITION_LFE] = "Low Frequency Emmiter",
115 [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "Front Left-of-center",
116 [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "Front Right-of-center",
118 [PA_CHANNEL_POSITION_SIDE_LEFT] = "Side Left",
119 [PA_CHANNEL_POSITION_SIDE_RIGHT] = "Side Right",
121 [PA_CHANNEL_POSITION_AUX0] = "Auxiliary 0",
122 [PA_CHANNEL_POSITION_AUX1] = "Auxiliary 1",
123 [PA_CHANNEL_POSITION_AUX2] = "Auxiliary 2",
124 [PA_CHANNEL_POSITION_AUX3] = "Auxiliary 3",
125 [PA_CHANNEL_POSITION_AUX4] = "Auxiliary 4",
126 [PA_CHANNEL_POSITION_AUX5] = "Auxiliary 5",
127 [PA_CHANNEL_POSITION_AUX6] = "Auxiliary 6",
128 [PA_CHANNEL_POSITION_AUX7] = "Auxiliary 7",
129 [PA_CHANNEL_POSITION_AUX8] = "Auxiliary 8",
130 [PA_CHANNEL_POSITION_AUX9] = "Auxiliary 9",
131 [PA_CHANNEL_POSITION_AUX10] = "Auxiliary 10",
132 [PA_CHANNEL_POSITION_AUX11] = "Auxiliary 11",
133 [PA_CHANNEL_POSITION_AUX12] = "Auxiliary 12",
134 [PA_CHANNEL_POSITION_AUX13] = "Auxiliary 13",
135 [PA_CHANNEL_POSITION_AUX14] = "Auxiliary 14",
136 [PA_CHANNEL_POSITION_AUX15] = "Auxiliary 15",
137 [PA_CHANNEL_POSITION_AUX16] = "Auxiliary 16",
138 [PA_CHANNEL_POSITION_AUX17] = "Auxiliary 17",
139 [PA_CHANNEL_POSITION_AUX18] = "Auxiliary 18",
140 [PA_CHANNEL_POSITION_AUX19] = "Auxiliary 19",
141 [PA_CHANNEL_POSITION_AUX20] = "Auxiliary 20",
142 [PA_CHANNEL_POSITION_AUX21] = "Auxiliary 21",
143 [PA_CHANNEL_POSITION_AUX22] = "Auxiliary 22",
144 [PA_CHANNEL_POSITION_AUX23] = "Auxiliary 23",
145 [PA_CHANNEL_POSITION_AUX24] = "Auxiliary 24",
146 [PA_CHANNEL_POSITION_AUX25] = "Auxiliary 25",
147 [PA_CHANNEL_POSITION_AUX26] = "Auxiliary 26",
148 [PA_CHANNEL_POSITION_AUX27] = "Auxiliary 27",
149 [PA_CHANNEL_POSITION_AUX28] = "Auxiliary 28",
150 [PA_CHANNEL_POSITION_AUX29] = "Auxiliary 29",
151 [PA_CHANNEL_POSITION_AUX30] = "Auxiliary 30",
152 [PA_CHANNEL_POSITION_AUX31] = "Auxiliary 31",
154 [PA_CHANNEL_POSITION_TOP_CENTER] = "Top Center",
156 [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "Top Front Center",
157 [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "Top Front Left",
158 [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "Top Front Right",
160 [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "Top Rear Center",
161 [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "Top Rear left",
162 [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "Top Rear Right"
165 pa_channel_map* pa_channel_map_init(pa_channel_map *m) {
171 for (c = 0; c < PA_CHANNELS_MAX; c++)
172 m->map[c] = PA_CHANNEL_POSITION_INVALID;
177 pa_channel_map* pa_channel_map_init_mono(pa_channel_map *m) {
180 pa_channel_map_init(m);
183 m->map[0] = PA_CHANNEL_POSITION_MONO;
187 pa_channel_map* pa_channel_map_init_stereo(pa_channel_map *m) {
190 pa_channel_map_init(m);
193 m->map[0] = PA_CHANNEL_POSITION_LEFT;
194 m->map[1] = PA_CHANNEL_POSITION_RIGHT;
198 pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
200 pa_assert(channels > 0);
201 pa_assert(channels <= PA_CHANNELS_MAX);
203 pa_channel_map_init(m);
205 m->channels = channels;
208 case PA_CHANNEL_MAP_AIFF:
210 /* This is somewhat compatible with RFC3551 */
214 m->map[0] = PA_CHANNEL_POSITION_MONO;
218 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
219 m->map[1] = PA_CHANNEL_POSITION_SIDE_LEFT;
220 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
221 m->map[3] = PA_CHANNEL_POSITION_FRONT_RIGHT;
222 m->map[4] = PA_CHANNEL_POSITION_SIDE_RIGHT;
223 m->map[5] = PA_CHANNEL_POSITION_LFE;
227 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
228 m->map[3] = PA_CHANNEL_POSITION_REAR_LEFT;
229 m->map[4] = PA_CHANNEL_POSITION_REAR_RIGHT;
233 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
234 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
238 m->map[0] = PA_CHANNEL_POSITION_LEFT;
239 m->map[1] = PA_CHANNEL_POSITION_RIGHT;
240 m->map[2] = PA_CHANNEL_POSITION_CENTER;
244 m->map[0] = PA_CHANNEL_POSITION_LEFT;
245 m->map[1] = PA_CHANNEL_POSITION_CENTER;
246 m->map[2] = PA_CHANNEL_POSITION_RIGHT;
247 m->map[3] = PA_CHANNEL_POSITION_LFE;
254 case PA_CHANNEL_MAP_ALSA:
258 m->map[0] = PA_CHANNEL_POSITION_MONO;
262 m->map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
263 m->map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
267 m->map[5] = PA_CHANNEL_POSITION_LFE;
271 m->map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
275 m->map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
276 m->map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
280 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
281 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
288 case PA_CHANNEL_MAP_AUX: {
291 if (channels >= PA_CHANNELS_MAX)
294 for (i = 0; i < channels; i++)
295 m->map[i] = PA_CHANNEL_POSITION_AUX0 + i;
300 case PA_CHANNEL_MAP_WAVEEX:
304 m->map[0] = PA_CHANNEL_POSITION_MONO;
308 m->map[15] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
309 m->map[16] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
310 m->map[17] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
314 m->map[12] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
315 m->map[13] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
316 m->map[14] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
320 m->map[11] = PA_CHANNEL_POSITION_TOP_CENTER;
324 m->map[9] = PA_CHANNEL_POSITION_SIDE_LEFT;
325 m->map[10] = PA_CHANNEL_POSITION_SIDE_RIGHT;
329 m->map[8] = PA_CHANNEL_POSITION_REAR_CENTER;
333 m->map[6] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
334 m->map[7] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
338 m->map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
339 m->map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
343 m->map[3] = PA_CHANNEL_POSITION_LFE;
347 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
351 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
352 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
359 case PA_CHANNEL_MAP_OSS:
363 m->map[0] = PA_CHANNEL_POSITION_MONO;
367 m->map[6] = PA_CHANNEL_POSITION_REAR_LEFT;
368 m->map[7] = PA_CHANNEL_POSITION_REAR_RIGHT;
372 m->map[4] = PA_CHANNEL_POSITION_SIDE_LEFT;
373 m->map[5] = PA_CHANNEL_POSITION_SIDE_RIGHT;
377 m->map[3] = PA_CHANNEL_POSITION_LFE;
381 m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
385 m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
386 m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
400 const char* pa_channel_position_to_string(pa_channel_position_t pos) {
402 if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
408 const char* pa_channel_position_to_pretty_string(pa_channel_position_t pos) {
409 if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
412 return pretty_table[pos];
415 int pa_channel_map_equal(const pa_channel_map *a, const pa_channel_map *b) {
421 if (a->channels != b->channels)
424 for (c = 0; c < a->channels; c++)
425 if (a->map[c] != b->map[c])
431 char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) {
442 for (channel = 0; channel < map->channels && l > 1; channel++) {
443 l -= pa_snprintf(e, l, "%s%s",
445 pa_channel_position_to_string(map->map[channel]));
454 pa_channel_map *pa_channel_map_parse(pa_channel_map *rmap, const char *s) {
462 memset(&map, 0, sizeof(map));
464 if (strcmp(s, "stereo") == 0) {
466 map.map[0] = PA_CHANNEL_POSITION_LEFT;
467 map.map[1] = PA_CHANNEL_POSITION_RIGHT;
474 while ((p = pa_split(s, ",", &state))) {
476 if (map.channels >= PA_CHANNELS_MAX) {
481 /* Some special aliases */
482 if (strcmp(p, "left") == 0)
483 map.map[map.channels++] = PA_CHANNEL_POSITION_LEFT;
484 else if (strcmp(p, "right") == 0)
485 map.map[map.channels++] = PA_CHANNEL_POSITION_RIGHT;
486 else if (strcmp(p, "center") == 0)
487 map.map[map.channels++] = PA_CHANNEL_POSITION_CENTER;
488 else if (strcmp(p, "subwoofer") == 0)
489 map.map[map.channels++] = PA_CHANNEL_POSITION_SUBWOOFER;
491 pa_channel_position_t i;
493 for (i = 0; i < PA_CHANNEL_POSITION_MAX; i++)
494 if (strcmp(p, table[i]) == 0) {
495 map.map[map.channels++] = i;
499 if (i >= PA_CHANNEL_POSITION_MAX) {
510 if (!pa_channel_map_valid(&map))
517 int pa_channel_map_valid(const pa_channel_map *map) {
522 if (map->channels <= 0 || map->channels > PA_CHANNELS_MAX)
525 for (c = 0; c < map->channels; c++) {
527 if (map->map[c] < 0 ||map->map[c] >= PA_CHANNEL_POSITION_MAX)