-
Notifications
You must be signed in to change notification settings - Fork 0
/
snx_audio_sigma_vol_ctrl.c
287 lines (242 loc) · 6.75 KB
/
snx_audio_sigma_vol_ctrl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <errno.h>
#include <assert.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <alsa/control.h>
#define error printf
int snx_audio_crtl_get_items(int card_num, int *items, const char *ctrl_name)
{
char card[64];
snd_ctl_elem_info_t *info;
snd_ctl_elem_id_t *id;
snd_ctl_elem_value_t *control;
snd_ctl_elem_type_t type;
snd_ctl_t *handle = NULL;
int err = 0;
sprintf(card, "hw:%i", card_num);
if((err = snd_ctl_open(&handle, card, 0)) < 0)
{
error("Control %s open error: %s\n", card, snd_strerror(err));
goto ctrl_get_items_exit1;
}
snd_ctl_elem_id_alloca(&id);
snd_ctl_elem_value_alloca(&control);
snd_ctl_elem_info_alloca(&info);
snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
snd_ctl_elem_id_set_name(id, ctrl_name);
snd_ctl_elem_info_set_id(info, id);
if((err = snd_ctl_elem_info(handle, info)) < 0)
{
error("Cannot find the given element '%s'\n", ctrl_name);
goto ctrl_get_items_exit2;
}
snd_ctl_elem_info_get_id(info, id);
type = snd_ctl_elem_info_get_type(info);
switch (type)
{
case SND_CTL_ELEM_TYPE_INTEGER:
*items = snd_ctl_elem_info_get_max(info) - snd_ctl_elem_info_get_min(info) + 1;
break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
*items = snd_ctl_elem_info_get_items(info);
break;
default:
error("the type of '%s' element is error.\n", ctrl_name);
err = -1;
goto ctrl_get_items_exit2;
}
ctrl_get_items_exit2:
// free(info);
// free(id);
// free(control);
snd_ctl_close(handle);
ctrl_get_items_exit1:
return err;
}
int snx_audio_ctrl_get(int card_num, int *ctrl_val, const char *ctrl_name)
{
char card[64];
snd_ctl_elem_info_t *info;
snd_ctl_elem_id_t *id;
snd_ctl_elem_value_t *control;
snd_ctl_elem_type_t type;
snd_ctl_t *handle = NULL;
int err = 0;
sprintf(card, "hw:%i", card_num);
if((err = snd_ctl_open(&handle, card, 0)) < 0)
{
error("Control %s open error: %s\n", card, snd_strerror(err));
goto ctrl_get_exit1;
}
snd_ctl_elem_info_alloca(&info);
snd_ctl_elem_id_alloca(&id);
snd_ctl_elem_value_alloca(&control);
snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
snd_ctl_elem_id_set_name(id, ctrl_name);
snd_ctl_elem_info_set_id(info, id);
if((err = snd_ctl_elem_info(handle, info)) < 0)
{
error("Cannot find the given element '%s'\n", ctrl_name);
goto ctrl_get_exit2;
}
snd_ctl_elem_info_get_id(info, id);
snd_ctl_elem_value_set_id(control, id);
if((err = snd_ctl_elem_read(handle, control)) < 0)
{
error("'%s' element read error: %s\n", ctrl_name, snd_strerror(err));
goto ctrl_get_exit2;
}
type = snd_ctl_elem_info_get_type(info);
switch (type)
{
case SND_CTL_ELEM_TYPE_INTEGER:
*ctrl_val = snd_ctl_elem_value_get_integer(control, 0);
break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
*ctrl_val = snd_ctl_elem_value_get_enumerated(control, 0);
break;
default:
error("the type of '%s' element is error.\n", ctrl_name);
err = -1;
goto ctrl_get_exit2;
}
ctrl_get_exit2:
// free(info);
// free(id);
// free(control);
snd_ctl_close(handle);
ctrl_get_exit1:
return err;
}
int snx_audio_ctrl_set(int card_num, int ctrl_val, const char *ctrl_name)
{
char card[64];
snd_ctl_elem_info_t *info;
snd_ctl_elem_id_t *id;
snd_ctl_elem_value_t *control;
snd_ctl_elem_type_t type;
snd_ctl_t *handle = NULL;
int err = 0;
sprintf(card, "hw:%i", card_num);
if((err = snd_ctl_open(&handle, card, 0)) < 0)
{
error("Control %s open error: %s\n", card, snd_strerror(err));
goto ctrl_set_exit1;
}
snd_ctl_elem_id_alloca(&id);
snd_ctl_elem_value_alloca(&control);
snd_ctl_elem_info_alloca(&info);
snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
snd_ctl_elem_id_set_name(id, ctrl_name);
snd_ctl_elem_info_set_id(info, id);
if((err = snd_ctl_elem_info(handle, info)) < 0)
{
error("Cannot find the given element '%s'\n", ctrl_name);
goto ctrl_set_exit2;
}
snd_ctl_elem_info_get_id(info, id);
snd_ctl_elem_value_set_id(control, id);
type = snd_ctl_elem_info_get_type(info);
switch (type)
{
case SND_CTL_ELEM_TYPE_INTEGER:
snd_ctl_elem_value_set_integer(control, 0, ctrl_val);
break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
snd_ctl_elem_value_set_enumerated(control, 0, ctrl_val);
break;
default:
error("the type of '%s' element is error.\n", ctrl_name);
err = -1;
goto ctrl_set_exit2;
}
if((err = snd_ctl_elem_write(handle, control)) < 0)
{
error("'%s' element write error: %s\n", ctrl_name, snd_strerror(err));
goto ctrl_set_exit2;
}
ctrl_set_exit2:
// free(info);
// free(id);
// free(control);
snd_ctl_close(handle);
ctrl_set_exit1:
return err;
}
#define CARDNUM 0
static const char short_options[] = "hrfv:i";
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"rough", no_argument, NULL, 'r'},
{"fine", no_argument, NULL, 'f'},
{"vol", required_argument, NULL, 'v'},
{0, 0, 0, 0}
};
static void usage(FILE * fp, int argc, char ** argv) {
fprintf(fp, "Usage: %s [options]/n\n"
"Options:\n"
"-h Print this message\n"
"-r Rough adjusting sound\n"
"-f Fine adjusting sound\n"
"-v Set vol value\n"
"", argv[0]);
}
int main(int argc, char *argv[])
{
int vol_flag = 0;
int vol_tune;
int cur_vol;
int info_vol_cur;
int info_vol_range;
char ctrl_name[128]="NULL";
for (;;)
{
int index;
int c;
c = getopt_long(argc, argv, short_options, long_options, &index);
if (-1 == c)
break;
switch (c) {
case 0: /* getopt_long() flag */
break;
case 'h':
usage(stdout, argc, argv);
exit(EXIT_SUCCESS);
case 'r':
strcpy(ctrl_name, "SIG Capture Boost");
break;
case 'f':
strcpy(ctrl_name, "SIG Capture PGA");
break;
case 'v':
vol_tune = atoi(optarg);
if (vol_tune <= 0)
vol_tune = 0;
vol_flag = 1;
break;
default:
usage(stderr, argc, argv);
exit(EXIT_FAILURE);
}
}
if(strcmp(ctrl_name, "NULL") != 0)
{
snx_audio_crtl_get_items(CARDNUM, &info_vol_range, ctrl_name);
snx_audio_ctrl_get(CARDNUM, &info_vol_cur, ctrl_name);
printf("[SNX-AUDIO] Sigma-delta Vol (%s) range: 0 ~ %d; Current Vol : %d\n", ctrl_name, (info_vol_range-1), info_vol_cur);
if(vol_flag == 1)
{
if(vol_tune > (info_vol_range - 1))
vol_tune = (info_vol_range - 1);
snx_audio_ctrl_set(CARDNUM, vol_tune, ctrl_name);
snx_audio_ctrl_get(CARDNUM, &cur_vol, ctrl_name);
printf("[SNX-AUDIO] New Sigma-delta Vol (%s) : %d\n", ctrl_name, cur_vol);
}
}
return 0;
}