4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include "sample-util.h"
45 static int add_key_value(struct pa_hashmap
*map
, char *key
, char *value
, const char* const* valid_keys
) {
47 assert(map
&& key
&& value
);
51 for (v
= valid_keys
; *v
; v
++)
52 if (strcmp(*v
, key
) == 0)
62 e
= malloc(sizeof(struct entry
));
66 pa_hashmap_put(map
, key
, e
);
70 struct pa_modargs
*pa_modargs_new(const char *args
, const char* const* valid_keys
) {
71 struct pa_hashmap
*map
= NULL
;
73 map
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
77 enum { WHITESPACE
, KEY
, VALUE_START
, VALUE_SIMPLE
, VALUE_DOUBLE_QUOTES
, VALUE_TICKS
} state
;
78 const char *p
, *key
, *value
;
79 size_t key_len
, value_len
;
83 for (p
= args
; *p
; p
++) {
88 else if (!isspace(*p
)) {
105 } else if (*p
== '"') {
106 state
= VALUE_DOUBLE_QUOTES
;
109 } else if (isspace(*p
)) {
110 if (add_key_value(map
, strndup(key
, key_len
), strdup(""), valid_keys
) < 0)
114 state
= VALUE_SIMPLE
;
121 if (add_key_value(map
, strndup(key
, key_len
), strndup(value
, value_len
), valid_keys
) < 0)
127 case VALUE_DOUBLE_QUOTES
:
129 if (add_key_value(map
, strndup(key
, key_len
), strndup(value
, value_len
), valid_keys
) < 0)
137 if (add_key_value(map
, strndup(key
, key_len
), strndup(value
, value_len
), valid_keys
) < 0)
146 if (state
== VALUE_START
) {
147 if (add_key_value(map
, strndup(key
, key_len
), strdup(""), valid_keys
) < 0)
149 } else if (state
== VALUE_SIMPLE
) {
150 if (add_key_value(map
, strndup(key
, key_len
), strdup(value
), valid_keys
) < 0)
152 } else if (state
!= WHITESPACE
)
156 return (struct pa_modargs
*) map
;
161 pa_modargs_free((struct pa_modargs
*) map
);
167 static void free_func(void *p
, void*userdata
) {
175 void pa_modargs_free(struct pa_modargs
*ma
) {
176 struct pa_hashmap
*map
= (struct pa_hashmap
*) ma
;
177 pa_hashmap_free(map
, free_func
, NULL
);
180 const char *pa_modargs_get_value(struct pa_modargs
*ma
, const char *key
, const char *def
) {
181 struct pa_hashmap
*map
= (struct pa_hashmap
*) ma
;
184 if (!(e
= pa_hashmap_get(map
, key
)))
190 int pa_modargs_get_value_u32(struct pa_modargs
*ma
, const char *key
, uint32_t *value
) {
194 assert(ma
&& key
&& value
);
196 if (!(v
= pa_modargs_get_value(ma
, key
, NULL
)))
202 l
= strtoul(v
, &e
, 0);
206 *value
= (uint32_t) l
;
210 int pa_modargs_get_sample_spec(struct pa_modargs
*ma
, struct pa_sample_spec
*rss
) {
213 struct pa_sample_spec ss
;
217 if ((pa_modargs_get_value_u32(ma
, "rate", &ss
.rate
)) < 0)
220 channels
= ss
.channels
;
221 if ((pa_modargs_get_value_u32(ma
, "channels", &channels
)) < 0)
223 ss
.channels
= (uint8_t) channels
;
225 if ((format
= pa_modargs_get_value(ma
, "format", NULL
))) {
226 if (strcmp(format
, "s16le") == 0)
227 ss
.format
= PA_SAMPLE_S16LE
;
228 else if (strcmp(format
, "s16be") == 0)
229 ss
.format
= PA_SAMPLE_S16BE
;
230 else if (strcmp(format
, "s16ne") == 0 || strcmp(format
, "s16") == 0 || strcmp(format
, "16") == 0)
231 ss
.format
= PA_SAMPLE_S16NE
;
232 else if (strcmp(format
, "u8") == 0 || strcmp(format
, "8") == 0)
233 ss
.format
= PA_SAMPLE_U8
;
234 else if (strcmp(format
, "float32") == 0 || strcmp(format
, "float32ne") == 0)
235 ss
.format
= PA_SAMPLE_FLOAT32
;
236 else if (strcmp(format
, "float32le") == 0)
237 ss
.format
= PA_SAMPLE_FLOAT32LE
;
238 else if (strcmp(format
, "float32be") == 0)
239 ss
.format
= PA_SAMPLE_FLOAT32BE
;
240 else if (strcmp(format
, "ulaw") == 0)
241 ss
.format
= PA_SAMPLE_ULAW
;
242 else if (strcmp(format
, "alaw") == 0)
243 ss
.format
= PA_SAMPLE_ALAW
;
248 if (!pa_sample_spec_valid(&ss
))
256 int pa_modargs_get_source_index(struct pa_modargs
*ma
, struct pa_core
*c
, uint32_t *index
) {
260 if (!(t
= pa_modargs_get_value(ma
, "source", NULL
)))
261 *index
= PA_IDXSET_INVALID
;
263 struct pa_source
*source
;
264 if (!(source
= pa_namereg_get(c
, t
, PA_NAMEREG_SOURCE
)))
267 *index
= source
->index
;
273 int pa_modargs_get_sink_index(struct pa_modargs
*ma
, struct pa_core
*c
, uint32_t *index
) {
277 if (!(t
= pa_modargs_get_value(ma
, "sink", NULL
)))
278 *index
= PA_IDXSET_INVALID
;
280 struct pa_sink
*sink
;
281 if (!(sink
= pa_namereg_get(c
, t
, PA_NAMEREG_SINK
)))
284 *index
= sink
->index
;