1 | //channelmap.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
5 | * The code (may) include prototypes and comments (and maybe |
---|
6 | * other code fragements) from libpulse*. They are mostly copyrighted by: |
---|
7 | * Lennart Poettering <poettering@users.sourceforge.net> and |
---|
8 | * Pierre Ossman <drzeus@drzeus.cx> |
---|
9 | * |
---|
10 | * This file is part of libroarpulse a part of RoarAudio, |
---|
11 | * a cross-platform sound system for both, home and professional use. |
---|
12 | * See README for details. |
---|
13 | * |
---|
14 | * This file is free software; you can redistribute it and/or modify |
---|
15 | * it under the terms of the GNU General Public License version 3 |
---|
16 | * as published by the Free Software Foundation. |
---|
17 | * |
---|
18 | * RoarAudio is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with this software; see the file COPYING. If not, write to |
---|
25 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
26 | * Boston, MA 02110-1301, USA. |
---|
27 | * |
---|
28 | * NOTE for everyone want's to change something and send patches: |
---|
29 | * read README and HACKING! There a addition information on |
---|
30 | * the license of this document you need to read before you send |
---|
31 | * any patches. |
---|
32 | * |
---|
33 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
34 | * or libpulse*: |
---|
35 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
36 | * and are therefore GPL. Because of this it may be illigal to use |
---|
37 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
38 | */ |
---|
39 | |
---|
40 | #include <libroarpulse/libroarpulse.h> |
---|
41 | |
---|
42 | /** Initialize the specified channel map and return a pointer to it */ |
---|
43 | pa_channel_map* pa_channel_map_init(pa_channel_map *m); |
---|
44 | |
---|
45 | /** Initialize the specified channel map for monoaural audio and return a pointer to it */ |
---|
46 | pa_channel_map* pa_channel_map_init_mono(pa_channel_map *m) { |
---|
47 | m->channels = 1; |
---|
48 | m->map[0] = PA_CHANNEL_POSITION_MONO; |
---|
49 | |
---|
50 | return m; |
---|
51 | } |
---|
52 | |
---|
53 | /** Initialize the specified channel map for stereophonic audio and return a pointer to it */ |
---|
54 | pa_channel_map* pa_channel_map_init_stereo(pa_channel_map *m) { |
---|
55 | m->channels = 2; |
---|
56 | m->map[0] = PA_CHANNEL_POSITION_LEFT; |
---|
57 | m->map[1] = PA_CHANNEL_POSITION_RIGHT; |
---|
58 | |
---|
59 | return m; |
---|
60 | } |
---|
61 | |
---|
62 | /** Initialize the specified channel map for the specified number |
---|
63 | * of channels using default labels and return a pointer to it. */ |
---|
64 | pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) { |
---|
65 | if ( m == NULL || channels == 0 ) |
---|
66 | return NULL; |
---|
67 | |
---|
68 | switch (channels) { |
---|
69 | case 1: return pa_channel_map_init_mono(m); break; |
---|
70 | case 2: return pa_channel_map_init_stereo(m); break; |
---|
71 | } |
---|
72 | |
---|
73 | switch (def) { |
---|
74 | default: |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | static struct { |
---|
80 | pa_channel_position_t pos; |
---|
81 | char * name; |
---|
82 | } _roar_po_chanpos[] = { |
---|
83 | {PA_CHANNEL_POSITION_INVALID, "invalid" }, |
---|
84 | {PA_CHANNEL_POSITION_MONO, "mono" }, |
---|
85 | {PA_CHANNEL_POSITION_LEFT, "left" }, |
---|
86 | {PA_CHANNEL_POSITION_RIGHT, "right" }, |
---|
87 | {PA_CHANNEL_POSITION_CENTER, "center" }, |
---|
88 | {PA_CHANNEL_POSITION_FRONT_LEFT, "front-left" }, |
---|
89 | {PA_CHANNEL_POSITION_FRONT_RIGHT, "front-right" }, |
---|
90 | {PA_CHANNEL_POSITION_FRONT_CENTER, "front-center" }, |
---|
91 | {PA_CHANNEL_POSITION_REAR_CENTER, "rear-center" }, |
---|
92 | {PA_CHANNEL_POSITION_REAR_LEFT, "rear-left" }, |
---|
93 | {PA_CHANNEL_POSITION_REAR_RIGHT, "rear-right" }, |
---|
94 | {PA_CHANNEL_POSITION_LFE, "lfe" }, |
---|
95 | {PA_CHANNEL_POSITION_SUBWOOFER, "subwoofer" }, |
---|
96 | {PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER, "front-left-of-center" }, |
---|
97 | {PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER, "front-right-of-center" }, |
---|
98 | {PA_CHANNEL_POSITION_SIDE_LEFT, "side-left" }, |
---|
99 | {PA_CHANNEL_POSITION_SIDE_RIGHT, "side-right" }, |
---|
100 | {PA_CHANNEL_POSITION_AUX0, "aux0" }, |
---|
101 | {PA_CHANNEL_POSITION_AUX1, "aux1" }, |
---|
102 | {PA_CHANNEL_POSITION_AUX2, "aux2" }, |
---|
103 | {PA_CHANNEL_POSITION_AUX3, "aux3" }, |
---|
104 | {PA_CHANNEL_POSITION_AUX4, "aux4" }, |
---|
105 | {PA_CHANNEL_POSITION_AUX5, "aux5" }, |
---|
106 | {PA_CHANNEL_POSITION_AUX6, "aux6" }, |
---|
107 | {PA_CHANNEL_POSITION_AUX7, "aux7" }, |
---|
108 | {PA_CHANNEL_POSITION_AUX8, "aux8" }, |
---|
109 | {PA_CHANNEL_POSITION_AUX9, "aux9" }, |
---|
110 | {PA_CHANNEL_POSITION_AUX10, "aux10" }, |
---|
111 | {PA_CHANNEL_POSITION_AUX11, "aux11" }, |
---|
112 | {PA_CHANNEL_POSITION_AUX12, "aux12" }, |
---|
113 | {PA_CHANNEL_POSITION_AUX13, "aux13" }, |
---|
114 | {PA_CHANNEL_POSITION_AUX14, "aux14" }, |
---|
115 | {PA_CHANNEL_POSITION_AUX15, "aux15" }, |
---|
116 | {PA_CHANNEL_POSITION_AUX16, "aux16" }, |
---|
117 | {PA_CHANNEL_POSITION_AUX17, "aux17" }, |
---|
118 | {PA_CHANNEL_POSITION_AUX18, "aux18" }, |
---|
119 | {PA_CHANNEL_POSITION_AUX19, "aux19" }, |
---|
120 | {PA_CHANNEL_POSITION_AUX20, "aux20" }, |
---|
121 | {PA_CHANNEL_POSITION_AUX21, "aux21" }, |
---|
122 | {PA_CHANNEL_POSITION_AUX22, "aux22" }, |
---|
123 | {PA_CHANNEL_POSITION_AUX23, "aux23" }, |
---|
124 | {PA_CHANNEL_POSITION_AUX24, "aux24" }, |
---|
125 | {PA_CHANNEL_POSITION_AUX25, "aux25" }, |
---|
126 | {PA_CHANNEL_POSITION_AUX26, "aux26" }, |
---|
127 | {PA_CHANNEL_POSITION_AUX27, "aux27" }, |
---|
128 | {PA_CHANNEL_POSITION_AUX28, "aux28" }, |
---|
129 | {PA_CHANNEL_POSITION_AUX29, "aux29" }, |
---|
130 | {PA_CHANNEL_POSITION_AUX30, "aux30" }, |
---|
131 | {PA_CHANNEL_POSITION_AUX31, "aux31" }, |
---|
132 | {PA_CHANNEL_POSITION_TOP_CENTER, "top-center" }, |
---|
133 | {PA_CHANNEL_POSITION_TOP_FRONT_LEFT, "top-front-left" }, |
---|
134 | {PA_CHANNEL_POSITION_TOP_FRONT_RIGHT, "top-front-right" }, |
---|
135 | {PA_CHANNEL_POSITION_TOP_FRONT_CENTER, "top-front-center" }, |
---|
136 | {PA_CHANNEL_POSITION_TOP_REAR_LEFT, "top-rear-left" }, |
---|
137 | {PA_CHANNEL_POSITION_TOP_REAR_RIGHT, "top-rear-right" }, |
---|
138 | {PA_CHANNEL_POSITION_TOP_REAR_CENTER, "top-rear-center" }, |
---|
139 | {PA_CHANNEL_POSITION_MAX, "max" }, |
---|
140 | {PA_CHANNEL_POSITION_INVALID, NULL} |
---|
141 | }; |
---|
142 | |
---|
143 | /** Return a text label for the specified channel position */ |
---|
144 | const char* pa_channel_position_to_string(pa_channel_position_t pos) { |
---|
145 | int i; |
---|
146 | |
---|
147 | for (i = 0; _roar_po_chanpos[i].name != NULL; i++) |
---|
148 | if ( _roar_po_chanpos[i].pos == pos ) |
---|
149 | return _roar_po_chanpos[i].name; |
---|
150 | |
---|
151 | return NULL; |
---|
152 | } |
---|
153 | |
---|
154 | /** The maximum length of strings returned by pa_channel_map_snprint() */ |
---|
155 | #define PA_CHANNEL_MAP_SNPRINT_MAX 336 |
---|
156 | |
---|
157 | /** Make a humand readable string from the specified channel map */ |
---|
158 | char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) { |
---|
159 | const char * g; |
---|
160 | char * c = s; |
---|
161 | int i; |
---|
162 | size_t len; |
---|
163 | size_t todo = l; |
---|
164 | |
---|
165 | if ( map == NULL || s == NULL || l == 0 ) |
---|
166 | return NULL; |
---|
167 | |
---|
168 | *c = 0; |
---|
169 | |
---|
170 | for (i = 0; i < map->channels; i++) { |
---|
171 | g = pa_channel_position_to_string(map->map[i]); |
---|
172 | |
---|
173 | if ( g == NULL ) |
---|
174 | return NULL; |
---|
175 | |
---|
176 | len = strlen(g); |
---|
177 | |
---|
178 | if ( (len + 1) < todo ) { |
---|
179 | memcpy(c, g, len); |
---|
180 | c[len] = ','; |
---|
181 | c += len + 1; |
---|
182 | todo -= len + 1; |
---|
183 | } else { |
---|
184 | return NULL; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | c[-1] = 0; |
---|
189 | s[l-1] = 0; |
---|
190 | |
---|
191 | return s; |
---|
192 | } |
---|
193 | |
---|
194 | /** Parse a channel position list into a channel map structure. \since 0.8.1 */ |
---|
195 | pa_channel_map *pa_channel_map_parse(pa_channel_map *map, const char *s) { |
---|
196 | int i; |
---|
197 | char * c; |
---|
198 | size_t len; |
---|
199 | pa_channel_position_t * v; |
---|
200 | |
---|
201 | if ( map == NULL ) |
---|
202 | return NULL; |
---|
203 | |
---|
204 | map->channels = 0; |
---|
205 | |
---|
206 | if ( s == NULL ) |
---|
207 | return map; |
---|
208 | |
---|
209 | while (*s != 0) { |
---|
210 | c = strstr(s, ","); |
---|
211 | |
---|
212 | if ( c == NULL ) { |
---|
213 | len = strlen(s); |
---|
214 | } else { |
---|
215 | len = c - s; |
---|
216 | } |
---|
217 | |
---|
218 | v = &(map->map[map->channels++]); |
---|
219 | |
---|
220 | *v = PA_CHANNEL_POSITION_INVALID; |
---|
221 | |
---|
222 | for (i = 0; *v == PA_CHANNEL_POSITION_INVALID && _roar_po_chanpos[i].name != NULL; i++) { |
---|
223 | if ( !strncasecmp(_roar_po_chanpos[i].name, s, len) && _roar_po_chanpos[i].name[len] == 0 ) { |
---|
224 | *v = _roar_po_chanpos[i].pos; |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | if ( c == NULL ) { |
---|
229 | break; |
---|
230 | } else { |
---|
231 | s = c + 1; |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | return map; |
---|
236 | } |
---|
237 | |
---|
238 | /** Compare two channel maps. Return 1 if both match. */ |
---|
239 | int pa_channel_map_equal(const pa_channel_map *a, const pa_channel_map *b); |
---|
240 | |
---|
241 | /** Return non-zero of the specified channel map is considered valid */ |
---|
242 | int pa_channel_map_valid(const pa_channel_map *map); |
---|
243 | |
---|
244 | //ll |
---|