aboutsummaryrefslogtreecommitdiff
path: root/audio/rat/files/patch-ah
blob: e4b2b41eb2161fd28c24abdf730f25aa0709f751 (plain) (blame)
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
Index: auddev_newpcm.c
===================================================================
RCS file: /cs/research/mice/starship/src/local/CVS_repository/rat/auddev_newpcm.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- rat/auddev_newpcm.c	2001/02/28 20:15:02	1.17
+++ rat/auddev_newpcm.c	2002/02/24 21:13:47	1.18
@@ -9,7 +9,7 @@
  
 #ifndef HIDE_SOURCE_STRINGS
 static const char cvsid[] = 
-"$Id: auddev_newpcm.c,v 1.17 2001/02/28 20:15:02 ucacoxh Exp $";
+"$Id: auddev_newpcm.c,v 1.18 2002/02/24 21:13:47 ucacoxh Exp $";
 #endif /* HIDE_SOURCE_STRINGS */
 
 #include "config_unix.h"
@@ -26,15 +26,32 @@
 #include <dirent.h>
 #include <errno.h>
 
+/* #define DEBUG_JUST_NEWPCM if not using debug-enable and want err msgs */
+#ifdef DEBUG_JUST_NEWPCM
+#undef debug_msg
+#define debug_msg(x...) fprintf(stderr, x)
+#endif /* DEBUG_JUST_NEWPCM */
+
 static char *port_names[] = SOUND_DEVICE_LABELS;
 static int  iport, oport, loop;
 static snd_chan_param pa;
 static struct snd_size sz;
 static int audio_fd = -1;
+static int mixer_fd = -1;
 
 #define RAT_TO_DEVICE(x) ((x) * 100 / MAX_AMP)
 #define DEVICE_TO_RAT(x) ((x) * MAX_AMP / 100)
 
