]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-external-tinyalsa.git/blob - tinymix.c
Update to latest tinyalsa
[android-sdk/platform-external-tinyalsa.git] / tinymix.c
1 /* tinymix.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 <tinyalsa/asoundlib.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
34 static void tinymix_list_controls(struct mixer *mixer);
35 static void tinymix_detail_control(struct mixer *mixer, unsigned int id);
36 static void tinymix_set_value(struct mixer *mixer, unsigned int id,
37                               char *value);
39 int main(int argc, char **argv)
40 {
41     struct mixer *mixer;
43     mixer = mixer_open(0);
44     if (!mixer) {
45         fprintf(stderr, "Failed to open mixer\n");
46         return EXIT_FAILURE;
47     }
49     if (argc == 1)
50         tinymix_list_controls(mixer);
51     else if (argc == 2)
52         tinymix_detail_control(mixer, atoi(argv[1]));
53     else if (argc == 3)
54         tinymix_set_value(mixer, atoi(argv[1]), argv[2]);
55     else
56         printf("Usage: tinymix [control id] [value to set]\n");
58     mixer_close(mixer);
60     return 0;
61 }
63 static void tinymix_list_controls(struct mixer *mixer)
64 {
65     struct mixer_ctl *ctl;
66     const char *type;
67     unsigned int num_ctls, num_values;
68     char buffer[256];
69     unsigned int i;
71     num_ctls = mixer_get_num_ctls(mixer);
73     printf("Number of controls: %d\n", num_ctls);
75     printf("ctl\ttype\tnum\tname\n");
76     for (i = 0; i < num_ctls; i++) {
77         ctl = mixer_get_ctl(mixer, i);
79         mixer_ctl_get_name(ctl, buffer, sizeof(buffer));
80         type = mixer_ctl_get_type_string(ctl);
81         num_values = mixer_ctl_get_num_values(ctl);
83         printf("%d\t%s\t%d\t%s\n", i, type, num_values, buffer);
84     }
85 }
87 static void tinymix_print_enum(struct mixer_ctl *ctl)
88 {
89     unsigned int num_enums;
90     char buffer[256];
91     unsigned int i;
93     num_enums = mixer_ctl_get_num_enums(ctl);
95     for (i = 0; i < num_enums; i++) {
96         mixer_ctl_get_enum_string(ctl, i, buffer, sizeof(buffer));
97         printf("\t%s%s", mixer_ctl_get_value(ctl, 0) == (int)i ? ">" : "",
98                buffer);
99     }
102 static void tinymix_detail_control(struct mixer *mixer, unsigned int id)
104     struct mixer_ctl *ctl;
105     enum mixer_ctl_type type;
106     unsigned int num_values;
107     char buffer[256];
108     unsigned int i;
109     int min, max;
111     if (id >= mixer_get_num_ctls(mixer)) {
112         fprintf(stderr, "Invalid mixer control\n");
113         return;
114     }
116     ctl = mixer_get_ctl(mixer, id);
118     mixer_ctl_get_name(ctl, buffer, sizeof(buffer));
119     type = mixer_ctl_get_type(ctl);
120     num_values = mixer_ctl_get_num_values(ctl);
122     printf("%s:", buffer);
123     for (i = 0; i < num_values; i++) {
124         switch (type)
125         {
126         case MIXER_CTL_TYPE_INT:
127             printf(" %d", mixer_ctl_get_value(ctl, i));
128             break;
129         case MIXER_CTL_TYPE_BOOL:
130             printf(" %s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
131             break;
132         case MIXER_CTL_TYPE_ENUM:
133             tinymix_print_enum(ctl);
134             break;
135         default:
136             printf(" unknown");
137             break;
138         };
139     }
140     if (type == MIXER_CTL_TYPE_INT) {
141         min = mixer_ctl_get_range_min(ctl);
142         max = mixer_ctl_get_range_max(ctl);
143         printf(" (range %d->%d)", min, max);
144     }
145     printf("\n");
148 static void tinymix_set_value(struct mixer *mixer, unsigned int id,
149                               char *string)
151     struct mixer_ctl *ctl;
152     enum mixer_ctl_type type;
153     unsigned int num_values;
154     unsigned int i;
156     ctl = mixer_get_ctl(mixer, id);
157     type = mixer_ctl_get_type(ctl);
158     num_values = mixer_ctl_get_num_values(ctl);
160     if (isdigit(string[0])) {
161         int value = atoi(string);
163         for (i = 0; i < num_values; i++) {
164             if (mixer_ctl_set_value(ctl, i, value)) {
165                 fprintf(stderr, "Error: invalid value\n");
166                 return;
167             }
168         }
169     } else {
170         if (type == MIXER_CTL_TYPE_ENUM) {
171             if (mixer_ctl_set_enum_by_string(ctl, string))
172                 fprintf(stderr, "Error: invalid enum value\n");
173         } else {
174             fprintf(stderr, "Error: only enum types can be set with strings\n");
175         }
176     }