summaryrefslogtreecommitdiff
blob: e9d98046a7fa7561b3fb078e7f5bdf5426c0aabe (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
Index: linux/src/al_ext.c
===================================================================
RCS file: /usr/local/cvs-repository/openal/linux/src/al_ext.c,v
retrieving revision 1.6
diff -u -r1.6 al_ext.c
--- linux/src/al_ext.c	20 Jun 2003 19:23:05 -0000	1.6
+++ linux/src/al_ext.c	2 Mar 2004 10:30:49 -0000
@@ -993,5 +993,9 @@
 		return ALC_TRUE;
 	}
 
+	if(ustrcmp("ALC_CAPTURE_SAMPLES", ename) == 0) {
+		return 0x312;
+	}
+
 	return 0;
 }
Index: linux/src/al_main.c
===================================================================
RCS file: /usr/local/cvs-repository/openal/linux/src/al_main.c,v
retrieving revision 1.9
diff -u -r1.9 al_main.c
--- linux/src/al_main.c	10 Aug 2003 07:58:31 -0000	1.9
+++ linux/src/al_main.c	2 Mar 2004 10:30:49 -0000
@@ -133,6 +133,7 @@
 
 #ifdef CAPTURE_SUPPORT
 	_alRegisterExtensionGroup( (const ALubyte*) "AL_EXT_capture" );
+	_alRegisterExtensionGroup( (const ALubyte*) "ALC_EXT_capture" );
 #endif /* CAPTURE_SUPPORT */
 
 #endif /* BUILTIN_EXT_LOKI */
Index: linux/src/alc/alc_context.c
===================================================================
RCS file: /usr/local/cvs-repository/openal/linux/src/alc/alc_context.c,v
retrieving revision 1.10
diff -u -r1.10 alc_context.c
--- linux/src/alc/alc_context.c	20 Jun 2003 19:23:06 -0000	1.10
+++ linux/src/alc/alc_context.c	2 Mar 2004 10:30:49 -0000
@@ -1525,6 +1525,9 @@
 	return deviceHandle->cc;
 }
 
+
+ALint __alcGetAvailableSamples(ALvoid);
+
 void alcGetIntegerv( ALCdevice *deviceHandle, ALenum  token,
 		     ALsizei  size , ALint *dest )
 {
@@ -1546,6 +1549,9 @@
 		     time.
 		     Check size
 		   */
+		case 0x312:  /*ALC_CAPTURE_SAMPLES:*/
+          *dest = __alcGetAvailableSamples();
+          break;
 		case ALC_MAJOR_VERSION:
 		  *dest = 1;
 		  break;
Index: linux/src/extensions/al_ext_capture.c
===================================================================
RCS file: /usr/local/cvs-repository/openal/linux/src/extensions/al_ext_capture.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 al_ext_capture.c
--- linux/src/extensions/al_ext_capture.c	18 Mar 2002 14:41:44 -0000	1.1.1.1
+++ linux/src/extensions/al_ext_capture.c	2 Mar 2004 10:30:50 -0000
@@ -45,6 +45,11 @@
 	AL_EXT_PAIR(alCaptureStop_EXT),
 	AL_EXT_PAIR(alCaptureGetData_EXT),
 	AL_EXT_PAIR(alCaptureDestroy_EXT),
+	AL_EXT_PAIR(alcCaptureOpenDevice),
+	AL_EXT_PAIR(alcCaptureCloseDevice),
+	AL_EXT_PAIR(alcCaptureStart),
+	AL_EXT_PAIR(alcCaptureStop),
+	AL_EXT_PAIR(alcCaptureSamples),
 	{ NULL, NULL }
 };
 
@@ -72,8 +77,8 @@
 	return;
 }
 