+#define MIXER_CHECK0(fd)	if ((fd) < 0) {	\
+		debug_msg("Failed mixer checked\n"); \
+		return; \
+	}
+
+#define MIXER_CHECK1(fd, err)	if ((fd) < 0) {	\
+		debug_msg("Failed mixer checked\n"); \
+		return (err); \
+	}
+
 #define NEWPCM_AUDIO_IOCTL(fd, cmd, val) \
 	newpcm_error = 0; \
 	if (ioctl((fd), (cmd), (val)) < 0) { \
@@ -53,6 +70,7 @@
 static audio_format *input_format, *output_format, *tmp_format;
 static snd_capabilities soundcaps[NEWPCM_MAX_AUDIO_DEVICES];
 
+static int  newpcm_mixer_open(const char* audiodev);
 static void newpcm_mixer_save(int fd);
 static void newpcm_mixer_restore(int fd);
 static void newpcm_mixer_init(int fd);
@@ -76,17 +94,15 @@
 		/* Ignore any earlier errors */
 		newpcm_error = 0;
 
-		newpcm_mixer_save(audio_fd);
-
 		NEWPCM_AUDIO_IOCTL(audio_fd, AIOGCAP, &soundcaps[ad]);
-		debug_msg("soundcaps[%d].rate_min = %d\n", ad, soundcaps[ad].rate_min);
-		debug_msg("soundcaps[%d].rate_max = %d\n", ad, soundcaps[ad].rate_max);
+		debug_msg("soundcaps[%d].rate_min = %ld\n", ad, soundcaps[ad].rate_min);
+		debug_msg("soundcaps[%d].rate_max = %ld\n", ad, soundcaps[ad].rate_max);
 		debug_msg("soundcaps[%d].formats  = 0x%08lx\n", ad, soundcaps[ad].formats);
-		debug_msg("soundcaps[%d].bufsize  = %d\n", ad, soundcaps[ad].bufsize);
+		debug_msg("soundcaps[%d].bufsize  = %ld\n", ad, soundcaps[ad].bufsize);
 		debug_msg("soundcaps[%d].mixers   = 0x%08lx\n", ad, soundcaps[ad].mixers);
 		debug_msg("soundcaps[%d].inputs   = 0x%08lx\n", ad, soundcaps[ad].inputs);
-		debug_msg("soundcaps[%d].left     = 0x%04lx\n", ad, soundcaps[ad].left);
-		debug_msg("soundcaps[%d].right    = 0x%04lx\n", ad, soundcaps[ad].right);
+		debug_msg("soundcaps[%d].left     = 0x%04x\n", ad, soundcaps[ad].left);
+		debug_msg("soundcaps[%d].right    = 0x%04x\n", ad, soundcaps[ad].right);
 
 		/* Setup input and output format settings */
 		assert(ofmt->channels == ifmt->channels);
@@ -125,7 +141,7 @@
 		    (IN_RANGE((uint32_t)ifmt->sample_rate, 
 			      soundcaps[ad].rate_min, 
 			      soundcaps[ad].rate_max) == 0)) {
-			debug_msg("(%d or %d) out of range %d -- %d Hz\n",
+			debug_msg("(%d or %d) out of range %ld -- %ld Hz\n",
 				  ofmt->sample_rate,
 				  ifmt->sample_rate,
 				  soundcaps[ad].rate_min,
@@ -190,7 +206,9 @@
 		}
 		output_format = tmp_format;
 
-		newpcm_mixer_init(audio_fd);
+		mixer_fd = newpcm_mixer_open(thedev);
+		newpcm_mixer_save(mixer_fd);
+		newpcm_mixer_init(mixer_fd);
 		/* Turn off loopback from input to output... not fatal so
 		 * after error check.
 		 */
@@ -224,10 +242,14 @@
 	if (output_format != NULL) {
 		audio_format_free(&output_format);
 	}
-	newpcm_mixer_restore(audio_fd);
+
 	newpcm_audio_drain(audio_fd);
 	close(audio_fd);
 	audio_fd = -1;
+
+	newpcm_mixer_restore(mixer_fd);
+	close(mixer_fd);
+	mixer_fd = -1;
 }
 
 /* Flush input buffer */
@@ -380,8 +402,9 @@
 {
 	int volume, lgport, op;
 
-	UNUSED(ad); assert(audio_fd > 0);
-
+	MIXER_CHECK0(mixer_fd);
+		
+	UNUSED(ad);
 	vol = RAT_TO_DEVICE(vol);
 	volume = vol << 8 | vol;
 	lgport = -1;
@@ -391,7 +414,7 @@
 		lgport ++;
 	}
 
-	NEWPCM_AUDIO_IOCTL(audio_fd, MIXER_WRITE(lgport), &volume);
+	NEWPCM_AUDIO_IOCTL(mixer_fd, MIXER_WRITE(lgport), &volume);
 }
 
 int
@@ -399,7 +422,8 @@
 {
 	int volume, lgport, op;
 
-	UNUSED(ad); assert(audio_fd > 0);
+	UNUSED(ad); 
+	MIXER_CHECK1(mixer_fd, 0);
 
 	lgport = -1;
 	op     = oport;
@@ -408,7 +432,7 @@
 		lgport ++;
 	}
 
-	NEWPCM_AUDIO_IOCTL(audio_fd, MIXER_READ(lgport), &volume);
+	NEWPCM_AUDIO_IOCTL(mixer_fd, MIXER_READ(lgport), &volume);
 	volume = DEVICE_TO_RAT(volume & 0xff);
 	if (volume > 100 || volume < 0) {
 		debug_msg("gain out of bounds (%08x %d--%d)" \
@@ -468,17 +492,19 @@
 	int volume = RAT_TO_DEVICE(gain);
 	volume |= (volume << 8);
 
-	UNUSED(ad); assert(audio_fd > 0);
+	UNUSED(ad); 
+	MIXER_CHECK0(mixer_fd);
+
 	newpcm_audio_loopback_config(gain);
 	/* Try AC97 */
-	NEWPCM_AUDIO_IOCTL(audio_fd, SOUND_MIXER_WRITE_RECLEV, &volume);
+	NEWPCM_AUDIO_IOCTL(mixer_fd, SOUND_MIXER_WRITE_RECLEV, &volume);
 
 	if (newpcm_error != 0 && iport != 0) { 
 		/* Fallback to non-ac97 */
 		int idx = 1;
 		while ((1 << idx) != iport)
 			idx++;
-		NEWPCM_AUDIO_IOCTL(audio_fd, MIXER_WRITE(idx), &volume);
+		NEWPCM_AUDIO_IOCTL(mixer_fd, MIXER_WRITE(idx), &volume);
 	}
 }
 
@@ -487,15 +513,17 @@
 {
 	int volume = 0;
 
-	UNUSED(ad); assert(audio_fd > 0);
+	UNUSED(ad);
+	MIXER_CHECK1(mixer_fd, 0);
+
 	/* Try AC97 */
-	NEWPCM_AUDIO_IOCTL(audio_fd, SOUND_MIXER_READ_RECLEV, &volume);
+	NEWPCM_AUDIO_IOCTL(mixer_fd, SOUND_MIXER_READ_RECLEV, &volume);
 	if (newpcm_error != 0 && iport != 0) {
 		/* Fallback to non-ac97 */
 		int idx = 1;
 		while ((1 << idx) != iport)
 			idx++;
-		NEWPCM_AUDIO_IOCTL(audio_fd, MIXER_READ(idx), &volume);
+		NEWPCM_AUDIO_IOCTL(mixer_fd, MIXER_READ(idx), &volume);
 	}
 	volume = DEVICE_TO_RAT(volume & 0xff); 
 	if (volume > 100 || volume < 0) {
@@ -512,6 +540,8 @@
 	/* Check port is in record mask */
 	int gain;
 
+	MIXER_CHECK0(mixer_fd);
+
 	debug_msg("port 0x%08x recmask 0x%08x\n", port, recmask);
 
 	if ((port & recmask) == 0) {
@@ -520,7 +550,7 @@
 		return;
 	}
 
-	if (ioctl(audio_fd, SOUND_MIXER_WRITE_RECSRC, &port) < 0) {
+	if (ioctl(mixer_fd, SOUND_MIXER_WRITE_RECSRC, &port) < 0) {
 		perror("Unable to write record mask\n");
 		return;
 	}
@@ -571,6 +601,8 @@
 {
 	int lgport, vol;
 
+	MIXER_CHECK0(mixer_fd);
+
 	/* Find current input port id */
 	lgport = newpcm_get_nth_port_mask(iport, 0);
 
@@ -580,7 +612,7 @@
 		vol = 0;
 	}
 
-	NEWPCM_AUDIO_IOCTL(audio_fd, MIXER_WRITE(lgport), &vol);
+	NEWPCM_AUDIO_IOCTL(mixer_fd, MIXER_WRITE(lgport), &vol);
 }
 
 void
@@ -706,6 +738,48 @@
 		return dummy;
 	}	
 	return NULL;
+}
+
+/* Mixer open / close related */
+
+static int
+newpcm_mixer_device(const char* audiodev)
+{
+	const char* p = audiodev;
+	int devno = 0;
+
+	/* 
+	 * Audio device looks like "/dev/fooN" or "dev/foo/N.n"
+	 * and we want "N"
+	 */
+	while (p && !isnumber(*p))
+		p++;
+	while (p && isnumber(*p)) {
+		devno = devno * 10 + (*p - '0');
+		p++;
+	}
+	assert(devno < 20);
+	return devno;
+}
+
+static int
+newpcm_mixer_open(const char* audiodev)
+{
+	char mixer_name[32] = "/dev/mixerXXX";
+	int m;
+
+#define END_OF_DEV_MIXER 10
+	sprintf(mixer_name + END_OF_DEV_MIXER, 
+		"%d", newpcm_mixer_device(audiodev));
+
+	m = open(mixer_name, O_RDWR);
+	if (m < 0) {
+		fprintf(stderr, "Could not open %s (%s): "
+			"mixer operations will not work.\n",
+			mixer_name, strerror(errno));
+		return 0;
+	}
+	return m;
 }
 
 /* Functions to save and restore recording source and mixer levels */