alAuxEffectSlot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. static ALenum ResizeEffectSlotArray(ALCcontext *Context, ALsizei count);
  31. static ALvoid RemoveEffectSlotArray(ALCcontext *Context, ALeffectslot *val);
  32. AL_API ALvoid AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
  33. {
  34. ALCcontext *Context;
  35. Context = GetContextRef();
  36. if(!Context) return;
  37. if(n < 0 || IsBadWritePtr((void*)effectslots, n * sizeof(ALuint)))
  38. alSetError(Context, AL_INVALID_VALUE);
  39. else
  40. {
  41. ALenum err;
  42. ALsizei i;
  43. err = ResizeEffectSlotArray(Context, n);
  44. if(err != AL_NO_ERROR)
  45. {
  46. alSetError(Context, err);
  47. n = 0;
  48. }
  49. for(i = 0;i < n;i++)
  50. {
  51. ALeffectslot *slot = calloc(1, sizeof(ALeffectslot));
  52. if(!slot || InitEffectSlot(slot) != AL_NO_ERROR)
  53. {
  54. free(slot);
  55. // We must have run out or memory
  56. alSetError(Context, AL_OUT_OF_MEMORY);
  57. alDeleteAuxiliaryEffectSlots(i, effectslots);
  58. break;
  59. }
  60. LockContext(Context);
  61. err = ResizeEffectSlotArray(Context, 1);
  62. if(err == AL_NO_ERROR)
  63. Context->ActiveEffectSlots[Context->ActiveEffectSlotCount++] = slot;
  64. UnlockContext(Context);
  65. if(err == AL_NO_ERROR)
  66. err = NewThunkEntry(&slot->effectslot);
  67. if(err == AL_NO_ERROR)
  68. err = InsertUIntMapEntry(&Context->EffectSlotMap, slot->effectslot, slot);
  69. if(err != AL_NO_ERROR)
  70. {
  71. RemoveEffectSlotArray(Context, slot);
  72. FreeThunkEntry(slot->effectslot);
  73. ALeffectState_Destroy(slot->EffectState);
  74. free(slot);
  75. alSetError(Context, err);
  76. alDeleteAuxiliaryEffectSlots(i, effectslots);
  77. break;
  78. }
  79. effectslots[i] = slot->effectslot;
  80. }
  81. }
  82. ALCcontext_DecRef(Context);
  83. }
  84. AL_API ALvoid AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, const ALuint *effectslots)
  85. {
  86. ALCcontext *Context;
  87. ALeffectslot *EffectSlot;
  88. ALsizei i;
  89. Context = GetContextRef();
  90. if(!Context) return;
  91. if(n < 0)
  92. alSetError(Context, AL_INVALID_VALUE);
  93. else
  94. {
  95. // Check that all effectslots are valid
  96. for(i = 0;i < n;i++)
  97. {
  98. if((EffectSlot=LookupEffectSlot(Context, effectslots[i])) == NULL)
  99. {
  100. alSetError(Context, AL_INVALID_NAME);
  101. n = 0;
  102. break;
  103. }
  104. else if(EffectSlot->ref != 0)
  105. {
  106. alSetError(Context, AL_INVALID_OPERATION);
  107. n = 0;
  108. break;
  109. }
  110. }
  111. // All effectslots are valid
  112. for(i = 0;i < n;i++)
  113. {
  114. // Recheck that the effectslot is valid, because there could be duplicated names
  115. if((EffectSlot=RemoveEffectSlot(Context, effectslots[i])) == NULL)
  116. continue;
  117. FreeThunkEntry(EffectSlot->effectslot);
  118. RemoveEffectSlotArray(Context, EffectSlot);
  119. ALeffectState_Destroy(EffectSlot->EffectState);
  120. memset(EffectSlot, 0, sizeof(ALeffectslot));
  121. free(EffectSlot);
  122. }
  123. }
  124. ALCcontext_DecRef(Context);
  125. }
  126. AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
  127. {
  128. ALCcontext *Context;
  129. ALboolean result;
  130. Context = GetContextRef();
  131. if(!Context) return AL_FALSE;
  132. result = (LookupEffectSlot(Context, effectslot) ? AL_TRUE : AL_FALSE);
  133. ALCcontext_DecRef(Context);
  134. return result;
  135. }
  136. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint iValue)
  137. {
  138. ALCdevice *Device;
  139. ALCcontext *Context;
  140. ALeffectslot *EffectSlot;
  141. Context = GetContextRef();
  142. if(!Context) return;
  143. Device = Context->Device;
  144. if((EffectSlot=LookupEffectSlot(Context, effectslot)) != NULL)
  145. {
  146. switch(param)
  147. {
  148. case AL_EFFECTSLOT_EFFECT: {
  149. ALeffect *effect = NULL;
  150. if(iValue == 0 ||
  151. (effect=LookupEffect(Device, iValue)) != NULL)
  152. {
  153. ALenum err;
  154. err = InitializeEffect(Device, EffectSlot, effect);
  155. if(err != AL_NO_ERROR)
  156. alSetError(Context, err);
  157. else
  158. Context->UpdateSources = AL_TRUE;
  159. }
  160. else
  161. alSetError(Context, AL_INVALID_VALUE);
  162. } break;
  163. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  164. if(iValue == AL_TRUE || iValue == AL_FALSE)
  165. {
  166. EffectSlot->AuxSendAuto = iValue;
  167. Context->UpdateSources = AL_TRUE;
  168. }
  169. else
  170. alSetError(Context, AL_INVALID_VALUE);
  171. break;
  172. default:
  173. alSetError(Context, AL_INVALID_ENUM);
  174. break;
  175. }
  176. }
  177. else
  178. alSetError(Context, AL_INVALID_NAME);
  179. ALCcontext_DecRef(Context);
  180. }
  181. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, const ALint *piValues)
  182. {
  183. ALCcontext *Context;
  184. switch(param)
  185. {
  186. case AL_EFFECTSLOT_EFFECT:
  187. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  188. alAuxiliaryEffectSloti(effectslot, param, piValues[0]);
  189. return;
  190. }
  191. Context = GetContextRef();
  192. if(!Context) return;
  193. if(LookupEffectSlot(Context, effectslot) != NULL)
  194. {
  195. switch(param)
  196. {
  197. default:
  198. alSetError(Context, AL_INVALID_ENUM);
  199. break;
  200. }
  201. }
  202. else
  203. alSetError(Context, AL_INVALID_NAME);
  204. ALCcontext_DecRef(Context);
  205. }
  206. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat flValue)
  207. {
  208. ALCcontext *Context;
  209. ALeffectslot *EffectSlot;
  210. Context = GetContextRef();
  211. if(!Context) return;
  212. if((EffectSlot=LookupEffectSlot(Context, effectslot)) != NULL)
  213. {
  214. switch(param)
  215. {
  216. case AL_EFFECTSLOT_GAIN:
  217. if(flValue >= 0.0f && flValue <= 1.0f)
  218. {
  219. EffectSlot->Gain = flValue;
  220. EffectSlot->NeedsUpdate = AL_TRUE;
  221. }
  222. else
  223. alSetError(Context, AL_INVALID_VALUE);
  224. break;
  225. default:
  226. alSetError(Context, AL_INVALID_ENUM);
  227. break;
  228. }
  229. }
  230. else
  231. alSetError(Context, AL_INVALID_NAME);
  232. ALCcontext_DecRef(Context);
  233. }
  234. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, const ALfloat *pflValues)
  235. {
  236. ALCcontext *Context;
  237. switch(param)
  238. {
  239. case AL_EFFECTSLOT_GAIN:
  240. alAuxiliaryEffectSlotf(effectslot, param, pflValues[0]);
  241. return;
  242. }
  243. Context = GetContextRef();
  244. if(!Context) return;
  245. if(LookupEffectSlot(Context, effectslot) != NULL)
  246. {
  247. switch(param)
  248. {
  249. default:
  250. alSetError(Context, AL_INVALID_ENUM);
  251. break;
  252. }
  253. }
  254. else
  255. alSetError(Context, AL_INVALID_NAME);
  256. ALCcontext_DecRef(Context);
  257. }
  258. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *piValue)
  259. {
  260. ALCcontext *Context;
  261. ALeffectslot *EffectSlot;
  262. Context = GetContextRef();
  263. if(!Context) return;
  264. if((EffectSlot=LookupEffectSlot(Context, effectslot)) != NULL)
  265. {
  266. switch(param)
  267. {
  268. case AL_EFFECTSLOT_EFFECT:
  269. *piValue = EffectSlot->effect.effect;
  270. break;
  271. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  272. *piValue = EffectSlot->AuxSendAuto;
  273. break;
  274. default:
  275. alSetError(Context, AL_INVALID_ENUM);
  276. break;
  277. }
  278. }
  279. else
  280. alSetError(Context, AL_INVALID_NAME);
  281. ALCcontext_DecRef(Context);
  282. }
  283. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *piValues)
  284. {
  285. ALCcontext *Context;
  286. switch(param)
  287. {
  288. case AL_EFFECTSLOT_EFFECT:
  289. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  290. alGetAuxiliaryEffectSloti(effectslot, param, piValues);
  291. return;
  292. }
  293. Context = GetContextRef();
  294. if(!Context) return;
  295. if(LookupEffectSlot(Context, effectslot) != NULL)
  296. {
  297. switch(param)
  298. {
  299. default:
  300. alSetError(Context, AL_INVALID_ENUM);
  301. break;
  302. }
  303. }
  304. else
  305. alSetError(Context, AL_INVALID_NAME);
  306. ALCcontext_DecRef(Context);
  307. }
  308. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *pflValue)
  309. {
  310. ALCcontext *Context;
  311. ALeffectslot *EffectSlot;
  312. Context = GetContextRef();
  313. if(!Context) return;
  314. if((EffectSlot=LookupEffectSlot(Context, effectslot)) != NULL)
  315. {
  316. switch(param)
  317. {
  318. case AL_EFFECTSLOT_GAIN:
  319. *pflValue = EffectSlot->Gain;
  320. break;
  321. default:
  322. alSetError(Context, AL_INVALID_ENUM);
  323. break;
  324. }
  325. }
  326. else
  327. alSetError(Context, AL_INVALID_NAME);
  328. ALCcontext_DecRef(Context);
  329. }
  330. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *pflValues)
  331. {
  332. ALCcontext *Context;
  333. switch(param)
  334. {
  335. case AL_EFFECTSLOT_GAIN:
  336. alGetAuxiliaryEffectSlotf(effectslot, param, pflValues);
  337. return;
  338. }
  339. Context = GetContextRef();
  340. if(!Context) return;
  341. if(LookupEffectSlot(Context, effectslot) != NULL)
  342. {
  343. switch(param)
  344. {
  345. default:
  346. alSetError(Context, AL_INVALID_ENUM);
  347. break;
  348. }
  349. }
  350. else
  351. alSetError(Context, AL_INVALID_NAME);
  352. ALCcontext_DecRef(Context);
  353. }
  354. static ALvoid NoneDestroy(ALeffectState *State)
  355. { free(State); }
  356. static ALboolean NoneDeviceUpdate(ALeffectState *State, ALCdevice *Device)
  357. {
  358. return AL_TRUE;
  359. (void)State;
  360. (void)Device;
  361. }
  362. static ALvoid NoneUpdate(ALeffectState *State, ALCdevice *Device, const ALeffectslot *Slot)
  363. {
  364. (void)State;
  365. (void)Device;
  366. (void)Slot;
  367. }
  368. static ALvoid NoneProcess(ALeffectState *State, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
  369. {
  370. (void)State;
  371. (void)SamplesToDo;
  372. (void)SamplesIn;
  373. (void)SamplesOut;
  374. }
  375. ALeffectState *NoneCreate(void)
  376. {
  377. ALeffectState *state;
  378. state = calloc(1, sizeof(*state));
  379. if(!state)
  380. return NULL;
  381. state->Destroy = NoneDestroy;
  382. state->DeviceUpdate = NoneDeviceUpdate;
  383. state->Update = NoneUpdate;
  384. state->Process = NoneProcess;
  385. return state;
  386. }
  387. static ALvoid RemoveEffectSlotArray(ALCcontext *Context, ALeffectslot *slot)
  388. {
  389. ALeffectslot **slotlist, **slotlistend;
  390. LockContext(Context);
  391. slotlist = Context->ActiveEffectSlots;
  392. slotlistend = slotlist + Context->ActiveEffectSlotCount;
  393. while(slotlist != slotlistend)
  394. {
  395. if(*slotlist == slot)
  396. {
  397. *slotlist = *(--slotlistend);
  398. Context->ActiveEffectSlotCount--;
  399. break;
  400. }
  401. slotlist++;
  402. }
  403. UnlockContext(Context);
  404. }
  405. static ALenum ResizeEffectSlotArray(ALCcontext *Context, ALsizei count)
  406. {
  407. ALsizei newcount;
  408. void *temp;
  409. if(count <= Context->MaxActiveEffectSlots-Context->ActiveEffectSlotCount)
  410. return AL_NO_ERROR;
  411. newcount = Context->MaxActiveEffectSlots ?
  412. (Context->MaxActiveEffectSlots<<1) : 1;
  413. if(newcount <= Context->MaxActiveEffectSlots ||
  414. !(temp=realloc(Context->ActiveEffectSlots, newcount *
  415. sizeof(*Context->ActiveEffectSlots))))
  416. return AL_OUT_OF_MEMORY;
  417. Context->ActiveEffectSlots = temp;
  418. Context->MaxActiveEffectSlots = newcount;
  419. return AL_NO_ERROR;
  420. }
  421. ALenum InitializeEffect(ALCdevice *Device, ALeffectslot *EffectSlot, ALeffect *effect)
  422. {
  423. ALenum newtype = (effect ? effect->type : AL_EFFECT_NULL);
  424. ALeffectState *State = NULL;
  425. ALenum err = AL_NO_ERROR;
  426. LockDevice(Device);
  427. if(newtype == AL_EFFECT_NULL && EffectSlot->effect.type != AL_EFFECT_NULL)
  428. {
  429. State = NoneCreate();
  430. if(!State) err = AL_OUT_OF_MEMORY;
  431. }
  432. else if(newtype == AL_EFFECT_EAXREVERB || newtype == AL_EFFECT_REVERB)
  433. {
  434. if(EffectSlot->effect.type != AL_EFFECT_EAXREVERB && EffectSlot->effect.type != AL_EFFECT_REVERB)
  435. {
  436. State = ReverbCreate();
  437. if(!State) err = AL_OUT_OF_MEMORY;
  438. }
  439. }
  440. else if(newtype == AL_EFFECT_ECHO && EffectSlot->effect.type != AL_EFFECT_ECHO)
  441. {
  442. State = EchoCreate();
  443. if(!State) err = AL_OUT_OF_MEMORY;
  444. }
  445. else if(newtype == AL_EFFECT_RING_MODULATOR && EffectSlot->effect.type != AL_EFFECT_RING_MODULATOR)
  446. {
  447. State = ModulatorCreate();
  448. if(!State) err = AL_OUT_OF_MEMORY;
  449. }
  450. else if(newtype == AL_EFFECT_DEDICATED_DIALOGUE || newtype == AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT)
  451. {
  452. if(EffectSlot->effect.type != AL_EFFECT_DEDICATED_DIALOGUE && EffectSlot->effect.type != AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT)
  453. {
  454. State = DedicatedCreate();
  455. if(!State) err = AL_OUT_OF_MEMORY;
  456. }
  457. }
  458. if(err != AL_NO_ERROR)
  459. {
  460. UnlockDevice(Device);
  461. return err;
  462. }
  463. if(State)
  464. {
  465. int oldMode;
  466. oldMode = SetMixerFPUMode();
  467. if(ALeffectState_DeviceUpdate(State, Device) == AL_FALSE)
  468. {
  469. RestoreFPUMode(oldMode);
  470. UnlockDevice(Device);
  471. ALeffectState_Destroy(State);
  472. return AL_OUT_OF_MEMORY;
  473. }
  474. State = ExchangePtr((XchgPtr*)&EffectSlot->EffectState, State);
  475. if(!effect)
  476. memset(&EffectSlot->effect, 0, sizeof(EffectSlot->effect));
  477. else
  478. memcpy(&EffectSlot->effect, effect, sizeof(*effect));
  479. /* FIXME: This should be done asynchronously, but since the EffectState
  480. * object was changed, it needs an update before its Process method can
  481. * be called. */
  482. EffectSlot->NeedsUpdate = AL_FALSE;
  483. ALeffectState_Update(EffectSlot->EffectState, Device, EffectSlot);
  484. UnlockDevice(Device);
  485. RestoreFPUMode(oldMode);
  486. ALeffectState_Destroy(State);
  487. State = NULL;
  488. }
  489. else
  490. {
  491. if(!effect)
  492. memset(&EffectSlot->effect, 0, sizeof(EffectSlot->effect));
  493. else
  494. memcpy(&EffectSlot->effect, effect, sizeof(*effect));
  495. UnlockDevice(Device);
  496. EffectSlot->NeedsUpdate = AL_TRUE;
  497. }
  498. return AL_NO_ERROR;
  499. }
  500. ALenum InitEffectSlot(ALeffectslot *slot)
  501. {
  502. ALint i;
  503. if(!(slot->EffectState=NoneCreate()))
  504. return AL_OUT_OF_MEMORY;
  505. slot->Gain = 1.0;
  506. slot->AuxSendAuto = AL_TRUE;
  507. slot->NeedsUpdate = AL_FALSE;
  508. for(i = 0;i < BUFFERSIZE;i++)
  509. slot->WetBuffer[i] = 0.0f;
  510. for(i = 0;i < 1;i++)
  511. {
  512. slot->ClickRemoval[i] = 0.0f;
  513. slot->PendingClicks[i] = 0.0f;
  514. }
  515. slot->ref = 0;
  516. return AL_NO_ERROR;
  517. }
  518. ALvoid ReleaseALAuxiliaryEffectSlots(ALCcontext *Context)
  519. {
  520. ALsizei pos;
  521. for(pos = 0;pos < Context->EffectSlotMap.size;pos++)
  522. {
  523. ALeffectslot *temp = Context->EffectSlotMap.array[pos].value;
  524. Context->EffectSlotMap.array[pos].value = NULL;
  525. // Release effectslot structure
  526. ALeffectState_Destroy(temp->EffectState);
  527. FreeThunkEntry(temp->effectslot);
  528. memset(temp, 0, sizeof(ALeffectslot));
  529. free(temp);
  530. }
  531. }