alFilter.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "AL/al.h"
  23. #include "AL/alc.h"
  24. #include "alMain.h"
  25. #include "alFilter.h"
  26. #include "alThunk.h"
  27. #include "alError.h"
  28. static void InitFilterParams(ALfilter *filter, ALenum type);
  29. AL_API ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
  30. {
  31. ALCcontext *Context;
  32. ALsizei i;
  33. Context = GetContextRef();
  34. if(!Context) return;
  35. if(n < 0 || IsBadWritePtr((void*)filters, n * sizeof(ALuint)))
  36. alSetError(Context, AL_INVALID_VALUE);
  37. else
  38. {
  39. ALCdevice *device = Context->Device;
  40. ALenum err;
  41. for(i = 0;i < n;i++)
  42. {
  43. ALfilter *filter = calloc(1, sizeof(ALfilter));
  44. if(!filter)
  45. {
  46. alSetError(Context, AL_OUT_OF_MEMORY);
  47. alDeleteFilters(i, filters);
  48. break;
  49. }
  50. InitFilterParams(filter, AL_FILTER_NULL);
  51. err = NewThunkEntry(&filter->filter);
  52. if(err == AL_NO_ERROR)
  53. err = InsertUIntMapEntry(&device->FilterMap, filter->filter, filter);
  54. if(err != AL_NO_ERROR)
  55. {
  56. FreeThunkEntry(filter->filter);
  57. memset(filter, 0, sizeof(ALfilter));
  58. free(filter);
  59. alSetError(Context, err);
  60. alDeleteFilters(i, filters);
  61. break;
  62. }
  63. filters[i] = filter->filter;
  64. }
  65. }
  66. ALCcontext_DecRef(Context);
  67. }
  68. AL_API ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, const ALuint *filters)
  69. {
  70. ALCcontext *Context;
  71. ALCdevice *device;
  72. ALfilter *ALFilter;
  73. ALsizei i;
  74. Context = GetContextRef();
  75. if(!Context) return;
  76. device = Context->Device;
  77. if(n < 0)
  78. alSetError(Context, AL_INVALID_VALUE);
  79. else
  80. {
  81. // Check that all filters are valid
  82. for(i = 0;i < n;i++)
  83. {
  84. if(!filters[i])
  85. continue;
  86. if(LookupFilter(device, filters[i]) == NULL)
  87. {
  88. alSetError(Context, AL_INVALID_NAME);
  89. n = 0;
  90. break;
  91. }
  92. }
  93. for(i = 0;i < n;i++)
  94. {
  95. // Recheck that the filter is valid, because there could be duplicated names
  96. if((ALFilter=RemoveFilter(device, filters[i])) == NULL)
  97. continue;
  98. FreeThunkEntry(ALFilter->filter);
  99. memset(ALFilter, 0, sizeof(ALfilter));
  100. free(ALFilter);
  101. }
  102. }
  103. ALCcontext_DecRef(Context);
  104. }
  105. AL_API ALboolean AL_APIENTRY alIsFilter(ALuint filter)
  106. {
  107. ALCcontext *Context;
  108. ALboolean result;
  109. Context = GetContextRef();
  110. if(!Context) return AL_FALSE;
  111. result = ((!filter || LookupFilter(Context->Device, filter)) ?
  112. AL_TRUE : AL_FALSE);
  113. ALCcontext_DecRef(Context);
  114. return result;
  115. }
  116. AL_API ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint iValue)
  117. {
  118. ALCcontext *Context;
  119. ALCdevice *Device;
  120. ALfilter *ALFilter;
  121. Context = GetContextRef();
  122. if(!Context) return;
  123. Device = Context->Device;
  124. if((ALFilter=LookupFilter(Device, filter)) != NULL)
  125. {
  126. switch(param)
  127. {
  128. case AL_FILTER_TYPE:
  129. if(iValue == AL_FILTER_NULL || iValue == AL_FILTER_LOWPASS)
  130. InitFilterParams(ALFilter, iValue);
  131. else
  132. alSetError(Context, AL_INVALID_VALUE);
  133. break;
  134. default:
  135. /* Call the appropriate handler */
  136. ALfilter_SetParami(ALFilter, Context, param, iValue);
  137. break;
  138. }
  139. }
  140. else
  141. alSetError(Context, AL_INVALID_NAME);
  142. ALCcontext_DecRef(Context);
  143. }
  144. AL_API ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, const ALint *piValues)
  145. {
  146. ALCcontext *Context;
  147. ALCdevice *Device;
  148. ALfilter *ALFilter;
  149. switch(param)
  150. {
  151. case AL_FILTER_TYPE:
  152. alFilteri(filter, param, piValues[0]);
  153. return;
  154. }
  155. Context = GetContextRef();
  156. if(!Context) return;
  157. Device = Context->Device;
  158. if((ALFilter=LookupFilter(Device, filter)) != NULL)
  159. {
  160. /* Call the appropriate handler */
  161. ALfilter_SetParamiv(ALFilter, Context, param, piValues);
  162. }
  163. else
  164. alSetError(Context, AL_INVALID_NAME);
  165. ALCcontext_DecRef(Context);
  166. }
  167. AL_API ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat flValue)
  168. {
  169. ALCcontext *Context;
  170. ALCdevice *Device;
  171. ALfilter *ALFilter;
  172. Context = GetContextRef();
  173. if(!Context) return;
  174. Device = Context->Device;
  175. if((ALFilter=LookupFilter(Device, filter)) != NULL)
  176. {
  177. /* Call the appropriate handler */
  178. ALfilter_SetParamf(ALFilter, Context, param, flValue);
  179. }
  180. else
  181. alSetError(Context, AL_INVALID_NAME);
  182. ALCcontext_DecRef(Context);
  183. }
  184. AL_API ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, const ALfloat *pflValues)
  185. {
  186. ALCcontext *Context;
  187. ALCdevice *Device;
  188. ALfilter *ALFilter;
  189. Context = GetContextRef();
  190. if(!Context) return;
  191. Device = Context->Device;
  192. if((ALFilter=LookupFilter(Device, filter)) != NULL)
  193. {
  194. /* Call the appropriate handler */
  195. ALfilter_SetParamfv(ALFilter, Context, param, pflValues);
  196. }
  197. else
  198. alSetError(Context, AL_INVALID_NAME);
  199. ALCcontext_DecRef(Context);
  200. }
  201. AL_API ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *piValue)
  202. {
  203. ALCcontext *Context;
  204. ALCdevice *Device;
  205. ALfilter *ALFilter;
  206. Context = GetContextRef();
  207. if(!Context) return;
  208. Device = Context->Device;
  209. if((ALFilter=LookupFilter(Device, filter)) != NULL)
  210. {
  211. switch(param)
  212. {
  213. case AL_FILTER_TYPE:
  214. *piValue = ALFilter->type;
  215. break;
  216. default:
  217. /* Call the appropriate handler */
  218. ALfilter_GetParami(ALFilter, Context, param, piValue);
  219. break;
  220. }
  221. }
  222. else
  223. alSetError(Context, AL_INVALID_NAME);
  224. ALCcontext_DecRef(Context);
  225. }
  226. AL_API ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *piValues)
  227. {
  228. ALCcontext *Context;
  229. ALCdevice *Device;
  230. ALfilter *ALFilter;
  231. switch(param)
  232. {
  233. case AL_FILTER_TYPE:
  234. alGetFilteri(filter, param, piValues);
  235. return;
  236. }
  237. Context = GetContextRef();
  238. if(!Context) return;
  239. Device = Context->Device;
  240. if((ALFilter=LookupFilter(Device, filter)) != NULL)
  241. {
  242. /* Call the appropriate handler */
  243. ALfilter_GetParamiv(ALFilter, Context, param, piValues);
  244. }
  245. else
  246. alSetError(Context, AL_INVALID_NAME);
  247. ALCcontext_DecRef(Context);
  248. }
  249. AL_API ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *pflValue)
  250. {
  251. ALCcontext *Context;
  252. ALCdevice *Device;
  253. ALfilter *ALFilter;
  254. Context = GetContextRef();
  255. if(!Context) return;
  256. Device = Context->Device;
  257. if((ALFilter=LookupFilter(Device, filter)) != NULL)
  258. {
  259. /* Call the appropriate handler */
  260. ALfilter_GetParamf(ALFilter, Context, param, pflValue);
  261. }
  262. else
  263. alSetError(Context, AL_INVALID_NAME);
  264. ALCcontext_DecRef(Context);
  265. }
  266. AL_API ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
  267. {
  268. ALCcontext *Context;
  269. ALCdevice *Device;
  270. ALfilter *ALFilter;
  271. Context = GetContextRef();
  272. if(!Context) return;
  273. Device = Context->Device;
  274. if((ALFilter=LookupFilter(Device, filter)) != NULL)
  275. {
  276. /* Call the appropriate handler */
  277. ALfilter_GetParamfv(ALFilter, Context, param, pflValues);
  278. }
  279. else
  280. alSetError(Context, AL_INVALID_NAME);
  281. ALCcontext_DecRef(Context);
  282. }
  283. ALfloat lpCoeffCalc(ALfloat g, ALfloat cw)
  284. {
  285. ALfloat a = 0.0f;
  286. /* Be careful with gains < 0.001, as that causes the coefficient head
  287. * towards 1, which will flatten the signal */
  288. if(g < 0.9999f) /* 1-epsilon */
  289. {
  290. g = maxf(g, 0.001f);
  291. a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) /
  292. (1 - g);
  293. }
  294. return a;
  295. }
  296. static void lp_SetParami(ALfilter *filter, ALCcontext *context, ALenum param, ALint val)
  297. { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
  298. static void lp_SetParamiv(ALfilter *filter, ALCcontext *context, ALenum param, const ALint *vals)
  299. { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
  300. static void lp_SetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val)
  301. {
  302. switch(param)
  303. {
  304. case AL_LOWPASS_GAIN:
  305. if(val >= AL_LOWPASS_MIN_GAIN && val <= AL_LOWPASS_MAX_GAIN)
  306. filter->Gain = val;
  307. else
  308. alSetError(context, AL_INVALID_VALUE);
  309. break;
  310. case AL_LOWPASS_GAINHF:
  311. if(val >= AL_LOWPASS_MIN_GAINHF && val <= AL_LOWPASS_MAX_GAINHF)
  312. filter->GainHF = val;
  313. else
  314. alSetError(context, AL_INVALID_VALUE);
  315. break;
  316. default:
  317. alSetError(context, AL_INVALID_ENUM);
  318. break;
  319. }
  320. }
  321. static void lp_SetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals)
  322. {
  323. lp_SetParamf(filter, context, param, vals[0]);
  324. }
  325. static void lp_GetParami(ALfilter *filter, ALCcontext *context, ALenum param, ALint *val)
  326. { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
  327. static void lp_GetParamiv(ALfilter *filter, ALCcontext *context, ALenum param, ALint *vals)
  328. { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
  329. static void lp_GetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val)
  330. {
  331. switch(param)
  332. {
  333. case AL_LOWPASS_GAIN:
  334. *val = filter->Gain;
  335. break;
  336. case AL_LOWPASS_GAINHF:
  337. *val = filter->GainHF;
  338. break;
  339. default:
  340. alSetError(context, AL_INVALID_ENUM);
  341. break;
  342. }
  343. }
  344. static void lp_GetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals)
  345. {
  346. lp_GetParamf(filter, context, param, vals);
  347. }
  348. static void null_SetParami(ALfilter *filter, ALCcontext *context, ALenum param, ALint val)
  349. { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
  350. static void null_SetParamiv(ALfilter *filter, ALCcontext *context, ALenum param, const ALint *vals)
  351. { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
  352. static void null_SetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val)
  353. { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
  354. static void null_SetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals)
  355. { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
  356. static void null_GetParami(ALfilter *filter, ALCcontext *context, ALenum param, ALint *val)
  357. { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
  358. static void null_GetParamiv(ALfilter *filter, ALCcontext *context, ALenum param, ALint *vals)
  359. { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
  360. static void null_GetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val)
  361. { (void)filter;(void)param;(void)val; alSetError(context, AL_INVALID_ENUM); }
  362. static void null_GetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals)
  363. { (void)filter;(void)param;(void)vals; alSetError(context, AL_INVALID_ENUM); }
  364. ALvoid ReleaseALFilters(ALCdevice *device)
  365. {
  366. ALsizei i;
  367. for(i = 0;i < device->FilterMap.size;i++)
  368. {
  369. ALfilter *temp = device->FilterMap.array[i].value;
  370. device->FilterMap.array[i].value = NULL;
  371. // Release filter structure
  372. FreeThunkEntry(temp->filter);
  373. memset(temp, 0, sizeof(ALfilter));
  374. free(temp);
  375. }
  376. }
  377. static void InitFilterParams(ALfilter *filter, ALenum type)
  378. {
  379. if(type == AL_FILTER_LOWPASS)
  380. {
  381. filter->Gain = AL_LOWPASS_DEFAULT_GAIN;
  382. filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF;
  383. filter->SetParami = lp_SetParami;
  384. filter->SetParamiv = lp_SetParamiv;
  385. filter->SetParamf = lp_SetParamf;
  386. filter->SetParamfv = lp_SetParamfv;
  387. filter->GetParami = lp_GetParami;
  388. filter->GetParamiv = lp_GetParamiv;
  389. filter->GetParamf = lp_GetParamf;
  390. filter->GetParamfv = lp_GetParamfv;
  391. }
  392. else
  393. {
  394. filter->SetParami = null_SetParami;
  395. filter->SetParamiv = null_SetParamiv;
  396. filter->SetParamf = null_SetParamf;
  397. filter->SetParamfv = null_SetParamfv;
  398. filter->GetParami = null_GetParami;
  399. filter->GetParamiv = null_GetParamiv;
  400. filter->GetParamf = null_GetParamf;
  401. filter->GetParamfv = null_GetParamfv;
  402. }
  403. filter->type = type;
  404. }