Mercurial > audio-send
view Alc/backends/send.c @ 9:a988ea53d982
added description of JNI stuff to README
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 27 Oct 2011 02:47:28 -0700 |
parents | 37f25cb34196 |
children | 92b416b4e027 |
line wrap: on
line source
1 #include "config.h"2 #include <stdlib.h>3 #include "alMain.h"4 #include "AL/al.h"5 #include "AL/alc.h"6 #include "alSource.h"7 #include <jni.h>9 //////////////////// Summary11 struct send_data;12 struct context_data;14 static void addContext(ALCdevice *, ALCcontext *);15 static void syncContexts(ALCcontext *master, ALCcontext *slave);16 static void syncSources(ALsource *master, ALsource *slave,17 ALCcontext *masterCtx, ALCcontext *slaveCtx);19 static void syncSourcei(ALuint master, ALuint slave,20 ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param);21 static void syncSourcef(ALuint master, ALuint slave,22 ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param);23 static void syncSource3f(ALuint master, ALuint slave,24 ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param);26 static void swapInContext(ALCdevice *, struct context_data *);27 static void saveContext(ALCdevice *, struct context_data *);28 static void limitContext(ALCdevice *, ALCcontext *);29 static void unLimitContext(ALCdevice *);31 static void init(ALCdevice *);32 static void renderData(ALCdevice *, int samples);34 #define UNUSED(x) (void)(x)36 //////////////////// State38 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;54 //////////////////// Context Creation / Synchronization56 #define _MAKE_SYNC(NAME, INIT_EXPR, GET_EXPR, SET_EXPR) \57 void NAME (ALuint sourceID1, ALuint sourceID2, \58 ALCcontext *ctx1, ALCcontext *ctx2, \59 ALenum param){ \60 INIT_EXPR; \61 ALCcontext *current = alcGetCurrentContext(); \62 alcMakeContextCurrent(ctx1); \63 GET_EXPR; \64 alcMakeContextCurrent(ctx2); \65 SET_EXPR; \66 alcMakeContextCurrent(current); \67 }69 #define MAKE_SYNC(NAME, TYPE, GET, SET) \70 _MAKE_SYNC(NAME, \71 TYPE value, \72 GET(sourceID1, param, &value), \73 SET(sourceID2, param, value))75 #define MAKE_SYNC3(NAME, TYPE, GET, SET) \76 _MAKE_SYNC(NAME, \77 TYPE value1; TYPE value2; TYPE value3;, \78 GET(sourceID1, param, &value1, &value2, &value3), \79 SET(sourceID2, param, value1, value2, value3))81 MAKE_SYNC( syncSourcei, ALint, alGetSourcei, alSourcei);82 MAKE_SYNC( syncSourcef, ALfloat, alGetSourcef, alSourcef);83 MAKE_SYNC3(syncSource3i, ALint, alGetSource3i, alSource3i);84 MAKE_SYNC3(syncSource3f, ALfloat, alGetSource3f, alSource3f);86 void syncSources(ALsource *masterSource, ALsource *slaveSource,87 ALCcontext *masterCtx, ALCcontext *slaveCtx){88 ALuint master = masterSource->source;89 ALuint slave = slaveSource->source;90 ALCcontext *current = alcGetCurrentContext();92 syncSourcef(master,slave,masterCtx,slaveCtx,AL_PITCH);93 syncSourcef(master,slave,masterCtx,slaveCtx,AL_GAIN);94 syncSourcef(master,slave,masterCtx,slaveCtx,AL_MAX_DISTANCE);95 syncSourcef(master,slave,masterCtx,slaveCtx,AL_ROLLOFF_FACTOR);96 syncSourcef(master,slave,masterCtx,slaveCtx,AL_REFERENCE_DISTANCE);97 syncSourcef(master,slave,masterCtx,slaveCtx,AL_MIN_GAIN);98 syncSourcef(master,slave,masterCtx,slaveCtx,AL_MAX_GAIN);99 syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_OUTER_GAIN);100 syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_INNER_ANGLE);101 syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_OUTER_ANGLE);102 syncSourcef(master,slave,masterCtx,slaveCtx,AL_SEC_OFFSET);103 syncSourcef(master,slave,masterCtx,slaveCtx,AL_SAMPLE_OFFSET);104 syncSourcef(master,slave,masterCtx,slaveCtx,AL_BYTE_OFFSET);106 syncSource3f(master,slave,masterCtx,slaveCtx,AL_POSITION);107 syncSource3f(master,slave,masterCtx,slaveCtx,AL_VELOCITY);108 syncSource3f(master,slave,masterCtx,slaveCtx,AL_DIRECTION);110 syncSourcei(master,slave,masterCtx,slaveCtx,AL_SOURCE_RELATIVE);111 syncSourcei(master,slave,masterCtx,slaveCtx,AL_LOOPING);113 alcMakeContextCurrent(masterCtx);114 ALint source_type;115 alGetSourcei(master, AL_SOURCE_TYPE, &source_type);117 // Only static sources are currently synchronized!118 if (AL_STATIC == source_type){119 ALint master_buffer;120 ALint slave_buffer;121 alGetSourcei(master, AL_BUFFER, &master_buffer);122 alcMakeContextCurrent(slaveCtx);123 alGetSourcei(slave, AL_BUFFER, &slave_buffer);124 if (master_buffer != slave_buffer){125 alSourcei(slave, AL_BUFFER, master_buffer);126 }127 }129 // Synchronize the state of the two sources.130 alcMakeContextCurrent(masterCtx);131 ALint masterState;132 ALint slaveState;134 alGetSourcei(master, AL_SOURCE_STATE, &masterState);135 alcMakeContextCurrent(slaveCtx);136 alGetSourcei(slave, AL_SOURCE_STATE, &slaveState);138 if (masterState != slaveState){139 switch (masterState){140 case AL_INITIAL : alSourceRewind(slave); break;141 case AL_PLAYING : alSourcePlay(slave); break;142 case AL_PAUSED : alSourcePause(slave); break;143 case AL_STOPPED : alSourceStop(slave); break;144 }145 }146 // Restore whatever context was previously active.147 alcMakeContextCurrent(current);148 }151 void syncContexts(ALCcontext *master, ALCcontext *slave){152 /* If there aren't sufficient sources in slave to mirror153 the sources in master, create them. */154 ALCcontext *current = alcGetCurrentContext();156 UIntMap *masterSourceMap = &(master->SourceMap);157 UIntMap *slaveSourceMap = &(slave->SourceMap);158 ALuint numMasterSources = masterSourceMap->size;159 ALuint numSlaveSources = slaveSourceMap->size;161 alcMakeContextCurrent(slave);162 if (numSlaveSources < numMasterSources){163 ALuint numMissingSources = numMasterSources - numSlaveSources;164 ALuint newSources[numMissingSources];165 alGenSources(numMissingSources, newSources);166 }168 /* Now, slave is gauranteed to have at least as many sources169 as master. Sync each source from master to the corresponding170 source in slave. */171 int i;172 for(i = 0; i < masterSourceMap->size; i++){173 syncSources((ALsource*)masterSourceMap->array[i].value,174 (ALsource*)slaveSourceMap->array[i].value,175 master, slave);176 }177 alcMakeContextCurrent(current);178 }180 static void addContext(ALCdevice *Device, ALCcontext *context){181 send_data *data = (send_data*)Device->ExtraData;182 // expand array if necessary183 if (data->numContexts >= data->maxContexts){184 ALuint newMaxContexts = data->maxContexts*2 + 1;185 data->contexts = realloc(data->contexts, newMaxContexts*sizeof(context_data));186 data->maxContexts = newMaxContexts;187 }188 // create context_data and add it to the main array189 context_data *ctxData;190 ctxData = (context_data*)calloc(1, sizeof(*ctxData));191 ctxData->renderBuffer = malloc(data->size);192 ctxData->ctx = context;194 data->contexts[data->numContexts] = ctxData;195 data->numContexts++;196 }199 //////////////////// Context Switching201 /* A device brings along with it two pieces of state202 * which have to be swapped in and out with each context.203 */204 static void swapInContext(ALCdevice *Device, context_data *ctxData){205 memcpy(Device->ClickRemoval, ctxData->ClickRemoval, sizeof(ALfloat)*MAXCHANNELS);206 memcpy(Device->PendingClicks, ctxData->PendingClicks, sizeof(ALfloat)*MAXCHANNELS);207 }209 static void saveContext(ALCdevice *Device, context_data *ctxData){210 memcpy(ctxData->ClickRemoval, Device->ClickRemoval, sizeof(ALfloat)*MAXCHANNELS);211 memcpy(ctxData->PendingClicks, Device->PendingClicks, sizeof(ALfloat)*MAXCHANNELS);212 }214 static ALCcontext **currentContext;215 static ALuint currentNumContext;217 /* By default, all contexts are rendered at once for each call to aluMixData.218 * This function uses the internals of the ALCdecice struct to temporarly219 * cause aluMixData to only render the chosen context.220 */221 static void limitContext(ALCdevice *Device, ALCcontext *ctx){222 currentContext = Device->Contexts;223 currentNumContext = Device->NumContexts;224 Device->Contexts = &ctx;225 Device->NumContexts = 1;226 }228 static void unLimitContext(ALCdevice *Device){229 Device->Contexts = currentContext;230 Device->NumContexts = currentNumContext;231 }234 //////////////////// Main Device Loop236 /* Establish the LWJGL context as the main context, which will237 * be synchronized to all the slave contexts238 */239 static void init(ALCdevice *Device){240 ALCcontext *masterContext = alcGetCurrentContext();241 addContext(Device, masterContext);242 }245 static void renderData(ALCdevice *Device, int samples){246 if(!Device->Connected){return;}247 send_data *data = (send_data*)Device->ExtraData;248 ALCcontext *current = alcGetCurrentContext();250 ALuint i;251 for (i = 1; i < data->numContexts; i++){252 syncContexts(data->contexts[0]->ctx , data->contexts[i]->ctx);253 }255 if ((uint) samples > Device->UpdateSize){256 printf("exceeding internal buffer size; dropping samples\n");257 printf("requested %d; available %d\n", samples, Device->UpdateSize);258 samples = (int) Device->UpdateSize;259 }261 for (i = 0; i < data->numContexts; i++){262 context_data *ctxData = data->contexts[i];263 ALCcontext *ctx = ctxData->ctx;264 alcMakeContextCurrent(ctx);265 limitContext(Device, ctx);266 swapInContext(Device, ctxData);267 aluMixData(Device, ctxData->renderBuffer, samples);268 saveContext(Device, ctxData);269 unLimitContext(Device);270 }271 alcMakeContextCurrent(current);272 }275 //////////////////// JNI Methods277 #include "com_aurellem_send_AudioSend.h"279 /*280 * Class: com_aurellem_send_AudioSend281 * Method: nstep282 * Signature: (JI)V283 */284 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nstep285 (JNIEnv *env, jclass clazz, jlong device, jint samples){286 UNUSED(env);UNUSED(clazz);UNUSED(device);287 renderData((ALCdevice*)((intptr_t)device), samples);288 }290 /*291 * Class: com_aurellem_send_AudioSend292 * Method: ngetSamples293 * Signature: (JLjava/nio/ByteBuffer;III)V294 */295 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_ngetSamples296 (JNIEnv *env, jclass clazz, jlong device, jobject buffer, jint position,297 jint samples, jint n){298 UNUSED(clazz);300 ALvoid *buffer_address =301 ((ALbyte *)(((char*)(*env)->GetDirectBufferAddress(env, buffer)) + position));302 ALCdevice *recorder = (ALCdevice*) ((intptr_t)device);303 send_data *data = (send_data*)recorder->ExtraData;304 if ((ALuint)n > data->numContexts){return;}305 if ((uint) samples > data->size){306 samples = (int) data->size;307 }308 memcpy(buffer_address, data->contexts[n]->renderBuffer, samples*sizeof(ALfloat));309 }311 /*312 * Class: com_aurellem_send_AudioSend313 * Method: naddListener314 * Signature: (J)V315 */316 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_naddListener317 (JNIEnv *env, jclass clazz, jlong device){318 UNUSED(env); UNUSED(clazz);319 printf("creating new context via naddListener\n");320 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);321 ALCcontext *new = alcCreateContext(Device, NULL);322 addContext(Device, new);323 }325 /*326 * Class: com_aurellem_send_AudioSend327 * Method: nsetNthListener3f328 * Signature: (IFFFJI)V329 */330 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nsetNthListener3f331 (JNIEnv *env, jclass clazz, jint param,332 jfloat v1, jfloat v2, jfloat v3, jlong device, jint contextNum){333 UNUSED(env);UNUSED(clazz);335 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);336 send_data *data = (send_data*)Device->ExtraData;338 ALCcontext *current = alcGetCurrentContext();339 if ((ALuint)contextNum > data->numContexts){return;}340 alcMakeContextCurrent(data->contexts[contextNum]->ctx);341 alListener3f(param, v1, v2, v3);342 alcMakeContextCurrent(current);343 }345 /*346 * Class: com_aurellem_send_AudioSend347 * Method: nsetNthListenerf348 * Signature: (IFJI)V349 */350 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nsetNthListenerf351 (JNIEnv *env, jclass clazz, jint param, jfloat v1, jlong device,352 jint contextNum){354 UNUSED(env);UNUSED(clazz);356 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);357 send_data *data = (send_data*)Device->ExtraData;359 ALCcontext *current = alcGetCurrentContext();360 if ((ALuint)contextNum > data->numContexts){return;}361 alcMakeContextCurrent(data->contexts[contextNum]->ctx);362 alListenerf(param, v1);363 alcMakeContextCurrent(current);364 }366 /*367 * Class: com_aurellem_send_AudioSend368 * Method: ninitDevice369 * Signature: (J)V370 */371 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_ninitDevice372 (JNIEnv *env, jclass clazz, jlong device){373 UNUSED(env);UNUSED(clazz);375 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);376 init(Device);378 }381 //////////////////// Device Initilization / Management383 static const ALCchar sendDevice[] = "Multiple Audio Send";385 static ALCboolean send_open_playback(ALCdevice *device,386 const ALCchar *deviceName)387 {388 send_data *data;389 // stop any buffering for stdout, so that I can390 // see the printf statements in my terminal immediatley391 setbuf(stdout, NULL);393 if(!deviceName)394 deviceName = sendDevice;395 else if(strcmp(deviceName, sendDevice) != 0)396 return ALC_FALSE;397 data = (send_data*)calloc(1, sizeof(*data));398 device->szDeviceName = strdup(deviceName);399 device->ExtraData = data;400 return ALC_TRUE;401 }403 static void send_close_playback(ALCdevice *device)404 {405 send_data *data = (send_data*)device->ExtraData;406 alcMakeContextCurrent(NULL);407 ALuint i;408 // Destroy all slave contexts. LWJGL will take care of409 // its own context.410 for (i = 1; i < data->numContexts; i++){411 context_data *ctxData = data->contexts[i];412 alcDestroyContext(ctxData->ctx);413 free(ctxData->renderBuffer);414 free(ctxData);415 }416 free(data);417 device->ExtraData = NULL;418 }420 static ALCboolean send_reset_playback(ALCdevice *device)421 {422 send_data *data = (send_data*)device->ExtraData;423 ALuint channels=0, bits=0;424 device->FmtType = DevFmtShort;425 bits = BytesFromDevFmt(device->FmtType) * 8;426 channels = ChannelsFromDevFmt(device->FmtChans);427 data->size = device->UpdateSize * channels * bits / 8;429 return ALC_TRUE;430 }432 static void send_stop_playback(ALCdevice *Device){433 UNUSED(Device);434 }436 static const BackendFuncs send_funcs = {437 send_open_playback,438 send_close_playback,439 send_reset_playback,440 send_stop_playback,441 NULL,442 NULL, /* These would be filled with functions to */443 NULL, /* handle capturing audio if we we into that */444 NULL, /* sort of thing... */445 NULL,446 NULL447 };449 ALCboolean alc_send_init(BackendFuncs *func_list){450 *func_list = send_funcs;451 return ALC_TRUE;452 }454 void alc_send_deinit(void){}456 void alc_send_probe(enum DevProbe type)457 {458 switch(type)459 {460 case DEVICE_PROBE:461 AppendDeviceList(sendDevice);462 break;463 case ALL_DEVICE_PROBE:464 AppendAllDeviceList(sendDevice);465 break;466 case CAPTURE_DEVICE_PROBE:467 break;468 }469 }