rlm@0:
rlm@0:
rlm@0:
rlm@0:
rlm@0:
rlm@0:
rlm@0: aurellem ☉
rlm@0: rlm@0:send.c
rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: rlm@0:#include "config.h" rlm@0: #include <stdlib.h> rlm@0: #include "alMain.h" rlm@0: #include "AL/al.h" rlm@0: #include "AL/alc.h" rlm@0: #include "alSource.h" rlm@0: #include <jni.h> rlm@0: rlm@0: //////////////////// Summary rlm@0: rlm@0: struct send_data; rlm@0: struct context_data; rlm@0: rlm@0: static void addContext(ALCdevice *, ALCcontext *); rlm@0: static void syncContexts(ALCcontext *master, ALCcontext *slave); rlm@0: static void syncSources(ALsource *master, ALsource *slave, rlm@0: ALCcontext *masterCtx, ALCcontext *slaveCtx); rlm@0: rlm@0: static void syncSourcei(ALuint master, ALuint slave, rlm@0: ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param); rlm@0: static void syncSourcef(ALuint master, ALuint slave, rlm@0: ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param); rlm@0: static void syncSource3f(ALuint master, ALuint slave, rlm@0: ALCcontext *masterCtx, ALCcontext *ctx2, ALenum param); rlm@0: rlm@0: static void swapInContext(ALCdevice *, struct context_data *); rlm@0: static void saveContext(ALCdevice *, struct context_data *); rlm@0: static void limitContext(ALCdevice *, ALCcontext *); rlm@0: static void unLimitContext(ALCdevice *); rlm@0: rlm@0: static void init(ALCdevice *); rlm@0: static void renderData(ALCdevice *, int samples); rlm@0: rlm@0: #define UNUSED(x) (void)(x) rlm@0: rlm@0: //////////////////// State rlm@0: rlm@0: typedef struct context_data { rlm@0: ALfloat ClickRemoval[MAXCHANNELS]; rlm@0: ALfloat PendingClicks[MAXCHANNELS]; rlm@0: ALvoid *renderBuffer; rlm@0: ALCcontext *ctx; rlm@0: } context_data; rlm@0: rlm@0: typedef struct send_data { rlm@0: ALuint size; rlm@0: context_data **contexts; rlm@0: ALuint numContexts; rlm@0: ALuint maxContexts; rlm@0: } send_data; rlm@0: rlm@0: rlm@0: rlm@0: //////////////////// Context Creation / Synchronization rlm@0: rlm@0: #define _MAKE_SYNC(NAME, INIT_EXPR, GET_EXPR, SET_EXPR) \ rlm@0: void NAME (ALuint sourceID1, ALuint sourceID2, \ rlm@0: ALCcontext *ctx1, ALCcontext *ctx2, \ rlm@0: ALenum param){ \ rlm@0: INIT_EXPR; \ rlm@0: ALCcontext *current = alcGetCurrentContext(); \ rlm@0: alcMakeContextCurrent(ctx1); \ rlm@0: GET_EXPR; \ rlm@0: alcMakeContextCurrent(ctx2); \ rlm@0: SET_EXPR; \ rlm@0: alcMakeContextCurrent(current); \ rlm@0: } rlm@0: rlm@0: #define MAKE_SYNC(NAME, TYPE, GET, SET) \ rlm@0: _MAKE_SYNC(NAME, \ rlm@0: TYPE value, \ rlm@0: GET(sourceID1, param, &value), \ rlm@0: SET(sourceID2, param, value)) rlm@0: rlm@0: #define MAKE_SYNC3(NAME, TYPE, GET, SET) \ rlm@0: _MAKE_SYNC(NAME, \ rlm@0: TYPE value1; TYPE value2; TYPE value3;, \ rlm@0: GET(sourceID1, param, &value1, &value2, &value3), \ rlm@0: SET(sourceID2, param, value1, value2, value3)) rlm@0: rlm@0: MAKE_SYNC( syncSourcei, ALint, alGetSourcei, alSourcei); rlm@0: MAKE_SYNC( syncSourcef, ALfloat, alGetSourcef, alSourcef); rlm@0: MAKE_SYNC3(syncSource3i, ALint, alGetSource3i, alSource3i); rlm@0: MAKE_SYNC3(syncSource3f, ALfloat, alGetSource3f, alSource3f); rlm@0: rlm@0: void syncSources(ALsource *masterSource, ALsource *slaveSource, rlm@0: ALCcontext *masterCtx, ALCcontext *slaveCtx){ rlm@0: ALuint master = masterSource->source; rlm@0: ALuint slave = slaveSource->source; rlm@0: ALCcontext *current = alcGetCurrentContext(); rlm@0: rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_PITCH); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_GAIN); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_MAX_DISTANCE); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_ROLLOFF_FACTOR); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_REFERENCE_DISTANCE); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_MIN_GAIN); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_MAX_GAIN); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_OUTER_GAIN); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_INNER_ANGLE); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_CONE_OUTER_ANGLE); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_SEC_OFFSET); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_SAMPLE_OFFSET); rlm@0: syncSourcef(master,slave,masterCtx,slaveCtx,AL_BYTE_OFFSET); rlm@0: rlm@0: syncSource3f(master,slave,masterCtx,slaveCtx,AL_POSITION); rlm@0: syncSource3f(master,slave,masterCtx,slaveCtx,AL_VELOCITY); rlm@0: syncSource3f(master,slave,masterCtx,slaveCtx,AL_DIRECTION); rlm@0: rlm@0: syncSourcei(master,slave,masterCtx,slaveCtx,AL_SOURCE_RELATIVE); rlm@0: syncSourcei(master,slave,masterCtx,slaveCtx,AL_LOOPING); rlm@0: rlm@0: alcMakeContextCurrent(masterCtx); rlm@0: ALint source_type; rlm@0: alGetSourcei(master, AL_SOURCE_TYPE, &source_type); rlm@0: rlm@0: // Only static sources are currently synchronized! rlm@0: if (AL_STATIC == source_type){ rlm@0: ALint master_buffer; rlm@0: ALint slave_buffer; rlm@0: alGetSourcei(master, AL_BUFFER, &master_buffer); rlm@0: alcMakeContextCurrent(slaveCtx); rlm@0: alGetSourcei(slave, AL_BUFFER, &slave_buffer); rlm@0: if (master_buffer != slave_buffer){ rlm@0: alSourcei(slave, AL_BUFFER, master_buffer); rlm@0: } rlm@0: } rlm@0: rlm@0: // Synchronize the state of the two sources. rlm@0: alcMakeContextCurrent(masterCtx); rlm@0: ALint masterState; rlm@0: ALint slaveState; rlm@0: rlm@0: alGetSourcei(master, AL_SOURCE_STATE, &masterState); rlm@0: alcMakeContextCurrent(slaveCtx); rlm@0: alGetSourcei(slave, AL_SOURCE_STATE, &slaveState); rlm@0: rlm@0: if (masterState != slaveState){ rlm@0: switch (masterState){ rlm@0: case AL_INITIAL : alSourceRewind(slave); break; rlm@0: case AL_PLAYING : alSourcePlay(slave); break; rlm@0: case AL_PAUSED : alSourcePause(slave); break; rlm@0: case AL_STOPPED : alSourceStop(slave); break; rlm@0: } rlm@0: } rlm@0: // Restore whatever context was previously active. rlm@0: alcMakeContextCurrent(current); rlm@0: } rlm@0: rlm@0: rlm@0: void syncContexts(ALCcontext *master, ALCcontext *slave){ rlm@0: /* If there aren't sufficient sources in slave to mirror rlm@0: the sources in master, create them. */ rlm@0: ALCcontext *current = alcGetCurrentContext(); rlm@0: rlm@0: UIntMap *masterSourceMap = &(master->SourceMap); rlm@0: UIntMap *slaveSourceMap = &(slave->SourceMap); rlm@0: ALuint numMasterSources = masterSourceMap->size; rlm@0: ALuint numSlaveSources = slaveSourceMap->size; rlm@0: rlm@0: alcMakeContextCurrent(slave); rlm@0: if (numSlaveSources < numMasterSources){ rlm@0: ALuint numMissingSources = numMasterSources - numSlaveSources; rlm@0: ALuint newSources[numMissingSources]; rlm@0: alGenSources(numMissingSources, newSources); rlm@0: } rlm@0: rlm@0: /* Now, slave is gauranteed to have at least as many sources rlm@0: as master. Sync each source from master to the corresponding rlm@0: source in slave. */ rlm@0: int i; rlm@0: for(i = 0; i < masterSourceMap->size; i++){ rlm@0: syncSources((ALsource*)masterSourceMap->array[i].value, rlm@0: (ALsource*)slaveSourceMap->array[i].value, rlm@0: master, slave); rlm@0: } rlm@0: alcMakeContextCurrent(current); rlm@0: } rlm@0: rlm@0: static void addContext(ALCdevice *Device, ALCcontext *context){ rlm@0: send_data *data = (send_data*)Device->ExtraData; rlm@0: // expand array if necessary rlm@0: if (data->numContexts >= data->maxContexts){ rlm@0: ALuint newMaxContexts = data->maxContexts*2 + 1; rlm@0: data->contexts = realloc(data->contexts, newMaxContexts*sizeof(context_data)); rlm@0: data->maxContexts = newMaxContexts; rlm@0: } rlm@0: // create context_data and add it to the main array rlm@0: context_data *ctxData; rlm@0: ctxData = (context_data*)calloc(1, sizeof(*ctxData)); rlm@0: ctxData->renderBuffer = malloc(data->size); rlm@0: ctxData->ctx = context; rlm@0: rlm@0: data->contexts[data->numContexts] = ctxData; rlm@0: data->numContexts++; rlm@0: } rlm@0: rlm@0: rlm@0: //////////////////// Context Switching rlm@0: rlm@0: /* A device brings along with it two pieces of state rlm@0: * which have to be swapped in and out with each context. rlm@0: */ rlm@0: static void swapInContext(ALCdevice *Device, context_data *ctxData){ rlm@0: memcpy(Device->ClickRemoval, ctxData->ClickRemoval, sizeof(ALfloat)*MAXCHANNELS); rlm@0: memcpy(Device->PendingClicks, ctxData->PendingClicks, sizeof(ALfloat)*MAXCHANNELS); rlm@0: } rlm@0: rlm@0: static void saveContext(ALCdevice *Device, context_data *ctxData){ rlm@0: memcpy(ctxData->ClickRemoval, Device->ClickRemoval, sizeof(ALfloat)*MAXCHANNELS); rlm@0: memcpy(ctxData->PendingClicks, Device->PendingClicks, sizeof(ALfloat)*MAXCHANNELS); rlm@0: } rlm@0: rlm@0: static ALCcontext **currentContext; rlm@0: static ALuint currentNumContext; rlm@0: rlm@0: /* By default, all contexts are rendered at once for each call to aluMixData. rlm@0: * This function uses the internals of the ALCdecice struct to temporarly rlm@0: * cause aluMixData to only render the chosen context. rlm@0: */ rlm@0: static void limitContext(ALCdevice *Device, ALCcontext *ctx){ rlm@0: currentContext = Device->Contexts; rlm@0: currentNumContext = Device->NumContexts; rlm@0: Device->Contexts = &ctx; rlm@0: Device->NumContexts = 1; rlm@0: } rlm@0: rlm@0: static void unLimitContext(ALCdevice *Device){ rlm@0: Device->Contexts = currentContext; rlm@0: Device->NumContexts = currentNumContext; rlm@0: } rlm@0: rlm@0: rlm@0: //////////////////// Main Device Loop rlm@0: rlm@0: /* Establish the LWJGL context as the main context, which will rlm@0: * be synchronized to all the slave contexts rlm@0: */ rlm@0: static void init(ALCdevice *Device){ rlm@0: ALCcontext *masterContext = alcGetCurrentContext(); rlm@0: addContext(Device, masterContext); rlm@0: } rlm@0: rlm@0: rlm@0: static void renderData(ALCdevice *Device, int samples){ rlm@0: if(!Device->Connected){return;} rlm@0: send_data *data = (send_data*)Device->ExtraData; rlm@0: ALCcontext *current = alcGetCurrentContext(); rlm@0: rlm@0: ALuint i; rlm@0: for (i = 1; i < data->numContexts; i++){ rlm@0: syncContexts(data->contexts[0]->ctx , data->contexts[i]->ctx); rlm@0: } rlm@0: rlm@0: if ((uint) samples > Device->UpdateSize){ rlm@0: printf("exceeding internal buffer size; dropping samples\n"); rlm@0: printf("requested %d; available %d\n", samples, Device->UpdateSize); rlm@0: samples = (int) Device->UpdateSize; rlm@0: } rlm@0: rlm@0: for (i = 0; i < data->numContexts; i++){ rlm@0: context_data *ctxData = data->contexts[i]; rlm@0: ALCcontext *ctx = ctxData->ctx; rlm@0: alcMakeContextCurrent(ctx); rlm@0: limitContext(Device, ctx); rlm@0: swapInContext(Device, ctxData); rlm@0: aluMixData(Device, ctxData->renderBuffer, samples); rlm@0: saveContext(Device, ctxData); rlm@0: unLimitContext(Device); rlm@0: } rlm@0: alcMakeContextCurrent(current); rlm@0: } rlm@0: rlm@0: rlm@0: //////////////////// JNI Methods rlm@0: rlm@0: #include "com_jme3_capture_AudioSend.h" rlm@0: rlm@0: /* rlm@0: * Class: com_jme3_capture_AudioSend rlm@0: * Method: nstep rlm@0: * Signature: (JI)V rlm@0: */ rlm@0: JNIEXPORT void JNICALL Java_com_jme3_capture_AudioSend_nstep rlm@0: (JNIEnv *env, jclass clazz, jlong device, jint samples){ rlm@0: UNUSED(env);UNUSED(clazz);UNUSED(device); rlm@0: renderData((ALCdevice*)((intptr_t)device), samples); rlm@0: } rlm@0: rlm@0: /* rlm@0: * Class: com_jme3_capture_AudioSend rlm@0: * Method: ngetSamples rlm@0: * Signature: (JLjava/nio/ByteBuffer;III)V rlm@0: */ rlm@0: JNIEXPORT void JNICALL Java_com_jme3_capture_AudioSend_ngetSamples rlm@0: (JNIEnv *env, jclass clazz, jlong device, jobject buffer, jint position, rlm@0: jint samples, jint n){ rlm@0: UNUSED(clazz); rlm@0: rlm@0: ALvoid *buffer_address = rlm@0: ((ALbyte *)(((char*)(*env)->GetDirectBufferAddress(env, buffer)) + position)); rlm@0: ALCdevice *recorder = (ALCdevice*) ((intptr_t)device); rlm@0: send_data *data = (send_data*)recorder->ExtraData; rlm@0: if ((ALuint)n > data->numContexts){return;} rlm@0: if ((uint) samples > data->size){ rlm@0: samples = (int) data->size; rlm@0: } rlm@0: memcpy(buffer_address, data->contexts[n]->renderBuffer, samples*sizeof(ALfloat)); rlm@0: } rlm@0: rlm@0: /* rlm@0: * Class: com_jme3_capture_AudioSend rlm@0: * Method: naddListener rlm@0: * Signature: (J)V rlm@0: */ rlm@0: JNIEXPORT void JNICALL Java_com_jme3_capture_AudioSend_naddListener rlm@0: (JNIEnv *env, jclass clazz, jlong device){ rlm@0: UNUSED(env); UNUSED(clazz); rlm@0: printf("creating new context via naddListener\n"); rlm@0: ALCdevice *Device = (ALCdevice*) ((intptr_t)device); rlm@0: ALCcontext *new = alcCreateContext(Device, NULL); rlm@0: addContext(Device, new); rlm@0: } rlm@0: rlm@0: /* rlm@0: * Class: com_jme3_capture_AudioSend rlm@0: * Method: nsetNthListener3f rlm@0: * Signature: (IFFFJI)V rlm@0: */ rlm@0: JNIEXPORT void JNICALL Java_com_jme3_capture_AudioSend_nsetNthListener3f rlm@0: (JNIEnv *env, jclass clazz, jint param, rlm@0: jfloat v1, jfloat v2, jfloat v3, jlong device, jint contextNum){ rlm@0: UNUSED(env);UNUSED(clazz); rlm@0: rlm@0: ALCdevice *Device = (ALCdevice*) ((intptr_t)device); rlm@0: send_data *data = (send_data*)Device->ExtraData; rlm@0: rlm@0: ALCcontext *current = alcGetCurrentContext(); rlm@0: if ((ALuint)contextNum > data->numContexts){return;} rlm@0: alcMakeContextCurrent(data->contexts[contextNum]->ctx); rlm@0: alListener3f(param, v1, v2, v3); rlm@0: alcMakeContextCurrent(current); rlm@0: } rlm@0: rlm@0: /* rlm@0: * Class: com_jme3_capture_AudioSend rlm@0: * Method: nsetNthListenerf rlm@0: * Signature: (IFJI)V rlm@0: */ rlm@0: JNIEXPORT void JNICALL Java_com_jme3_capture_AudioSend_nsetNthListenerf rlm@0: (JNIEnv *env, jclass clazz, jint param, jfloat v1, jlong device, rlm@0: jint contextNum){ rlm@0: rlm@0: UNUSED(env);UNUSED(clazz); rlm@0: rlm@0: ALCdevice *Device = (ALCdevice*) ((intptr_t)device); rlm@0: send_data *data = (send_data*)Device->ExtraData; rlm@0: rlm@0: ALCcontext *current = alcGetCurrentContext(); rlm@0: if ((ALuint)contextNum > data->numContexts){return;} rlm@0: alcMakeContextCurrent(data->contexts[contextNum]->ctx); rlm@0: alListenerf(param, v1); rlm@0: alcMakeContextCurrent(current); rlm@0: } rlm@0: rlm@0: /* rlm@0: * Class: com_jme3_capture_AudioSend rlm@0: * Method: ninitDevice rlm@0: * Signature: (J)V rlm@0: */ rlm@0: JNIEXPORT void JNICALL Java_com_jme3_capture_AudioSend_ninitDevice rlm@0: (JNIEnv *env, jclass clazz, jlong device){ rlm@0: UNUSED(env);UNUSED(clazz); rlm@0: rlm@0: ALCdevice *Device = (ALCdevice*) ((intptr_t)device); rlm@0: init(Device); rlm@0: rlm@0: } rlm@0: rlm@0: rlm@0: //////////////////// Device Initilization / Management rlm@0: rlm@0: static const ALCchar sendDevice[] = "Multiple Audio Send"; rlm@0: rlm@0: static ALCboolean send_open_playback(ALCdevice *device, rlm@0: const ALCchar *deviceName) rlm@0: { rlm@0: send_data *data; rlm@0: // stop any buffering for stdout, so that I can rlm@0: // see the printf statements in my terminal immediatley rlm@0: setbuf(stdout, NULL); rlm@0: rlm@0: if(!deviceName) rlm@0: deviceName = sendDevice; rlm@0: else if(strcmp(deviceName, sendDevice) != 0) rlm@0: return ALC_FALSE; rlm@0: data = (send_data*)calloc(1, sizeof(*data)); rlm@0: device->szDeviceName = strdup(deviceName); rlm@0: device->ExtraData = data; rlm@0: return ALC_TRUE; rlm@0: } rlm@0: rlm@0: static void send_close_playback(ALCdevice *device) rlm@0: { rlm@0: send_data *data = (send_data*)device->ExtraData; rlm@0: alcMakeContextCurrent(NULL); rlm@0: ALuint i; rlm@0: // Destroy all slave contexts. LWJGL will take care of rlm@0: // its own context. rlm@0: for (i = 1; i < data->numContexts; i++){ rlm@0: context_data *ctxData = data->contexts[i]; rlm@0: alcDestroyContext(ctxData->ctx); rlm@0: free(ctxData->renderBuffer); rlm@0: free(ctxData); rlm@0: } rlm@0: free(data); rlm@0: device->ExtraData = NULL; rlm@0: } rlm@0: rlm@0: static ALCboolean send_reset_playback(ALCdevice *device) rlm@0: { rlm@0: send_data *data = (send_data*)device->ExtraData; rlm@0: ALuint channels=0, bits=0; rlm@0: device->FmtType = DevFmtShort; rlm@0: bits = BytesFromDevFmt(device->FmtType) * 8; rlm@0: channels = ChannelsFromDevFmt(device->FmtChans); rlm@0: data->size = device->UpdateSize * channels * bits / 8; rlm@0: rlm@0: return ALC_TRUE; rlm@0: } rlm@0: rlm@0: static void send_stop_playback(ALCdevice *Device){ rlm@0: UNUSED(Device); rlm@0: } rlm@0: rlm@0: static const BackendFuncs send_funcs = { rlm@0: send_open_playback, rlm@0: send_close_playback, rlm@0: send_reset_playback, rlm@0: send_stop_playback, rlm@0: NULL, rlm@0: NULL, /* These would be filled with functions to */ rlm@0: NULL, /* handle capturing audio if we we into that */ rlm@0: NULL, /* sort of thing... */ rlm@0: NULL, rlm@0: NULL rlm@0: }; rlm@0: rlm@0: ALCboolean alc_send_init(BackendFuncs *func_list){ rlm@0: *func_list = send_funcs; rlm@0: return ALC_TRUE; rlm@0: } rlm@0: rlm@0: void alc_send_deinit(void){} rlm@0: rlm@0: void alc_send_probe(enum DevProbe type) rlm@0: { rlm@0: switch(type) rlm@0: { rlm@0: case DEVICE_PROBE: rlm@0: AppendDeviceList(sendDevice); rlm@0: break; rlm@0: case ALL_DEVICE_PROBE: rlm@0: AppendAllDeviceList(sendDevice); rlm@0: break; rlm@0: case CAPTURE_DEVICE_PROBE: rlm@0: break; rlm@0: } rlm@0: } rlm@0: rlm@0: rlm@0: rlm@0:rlm@0: rlm@0: rlm@0: rlm@0: rlm@0: rlm@0:
rlm@0:
rlm@0: Date: 2011-10-20 15:48:47 MST
rlm@0: rlm@0:Org version 7.6 with Emacs version 23
rlm@0: Validate XHTML 1.0 rlm@0: