view Alc/backends/send.c @ 23:45dc4f76e548

adding send.c for cross compiling purposes
author Robert McIntyre <rlm@mit.edu>
date Fri, 18 Nov 2011 16:02:44 -0700
parents
children f4c7260d397a
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 }
237 static void renderData(ALCdevice *Device, int samples){
238 if(!Device->Connected){return;}
239 send_data *data = (send_data*)Device->ExtraData;
240 ALCcontext *current = alcGetCurrentContext();
242 ALuint i;
243 for (i = 1; i < data->numContexts; i++){
244 syncContexts(data->contexts[0]->ctx , data->contexts[i]->ctx);
245 }
247 if ((uint) samples > Device->UpdateSize){
248 printf("exceeding internal buffer size; dropping samples\n");
249 printf("requested %d; available %d\n", samples, Device->UpdateSize);
250 samples = (int) Device->UpdateSize;
251 }
253 for (i = 0; i < data->numContexts; i++){
254 context_data *ctxData = data->contexts[i];
255 ALCcontext *ctx = ctxData->ctx;
256 alcMakeContextCurrent(ctx);
257 limitContext(Device, ctx);
258 swapInContext(Device, ctxData);
259 aluMixData(Device, ctxData->renderBuffer, samples);
260 saveContext(Device, ctxData);
261 unLimitContext(Device);
262 }
263 alcMakeContextCurrent(current);
264 }
265 //////////////////// JNI Methods
267 #include "com_aurellem_send_AudioSend.h"
269 /*
270 * Class: com_aurellem_send_AudioSend
271 * Method: nstep
272 * Signature: (JI)V
273 */
274 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nstep
275 (JNIEnv *env, jclass clazz, jlong device, jint samples){
276 UNUSED(env);UNUSED(clazz);UNUSED(device);
277 renderData((ALCdevice*)((intptr_t)device), samples);
278 }
279 /*
280 * Class: com_aurellem_send_AudioSend
281 * Method: ngetSamples
282 * Signature: (JLjava/nio/ByteBuffer;III)V
283 */
284 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_ngetSamples
285 (JNIEnv *env, jclass clazz, jlong device, jobject buffer, jint position,
286 jint samples, jint n){
287 UNUSED(clazz);
289 ALvoid *buffer_address =
290 ((ALbyte *)(((char*)(*env)->GetDirectBufferAddress(env, buffer)) + position));
291 ALCdevice *recorder = (ALCdevice*) ((intptr_t)device);
292 send_data *data = (send_data*)recorder->ExtraData;
293 if ((ALuint)n > data->numContexts){return;}
294 memcpy(buffer_address, data->contexts[n]->renderBuffer,
295 BytesFromDevFmt(recorder->FmtType) * recorder->NumChan * samples);
296 }
297 /*
298 * Class: com_aurellem_send_AudioSend
299 * Method: naddListener
300 * Signature: (J)V
301 */
302 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_naddListener
303 (JNIEnv *env, jclass clazz, jlong device){
304 UNUSED(env); UNUSED(clazz);
305 //printf("creating new context via naddListener\n");
306 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
307 ALCcontext *new = alcCreateContext(Device, NULL);
308 addContext(Device, new);
309 }
311 /*
312 * Class: com_aurellem_send_AudioSend
313 * Method: nsetNthListener3f
314 * Signature: (IFFFJI)V
315 */
316 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nsetNthListener3f
317 (JNIEnv *env, jclass clazz, jint param,
318 jfloat v1, jfloat v2, jfloat v3, jlong device, jint contextNum){
319 UNUSED(env);UNUSED(clazz);
321 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
322 send_data *data = (send_data*)Device->ExtraData;
324 ALCcontext *current = alcGetCurrentContext();
325 if ((ALuint)contextNum > data->numContexts){return;}
326 alcMakeContextCurrent(data->contexts[contextNum]->ctx);
327 alListener3f(param, v1, v2, v3);
328 alcMakeContextCurrent(current);
329 }
331 /*
332 * Class: com_aurellem_send_AudioSend
333 * Method: nsetNthListenerf
334 * Signature: (IFJI)V
335 */
336 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nsetNthListenerf
337 (JNIEnv *env, jclass clazz, jint param, jfloat v1, jlong device,
338 jint contextNum){
340 UNUSED(env);UNUSED(clazz);
342 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
343 send_data *data = (send_data*)Device->ExtraData;
345 ALCcontext *current = alcGetCurrentContext();
346 if ((ALuint)contextNum > data->numContexts){return;}
347 alcMakeContextCurrent(data->contexts[contextNum]->ctx);
348 alListenerf(param, v1);
349 alcMakeContextCurrent(current);
350 }
351 /*
352 * Class: com_aurellem_send_AudioSend
353 * Method: ninitDevice
354 * Signature: (J)V
355 */
356 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_ninitDevice
357 (JNIEnv *env, jclass clazz, jlong device){
358 UNUSED(env);UNUSED(clazz);
359 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
360 init(Device);
361 }
363 /*
364 * Class: com_aurellem_send_AudioSend
365 * Method: ngetAudioFormat
366 * Signature: (J)Ljavax/sound/sampled/AudioFormat;
367 */
368 JNIEXPORT jobject JNICALL Java_com_aurellem_send_AudioSend_ngetAudioFormat
369 (JNIEnv *env, jclass clazz, jlong device){
370 UNUSED(clazz);
371 jclass AudioFormatClass =
372 (*env)->FindClass(env, "javax/sound/sampled/AudioFormat");
373 jmethodID AudioFormatConstructor =
374 (*env)->GetMethodID(env, AudioFormatClass, "<init>", "(FIIZZ)V");
376 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);
377 int isSigned;
378 switch (Device->FmtType)
379 {
380 case DevFmtUByte:
381 case DevFmtUShort: isSigned = 0; break;
382 default : isSigned = 1;
383 }
384 float frequency = Device->Frequency;
385 int bitsPerFrame = (8 * BytesFromDevFmt(Device->FmtType));
386 int channels = Device->NumChan;
387 jobject format = (*env)->
388 NewObject(
389 env,AudioFormatClass,AudioFormatConstructor,
390 frequency,
391 bitsPerFrame,
392 channels,
393 isSigned,
394 0);
395 return format;
396 }
397 //////////////////// Device Initialization / Management
399 static const ALCchar sendDevice[] = "Multiple Audio Send";
401 static ALCboolean send_open_playback(ALCdevice *device,
402 const ALCchar *deviceName)
403 {
404 send_data *data;
405 // stop any buffering for stdout, so that I can
406 // see the printf statements in my terminal immediately
407 setbuf(stdout, NULL);
409 if(!deviceName)
410 deviceName = sendDevice;
411 else if(strcmp(deviceName, sendDevice) != 0)
412 return ALC_FALSE;
413 data = (send_data*)calloc(1, sizeof(*data));
414 device->szDeviceName = strdup(deviceName);
415 device->ExtraData = data;
416 return ALC_TRUE;
417 }
419 static void send_close_playback(ALCdevice *device)
420 {
421 send_data *data = (send_data*)device->ExtraData;
422 alcMakeContextCurrent(NULL);
423 ALuint i;
424 // Destroy all slave contexts. LWJGL will take care of
425 // its own context.
426 for (i = 1; i < data->numContexts; i++){
427 context_data *ctxData = data->contexts[i];
428 alcDestroyContext(ctxData->ctx);
429 free(ctxData->renderBuffer);
430 free(ctxData);
431 }
432 free(data);
433 device->ExtraData = NULL;
434 }
436 static ALCboolean send_reset_playback(ALCdevice *device)
437 {
438 SetDefaultWFXChannelOrder(device);
439 return ALC_TRUE;
440 }
442 static void send_stop_playback(ALCdevice *Device){
443 UNUSED(Device);
444 }
446 static const BackendFuncs send_funcs = {
447 send_open_playback,
448 send_close_playback,
449 send_reset_playback,
450 send_stop_playback,
451 NULL,
452 NULL, /* These would be filled with functions to */
453 NULL, /* handle capturing audio if we we into that */
454 NULL, /* sort of thing... */
455 NULL,
456 NULL
457 };
459 ALCboolean alc_send_init(BackendFuncs *func_list){
460 *func_list = send_funcs;
461 return ALC_TRUE;
462 }
464 void alc_send_deinit(void){}
466 void alc_send_probe(enum DevProbe type)
467 {
468 switch(type)
469 {
470 case DEVICE_PROBE:
471 AppendDeviceList(sendDevice);
472 break;
473 case ALL_DEVICE_PROBE:
474 AppendAllDeviceList(sendDevice);
475 break;
476 case CAPTURE_DEVICE_PROBE:
477 break;
478 }
479 }