alAuxEffectSlot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include "AL/al.h"
  24. #include "AL/alc.h"
  25. #include "alMain.h"
  26. #include "alAuxEffectSlot.h"
  27. #include "alThunk.h"
  28. #include "alError.h"
  29. #include "alSource.h"
  30. extern inline struct ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id);
  31. extern inline struct ALeffectslot *RemoveEffectSlot(ALCcontext *context, ALuint id);
  32. static ALenum AddEffectSlotArray(ALCcontext *Context, ALsizei count, const ALuint *slots);
  33. static ALvoid RemoveEffectSlotArray(ALCcontext *Context, ALeffectslot *slot);
  34. static UIntMap EffectStateFactoryMap;
  35. static inline ALeffectStateFactory *getFactoryByType(ALenum type)
  36. {
  37. ALeffectStateFactory* (*getFactory)(void) = LookupUIntMapKey(&EffectStateFactoryMap, type);
  38. if(getFactory != NULL)
  39. return getFactory();
  40. return NULL;
  41. }
  42. AL_API ALvoid AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
  43. {
  44. ALCcontext *context;
  45. ALsizei cur;
  46. ALenum err;
  47. context = GetContextRef();
  48. if(!context) return;
  49. if(!(n >= 0))
  50. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  51. for(cur = 0;cur < n;cur++)
  52. {
  53. ALeffectslot *slot = al_calloc(16, sizeof(ALeffectslot));
  54. err = AL_OUT_OF_MEMORY;
  55. if(!slot || (err=InitEffectSlot(slot)) != AL_NO_ERROR)
  56. {
  57. al_free(slot);
  58. alDeleteAuxiliaryEffectSlots(cur, effectslots);
  59. SET_ERROR_AND_GOTO(context, err, done);
  60. }
  61. err = NewThunkEntry(&slot->id);
  62. if(err == AL_NO_ERROR)
  63. err = InsertUIntMapEntry(&context->EffectSlotMap, slot->id, slot);
  64. if(err != AL_NO_ERROR)
  65. {
  66. FreeThunkEntry(slot->id);
  67. DELETE_OBJ(slot->EffectState);
  68. al_free(slot);
  69. alDeleteAuxiliaryEffectSlots(cur, effectslots);
  70. SET_ERROR_AND_GOTO(context, err, done);
  71. }
  72. effectslots[cur] = slot->id;
  73. }
  74. err = AddEffectSlotArray(context, n, effectslots);
  75. if(err != AL_NO_ERROR)
  76. {
  77. alDeleteAuxiliaryEffectSlots(cur, effectslots);
  78. SET_ERROR_AND_GOTO(context, err, done);
  79. }
  80. done:
  81. ALCcontext_DecRef(context);
  82. }
  83. AL_API ALvoid AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, const ALuint *effectslots)
  84. {
  85. ALCcontext *context;
  86. ALeffectslot *slot;
  87. ALsizei i;
  88. context = GetContextRef();
  89. if(!context) return;
  90. if(!(n >= 0))
  91. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  92. for(i = 0;i < n;i++)
  93. {
  94. if((slot=LookupEffectSlot(context, effectslots[i])) == NULL)
  95. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  96. if(slot->ref != 0)
  97. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  98. }
  99. // All effectslots are valid
  100. for(i = 0;i < n;i++)
  101. {
  102. if((slot=RemoveEffectSlot(context, effectslots[i])) == NULL)
  103. continue;
  104. FreeThunkEntry(slot->id);
  105. RemoveEffectSlotArray(context, slot);
  106. DELETE_OBJ(slot->EffectState);
  107. memset(slot, 0, sizeof(*slot));
  108. al_free(slot);
  109. }
  110. done:
  111. ALCcontext_DecRef(context);
  112. }
  113. AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
  114. {
  115. ALCcontext *context;
  116. ALboolean ret;
  117. context = GetContextRef();
  118. if(!context) return AL_FALSE;
  119. ret = (LookupEffectSlot(context, effectslot) ? AL_TRUE : AL_FALSE);
  120. ALCcontext_DecRef(context);
  121. return ret;
  122. }
  123. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint value)
  124. {
  125. ALCdevice *device;
  126. ALCcontext *context;
  127. ALeffectslot *slot;
  128. ALeffect *effect = NULL;
  129. ALenum err;
  130. context = GetContextRef();
  131. if(!context) return;
  132. device = context->Device;
  133. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  134. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  135. switch(param)
  136. {
  137. case AL_EFFECTSLOT_EFFECT:
  138. effect = (value ? LookupEffect(device, value) : NULL);
  139. if(!(value == 0 || effect != NULL))
  140. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  141. err = InitializeEffect(device, slot, effect);
  142. if(err != AL_NO_ERROR)
  143. SET_ERROR_AND_GOTO(context, err, done);
  144. context->UpdateSources = AL_TRUE;
  145. break;
  146. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  147. if(!(value == AL_TRUE || value == AL_FALSE))
  148. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  149. slot->AuxSendAuto = value;
  150. context->UpdateSources = AL_TRUE;
  151. break;
  152. default:
  153. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  154. }
  155. done:
  156. ALCcontext_DecRef(context);
  157. }
  158. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, const ALint *values)
  159. {
  160. ALCcontext *context;
  161. switch(param)
  162. {
  163. case AL_EFFECTSLOT_EFFECT:
  164. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  165. alAuxiliaryEffectSloti(effectslot, param, values[0]);
  166. return;
  167. }
  168. context = GetContextRef();
  169. if(!context) return;
  170. if(LookupEffectSlot(context, effectslot) == NULL)
  171. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  172. switch(param)
  173. {
  174. default:
  175. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  176. }
  177. done:
  178. ALCcontext_DecRef(context);
  179. }
  180. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat value)
  181. {
  182. ALCcontext *context;
  183. ALeffectslot *slot;
  184. context = GetContextRef();
  185. if(!context) return;
  186. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  187. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  188. switch(param)
  189. {
  190. case AL_EFFECTSLOT_GAIN:
  191. if(!(value >= 0.0f && value <= 1.0f))
  192. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  193. slot->Gain = value;
  194. slot->NeedsUpdate = AL_TRUE;
  195. break;
  196. default:
  197. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  198. }
  199. done:
  200. ALCcontext_DecRef(context);
  201. }
  202. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, const ALfloat *values)
  203. {
  204. ALCcontext *context;
  205. switch(param)
  206. {
  207. case AL_EFFECTSLOT_GAIN:
  208. alAuxiliaryEffectSlotf(effectslot, param, values[0]);
  209. return;
  210. }
  211. context = GetContextRef();
  212. if(!context) return;
  213. if(LookupEffectSlot(context, effectslot) == NULL)
  214. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  215. switch(param)
  216. {
  217. default:
  218. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  219. }
  220. done:
  221. ALCcontext_DecRef(context);
  222. }
  223. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *value)
  224. {
  225. ALCcontext *context;
  226. ALeffectslot *slot;
  227. context = GetContextRef();
  228. if(!context) return;
  229. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  230. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  231. switch(param)
  232. {
  233. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  234. *value = slot->AuxSendAuto;
  235. break;
  236. default:
  237. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  238. }
  239. done:
  240. ALCcontext_DecRef(context);
  241. }
  242. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *values)
  243. {
  244. ALCcontext *context;
  245. switch(param)
  246. {
  247. case AL_EFFECTSLOT_EFFECT:
  248. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  249. alGetAuxiliaryEffectSloti(effectslot, param, values);
  250. return;
  251. }
  252. context = GetContextRef();
  253. if(!context) return;
  254. if(LookupEffectSlot(context, effectslot) == NULL)
  255. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  256. switch(param)
  257. {
  258. default:
  259. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  260. }
  261. done:
  262. ALCcontext_DecRef(context);
  263. }
  264. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *value)
  265. {
  266. ALCcontext *context;
  267. ALeffectslot *slot;
  268. context = GetContextRef();
  269. if(!context) return;
  270. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  271. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  272. switch(param)
  273. {
  274. case AL_EFFECTSLOT_GAIN:
  275. *value = slot->Gain;
  276. break;
  277. default:
  278. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  279. }
  280. done:
  281. ALCcontext_DecRef(context);
  282. }
  283. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *values)
  284. {
  285. ALCcontext *context;
  286. switch(param)
  287. {
  288. case AL_EFFECTSLOT_GAIN:
  289. alGetAuxiliaryEffectSlotf(effectslot, param, values);
  290. return;
  291. }
  292. context = GetContextRef();
  293. if(!context) return;
  294. if(LookupEffectSlot(context, effectslot) == NULL)
  295. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  296. switch(param)
  297. {
  298. default:
  299. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  300. }
  301. done:
  302. ALCcontext_DecRef(context);
  303. }
  304. static ALvoid RemoveEffectSlotArray(ALCcontext *context, ALeffectslot *slot)
  305. {
  306. ALeffectslot **slotlist, **slotlistend;
  307. LockContext(context);
  308. slotlist = context->ActiveEffectSlots;
  309. slotlistend = slotlist + context->ActiveEffectSlotCount;
  310. while(slotlist != slotlistend)
  311. {
  312. if(*slotlist == slot)
  313. {
  314. *slotlist = *(--slotlistend);
  315. context->ActiveEffectSlotCount--;
  316. break;
  317. }
  318. slotlist++;
  319. }
  320. UnlockContext(context);
  321. }
  322. static ALenum AddEffectSlotArray(ALCcontext *context, ALsizei count, const ALuint *slots)
  323. {
  324. ALsizei i;
  325. LockContext(context);
  326. if(count > context->MaxActiveEffectSlots-context->ActiveEffectSlotCount)
  327. {
  328. ALsizei newcount;
  329. void *temp = NULL;
  330. newcount = context->MaxActiveEffectSlots ? (context->MaxActiveEffectSlots<<1) : 1;
  331. if(newcount > context->MaxActiveEffectSlots)
  332. temp = realloc(context->ActiveEffectSlots,
  333. newcount * sizeof(*context->ActiveEffectSlots));
  334. if(!temp)
  335. {
  336. UnlockContext(context);
  337. return AL_OUT_OF_MEMORY;
  338. }
  339. context->ActiveEffectSlots = temp;
  340. context->MaxActiveEffectSlots = newcount;
  341. }
  342. for(i = 0;i < count;i++)
  343. {
  344. ALeffectslot *slot = LookupEffectSlot(context, slots[i]);
  345. assert(slot != NULL);
  346. context->ActiveEffectSlots[context->ActiveEffectSlotCount++] = slot;
  347. }
  348. UnlockContext(context);
  349. return AL_NO_ERROR;
  350. }
  351. void InitEffectFactoryMap(void)
  352. {
  353. InitUIntMap(&EffectStateFactoryMap, ~0);
  354. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_NULL, ALnullStateFactory_getFactory);
  355. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_EAXREVERB, ALreverbStateFactory_getFactory);
  356. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_REVERB, ALreverbStateFactory_getFactory);
  357. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_AUTOWAH, ALautowahStateFactory_getFactory);
  358. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_CHORUS, ALchorusStateFactory_getFactory);
  359. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_COMPRESSOR, ALcompressorStateFactory_getFactory);
  360. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DISTORTION, ALdistortionStateFactory_getFactory);
  361. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_ECHO, ALechoStateFactory_getFactory);
  362. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_EQUALIZER, ALequalizerStateFactory_getFactory);
  363. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_FLANGER, ALflangerStateFactory_getFactory);
  364. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_RING_MODULATOR, ALmodulatorStateFactory_getFactory);
  365. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DEDICATED_DIALOGUE, ALdedicatedStateFactory_getFactory);
  366. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT, ALdedicatedStateFactory_getFactory);
  367. }
  368. void DeinitEffectFactoryMap(void)
  369. {
  370. ResetUIntMap(&EffectStateFactoryMap);
  371. }
  372. ALenum InitializeEffect(ALCdevice *Device, ALeffectslot *EffectSlot, ALeffect *effect)
  373. {
  374. ALenum newtype = (effect ? effect->type : AL_EFFECT_NULL);
  375. ALeffectStateFactory *factory;
  376. if(newtype != EffectSlot->EffectType)
  377. {
  378. ALeffectState *State;
  379. FPUCtl oldMode;
  380. factory = getFactoryByType(newtype);
  381. if(!factory)
  382. {
  383. ERR("Failed to find factory for effect type 0x%04x\n", newtype);
  384. return AL_INVALID_ENUM;
  385. }
  386. State = V0(factory,create)();
  387. if(!State)
  388. return AL_OUT_OF_MEMORY;
  389. SetMixerFPUMode(&oldMode);
  390. ALCdevice_Lock(Device);
  391. if(V(State,deviceUpdate)(Device) == AL_FALSE)
  392. {
  393. ALCdevice_Unlock(Device);
  394. RestoreFPUMode(&oldMode);
  395. DELETE_OBJ(State);
  396. return AL_OUT_OF_MEMORY;
  397. }
  398. State = ExchangePtr((XchgPtr*)&EffectSlot->EffectState, State);
  399. if(!effect)
  400. {
  401. memset(&EffectSlot->EffectProps, 0, sizeof(EffectSlot->EffectProps));
  402. EffectSlot->EffectType = AL_EFFECT_NULL;
  403. }
  404. else
  405. {
  406. memcpy(&EffectSlot->EffectProps, &effect->Props, sizeof(effect->Props));
  407. EffectSlot->EffectType = effect->type;
  408. }
  409. /* FIXME: This should be done asynchronously, but since the EffectState
  410. * object was changed, it needs an update before its Process method can
  411. * be called. */
  412. EffectSlot->NeedsUpdate = AL_FALSE;
  413. V(EffectSlot->EffectState,update)(Device, EffectSlot);
  414. ALCdevice_Unlock(Device);
  415. RestoreFPUMode(&oldMode);
  416. DELETE_OBJ(State);
  417. State = NULL;
  418. }
  419. else
  420. {
  421. if(effect)
  422. {
  423. ALCdevice_Lock(Device);
  424. memcpy(&EffectSlot->EffectProps, &effect->Props, sizeof(effect->Props));
  425. ALCdevice_Unlock(Device);
  426. EffectSlot->NeedsUpdate = AL_TRUE;
  427. }
  428. }
  429. return AL_NO_ERROR;
  430. }
  431. ALenum InitEffectSlot(ALeffectslot *slot)
  432. {
  433. ALeffectStateFactory *factory;
  434. ALuint i, c;
  435. slot->EffectType = AL_EFFECT_NULL;
  436. factory = getFactoryByType(AL_EFFECT_NULL);
  437. if(!(slot->EffectState=V0(factory,create)()))
  438. return AL_OUT_OF_MEMORY;
  439. slot->Gain = 1.0;
  440. slot->AuxSendAuto = AL_TRUE;
  441. slot->NeedsUpdate = AL_FALSE;
  442. for(c = 0;c < 1;c++)
  443. {
  444. for(i = 0;i < BUFFERSIZE;i++)
  445. slot->WetBuffer[c][i] = 0.0f;
  446. slot->ClickRemoval[c] = 0.0f;
  447. slot->PendingClicks[c] = 0.0f;
  448. }
  449. slot->ref = 0;
  450. return AL_NO_ERROR;
  451. }
  452. ALvoid ReleaseALAuxiliaryEffectSlots(ALCcontext *Context)
  453. {
  454. ALsizei pos;
  455. for(pos = 0;pos < Context->EffectSlotMap.size;pos++)
  456. {
  457. ALeffectslot *temp = Context->EffectSlotMap.array[pos].value;
  458. Context->EffectSlotMap.array[pos].value = NULL;
  459. DELETE_OBJ(temp->EffectState);
  460. FreeThunkEntry(temp->id);
  461. memset(temp, 0, sizeof(ALeffectslot));
  462. al_free(temp);
  463. }
  464. }