Mix.c

filter: Statementstmt 12.9% 18 / 139 Branchbran 8.0% 8 / 100 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 10.8% 26 / 239 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Mixer.xs ↑ Summary
1 /*
2 * Library to query / set various sound mixer parameters.
3 *
4 * This code is based on setmixer program by Michal Jaegermann
5 *
6 * Copyright (c) 2000 Sergey Gribov <sergey@sergey.com>
7 * This is free software with ABSOLUTELY NO WARRANTY.
8 * You can redistribute and modify it freely, but please leave
9 * this message attached to this file.
10 *
11 * Subject to terms of GNU General Public License (www.gnu.org)
12 *
13 * Last update: $Date: 2002/04/30 00:48:21 $ by $Author: sergey $
14 * Revision: $Revision: 1.5 $
15 *
16 */
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <sys/ioctl.h>
23 #include <sys/soundcard.h>
24  
25 #include "Mix.h"
26  
27 #define BUFSIZE 512
28  
29 const char * dname[] = SOUND_DEVICE_NAMES;
30  
31 static int devmask, stereod, recmask, mixer_fd = -1, init_flag = 0;
32  
33 static char dev_fname[BUFSIZE] = "";
34  
35 int
36 0 set_mixer_dev(char *fname) {
37 #ifdef DEBUG
38 fprintf(stderr, "set_mixer_dev(%s)\n", fname);
39 #endif
40 0 strncpy(dev_fname, fname, BUFSIZE-1);
41 0 return(0);
42 }
43  
44 int
45 6 open_mixer() {
46 #ifdef DEBUG
47 fprintf(stderr, "open_mixer()\n");
48 #endif
49 6 ▶ if (dev_fname[0] == '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (dev_fname[0] == '\0') { 1 5 6
50 1 strncpy(dev_fname, MIXER, BUFSIZE-1);
51 }
52 6 ▶ if ((mixer_fd = open(dev_fname, O_RDWR)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((mixer_fd = open(dev_fname, O_RDWR)) < 0) { 6 0 6
53 6 fprintf(stderr, "Error opening %s.", MIXER);
54 6 return(-1);
55 }
56 0 ▶ if (ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) { 0 0 0
57 0 perror("SOUND_MIXER_READ_DEVMASK");
58 0 return(-1);
59 }
60 0 ▶ if (ioctl(mixer_fd, SOUND_MIXER_READ_STEREODEVS, &stereod) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(mixer_fd, SOUND_MIXER_READ_STEREODEVS, &stereod) == -1) { 0 0 0
61 0 perror("SOUND_MIXER_READ_STEREODEVS");
62 0 return(-1);
63 }
64 0 ▶ if (ioctl(mixer_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(mixer_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1) { 0 0 0
65 0 perror("SOUND_MIXER_READ_RECMASK");
66 0 return(-1);
67 }
68  
69 0 ▶ if (!devmask) {
Branch: 1 branch
BranchTrueFalseTotal
if (!devmask) { 0 0 0
70 0 fprintf(stderr, "No device found.");
71 0 return(-1);
72 }
73 0 return(0);
74 }
75  
76 int
77 0 close_mixer() {
78 #ifdef DEBUG
79 fprintf(stderr, "close_mixer()\n");
80 #endif
81 0 ▶ if (mixer_fd < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (mixer_fd < 0) return; 0 0 0
82 0 close(mixer_fd);
83 0 init_flag = 0;
84 0 mixer_fd = -1;
85 0 return(0);
86 }
87  
88 /*
89 * Get parameter value
90 * Parameter:
91 * cntrl - name of parameter
92 * Returns:
93 * integer value, which will be constructed as follows:
94 * lower byte (x & 0xff) - value of the left channel (or whole value)
95 * next byte (x & 0xff00) - value of the right channel
96 * third byte (x & 0xff0000) - flags (if x & 0x10000 then 2 channels exist)
97 */
98 int
99 3 get_param_val(char *cntrl) {
100 3 int i, d, len, lcval, ret = 0;
101  
102 #ifdef DEBUG
103 fprintf(stderr, "get_param_val(%s)\n", cntrl);
104 #endif
105
106 3 ▶ if (!init_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) { 3 0 3
107 3 ▶ if (open_mixer()) {
Branch: 1 branch
BranchTrueFalseTotal
if (open_mixer()) { 3 0 3
108 3 return(-1);
109 }
110 }
111 0 len = strlen(cntrl);
112 0 ▶ for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 0 0 0
113 0 d = (1 << i);
114 0 ▶ if ((0 == strncmp(dname[i], cntrl, len)) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((0 == strncmp(dname[i], cntrl, len)) && 0 0 0
115 0 ▶ (0 != (devmask & d))) {
Branch: 1 branch
BranchTrueFalseTotal
(0 != (devmask & d))) { 0 0 0
116 0 ▶ if (-1 == ioctl(mixer_fd, MIXER_READ(i), &lcval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == ioctl(mixer_fd, MIXER_READ(i), &lcval)) { 0 0 0
117 0 perror("MIXER_READ");
118 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
119 0 close_mixer();
120 0 return(-1);
121 }
122 else {
123 0 ret = lcval & 0x7f;
124 0 ▶ if (d & stereod) {
Branch: 1 branch
BranchTrueFalseTotal
if (d & stereod) { 0 0 0
125 0 ret = ret | 0x10000;
126 0 ret = ret | (lcval & 0x7f00);
127 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
128 0 close_mixer();
129 0 return(ret);
130 }
131 }
132 }
133 }
134 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
135 0 close_mixer();
136 0 return(-1);
137 }
138  
139  
140 char *
141 0 get_source()
142 {
143 int j;
144 0 unsigned int source = 0;
145 0 ▶ if (!init_flag){
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag){ 0 0 0
146 0 ▶ if (open_mixer())
Branch: 1 branch
BranchTrueFalseTotal
if (open_mixer()) 0 0 0
147 0 return("");
148 }
149 0 ▶ if (-1 == ioctl(mixer_fd, SOUND_MIXER_READ_RECSRC, &source)){
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == ioctl(mixer_fd, SOUND_MIXER_READ_RECSRC, &source)){ 0 0 0
150 0 perror("MIXER_READ_RECSRC");
151 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
152 0 close_mixer();
153 0 return("");
154 }
155 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
156 0 close_mixer();
157 0 source &= recmask;
158 0 ▶ for (j = 0; source; source >>= 1, j++){
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; source; source >>= 1, j++){ 0 0 0
159 0 ▶ if (source & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (source & 1) 0 0 0
160 0 return((char *) dname[j]);
161 }
162 0 return("");
163 }
164  
165 int
166 0 set_source(char *cntrl)
167 {
168 0 int i, d, len, ret = 0;
169  
170 #ifdef DEBUG
171 fprintf(stderr, "set_recsrc(%s)\n", cntrl);
172 #endif
173  
174 0 ▶ if (!init_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) { 0 0 0
175 0 ▶ if (open_mixer()) {
Branch: 1 branch
BranchTrueFalseTotal
if (open_mixer()) { 0 0 0
176 0 return(-1);
177 }
178 }
179 0 len = strlen(cntrl);
180 0 ▶ for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 0 0 0
181 0 d = (1 << i);
182 0 ▶ if ((0 == strncmp(dname[i], cntrl, len)) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((0 == strncmp(dname[i], cntrl, len)) && 0 0 0
183 0 ▶ (0 != (recmask & d))) {
Branch: 1 branch
BranchTrueFalseTotal
(0 != (recmask & d))) { 0 0 0
184 0 ▶ if (-1 == ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC, &d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC, &d)) { 0 0 0
185 0 perror("MIXER_WRITE_RECSRC");
186 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
187 0 close_mixer();
188 0 return(-1);
189 }
190 else {
191 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
192 0 close_mixer();
193 0 return(0);
194 }
195 }
196 }
197 0 d = 0;
198 0 ▶ if (-1 == ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC, &d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC, &d)) { 0 0 0
199 0 perror("MIXER_WRITE_RECSRC");
200 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
201 0 close_mixer();
202 0 return(-1);
203 }
204 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
205 0 close_mixer();
206 0 return(0);
207 }
208  
209 /*
210 * Set parameter value.
211 * Parameters:
212 * cntrl - name of parameter
213 * lcval - left channel value
214 * rcval - right channel value
215 * Returns 0 if Ok, -1 if failed
216 */
217 int
218 2 set_param_val(char *cntrl, int lcval, int rcval) {
219 int len, i, d;
220  
221 #ifdef DEBUG
222 fprintf(stderr, "set_param_val(%s, %d, %d)\n", cntrl, lcval, rcval);
223 #endif
224
225 2 ▶ if (!init_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) { 2 0 2
226 2 ▶ if (open_mixer()) {
Branch: 1 branch
BranchTrueFalseTotal
if (open_mixer()) { 2 0 2
227 2 return(-1);
228 }
229 }
230 0 len = strlen(cntrl);
231 0 ▶ for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 0 0 0
232 0 ▶ if (0 == strncmp(dname[i], cntrl, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == strncmp(dname[i], cntrl, len)) { 0 0 0
233 0 d = (1 << i);
234 0 ▶ if (0 != (devmask & d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 != (devmask & d)) { 0 0 0
235 0 ▶ lcval = (lcval < 0 ? 0 : (lcval > 100 ? 100 : lcval));
Branch: 1 branch
BranchTrueFalseTotal
lcval = (lcval < 0 ? 0 : (lcval > 100 ? 100 : lcval)); 0 0 0
236 0 ▶ if (d & stereod) {
Branch: 1 branch
BranchTrueFalseTotal
if (d & stereod) { 0 0 0
237 0 ▶ rcval = (rcval < 0 ? 0 : (rcval > 100 ? 100 : rcval));
Branch: 1 branch
BranchTrueFalseTotal
rcval = (rcval < 0 ? 0 : (rcval > 100 ? 100 : rcval)); 0 0 0
238 0 lcval |= (rcval << 8);
239 }
240 0 ▶ if (-1 == ioctl(mixer_fd, MIXER_WRITE(i), &lcval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == ioctl(mixer_fd, MIXER_WRITE(i), &lcval)) { 0 0 0
241 0 perror("MIXER_WRITE");
242 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
243 0 close_mixer();
244 0 return(-1);
245 }
246 }
247 0 break;
248 }
249 }
250 0 ▶ if (!init_flag)
Branch: 1 branch
BranchTrueFalseTotal
if (!init_flag) 0 0 0
251 0 close_mixer();
252 0 return(0);
253 }
254  
255 int
256 1 init_mixer() {
257 1 ▶ if (open_mixer()) {
Branch: 1 branch
BranchTrueFalseTotal
if (open_mixer()) { 1 0 1
258 1 return(-1);
259 }
260 0 init_flag = 1;
261 0 return(0);
262 }
263  
264 int
265 0 get_params_num() {
266 0 return(SOUND_MIXER_NRDEVICES);
267 }
268  
269 char *
270 0 get_params_list() {
271 static char buf[BUFSIZE];
272 0 int i, l, len = 0;
273 0 buf[0] = '\0';
274 0 ▶ for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { 0 0 0
275 0 l = strlen(dname[i]);
276 0 ▶ if ((len >= (BUFSIZE - 2)) || ((len + l + 3) >= BUFSIZE)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((len >= (BUFSIZE - 2)) || ((len + l + 3) >= BUFSIZE)) { 0 0 0
if ((len >= (BUFSIZE - 2)) || ((len + l + 3) >= BUFSIZE)) { 0 0 0
277 0 buf[len] = '\0';
278 0 return(buf);
279 }
280 0 strcat(buf, dname[i]);
281 0 strcat(buf, " ");
282 0 len += l + 1;
283 }
284 0 buf[len] = '\0';
285 0 return(buf);
286 }
287  
« Mixer.xs ↑ Summary