view Alc/backends/send.c @ 33:3caceef436ea tip

formatting for web
author Robert McIntyre <rlm@mit.edu>
date Sat, 11 Feb 2012 12:25:55 -0700
parents f4c7260d397a
children
line wrap: on
line source

2 #include "config.h"
3 #include <stdlib.h>
4 #include "alMain.h"
5 #include "AL/al.h"
6 #include "AL/alc.h"
7 #include "alSource.h"
8 #include <jni.h>
10 //////////////////// Summary
12 struct send_data;
13 struct context_data;
15 static void addContext(ALCdevice *, ALCcontext *);
16 static void syncContexts(ALCcontext *master, ALCcontext *slave);
17 static void syncSources(ALsource *master, ALsource *slave,
18 ALCcontext *masterCtx, ALCcontext *slaveCtx);
20 static void syncSourcei(ALuint master, ALuint slave,
21 ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param);
22 static void syncSourcef(ALuint master, ALuint slave,
23 ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param);
24 static void syncSource3f(ALuint master, ALuint slave,
25 ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param);
27 static void swapInContext(ALCdevice *, struct context_data *);
28 static void saveContext(ALCdevice *, struct context_data *);
29 static void limitContext(ALCdevice *, ALCcontext *);
30 static void unLimitContext(ALCdevice *);
32 static void init(ALCdevice *);
33 static void renderData(ALCdevice *, int samples);
35 #define UNUSED(x) (void)(x)
36 //////////////////// State
38 typedef struct context_data {
39 ALfloat ClickRemoval[MAXCHANNELS];
40 ALfloat PendingClicks[MAXCHANNELS];
41 ALvoid *renderBuffer;
42 ALCcontext *ctx;
43 } context_data;
45 typedef struct send_data {
46 ALuint size;
47 context_data **contexts;
48 ALuint numContexts;
49 ALuint maxContexts;
50 } send_data;
51 //////////////////// Context Creation / Synchronization
53 #define _MAKE_SYNC(NAME, INIT_EXPR, GET_EXPR, SET_EXPR) \
54 void NAME (ALuint sourceID1, ALuint sourceID2, \
55 ALCcontext *ctx1, ALCcontext *ctx2, \
56 ALenum param){ \
57 INIT_EXPR; \
58 ALCcontext *current = alcGetCurrentContext(); \
59 alcMakeContextCurrent(ctx1); \
60 GET_EXPR; \
61 alcMakeContextCurrent(ctx2); \
62 SET_EXPR; \
63 alcMakeContextCurrent(current); \
64 }
66 #define MAKE_SYNC(NAME, TYPE, GET, SET) \
67 _MAKE_SYNC(NAME, \
68 TYPE value, \
69 GET(sourceID1, param, &value), \
70 SET(sourceID2, param, value))
72 #define MAKE_SYNC3(NAME, TYPE, GET, SET) \
73 _MAKE_SYNC(NAME, \
74 TYPE value1; TYPE value2; TYPE value3;, \
75 GET(sourceID1, param, &value1, &value2, &value3), \
76 SET(sourceID2, param, value1, value2, value3))
78 MAKE_SYNC( syncSourcei, ALint, alGetSourcei, alSourcei);
79 MAKE_SYNC( syncSourcef, ALfloat, alGetSourcef, alSourcef);
80 MAKE_SYNC3(syncSource3i, ALint, alGetSource3i, alSource3i);
81 MAKE_SYNC3(syncSource3f, ALfloat, alGetSource3f, alSource3f);
83 void syncSources(ALsource *masterSource, ALsource *slaveSource,
84 ALCcontext *masterCtx, ALCcontext *slaveCtx){
85 ALuint master = masterSource->source;
86 ALuint slave = slaveSource->source;
87 ALCcontext *current = alcGetCurrentContext();
89 syncSourcef(master,slave,masterCtx,slaveCtx,AL_PITCH);
90 syncSourcef(master,slave,masterCtx,slaveCtx,AL_GAIN);
91 syncSourcef(master,slave,masterCtx,slaveCtx,AL_MAX_DISTANCE);
92 syncSourcef(master,slave,masterCtx,slaveCtx,AL_ROLLOFF_FACTOR);
93 syncSourcef(master,slave,masterCtx,slaveCtx,AL_REFERENCE_DISTANCE);
94 syncSourcef(master,slave,masterCtx,slaveCtx,AL_MIN_GAIN);
95 syncSourcef(master,slave,masterCtx,slaveCtx,AL_MAX_GAIN);
96 syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_OUTER_GAIN);
97 syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_INNER_ANGLE);
98 syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_OUTER_ANGLE);
99 syncSourcef(master,slave,masterCtx,slaveCtx,AL_SEC_OFFSET);
100 syncSourcef(master,slave,masterCtx,slaveCtx,AL_SAMPLE_OFFSET);
101 syncSourcef(master,slave,masterCtx,slaveCtx,AL_BYTE_OFFSET);
103 syncSource3f(master,slave,masterCtx,slaveCtx,AL_POSITION);
104 syncSource3f(master,slave,masterCtx,slaveCtx,AL_VELOCITY);
105 syncSource3f(master,slave,masterCtx,slaveCtx,AL_DIRECTION);
107 syncSourcei(master,slave,masterCtx,slaveCtx,AL_SOURCE_RELATIVE);
108 syncSourcei(master,slave,masterCtx,slaveCtx,AL_LOOPING);
110 alcMakeContextCurrent(masterCtx);
111 ALint source_type;
112 alGetSourcei(master, AL_SOURCE_TYPE, &source_type);
114 // Only static sources are currently synchronized!
115 if (AL_STATIC == source_type){
116 ALint master_buffer;
117 ALint slave_buffer;
118 alGetSourcei(master, AL_BUFFER, &master_buffer);
119 alcMakeContextCurrent(slaveCtx);
120 alGetSourcei(slave, AL_BUFFER, &slave_buffer);
121 if (master_buffer != slave_buffer){
122 alSourcei(slave, AL_BUFFER, master_buffer);
123 }
124 }
126 // Synchronize the state of the two sources.
127 alcMakeContextCurrent(masterCtx);
128 ALint masterState;
129 ALint slaveState;
131 alGetSourcei(master, AL_SOURCE_STATE, &masterState);
132 alcMakeContextCurrent(slaveCtx);
133 alGetSourcei(slave, AL_SOURCE_STATE, &slaveState);
135 if (masterState != slaveState){
136 switch (masterState){
137 case AL_INITIAL : alSourceRewind(slave); break;
138 case AL_PLAYING : alSourcePlay(slave); break;
139 case AL_PAUSED : alSourcePause(slave); break;
140 case AL_STOPPED : alSourceStop(slave); break;
141 }
142 }
143 // Restore whatever context was previously active.
144 alcMakeContextCurrent(current);
145 }
146 void syncContexts(ALCcontext *master, ALCcontext *slave){
147 /* If there aren't sufficient sources in slave to mirror
148 the sources in master, create them. */
149 ALCcontext *current = alcGetCurrentContext();
151 UIntMap *masterSourceMap = &(master->SourceMap);
152 UIntMap *slaveSourceMap = &(slave->SourceMap);
153 ALuint numMasterSources = masterSourceMap->size;
154 ALuint numSlaveSources = slaveSourceMap->size;
156 alcMakeContextCurrent(slave);
157 if (numSlaveSources < numMasterSources){
158 ALuint numMissingSources = numMasterSources - numSlaveSources;
159 ALuint newSources[numMissingSources];
160 alGenSources(numMissingSources, newSources);
161 }
163 /* Now, slave is guaranteed to have at least as many sources
164 as master. Sync each source from master to the corresponding
165 source in slave. */
166 int i;
167 for(i = 0; i < masterSourceMap->size; i++){
168 syncSources((ALsource*)masterSourceMap->array[i].value,
169 (ALsource*)slaveSourceMap->array[i].value,
170 master, slave);
171 }
172 alcMakeContextCurrent(current);
173 }
174 static void addContext(ALCdevice *Device, ALCcontext *context){
175 send_data *data = (send_data*)Device->ExtraData;
176 // expand array if necessary
177 if (data->numContexts >= data->maxContexts){
178 ALuint newMaxContexts = data->maxContexts*2 + 1;
179 data->contexts = realloc(data->contexts, newMaxContexts*sizeof(context_data));
180 data->maxContexts = newMaxContexts;
181 }
182 // create context_data and add it to the main array
183 context_data *ctxData;
184 ctxData = (context_data*)calloc(1, sizeof(*ctxData));
185 ctxData->renderBuffer =
186 malloc(BytesFromDevFmt(Device->FmtType) *
187 Device->NumChan * Device->UpdateSize);
188 ctxData->ctx = context;
190 data->contexts[data->numContexts] = ctxData;
191 data->numContexts++;
192 }
193 //////////////////// Context Switching
195 /* A device brings along with it two pieces of state
196 * which have to be swapped in and out with each context.
197 */
198 static void swapInContext(ALCdevice *Device, context_data *ctxData){
199 memcpy(Device->ClickRemoval, ctxData->ClickRemoval, sizeof(ALfloat)*MAXCHANNELS);
200 memcpy(Device->PendingClicks, ctxData->PendingClicks, sizeof(ALfloat)*MAXCHANNELS);
201 }
203 static void saveContext(ALCdevice *Device, context_data *ctxData){
204 memcpy(ctxData->ClickRemoval, Device->ClickRemoval, sizeof(ALfloat)*MAXCHANNELS);
205 memcpy(ctxData->PendingClicks, Device->PendingClicks, sizeof(ALfloat)*MAXCHANNELS);
206 }
208 static ALCcontext **currentContext;
209 static ALuint currentNumContext;
211 /* By default, all contexts are rendered at once for each call to aluMixData.
212 * This function uses the internals of the ALCdevice struct to temporally
213 * cause aluMixData to only render the chosen context.
214 */
215 static void limitContext(ALCdevice *Device, ALCcontext *ctx){
216 currentContext = Device->Contexts;
217 currentNumContext = Device->NumContexts;
218 Device->Contexts = &ctx;
219 Device->NumContexts = 1;
220 }
222 static void unLimitContext(ALCdevice *Device){
223 Device->Contexts = currentContext;
224 Device->NumContexts = currentNumContext;
225 }
226 //////////////////// Main Device Loop
228 /* Establish the LWJGL context as the master context, which will
229 * be synchronized to all the slave contexts
230 */
231 static void init(ALCdevice *Device){
232 ALCcontext *masterContext = alcGetCurrentContext();
233 addContext(Device, masterContext);
234 }
236 static void renderData(ALCdevice *Device, int samples){
237 if(!Device->Connected){return;}
238 send_data *data = (send_data*)Device->ExtraData;
239 ALCcontext *current = alcGetCurrentContext();
241 ALuint i;
242 for (i = 1; i < data->numContexts; i++){
243 syncContexts(data->contexts[0]->ctx , data->contexts[i]->ctx);
244 }
246 if ((ALuint) samples > Device->UpdateSize){
247 printf("exceeding internal buffer size; dropping samples\n");
248 printf("requested %d; available %d\n", samples, Device->UpdateSize);
249 samples = (int) Device->UpdateSize;
250 }
252 for (i = 0; i < data->numContexts; i++){
253 context_data *ctxData = data->contexts[i];
254 ALCcontext *ctx = ctxData->ctx;
255 alcMakeContextCurrent(ctx);
256 limitContext(Device, ctx);
257 swapInContext(Device, ctxData);
258 aluMixData(Device, ctxData->renderBuffer, samples);
259 saveContext(Device, ctxData);
260 unLimitContext(Device);
261 }
262 alcMakeContextCurrent(current);
263 }
264 //////////////////// JNI Methods
266 #include "com_aurellem_send_AudioSend.h"
268 /*
269 * Class: com_aurellem_send_AudioSend
270 * Method: nstep
271 * Signature: (JI)V
272 */
273 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nstep
274 (JNIEnv *env, jclass clazz, jlong device, jint samples){
275 UNUSED(env);UNUSED(clazz);UNUSED(device);
276 renderData((ALCdevice*)((intptr_t)device), samples);
277 }
278 /*
279 * Class: com_aurellem_send_AudioSend
280 * Method: ngetSamples
281 * Signature: (JLjava/nio/ByteBuffer;III)V
282 */
283 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_ngetSamples
284 (JNIEnv *env, jclass clazz, jlong device, jobject buffer, jint position,
285 jint samples, jint n){
286 UNUSED(clazz);
288 ALvoid *buffer_address =
289 ((ALbyte *)(((char*)(*env)->GetDirectBufferAddress(env, buffer)) + position));
290 ALCdevice *recorder = (ALCdevice*) ((intptr_t)device);
291 send_data *data = (send_data*)recorder->ExtraData;
292 if ((ALuint)n > data->numContexts){return;}
293 memcpy(buffer_address, data->contexts[n]->renderBuffer,
294 BytesFromDevFmt(recorder->FmtType) * recorder->NumChan * samples);
295 }
296 /*
297 * Class: com_aurellem_send_AudioSend
298 * Method: naddListener
299 * Signature: (J)V
300 */
301 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_naddListener
302 (JNIEnv *env, jclass clazz, jlong device){
303 UNUSED(env); UNUSED(clazz);
304 //printf("creating new context via naddListener\n");
305 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
306 ALCcontext *new = alcCreateContext(Device, NULL);
307 addContext(Device, new);
308 }
310 /*
311 * Class: com_aurellem_send_AudioSend
312 * Method: nsetNthListener3f
313 * Signature: (IFFFJI)V
314 */
315 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nsetNthListener3f
316 (JNIEnv *env, jclass clazz, jint param,
317 jfloat v1, jfloat v2, jfloat v3, jlong device, jint contextNum){
318 UNUSED(env);UNUSED(clazz);
320 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
321 send_data *data = (send_data*)Device->ExtraData;
323 ALCcontext *current = alcGetCurrentContext();
324 if ((ALuint)contextNum > data->numContexts){return;}
325 alcMakeContextCurrent(data->contexts[contextNum]->ctx);
326 alListener3f(param, v1, v2, v3);
327 alcMakeContextCurrent(current);
328 }
330 /*
331 * Class: com_aurellem_send_AudioSend
332 * Method: nsetNthListenerf
333 * Signature: (IFJI)V
334 */
335 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nsetNthListenerf
336 (JNIEnv *env, jclass clazz, jint param, jfloat v1, jlong device,
337 jint contextNum){
339 UNUSED(env);UNUSED(clazz);
341 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
342 send_data *data = (send_data*)Device->ExtraData;
344 ALCcontext *current = alcGetCurrentContext();
345 if ((ALuint)contextNum > data->numContexts){return;}
346 alcMakeContextCurrent(data->contexts[contextNum]->ctx);
347 alListenerf(param, v1);
348 alcMakeContextCurrent(current);
349 }
350 /*
351 * Class: com_aurellem_send_AudioSend
352 * Method: ninitDevice
353 * Signature: (J)V
354 */
355 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_ninitDevice
356 (JNIEnv *env, jclass clazz, jlong device){
357 UNUSED(env);UNUSED(clazz);
358 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
359 init(Device);
360 }
362 /*
363 * Class: com_aurellem_send_AudioSend
364 * Method: ngetAudioFormat
365 * Signature: (J)Ljavax/sound/sampled/AudioFormat;
366 */
367 JNIEXPORT jobject JNICALL Java_com_aurellem_send_AudioSend_ngetAudioFormat
368 (JNIEnv *env, jclass clazz, jlong device){
369 UNUSED(clazz);
370 jclass AudioFormatClass =
371 (*env)->FindClass(env, "javax/sound/sampled/AudioFormat");
372 jmethodID AudioFormatConstructor =
373 (*env)->GetMethodID(env, AudioFormatClass, "<init>", "(FIIZZ)V");
375 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
376 int isSigned;
377 switch (Device->FmtType)
378 {
379 case DevFmtUByte:
380 case DevFmtUShort: isSigned = 0; break;
381 default : isSigned = 1;
382 }
383 float frequency = Device->Frequency;
384 int bitsPerFrame = (8 * BytesFromDevFmt(Device->FmtType));
385 int channels = Device->NumChan;
386 jobject format = (*env)->
387 NewObject(
388 env,AudioFormatClass,AudioFormatConstructor,
389 frequency,
390 bitsPerFrame,
391 channels,
392 isSigned,
393 0);
394 return format;
395 }
396 //////////////////// Device Initialization / Management
398 static const ALCchar sendDevice[] = "Multiple Audio Send";
400 static ALCboolean send_open_playback(ALCdevice *device,
401 const ALCchar *deviceName)
402 {
403 send_data *data;
404 // stop any buffering for stdout, so that I can
405 // see the printf statements in my terminal immediately
406 setbuf(stdout, NULL);
408 if(!deviceName)
409 deviceName = sendDevice;
410 else if(strcmp(deviceName, sendDevice) != 0)
411 return ALC_FALSE;
412 data = (send_data*)calloc(1, sizeof(*data));
413 device->szDeviceName = strdup(deviceName);
414 device->ExtraData = data;
415 return ALC_TRUE;
416 }
418 static void send_close_playback(ALCdevice *device)
419 {
420 send_data *data = (send_data*)device->ExtraData;
421 alcMakeContextCurrent(NULL);
422 ALuint i;
423 // Destroy all slave contexts. LWJGL will take care of
424 // its own context.
425 for (i = 1; i < data->numContexts; i++){
426 context_data *ctxData = data->contexts[i];
427 alcDestroyContext(ctxData->ctx);
428 free(ctxData->renderBuffer);
429 free(ctxData);
430 }
431 free(data);
432 device->ExtraData = NULL;
433 }
435 static ALCboolean send_reset_playback(ALCdevice *device)
436 {
437 SetDefaultWFXChannelOrder(device);
438 return ALC_TRUE;
439 }
441 static void send_stop_playback(ALCdevice *Device){
442 UNUSED(Device);
443 }
445 static const BackendFuncs send_funcs = {
446 send_open_playback,
447 send_close_playback,
448 send_reset_playback,
449 send_stop_playback,
450 NULL,
451 NULL, /* These would be filled with functions to */
452 NULL, /* handle capturing audio if we we into that */
453 NULL, /* sort of thing... */
454 NULL,
455 NULL
456 };
458 ALCboolean alc_send_init(BackendFuncs *func_list){
459 *func_list = send_funcs;
460 return ALC_TRUE;
461 }
463 void alc_send_deinit(void){}
465 void alc_send_probe(enum DevProbe type)
466 {
467 switch(type)
468 {
469 case DEVICE_PROBE:
470 AppendDeviceList(sendDevice);
471 break;
472 case ALL_DEVICE_PROBE:
473 AppendAllDeviceList(sendDevice);
474 break;
475 case CAPTURE_DEVICE_PROBE:
476 break;
477 }
478 }