alFilter.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include "alMain.h"
  23. #include "alu.h"
  24. #include "alFilter.h"
  25. #include "alThunk.h"
  26. #include "alError.h"
  27. extern inline struct ALfilter *LookupFilter(ALCdevice *device, ALuint id);
  28. extern inline struct ALfilter *RemoveFilter(ALCdevice *device, ALuint id);
  29. extern inline ALfloat ALfilterState_processSingle(ALfilterState *filter, ALfloat sample);
  30. extern inline ALfloat calc_rcpQ_from_slope(ALfloat gain, ALfloat slope);
  31. extern inline ALfloat calc_rcpQ_from_bandwidth(ALfloat freq_mult, ALfloat bandwidth);
  32. static void InitFilterParams(ALfilter *filter, ALenum type);
  33. AL_API ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
  34. {
  35. ALCdevice *device;
  36. ALCcontext *context;
  37. ALsizei cur = 0;
  38. ALenum err;
  39. context = GetContextRef();
  40. if(!context) return;
  41. if(!(n >= 0))
  42. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  43. device = context->Device;
  44. for(cur = 0;cur < n;cur++)
  45. {
  46. ALfilter *filter = calloc(1, sizeof(ALfilter));
  47. if(!filter)
  48. {
  49. alDeleteFilters(cur, filters);
  50. SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done);
  51. }
  52. InitFilterParams(filter, AL_FILTER_NULL);
  53. err = NewThunkEntry(&filter->id);
  54. if(err == AL_NO_ERROR)
  55. err = InsertUIntMapEntry(&device->FilterMap, filter->id, filter);
  56. if(err != AL_NO_ERROR)
  57. {
  58. FreeThunkEntry(filter->id);
  59. memset(filter, 0, sizeof(ALfilter));
  60. free(filter);
  61. alDeleteFilters(cur, filters);
  62. SET_ERROR_AND_GOTO(context, err, done);
  63. }
  64. filters[cur] = filter->id;
  65. }
  66. done:
  67. ALCcontext_DecRef(context);
  68. }
  69. AL_API ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, const ALuint *filters)
  70. {
  71. ALCdevice *device;
  72. ALCcontext *context;
  73. ALfilter *filter;
  74. ALsizei i;
  75. context = GetContextRef();
  76. if(!context) return;
  77. if(!(n >= 0))
  78. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  79. device = context->Device;
  80. for(i = 0;i < n;i++)
  81. {
  82. if(filters[i] && LookupFilter(device, filters[i]) == NULL)
  83. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  84. }
  85. for(i = 0;i < n;i++)
  86. {
  87. if((filter=RemoveFilter(device, filters[i])) == NULL)
  88. continue;
  89. FreeThunkEntry(filter->id);
  90. memset(filter, 0, sizeof(*filter));
  91. free(filter);
  92. }
  93. done:
  94. ALCcontext_DecRef(context);
  95. }
  96. AL_API ALboolean AL_APIENTRY alIsFilter(ALuint filter)
  97. {
  98. ALCcontext *Context;
  99. ALboolean result;
  100. Context = GetContextRef();
  101. if(!Context) return AL_FALSE;
  102. result = ((!filter || LookupFilter(Context->Device, filter)) ?
  103. AL_TRUE : AL_FALSE);
  104. ALCcontext_DecRef(Context);
  105. return result;
  106. }
  107. AL_API ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint value)
  108. {
  109. ALCcontext *Context;
  110. ALCdevice *Device;
  111. ALfilter *ALFilter;
  112. Context = GetContextRef();
  113. if(!Context) return;
  114. Device = Context->Device;
  115. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  116. alSetError(Context, AL_INVALID_NAME);
  117. else
  118. {
  119. if(param == AL_FILTER_TYPE)
  120. {
  121. if(value == AL_FILTER_NULL || value == AL_FILTER_LOWPASS ||
  122. value == AL_FILTER_HIGHPASS || value == AL_FILTER_BANDPASS)
  123. InitFilterParams(ALFilter, value);
  124. else
  125. alSetError(Context, AL_INVALID_VALUE);
  126. }
  127. else
  128. {
  129. /* Call the appropriate handler */
  130. ALfilter_SetParami(ALFilter, Context, param, value);
  131. }
  132. }
  133. ALCcontext_DecRef(Context);
  134. }
  135. AL_API ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, const ALint *values)
  136. {
  137. ALCcontext *Context;
  138. ALCdevice *Device;
  139. ALfilter *ALFilter;
  140. switch(param)
  141. {
  142. case AL_FILTER_TYPE:
  143. alFilteri(filter, param, values[0]);
  144. return;
  145. }
  146. Context = GetContextRef();
  147. if(!Context) return;
  148. Device = Context->Device;
  149. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  150. alSetError(Context, AL_INVALID_NAME);
  151. else
  152. {
  153. /* Call the appropriate handler */
  154. ALfilter_SetParamiv(ALFilter, Context, param, values);
  155. }
  156. ALCcontext_DecRef(Context);
  157. }
  158. AL_API ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat value)
  159. {
  160. ALCcontext *Context;
  161. ALCdevice *Device;
  162. ALfilter *ALFilter;
  163. Context = GetContextRef();
  164. if(!Context) return;
  165. Device = Context->Device;
  166. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  167. alSetError(Context, AL_INVALID_NAME);
  168. else
  169. {
  170. /* Call the appropriate handler */
  171. ALfilter_SetParamf(ALFilter, Context, param, value);
  172. }
  173. ALCcontext_DecRef(Context);
  174. }
  175. AL_API ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, const ALfloat *values)
  176. {
  177. ALCcontext *Context;
  178. ALCdevice *Device;
  179. ALfilter *ALFilter;
  180. Context = GetContextRef();
  181. if(!Context) return;
  182. Device = Context->Device;
  183. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  184. alSetError(Context, AL_INVALID_NAME);
  185. else
  186. {
  187. /* Call the appropriate handler */
  188. ALfilter_SetParamfv(ALFilter, Context, param, values);
  189. }
  190. ALCcontext_DecRef(Context);
  191. }
  192. AL_API ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *value)
  193. {
  194. ALCcontext *Context;
  195. ALCdevice *Device;
  196. ALfilter *ALFilter;
  197. Context = GetContextRef();
  198. if(!Context) return;
  199. Device = Context->Device;
  200. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  201. alSetError(Context, AL_INVALID_NAME);
  202. else
  203. {
  204. if(param == AL_FILTER_TYPE)
  205. *value = ALFilter->type;
  206. else
  207. {
  208. /* Call the appropriate handler */
  209. ALfilter_GetParami(ALFilter, Context, param, value);
  210. }
  211. }
  212. ALCcontext_DecRef(Context);
  213. }
  214. AL_API ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *values)
  215. {
  216. ALCcontext *Context;
  217. ALCdevice *Device;
  218. ALfilter *ALFilter;
  219. switch(param)
  220. {
  221. case AL_FILTER_TYPE:
  222. alGetFilteri(filter, param, values);
  223. return;
  224. }
  225. Context = GetContextRef();
  226. if(!Context) return;
  227. Device = Context->Device;
  228. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  229. alSetError(Context, AL_INVALID_NAME);
  230. else
  231. {
  232. /* Call the appropriate handler */
  233. ALfilter_GetParamiv(ALFilter, Context, param, values);
  234. }
  235. ALCcontext_DecRef(Context);
  236. }
  237. AL_API ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *value)
  238. {
  239. ALCcontext *Context;
  240. ALCdevice *Device;
  241. ALfilter *ALFilter;
  242. Context = GetContextRef();
  243. if(!Context) return;
  244. Device = Context->Device;
  245. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  246. alSetError(Context, AL_INVALID_NAME);
  247. else
  248. {
  249. /* Call the appropriate handler */
  250. ALfilter_GetParamf(ALFilter, Context, param, value);
  251. }
  252. ALCcontext_DecRef(Context);
  253. }
  254. AL_API ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *values)
  255. {
  256. ALCcontext *Context;
  257. ALCdevice *Device;
  258. ALfilter *ALFilter;
  259. Context = GetContextRef();
  260. if(!Context) return;
  261. Device = Context->Device;
  262. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  263. alSetError(Context, AL_INVALID_NAME);
  264. else
  265. {
  266. /* Call the appropriate handler */
  267. ALfilter_GetParamfv(ALFilter, Context, param, values);
  268. }
  269. ALCcontext_DecRef(Context);
  270. }
  271. void ALfilterState_clear(ALfilterState *filter)
  272. {
  273. filter->x[0] = 0.0f;
  274. filter->x[1] = 0.0f;
  275. filter->y[0] = 0.0f;
  276. filter->y[1] = 0.0f;
  277. }
  278. void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat gain, ALfloat freq_mult, ALfloat rcpQ)
  279. {
  280. ALfloat alpha, sqrtgain_alpha_2;
  281. ALfloat w0, sin_w0, cos_w0;
  282. // Limit gain to -100dB
  283. gain = maxf(gain, 0.00001f);
  284. w0 = F_TAU * freq_mult;
  285. sin_w0 = sinf(w0);
  286. cos_w0 = cosf(w0);
  287. alpha = sin_w0/2.0f * rcpQ;
  288. /* Calculate filter coefficients depending on filter type */
  289. switch(type)
  290. {
  291. case ALfilterType_HighShelf:
  292. sqrtgain_alpha_2 = 2.0f * sqrtf(gain) * alpha;
  293. filter->b[0] = gain*((gain+1.0f) + (gain-1.0f)*cos_w0 + sqrtgain_alpha_2);
  294. filter->b[1] = -2.0f*gain*((gain-1.0f) + (gain+1.0f)*cos_w0 );
  295. filter->b[2] = gain*((gain+1.0f) + (gain-1.0f)*cos_w0 - sqrtgain_alpha_2);
  296. filter->a[0] = (gain+1.0f) - (gain-1.0f)*cos_w0 + sqrtgain_alpha_2;
  297. filter->a[1] = 2.0f* ((gain-1.0f) - (gain+1.0f)*cos_w0 );
  298. filter->a[2] = (gain+1.0f) - (gain-1.0f)*cos_w0 - sqrtgain_alpha_2;
  299. break;
  300. case ALfilterType_LowShelf:
  301. sqrtgain_alpha_2 = 2.0f * sqrtf(gain) * alpha;
  302. filter->b[0] = gain*((gain+1.0f) - (gain-1.0f)*cos_w0 + sqrtgain_alpha_2);
  303. filter->b[1] = 2.0f*gain*((gain-1.0f) - (gain+1.0f)*cos_w0 );
  304. filter->b[2] = gain*((gain+1.0f) - (gain-1.0f)*cos_w0 - sqrtgain_alpha_2);
  305. filter->a[0] = (gain+1.0f) + (gain-1.0f)*cos_w0 + sqrtgain_alpha_2;
  306. filter->a[1] = -2.0f* ((gain-1.0f) + (gain+1.0f)*cos_w0 );
  307. filter->a[2] = (gain+1.0f) + (gain-1.0f)*cos_w0 - sqrtgain_alpha_2;
  308. break;
  309. case ALfilterType_Peaking:
  310. gain = sqrtf(gain);
  311. filter->b[0] = 1.0f + alpha * gain;
  312. filter->b[1] = -2.0f * cos_w0;
  313. filter->b[2] = 1.0f - alpha * gain;
  314. filter->a[0] = 1.0f + alpha / gain;
  315. filter->a[1] = -2.0f * cos_w0;
  316. filter->a[2] = 1.0f - alpha / gain;
  317. break;
  318. case ALfilterType_LowPass:
  319. filter->b[0] = (1.0f - cos_w0) / 2.0f;
  320. filter->b[1] = 1.0f - cos_w0;
  321. filter->b[2] = (1.0f - cos_w0) / 2.0f;
  322. filter->a[0] = 1.0f + alpha;
  323. filter->a[1] = -2.0f * cos_w0;
  324. filter->a[2] = 1.0f - alpha;
  325. break;
  326. case ALfilterType_HighPass:
  327. filter->b[0] = (1.0f + cos_w0) / 2.0f;
  328. filter->b[1] = -(1.0f + cos_w0);
  329. filter->b[2] = (1.0f + cos_w0) / 2.0f;
  330. filter->a[0] = 1.0f + alpha;
  331. filter->a[1] = -2.0f * cos_w0;
  332. filter->a[2] = 1.0f - alpha;
  333. break;
  334. case ALfilterType_BandPass:
  335. filter->b[0] = alpha;
  336. filter->b[1] = 0;
  337. filter->b[2] = -alpha;
  338. filter->a[0] = 1.0f + alpha;
  339. filter->a[1] = -2.0f * cos_w0;
  340. filter->a[2] = 1.0f - alpha;
  341. break;
  342. }
  343. filter->b[2] /= filter->a[0];
  344. filter->b[1] /= filter->a[0];
  345. filter->b[0] /= filter->a[0];
  346. filter->a[2] /= filter->a[0];
  347. filter->a[1] /= filter->a[0];
  348. filter->a[0] /= filter->a[0];
  349. filter->process = ALfilterState_processC;
  350. }
  351. void ALfilterState_processPassthru(ALfilterState *filter, const ALfloat *src, ALuint numsamples)
  352. {
  353. if(numsamples >= 2)
  354. {
  355. filter->x[1] = src[numsamples-2];
  356. filter->x[0] = src[numsamples-1];
  357. filter->y[1] = src[numsamples-2];
  358. filter->y[0] = src[numsamples-1];
  359. }
  360. else if(numsamples == 1)
  361. {
  362. filter->x[1] = filter->x[0];
  363. filter->x[0] = src[0];
  364. filter->y[1] = filter->y[0];
  365. filter->y[0] = src[0];
  366. }
  367. }
  368. static void lp_SetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  369. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  370. static void lp_SetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), const ALint *UNUSED(vals))
  371. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  372. static void lp_SetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val)
  373. {
  374. switch(param)
  375. {
  376. case AL_LOWPASS_GAIN:
  377. if(!(val >= AL_LOWPASS_MIN_GAIN && val <= AL_LOWPASS_MAX_GAIN))
  378. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  379. filter->Gain = val;
  380. break;
  381. case AL_LOWPASS_GAINHF:
  382. if(!(val >= AL_LOWPASS_MIN_GAINHF && val <= AL_LOWPASS_MAX_GAINHF))
  383. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  384. filter->GainHF = val;
  385. break;
  386. default:
  387. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  388. }
  389. }
  390. static void lp_SetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals)
  391. {
  392. lp_SetParamf(filter, context, param, vals[0]);
  393. }
  394. static void lp_GetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  395. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  396. static void lp_GetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(vals))
  397. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  398. static void lp_GetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val)
  399. {
  400. switch(param)
  401. {
  402. case AL_LOWPASS_GAIN:
  403. *val = filter->Gain;
  404. break;
  405. case AL_LOWPASS_GAINHF:
  406. *val = filter->GainHF;
  407. break;
  408. default:
  409. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  410. }
  411. }
  412. static void lp_GetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals)
  413. {
  414. lp_GetParamf(filter, context, param, vals);
  415. }
  416. static void hp_SetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  417. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  418. static void hp_SetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), const ALint *UNUSED(vals))
  419. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  420. static void hp_SetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val)
  421. {
  422. switch(param)
  423. {
  424. case AL_HIGHPASS_GAIN:
  425. if(!(val >= AL_HIGHPASS_MIN_GAIN && val <= AL_HIGHPASS_MAX_GAIN))
  426. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  427. filter->Gain = val;
  428. break;
  429. case AL_HIGHPASS_GAINLF:
  430. if(!(val >= AL_HIGHPASS_MIN_GAINLF && val <= AL_HIGHPASS_MAX_GAINLF))
  431. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  432. filter->GainLF = val;
  433. break;
  434. default:
  435. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  436. }
  437. }
  438. static void hp_SetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals)
  439. {
  440. hp_SetParamf(filter, context, param, vals[0]);
  441. }
  442. static void hp_GetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  443. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  444. static void hp_GetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(vals))
  445. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  446. static void hp_GetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val)
  447. {
  448. switch(param)
  449. {
  450. case AL_HIGHPASS_GAIN:
  451. *val = filter->Gain;
  452. break;
  453. case AL_HIGHPASS_GAINLF:
  454. *val = filter->GainLF;
  455. break;
  456. default:
  457. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  458. }
  459. }
  460. static void hp_GetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals)
  461. {
  462. hp_GetParamf(filter, context, param, vals);
  463. }
  464. static void bp_SetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  465. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  466. static void bp_SetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), const ALint *UNUSED(vals))
  467. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  468. static void bp_SetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val)
  469. {
  470. switch(param)
  471. {
  472. case AL_BANDPASS_GAIN:
  473. if(!(val >= AL_BANDPASS_MIN_GAIN && val <= AL_BANDPASS_MAX_GAIN))
  474. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  475. filter->Gain = val;
  476. break;
  477. case AL_BANDPASS_GAINHF:
  478. if(!(val >= AL_BANDPASS_MIN_GAINHF && val <= AL_BANDPASS_MAX_GAINHF))
  479. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  480. filter->GainHF = val;
  481. break;
  482. case AL_BANDPASS_GAINLF:
  483. if(!(val >= AL_BANDPASS_MIN_GAINLF && val <= AL_BANDPASS_MAX_GAINLF))
  484. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  485. filter->GainLF = val;
  486. break;
  487. default:
  488. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  489. }
  490. }
  491. static void bp_SetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals)
  492. {
  493. bp_SetParamf(filter, context, param, vals[0]);
  494. }
  495. static void bp_GetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  496. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  497. static void bp_GetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(vals))
  498. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  499. static void bp_GetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val)
  500. {
  501. switch(param)
  502. {
  503. case AL_BANDPASS_GAIN:
  504. *val = filter->Gain;
  505. break;
  506. case AL_BANDPASS_GAINHF:
  507. *val = filter->GainHF;
  508. break;
  509. case AL_BANDPASS_GAINLF:
  510. *val = filter->GainLF;
  511. break;
  512. default:
  513. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  514. }
  515. }
  516. static void bp_GetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals)
  517. {
  518. bp_GetParamf(filter, context, param, vals);
  519. }
  520. static void null_SetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  521. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  522. static void null_SetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), const ALint *UNUSED(vals))
  523. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  524. static void null_SetParamf(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
  525. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  526. static void null_SetParamfv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), const ALfloat *UNUSED(vals))
  527. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  528. static void null_GetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  529. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  530. static void null_GetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(vals))
  531. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  532. static void null_GetParamf(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
  533. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  534. static void null_GetParamfv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(vals))
  535. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  536. ALvoid ReleaseALFilters(ALCdevice *device)
  537. {
  538. ALsizei i;
  539. for(i = 0;i < device->FilterMap.size;i++)
  540. {
  541. ALfilter *temp = device->FilterMap.array[i].value;
  542. device->FilterMap.array[i].value = NULL;
  543. // Release filter structure
  544. FreeThunkEntry(temp->id);
  545. memset(temp, 0, sizeof(ALfilter));
  546. free(temp);
  547. }
  548. }
  549. static void InitFilterParams(ALfilter *filter, ALenum type)
  550. {
  551. if(type == AL_FILTER_LOWPASS)
  552. {
  553. filter->Gain = AL_LOWPASS_DEFAULT_GAIN;
  554. filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF;
  555. filter->HFReference = LOWPASSFREQREF;
  556. filter->GainLF = 1.0f;
  557. filter->LFReference = HIGHPASSFREQREF;
  558. filter->SetParami = lp_SetParami;
  559. filter->SetParamiv = lp_SetParamiv;
  560. filter->SetParamf = lp_SetParamf;
  561. filter->SetParamfv = lp_SetParamfv;
  562. filter->GetParami = lp_GetParami;
  563. filter->GetParamiv = lp_GetParamiv;
  564. filter->GetParamf = lp_GetParamf;
  565. filter->GetParamfv = lp_GetParamfv;
  566. }
  567. else if(type == AL_FILTER_HIGHPASS)
  568. {
  569. filter->Gain = AL_HIGHPASS_DEFAULT_GAIN;
  570. filter->GainHF = 1.0f;
  571. filter->HFReference = LOWPASSFREQREF;
  572. filter->GainLF = AL_HIGHPASS_DEFAULT_GAINLF;
  573. filter->LFReference = HIGHPASSFREQREF;
  574. filter->SetParami = hp_SetParami;
  575. filter->SetParamiv = hp_SetParamiv;
  576. filter->SetParamf = hp_SetParamf;
  577. filter->SetParamfv = hp_SetParamfv;
  578. filter->GetParami = hp_GetParami;
  579. filter->GetParamiv = hp_GetParamiv;
  580. filter->GetParamf = hp_GetParamf;
  581. filter->GetParamfv = hp_GetParamfv;
  582. }
  583. else if(type == AL_FILTER_BANDPASS)
  584. {
  585. filter->Gain = AL_BANDPASS_DEFAULT_GAIN;
  586. filter->GainHF = AL_BANDPASS_DEFAULT_GAINHF;
  587. filter->HFReference = LOWPASSFREQREF;
  588. filter->GainLF = AL_BANDPASS_DEFAULT_GAINLF;
  589. filter->LFReference = HIGHPASSFREQREF;
  590. filter->SetParami = bp_SetParami;
  591. filter->SetParamiv = bp_SetParamiv;
  592. filter->SetParamf = bp_SetParamf;
  593. filter->SetParamfv = bp_SetParamfv;
  594. filter->GetParami = bp_GetParami;
  595. filter->GetParamiv = bp_GetParamiv;
  596. filter->GetParamf = bp_GetParamf;
  597. filter->GetParamfv = bp_GetParamfv;
  598. }
  599. else
  600. {
  601. filter->Gain = 1.0f;
  602. filter->GainHF = 1.0f;
  603. filter->HFReference = LOWPASSFREQREF;
  604. filter->GainLF = 1.0f;
  605. filter->LFReference = HIGHPASSFREQREF;
  606. filter->SetParami = null_SetParami;
  607. filter->SetParamiv = null_SetParamiv;
  608. filter->SetParamf = null_SetParamf;
  609. filter->SetParamfv = null_SetParamfv;
  610. filter->GetParami = null_GetParami;
  611. filter->GetParamiv = null_GetParamiv;
  612. filter->GetParamf = null_GetParamf;
  613. filter->GetParamfv = null_GetParamfv;
  614. }
  615. filter->type = type;
  616. }