]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-external-tinyalsa.git/blob - mixer.c
Initial version of tinyalsa
[android-sdk/platform-external-tinyalsa.git] / mixer.c
1 /* mixer.c
2 **
3 ** Copyright 2011, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 **     * Redistributions of source code must retain the above copyright
8 **       notice, this list of conditions and the following disclaimer.
9 **     * Redistributions in binary form must reproduce the above copyright
10 **       notice, this list of conditions and the following disclaimer in the
11 **       documentation and/or other materials provided with the distribution.
12 **     * Neither the name of The Android Open Source Project nor the names of
13 **       its contributors may be used to endorse or promote products derived
14 **       from this software without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 ** DAMAGE.
27 */
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <ctype.h>
37 #include <linux/ioctl.h>
38 #define __force
39 #define __bitwise
40 #define __user
41 #include <sound/asound.h>
43 #include <tinyalsa/asoundlib.h>
45 struct mixer_ctl {
46     struct mixer *mixer;
47     struct snd_ctl_elem_info *info;
48     char **ename;
49 };
51 struct mixer {
52     int fd;
53     struct snd_ctl_elem_info *info;
54     struct mixer_ctl *ctl;
55     unsigned int count;
56 };
58 void mixer_close(struct mixer *mixer)
59 {
60     unsigned int n,m;
62     if (!mixer)
63         return;
65     if (mixer->fd >= 0)
66         close(mixer->fd);
68     if (mixer->ctl) {
69         for (n = 0; n < mixer->count; n++) {
70             if (mixer->ctl[n].ename) {
71                 unsigned int max = mixer->ctl[n].info->value.enumerated.items;
72                 for (m = 0; m < max; m++)
73                     free(mixer->ctl[n].ename[m]);
74                 free(mixer->ctl[n].ename);
75             }
76         }
77         free(mixer->ctl);
78     }
80     if (mixer->info)
81         free(mixer->info);
83     free(mixer);
85     /* TODO: verify frees */
86 }
88 struct mixer *mixer_open(unsigned int card)
89 {
90     struct snd_ctl_elem_list elist;
91     struct snd_ctl_elem_info tmp;
92     struct snd_ctl_elem_id *eid = NULL;
93     struct mixer *mixer = NULL;
94     unsigned int n, m;
95     int fd;
96     char fn[256];
98     snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
99     fd = open(fn, O_RDWR);
100     if (fd < 0)
101         return 0;
103     memset(&elist, 0, sizeof(elist));
104     if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
105         goto fail;
107     mixer = calloc(1, sizeof(*mixer));
108     if (!mixer)
109         goto fail;
111     mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
112     mixer->info = calloc(elist.count, sizeof(struct snd_ctl_elem_info));
113     if (!mixer->ctl || !mixer->info)
114         goto fail;
116     eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
117     if (!eid)
118         goto fail;
120     mixer->count = elist.count;
121     mixer->fd = fd;
122     elist.space = mixer->count;
123     elist.pids = eid;
124     if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
125         goto fail;
127     for (n = 0; n < mixer->count; n++) {
128         struct snd_ctl_elem_info *ei = mixer->info + n;
129         ei->id.numid = eid[n].numid;
130         if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
131             goto fail;
132         mixer->ctl[n].info = ei;
133         mixer->ctl[n].mixer = mixer;
134         if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
135             char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
136             if (!enames)
137                 goto fail;
138             mixer->ctl[n].ename = enames;
139             for (m = 0; m < ei->value.enumerated.items; m++) {
140                 memset(&tmp, 0, sizeof(tmp));
141                 tmp.id.numid = ei->id.numid;
142                 tmp.value.enumerated.item = m;
143                 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
144                     goto fail;
145                 enames[m] = strdup(tmp.value.enumerated.name);
146                 if (!enames[m])
147                     goto fail;
148             }
149         }
150     }
152     free(eid);
153     return mixer;
155 fail:
156     /* TODO: verify frees in failure case */
157     if (eid)
158         free(eid);
159     if (mixer)
160         mixer_close(mixer);
161     else if (fd >= 0)
162         close(fd);
163     return 0;
166 unsigned int mixer_get_num_ctls(struct mixer *mixer)
168     if (!mixer)
169         return 0;
171     return mixer->count;
174 struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
176     if (mixer && (id < mixer->count))
177         return mixer->ctl + id;
179     return NULL;
182 struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
184     unsigned int n;
186     if (!mixer)
187         return NULL;
189     for (n = 0; n < mixer->count; n++)
190         if (!strcmp(name, (char*) mixer->info[n].id.name))
191             return mixer->ctl + n;
193     return NULL;
196 int mixer_ctl_get_name(struct mixer_ctl *ctl, char *name, unsigned int size)
198     if (!ctl || !name || (size == 0))
199         return -EINVAL;
201     strncpy(name, (char *)ctl->info->id.name, size);
202     return 0;
205 enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
207     if (!ctl)
208         return MIXER_CTL_TYPE_UNKNOWN;
210     switch (ctl->info->type) {
211     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:    return MIXER_CTL_TYPE_BOOL;
212     case SNDRV_CTL_ELEM_TYPE_INTEGER:    return MIXER_CTL_TYPE_INT;
213     case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
214     case SNDRV_CTL_ELEM_TYPE_BYTES:      return MIXER_CTL_TYPE_BYTE;
215     case SNDRV_CTL_ELEM_TYPE_IEC958:     return MIXER_CTL_TYPE_IEC958;
216     case SNDRV_CTL_ELEM_TYPE_INTEGER64:  return MIXER_CTL_TYPE_INT64;
217     default:                             return MIXER_CTL_TYPE_UNKNOWN;
218     };
221 const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
223     if (!ctl)
224         return "";
226     switch (ctl->info->type) {
227     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:    return "BOOL";
228     case SNDRV_CTL_ELEM_TYPE_INTEGER:    return "INT";
229     case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
230     case SNDRV_CTL_ELEM_TYPE_BYTES:      return "BYTE";
231     case SNDRV_CTL_ELEM_TYPE_IEC958:     return "IEC958";
232     case SNDRV_CTL_ELEM_TYPE_INTEGER64:  return "INT64";
233     default:                             return "Unknown";
234     };
237 unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
239     if (!ctl)
240         return 0;
242     return ctl->info->count;
245 static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
247     int range;
249     if (percent > 100)
250         percent = 100;
251     else if (percent < 0)
252         percent = 0;
254     range = (ei->value.integer.max - ei->value.integer.min);
256     return ei->value.integer.min + (range * percent) / 100;
259 static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
261     int range = (ei->value.integer.max - ei->value.integer.min);
263     if (range == 0)
264         return 0;
266     return ((value - ei->value.integer.min) / range) * 100;
269 int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
271     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
272         return -EINVAL;
274     return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
277 int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
279     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
280         return -EINVAL;
282     return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
285 int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
287     struct snd_ctl_elem_value ev;
288     int ret;
290     if (!ctl || (id >= ctl->info->count))
291         return -EINVAL;
293     memset(&ev, 0, sizeof(ev));
294     ev.id.numid = ctl->info->id.numid;
295     ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
296     if (ret < 0)
297         return ret;
299     switch (ctl->info->type) {
300     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
301         return !!ev.value.integer.value[id];
303     case SNDRV_CTL_ELEM_TYPE_INTEGER:
304         return ev.value.integer.value[id];
306     case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
307         return ev.value.enumerated.item[id];
309     default:
310         return -EINVAL;
311     }
313     return 0;
316 int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
318     struct snd_ctl_elem_value ev;
319     int ret;
321     if (!ctl || (id >= ctl->info->count))
322         return -EINVAL;
324     memset(&ev, 0, sizeof(ev));
325     ev.id.numid = ctl->info->id.numid;
326     ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
327     if (ret < 0)
328         return ret;
330     switch (ctl->info->type) {
331     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
332         ev.value.integer.value[id] = !!value;
333         break;
335     case SNDRV_CTL_ELEM_TYPE_INTEGER:
336         ev.value.integer.value[id] = value;
337         break;
339     case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
340         ev.value.enumerated.item[id] = value;
341         break;
343     default:
344         return -EINVAL;
345     }
347     return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
350 int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
352     struct snd_ctl_elem_value ev;
353     int ret;
355     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
356         return -EINVAL;
358     memset(&ev, 0, sizeof(ev));
359     ev.id.numid = ctl->info->id.numid;
360     ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
361     if (ret < 0)
362         return ret;
364     return ctl->info->value.integer.min;
367 int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
369     struct snd_ctl_elem_value ev;
370     int ret;
372     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
373         return -EINVAL;
375     memset(&ev, 0, sizeof(ev));
376     ev.id.numid = ctl->info->id.numid;
377     ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
378     if (ret < 0)
379         return ret;
381     return ctl->info->value.integer.max;
384 unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
386     if (!ctl)
387         return 0;
389     return ctl->info->value.enumerated.items;
392 int mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id,
393                               char *string, unsigned int size)
395     struct snd_ctl_elem_value ev;
396     int ret;
398     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
399         (enum_id >= ctl->info->value.enumerated.items))
400         return -EINVAL;
402     memset(&ev, 0, sizeof(ev));
403     ev.id.numid = ctl->info->id.numid;
404     ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
405     if (ret < 0)
406         return ret;
407     strncpy(string, (char *)ctl->ename[enum_id], size);
409     return 0;
412 int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
414     unsigned int i, num_enums;
415     struct snd_ctl_elem_value ev;
416     int ret;
418     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
419         return -EINVAL;
421     num_enums = ctl->info->value.enumerated.items;
422     for (i = 0; i < num_enums; i++) {
423         if (!strcmp(string, ctl->ename[i])) {
424             memset(&ev, 0, sizeof(ev));
425             ev.value.enumerated.item[0] = i;
426             ev.id.numid = ctl->info->id.numid;
427             ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
428             if (ret < 0)
429                 return ret;
430             return 0;
431         }
432     }
434     return -EINVAL;