alFilter.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #define LookupFilter(m, k) ((ALfilter*)LookupUIntMapKey(&(m), (k)))
  30. AL_API ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
  31. {
  32. ALCcontext *Context;
  33. ALsizei i=0;
  34. Context = GetContextSuspended();
  35. if(!Context) return;
  36. if (n > 0)
  37. {
  38. ALCdevice *device = Context->Device;
  39. // Check that enough memory has been allocted in the 'filters' array for n Filters
  40. if (!IsBadWritePtr((void*)filters, n * sizeof(ALuint)))
  41. {
  42. ALenum err;
  43. while(i < n)
  44. {
  45. ALfilter *filter = calloc(1, sizeof(ALfilter));
  46. if(!filter)
  47. {
  48. alSetError(Context, AL_OUT_OF_MEMORY);
  49. alDeleteFilters(i, filters);
  50. break;
  51. }
  52. filter->filter = ALTHUNK_ADDENTRY(filter);
  53. err = InsertUIntMapEntry(&device->FilterMap, filter->filter,
  54. filter);
  55. if(err != AL_NO_ERROR)
  56. {
  57. ALTHUNK_REMOVEENTRY(filter->filter);
  58. memset(filter, 0, sizeof(ALfilter));
  59. free(filter);
  60. alSetError(Context, err);
  61. alDeleteFilters(i, filters);
  62. break;
  63. }
  64. filters[i++] = filter->filter;
  65. InitFilterParams(filter, AL_FILTER_NULL);
  66. }
  67. }
  68. }
  69. ProcessContext(Context);
  70. }
  71. AL_API ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, ALuint *filters)
  72. {
  73. ALCcontext *Context;
  74. ALfilter *ALFilter;
  75. ALsizei i;
  76. Context = GetContextSuspended();
  77. if(!Context) return;
  78. if (n >= 0)
  79. {
  80. ALCdevice *device = Context->Device;
  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->FilterMap, filters[i]))
  87. {
  88. alSetError(Context, AL_INVALID_NAME);
  89. break;
  90. }
  91. }
  92. if (i == n)
  93. {
  94. // All filters are valid
  95. for (i = 0; i < n; i++)
  96. {
  97. // Recheck that the filter is valid, because there could be duplicated names
  98. if((ALFilter=LookupFilter(device->FilterMap, filters[i])) != NULL)
  99. {
  100. RemoveUIntMapKey(&device->FilterMap, ALFilter->filter);
  101. ALTHUNK_REMOVEENTRY(ALFilter->filter);
  102. memset(ALFilter, 0, sizeof(ALfilter));
  103. free(ALFilter);
  104. }
  105. }
  106. }
  107. }
  108. else
  109. alSetError(Context, AL_INVALID_VALUE);
  110. ProcessContext(Context);
  111. }
  112. AL_API ALboolean AL_APIENTRY alIsFilter(ALuint filter)
  113. {
  114. ALCcontext *Context;
  115. ALboolean result;
  116. Context = GetContextSuspended();
  117. if(!Context) return AL_FALSE;
  118. result = ((!filter || LookupFilter(Context->Device->FilterMap, filter)) ?
  119. AL_TRUE : AL_FALSE);
  120. ProcessContext(Context);
  121. return result;
  122. }
  123. AL_API ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint iValue)
  124. {
  125. ALCcontext *Context;
  126. ALCdevice *Device;
  127. ALfilter *ALFilter;
  128. Context = GetContextSuspended();
  129. if(!Context) return;
  130. Device = Context->Device;
  131. if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
  132. {
  133. switch(param)
  134. {
  135. case AL_FILTER_TYPE:
  136. if(iValue == AL_FILTER_NULL ||
  137. iValue == AL_FILTER_LOWPASS)
  138. InitFilterParams(ALFilter, iValue);
  139. else
  140. alSetError(Context, AL_INVALID_VALUE);
  141. break;
  142. default:
  143. alSetError(Context, AL_INVALID_ENUM);
  144. break;
  145. }
  146. }
  147. else
  148. alSetError(Context, AL_INVALID_NAME);
  149. ProcessContext(Context);
  150. }
  151. AL_API ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, ALint *piValues)
  152. {
  153. ALCcontext *Context;
  154. ALCdevice *Device;
  155. Context = GetContextSuspended();
  156. if(!Context) return;
  157. Device = Context->Device;
  158. if(LookupFilter(Device->FilterMap, filter) != NULL)
  159. {
  160. switch(param)
  161. {
  162. case AL_FILTER_TYPE:
  163. alFilteri(filter, param, piValues[0]);
  164. break;
  165. default:
  166. alSetError(Context, AL_INVALID_ENUM);
  167. break;
  168. }
  169. }
  170. else
  171. alSetError(Context, AL_INVALID_NAME);
  172. ProcessContext(Context);
  173. }
  174. AL_API ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat flValue)
  175. {
  176. ALCcontext *Context;
  177. ALCdevice *Device;
  178. ALfilter *ALFilter;
  179. Context = GetContextSuspended();
  180. if(!Context) return;
  181. Device = Context->Device;
  182. if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
  183. {
  184. switch(ALFilter->type)
  185. {
  186. case AL_FILTER_LOWPASS:
  187. switch(param)
  188. {
  189. case AL_LOWPASS_GAIN:
  190. if(flValue >= 0.0f && flValue <= 1.0f)
  191. ALFilter->Gain = flValue;
  192. else
  193. alSetError(Context, AL_INVALID_VALUE);
  194. break;
  195. case AL_LOWPASS_GAINHF:
  196. if(flValue >= 0.0f && flValue <= 1.0f)
  197. ALFilter->GainHF = flValue;
  198. else
  199. alSetError(Context, AL_INVALID_VALUE);
  200. break;
  201. default:
  202. alSetError(Context, AL_INVALID_ENUM);
  203. break;
  204. }
  205. break;
  206. default:
  207. alSetError(Context, AL_INVALID_ENUM);
  208. break;
  209. }
  210. }
  211. else
  212. alSetError(Context, AL_INVALID_NAME);
  213. ProcessContext(Context);
  214. }
  215. AL_API ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
  216. {
  217. ALCcontext *Context;
  218. ALCdevice *Device;
  219. Context = GetContextSuspended();
  220. if(!Context) return;
  221. Device = Context->Device;
  222. if(LookupFilter(Device->FilterMap, filter) != NULL)
  223. {
  224. switch(param)
  225. {
  226. default:
  227. alFilterf(filter, param, pflValues[0]);
  228. break;
  229. }
  230. }
  231. else
  232. alSetError(Context, AL_INVALID_NAME);
  233. ProcessContext(Context);
  234. }
  235. AL_API ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *piValue)
  236. {
  237. ALCcontext *Context;
  238. ALCdevice *Device;
  239. ALfilter *ALFilter;
  240. Context = GetContextSuspended();
  241. if(!Context) return;
  242. Device = Context->Device;
  243. if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
  244. {
  245. switch(param)
  246. {
  247. case AL_FILTER_TYPE:
  248. *piValue = ALFilter->type;
  249. break;
  250. default:
  251. alSetError(Context, AL_INVALID_ENUM);
  252. break;
  253. }
  254. }
  255. else
  256. alSetError(Context, AL_INVALID_NAME);
  257. ProcessContext(Context);
  258. }
  259. AL_API ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *piValues)
  260. {
  261. ALCcontext *Context;
  262. ALCdevice *Device;
  263. Context = GetContextSuspended();
  264. if(!Context) return;
  265. Device = Context->Device;
  266. if(LookupFilter(Device->FilterMap, filter) != NULL)
  267. {
  268. switch(param)
  269. {
  270. case AL_FILTER_TYPE:
  271. alGetFilteri(filter, param, piValues);
  272. break;
  273. default:
  274. alSetError(Context, AL_INVALID_ENUM);
  275. break;
  276. }
  277. }
  278. else
  279. alSetError(Context, AL_INVALID_NAME);
  280. ProcessContext(Context);
  281. }
  282. AL_API ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *pflValue)
  283. {
  284. ALCcontext *Context;
  285. ALCdevice *Device;
  286. ALfilter *ALFilter;
  287. Context = GetContextSuspended();
  288. if(!Context) return;
  289. Device = Context->Device;
  290. if((ALFilter=LookupFilter(Device->FilterMap, filter)) != NULL)
  291. {
  292. switch(ALFilter->type)
  293. {
  294. case AL_FILTER_LOWPASS:
  295. switch(param)
  296. {
  297. case AL_LOWPASS_GAIN:
  298. *pflValue = ALFilter->Gain;
  299. break;
  300. case AL_LOWPASS_GAINHF:
  301. *pflValue = ALFilter->GainHF;
  302. break;
  303. default:
  304. alSetError(Context, AL_INVALID_ENUM);
  305. break;
  306. }
  307. break;
  308. default:
  309. alSetError(Context, AL_INVALID_ENUM);
  310. break;
  311. }
  312. }
  313. else
  314. alSetError(Context, AL_INVALID_NAME);
  315. ProcessContext(Context);
  316. }
  317. AL_API ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *pflValues)
  318. {
  319. ALCcontext *Context;
  320. ALCdevice *Device;
  321. Context = GetContextSuspended();
  322. if(!Context) return;
  323. Device = Context->Device;
  324. if(LookupFilter(Device->FilterMap, filter) != NULL)
  325. {
  326. switch(param)
  327. {
  328. default:
  329. alGetFilterf(filter, param, pflValues);
  330. break;
  331. }
  332. }
  333. else
  334. alSetError(Context, AL_INVALID_NAME);
  335. ProcessContext(Context);
  336. }
  337. ALvoid ReleaseALFilters(ALCdevice *device)
  338. {
  339. ALsizei i;
  340. for(i = 0;i < device->FilterMap.size;i++)
  341. {
  342. ALfilter *temp = device->FilterMap.array[i].value;
  343. device->FilterMap.array[i].value = NULL;
  344. // Release filter structure
  345. ALTHUNK_REMOVEENTRY(temp->filter);
  346. memset(temp, 0, sizeof(ALfilter));
  347. free(temp);
  348. }
  349. }
  350. static void InitFilterParams(ALfilter *filter, ALenum type)
  351. {
  352. filter->type = type;
  353. filter->Gain = 1.0;
  354. filter->GainHF = 1.0;
  355. }