Mercurial > audio-send
view Alc/backends/send.c @ 14:63312ec4a2bf
limit to 16bit mono for now.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 31 Oct 2011 08:01:08 -0700 |
parents | 92b416b4e027 |
children | 19ff95c69cf5 |
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 =192 malloc(BytesFromDevFmt(Device->FmtType) *193 Device->NumChan * Device->UpdateSize);194 ctxData->ctx = context;196 data->contexts[data->numContexts] = ctxData;197 data->numContexts++;198 }201 //////////////////// Context Switching203 /* A device brings along with it two pieces of state204 * which have to be swapped in and out with each context.205 */206 static void swapInContext(ALCdevice *Device, context_data *ctxData){207 memcpy(Device->ClickRemoval, ctxData->ClickRemoval, sizeof(ALfloat)*MAXCHANNELS);208 memcpy(Device->PendingClicks, ctxData->PendingClicks, sizeof(ALfloat)*MAXCHANNELS);209 }211 static void saveContext(ALCdevice *Device, context_data *ctxData){212 memcpy(ctxData->ClickRemoval, Device->ClickRemoval, sizeof(ALfloat)*MAXCHANNELS);213 memcpy(ctxData->PendingClicks, Device->PendingClicks, sizeof(ALfloat)*MAXCHANNELS);214 }216 static ALCcontext **currentContext;217 static ALuint currentNumContext;219 /* By default, all contexts are rendered at once for each call to aluMixData.220 * This function uses the internals of the ALCdecice struct to temporarly221 * cause aluMixData to only render the chosen context.222 */223 static void limitContext(ALCdevice *Device, ALCcontext *ctx){224 currentContext = Device->Contexts;225 currentNumContext = Device->NumContexts;226 Device->Contexts = &ctx;227 Device->NumContexts = 1;228 }230 static void unLimitContext(ALCdevice *Device){231 Device->Contexts = currentContext;232 Device->NumContexts = currentNumContext;233 }236 //////////////////// Main Device Loop238 /* Establish the LWJGL context as the main context, which will239 * be synchronized to all the slave contexts240 */241 static void init(ALCdevice *Device){242 ALCcontext *masterContext = alcGetCurrentContext();243 addContext(Device, masterContext);244 }247 static void renderData(ALCdevice *Device, int samples){248 if(!Device->Connected){return;}249 send_data *data = (send_data*)Device->ExtraData;250 ALCcontext *current = alcGetCurrentContext();252 ALuint i;253 for (i = 1; i < data->numContexts; i++){254 syncContexts(data->contexts[0]->ctx , data->contexts[i]->ctx);255 }257 if ((uint) samples > Device->UpdateSize){258 printf("exceeding internal buffer size; dropping samples\n");259 printf("requested %d; available %d\n", samples, Device->UpdateSize);260 samples = (int) Device->UpdateSize;261 }263 for (i = 0; i < data->numContexts; i++){264 context_data *ctxData = data->contexts[i];265 ALCcontext *ctx = ctxData->ctx;266 alcMakeContextCurrent(ctx);267 limitContext(Device, ctx);268 swapInContext(Device, ctxData);269 aluMixData(Device, ctxData->renderBuffer, samples);270 saveContext(Device, ctxData);271 unLimitContext(Device);272 }273 alcMakeContextCurrent(current);274 }277 //////////////////// JNI Methods279 #include "com_aurellem_send_AudioSend.h"281 /*282 * Class: com_aurellem_send_AudioSend283 * Method: nstep284 * Signature: (JI)V285 */286 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nstep287 (JNIEnv *env, jclass clazz, jlong device, jint samples){288 UNUSED(env);UNUSED(clazz);UNUSED(device);289 renderData((ALCdevice*)((intptr_t)device), samples);290 }292 /*293 * Class: com_aurellem_send_AudioSend294 * Method: ngetSamples295 * Signature: (JLjava/nio/ByteBuffer;III)V296 */297 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_ngetSamples298 (JNIEnv *env, jclass clazz, jlong device, jobject buffer, jint position,299 jint samples, jint n){300 UNUSED(clazz);302 ALvoid *buffer_address =303 ((ALbyte *)(((char*)(*env)->GetDirectBufferAddress(env, buffer)) + position));304 ALCdevice *recorder = (ALCdevice*) ((intptr_t)device);305 send_data *data = (send_data*)recorder->ExtraData;306 if ((ALuint)n > data->numContexts){return;}307 //if ((uint) samples > data->size){308 // samples = (int) data->size;309 //}310 printf("Want %d samples for listener %d\n", samples, n);311 printf("Device's format type is %d bytes per sample,\n",312 BytesFromDevFmt(recorder->FmtType));313 printf("and it has %d channels, making for %d requested bytes\n",314 recorder->NumChan,315 BytesFromDevFmt(recorder->FmtType) * recorder->NumChan * samples);317 memcpy(buffer_address, data->contexts[n]->renderBuffer,318 BytesFromDevFmt(recorder->FmtType) * recorder->NumChan * samples);319 //samples*sizeof(ALfloat));320 }322 /*323 * Class: com_aurellem_send_AudioSend324 * Method: naddListener325 * Signature: (J)V326 */327 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_naddListener328 (JNIEnv *env, jclass clazz, jlong device){329 UNUSED(env); UNUSED(clazz);330 printf("creating new context via naddListener\n");331 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);332 ALCcontext *new = alcCreateContext(Device, NULL);333 addContext(Device, new);334 }336 /*337 * Class: com_aurellem_send_AudioSend338 * Method: nsetNthListener3f339 * Signature: (IFFFJI)V340 */341 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nsetNthListener3f342 (JNIEnv *env, jclass clazz, jint param,343 jfloat v1, jfloat v2, jfloat v3, jlong device, jint contextNum){344 UNUSED(env);UNUSED(clazz);346 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);347 send_data *data = (send_data*)Device->ExtraData;349 ALCcontext *current = alcGetCurrentContext();350 if ((ALuint)contextNum > data->numContexts){return;}351 alcMakeContextCurrent(data->contexts[contextNum]->ctx);352 alListener3f(param, v1, v2, v3);353 alcMakeContextCurrent(current);354 }356 /*357 * Class: com_aurellem_send_AudioSend358 * Method: nsetNthListenerf359 * Signature: (IFJI)V360 */361 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_nsetNthListenerf362 (JNIEnv *env, jclass clazz, jint param, jfloat v1, jlong device,363 jint contextNum){365 UNUSED(env);UNUSED(clazz);367 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);368 send_data *data = (send_data*)Device->ExtraData;370 ALCcontext *current = alcGetCurrentContext();371 if ((ALuint)contextNum > data->numContexts){return;}372 alcMakeContextCurrent(data->contexts[contextNum]->ctx);373 alListenerf(param, v1);374 alcMakeContextCurrent(current);375 }377 /*378 * Class: com_aurellem_send_AudioSend379 * Method: ninitDevice380 * Signature: (J)V381 */382 JNIEXPORT void JNICALL Java_com_aurellem_send_AudioSend_ninitDevice383 (JNIEnv *env, jclass clazz, jlong device){384 UNUSED(env);UNUSED(clazz);386 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);387 init(Device);389 }392 /*393 * Class: com_aurellem_send_AudioSend394 * Method: ngetAudioFormat395 * Signature: (J)Ljavax/sound/sampled/AudioFormat;396 */397 JNIEXPORT jobject JNICALL Java_com_aurellem_send_AudioSend_ngetAudioFormat398 (JNIEnv *env, jclass clazz, jlong device){399 UNUSED(clazz);400 jclass AudioFormatClass =401 (*env)->FindClass(env, "javax/sound/sampled/AudioFormat");402 jmethodID AudioFormatConstructor =403 (*env)->GetMethodID(env, AudioFormatClass, "<init>", "(FIIZZ)V");405 ALCdevice *Device = (ALCdevice*) ((intptr_t)device);407 //float frequency409 int isSigned;410 switch (Device->FmtType)411 {412 case DevFmtUByte:413 case DevFmtUShort: isSigned = 0; break;414 default : isSigned = 1;415 }416 float frequency = Device->Frequency;417 int bitsPerFrame = (8 * BytesFromDevFmt(Device->FmtType));418 int channels = Device->NumChan;421 printf("freq = %f, bpf = %d, channels = %d, signed? = %d\n",422 frequency, bitsPerFrame, channels, isSigned);424 jobject format = (*env)->425 NewObject(426 env,AudioFormatClass,AudioFormatConstructor,427 frequency,428 bitsPerFrame,429 channels,430 isSigned,431 0);432 return format;433 }437 //////////////////// Device Initilization / Management439 static const ALCchar sendDevice[] = "Multiple Audio Send";441 static ALCboolean send_open_playback(ALCdevice *device,442 const ALCchar *deviceName)443 {444 send_data *data;445 // stop any buffering for stdout, so that I can446 // see the printf statements in my terminal immediatley447 setbuf(stdout, NULL);449 if(!deviceName)450 deviceName = sendDevice;451 else if(strcmp(deviceName, sendDevice) != 0)452 return ALC_FALSE;453 data = (send_data*)calloc(1, sizeof(*data));454 device->szDeviceName = strdup(deviceName);455 device->ExtraData = data;456 return ALC_TRUE;457 }459 static void send_close_playback(ALCdevice *device)460 {461 send_data *data = (send_data*)device->ExtraData;462 alcMakeContextCurrent(NULL);463 ALuint i;464 // Destroy all slave contexts. LWJGL will take care of465 // its own context.466 for (i = 1; i < data->numContexts; i++){467 context_data *ctxData = data->contexts[i];468 alcDestroyContext(ctxData->ctx);469 free(ctxData->renderBuffer);470 free(ctxData);471 }472 free(data);473 device->ExtraData = NULL;474 }476 static ALCboolean send_reset_playback(ALCdevice *device)477 {478 SetDefaultWFXChannelOrder(device);479 return ALC_TRUE;480 }482 static void send_stop_playback(ALCdevice *Device){483 UNUSED(Device);484 }486 static const BackendFuncs send_funcs = {487 send_open_playback,488 send_close_playback,489 send_reset_playback,490 send_stop_playback,491 NULL,492 NULL, /* These would be filled with functions to */493 NULL, /* handle capturing audio if we we into that */494 NULL, /* sort of thing... */495 NULL,496 NULL497 };499 ALCboolean alc_send_init(BackendFuncs *func_list){500 *func_list = send_funcs;501 return ALC_TRUE;502 }504 void alc_send_deinit(void){}506 void alc_send_probe(enum DevProbe type)507 {508 switch(type)509 {510 case DEVICE_PROBE:511 AppendDeviceList(sendDevice);512 break;513 case ALL_DEVICE_PROBE:514 AppendAllDeviceList(sendDevice);515 break;516 case CAPTURE_DEVICE_PROBE:517 break;518 }519 }