-ALboolean alCaptureInit_EXT( UNUSED(ALenum format),
-                             UNUSED(ALuint rate),
+ALboolean alCaptureInit_EXT( ALenum format,
+                             ALuint rate,
                              UNUSED(ALsizei bufferSize) )
 {
 	ALuint cid;
@@ -89,9 +94,26 @@
 		capture_device = cc->read_device;
 		if ( capture_device == NULL ) {
 			char spec[1024];
-			char *fmt="'( (direction \"read\") (sampling-rate %d))";
+			int speakers;
+			char *fmt="'( (direction \"read\") (sampling-rate %d) (speaker-num %d) )";
 
-			sprintf(spec, fmt, rate);
+			switch (format) {
+				case AL_FORMAT_MONO8:
+				case AL_FORMAT_MONO16:
+				default:
+					speakers = 1;
+					break;
+				case AL_FORMAT_STEREO8:
+				case AL_FORMAT_STEREO16:
+					speakers = 2;
+					break;
+				case AL_FORMAT_QUAD8_LOKI:
+				case AL_FORMAT_QUAD16_LOKI:
+					speakers = 4;
+					break;
+			}
+
+			sprintf(spec, fmt, rate, speakers);
 			capture_device = alcOpenDevice((ALubyte *)spec);
 			if ( capture_device ) {
 				_alcSetContext(NULL, cid, capture_device);
@@ -171,14 +193,16 @@
 		                       format, dev->format, samples);
 		size *= (_al_formatbits(dev->format) / 8);
 
-        	if ( n > (ALsizei)size )
+       	if ( n > (ALsizei)size )
 			temp = malloc( n );
 		else
 			temp = malloc( size );
 
 		if ( size > 0 ) {
 			size = _alcDeviceRead(cid, temp, size);
+		}
 
+		if ( size > 0 ) {
 			temp = _alBufferCanonizeData(dev->format,
 						     temp,
 						     size,
@@ -189,8 +213,8 @@
 						     AL_TRUE);
 		} else {
 			/* Hmm, zero size in record.. */
-			memset(temp, 0, n);
-			size = n;
+			/*memset(temp, 0, n);*/
+			size = 0;
 		}
 		if(temp == NULL) {
 			fprintf(stderr, "could not canonize data\n");
@@ -203,3 +227,228 @@
 	}
 	return size;
 }
+
+
+/* Hacked in ALC_EXT_capture support.  --ryan. */
+/* This doesn't support multiple devices, device enumeration, or capture */
+/*  devices seperate from an existing context. How painful. */
+
+/* ring buffer functionality... */
+
+typedef struct
+{
+    ALubyte *buffer;
+    ALsizei size;
+    ALsizei write;
+    ALsizei read;
+    ALsizei used;
+} __ALRingBuffer;
+
+static ALboolean __alRingBufferInit(__ALRingBuffer *ring, ALsizei size);
+static ALvoid __alRingBufferShutdown(__ALRingBuffer *ring);
+static ALsizei __alRingBufferSize(__ALRingBuffer *ring);
+static ALvoid __alRingBufferPut(__ALRingBuffer *ring, ALubyte *data, ALsizei size);
+static ALsizei __alRingBufferGet(__ALRingBuffer *ring, ALubyte *data, ALsizei size);
+
+static __ALRingBuffer captureRing;
+
+static ALboolean __alRingBufferInit(__ALRingBuffer *ring, ALsizei size)
+{
+    ALubyte *ptr = (ALubyte *) realloc(ring->buffer, size);
+    if (ptr == NULL)
+        return(AL_FALSE);
+
+    ring->buffer = ptr;
+    ring->size = size;
+    ring->write = 0;
+    ring->read = 0;
+    ring->used = 0;
+    return(AL_TRUE);
+} /* __alRingBufferInit */
+
+static ALvoid __alRingBufferShutdown(__ALRingBuffer *ring)
+{
+    free(ring->buffer);
+    ring->buffer = NULL;
+} /* __alRingBufferShutdown */
+
+static ALsizei __alRingBufferSize(__ALRingBuffer *ring)
+{
+    return(ring->used);
+} /* __alRingBufferSize */
+
+static ALvoid __alRingBufferPut(__ALRingBuffer *ring, ALubyte *data, ALsizei _size)
+{
+    register ALsizei size = _size;
+    register ALsizei cpy;
+    register ALsizei avail;
+
+    if (!size)   /* just in case... */
+        return;
+
+    /* Putting more data than ring buffer holds in total? Replace it all. */
+    if (size > ring->size)
+    {
+        ring->write = 0;
+        ring->read = 0;
+        ring->used = ring->size;
+        memcpy(ring->buffer, data + (size - ring->size), ring->size);
+        return;
+    } /* if */
+
+    /* Buffer overflow? Push read pointer to oldest sample not overwritten. */
+    avail = ring->size - ring->used;
+    if (size > avail)
+    {
+        ring->read += size - avail;
+        if (ring->read > ring->size)
+            ring->read -= ring->size;
+    } /* if */
+
+    /* Clip to end of buffer and copy first block... */
+    cpy = ring->size - ring->write;
+    if (size < cpy)
+        cpy = size;
+    if (cpy) memcpy(ring->buffer + ring->write, data, cpy);
+
+    /* Wrap around to front of ring buffer and copy remaining data... */
+    avail = size - cpy;
+    if (avail) memcpy(ring->buffer, data + cpy, avail);
+
+    /* Update write pointer... */
+    ring->write += size;
+    if (ring->write > ring->size)
+        ring->write -= ring->size;
+
+    ring->used += size;
+    if (ring->used > ring->size)
+        ring->used = ring->size;
+} /* __alRingBufferPut */
+
+static ALsizei __alRingBufferGet(__ALRingBuffer *ring, ALubyte *data, ALsizei _size)
+{
+    register ALsizei cpy;
+    register ALsizei size = _size;
+    register ALsizei avail = ring->used;
+
+    /* Clamp amount to read to available data... */
+    if (size > avail)
+        size = avail;
+    
+    /* Clip to end of buffer and copy first block... */
+    cpy = ring->size - ring->read;
+    if (cpy > size) cpy = size;
+    if (cpy) memcpy(data, ring->buffer + ring->read, cpy);
+    
+    /* Wrap around to front of ring buffer and copy remaining data... */
+    avail = size - cpy;
+    if (avail) memcpy(data + cpy, ring->buffer, avail);
+
+    /* Update read pointer... */
+    ring->read += size;
+    if (ring->read > ring->size)
+        ring->read -= ring->size;
+
+    ring->used -= size;
+
+    return(size);  /* may have been clamped if there wasn't enough data... */
+} /* __alRingBufferGet */
+
+
+static ALenum captureFmt = AL_NONE;
+static ALuint captureFreq = 0;
+static ALint captureFmtSize = 0;
+
+ALCdevice *alcCaptureOpenDevice(const ALubyte *deviceName,
+                                ALuint freq, ALenum fmt,
+                                ALsizei bufsize)
+{
+    ALCdevice *retval;
+	AL_context *cc;
+	ALuint cid;
+
+    if (deviceName != NULL)  /* !!! FIXME */
+        return NULL;
+
+    switch (fmt)  /* try to keep this sane for now... */
+    {
+        case AL_FORMAT_MONO8:
+        case AL_FORMAT_MONO16:
+        case AL_FORMAT_STEREO8:
+        case AL_FORMAT_STEREO16:
+            break;  /* okay format. */
+
+        default:
+            return(NULL);
+    }
+
+    captureFmt = fmt;
+    captureFreq = freq;
+    captureFmtSize = (_al_formatbits(fmt) / 8);
+    if ((fmt == AL_FORMAT_STEREO8) || (fmt == AL_FORMAT_STEREO16))
+        captureFmtSize *= 2;
+
+    bufsize *= captureFmtSize;
+
+    if (!__alRingBufferInit(&captureRing, bufsize))
+        return NULL;
+
+    if (!alCaptureInit_EXT(fmt, freq, bufsize))
+        return NULL;
+
+	cid = _alcCCId;
+	_alcLockContext( cid );
+	cc = _alcGetContext(cid);
+    retval = cc->read_device;
+	retval->cc = cc;
+    _alcUnlockContext( cid );
+
+    fprintf(stderr, "WARNING: ALC_EXT_capture is subject to change!\n");
+
+    return(retval);
+}
+
+ALvoid alcCaptureCloseDevice(ALCdevice *dev)
+{
+    if (dev == NULL)
+        return;
+
+    alCaptureDestroy_EXT();
+    __alRingBufferShutdown(&captureRing);
+}
+
+ALvoid alcCaptureStart(void)
+{
+    alCaptureStart_EXT();
+}
+
+ALvoid alcCaptureStop(void)
+{
+    alCaptureStop_EXT();
+}
+
+/* !!! FIXME: Not ideal; reads samples in ALC_CAPTURE_SAMPLES query */
+/* !!! FIXME: should query hardware here and do read in alcCaptureSamples() */
+ALint __alcGetAvailableSamples(ALvoid)
+{
+    static ALubyte buf[2048];
+    ALsizei got;
+    
+    while ((got = alCaptureGetData_EXT(buf, sizeof (buf),
+                                       captureFmt, captureFreq)) > 0)
+        __alRingBufferPut(&captureRing, buf, got);
+
+    /*printf("got %d have %d\n", (int) got, (int) (__alRingBufferSize(&captureRing) / captureFmtSize));*/
+
+    return(__alRingBufferSize(&captureRing) / captureFmtSize);
+}
+
+
+ALvoid alcCaptureSamples(UNUSED(ALCdevice *device), ALvoid *buf, ALsizei samps)
+{
+    if ((__alRingBufferSize(&captureRing) / captureFmtSize) < samps)
+        return;  /* !!! FIXME: This is an error condition! */
+
+    __alRingBufferGet(&captureRing, buf, samps * captureFmtSize);
+}
+
Index: linux/src/extensions/al_ext_capture.h
===================================================================
RCS file: /usr/local/cvs-repository/openal/linux/src/extensions/al_ext_capture.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 al_ext_capture.h
--- linux/src/extensions/al_ext_capture.h	18 Mar 2002 14:41:44 -0000	1.1.1.1
+++ linux/src/extensions/al_ext_capture.h	2 Mar 2004 10:30:50 -0000
@@ -13,6 +13,16 @@
 
 #include <AL/alext.h>
 
+ALCdevice *alcCaptureOpenDevice(const ALubyte *deviceName,
+                                ALuint freq, ALenum fmt,
+                                ALsizei bufsize);
+ALvoid alcCaptureCloseDevice(ALCdevice *dev);
+ALvoid alcCaptureStart(void);
+ALvoid alcCaptureStop(void);
+ALint __alcGetAvailableSamples(ALvoid);
+ALvoid alcCaptureSamples(ALCdevice *device, ALvoid *buf, ALsizei samps);
+
+
 /*
  * alInitCapture( void )
  *
@@ -38,7 +48,12 @@
 	AL_EXT_PAIR(alCaptureStart_EXT),                           \
 	AL_EXT_PAIR(alCaptureStop_EXT),                            \
 	AL_EXT_PAIR(alCaptureGetData_EXT),                         \
-	AL_EXT_PAIR(alCaptureDestroy_EXT)                         \
+	AL_EXT_PAIR(alCaptureDestroy_EXT),                         \
+	AL_EXT_PAIR(alcCaptureOpenDevice),                         \
+	AL_EXT_PAIR(alcCaptureCloseDevice),                         \
+	AL_EXT_PAIR(alcCaptureStart),                         \
+	AL_EXT_PAIR(alcCaptureStop),                         \
+	AL_EXT_PAIR(alcCaptureSamples)                         \
 
 /* initialization and destruction functions */