alFilter.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 "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 ALfilterState_processSingleC(const ALfilterState *filter, ALfloat sample);
  31. static void InitFilterParams(ALfilter *filter, ALenum type);
  32. AL_API ALvoid AL_APIENTRY alGenFilters(ALsizei n, ALuint *filters)
  33. {
  34. ALCdevice *device;
  35. ALCcontext *context;
  36. ALsizei cur = 0;
  37. ALenum err;
  38. context = GetContextRef();
  39. if(!context) return;
  40. if(!(n >= 0))
  41. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  42. device = context->Device;
  43. for(cur = 0;cur < n;cur++)
  44. {
  45. ALfilter *filter = calloc(1, sizeof(ALfilter));
  46. if(!filter)
  47. {
  48. alDeleteFilters(cur, filters);
  49. SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done);
  50. }
  51. InitFilterParams(filter, AL_FILTER_NULL);
  52. err = NewThunkEntry(&filter->id);
  53. if(err == AL_NO_ERROR)
  54. err = InsertUIntMapEntry(&device->FilterMap, filter->id, filter);
  55. if(err != AL_NO_ERROR)
  56. {
  57. FreeThunkEntry(filter->id);
  58. memset(filter, 0, sizeof(ALfilter));
  59. free(filter);
  60. alDeleteFilters(cur, filters);
  61. SET_ERROR_AND_GOTO(context, err, done);
  62. }
  63. filters[cur] = filter->id;
  64. }
  65. done:
  66. ALCcontext_DecRef(context);
  67. }
  68. AL_API ALvoid AL_APIENTRY alDeleteFilters(ALsizei n, const ALuint *filters)
  69. {
  70. ALCdevice *device;
  71. ALCcontext *context;
  72. ALfilter *filter;
  73. ALsizei i;
  74. context = GetContextRef();
  75. if(!context) return;
  76. if(!(n >= 0))
  77. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  78. device = context->Device;
  79. for(i = 0;i < n;i++)
  80. {
  81. if(filters[i] && LookupFilter(device, filters[i]) == NULL)
  82. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  83. }
  84. for(i = 0;i < n;i++)
  85. {
  86. if((filter=RemoveFilter(device, filters[i])) == NULL)
  87. continue;
  88. FreeThunkEntry(filter->id);
  89. memset(filter, 0, sizeof(*filter));
  90. free(filter);
  91. }
  92. done:
  93. ALCcontext_DecRef(context);
  94. }
  95. AL_API ALboolean AL_APIENTRY alIsFilter(ALuint filter)
  96. {
  97. ALCcontext *Context;
  98. ALboolean result;
  99. Context = GetContextRef();
  100. if(!Context) return AL_FALSE;
  101. result = ((!filter || LookupFilter(Context->Device, filter)) ?
  102. AL_TRUE : AL_FALSE);
  103. ALCcontext_DecRef(Context);
  104. return result;
  105. }
  106. AL_API ALvoid AL_APIENTRY alFilteri(ALuint filter, ALenum param, ALint value)
  107. {
  108. ALCcontext *Context;
  109. ALCdevice *Device;
  110. ALfilter *ALFilter;
  111. Context = GetContextRef();
  112. if(!Context) return;
  113. Device = Context->Device;
  114. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  115. alSetError(Context, AL_INVALID_NAME);
  116. else
  117. {
  118. if(param == AL_FILTER_TYPE)
  119. {
  120. if(value == AL_FILTER_NULL || value == AL_FILTER_LOWPASS)
  121. InitFilterParams(ALFilter, value);
  122. else
  123. alSetError(Context, AL_INVALID_VALUE);
  124. }
  125. else
  126. {
  127. /* Call the appropriate handler */
  128. ALfilter_SetParami(ALFilter, Context, param, value);
  129. }
  130. }
  131. ALCcontext_DecRef(Context);
  132. }
  133. AL_API ALvoid AL_APIENTRY alFilteriv(ALuint filter, ALenum param, const ALint *values)
  134. {
  135. ALCcontext *Context;
  136. ALCdevice *Device;
  137. ALfilter *ALFilter;
  138. switch(param)
  139. {
  140. case AL_FILTER_TYPE:
  141. alFilteri(filter, param, values[0]);
  142. return;
  143. }
  144. Context = GetContextRef();
  145. if(!Context) return;
  146. Device = Context->Device;
  147. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  148. alSetError(Context, AL_INVALID_NAME);
  149. else
  150. {
  151. /* Call the appropriate handler */
  152. ALfilter_SetParamiv(ALFilter, Context, param, values);
  153. }
  154. ALCcontext_DecRef(Context);
  155. }
  156. AL_API ALvoid AL_APIENTRY alFilterf(ALuint filter, ALenum param, ALfloat value)
  157. {
  158. ALCcontext *Context;
  159. ALCdevice *Device;
  160. ALfilter *ALFilter;
  161. Context = GetContextRef();
  162. if(!Context) return;
  163. Device = Context->Device;
  164. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  165. alSetError(Context, AL_INVALID_NAME);
  166. else
  167. {
  168. /* Call the appropriate handler */
  169. ALfilter_SetParamf(ALFilter, Context, param, value);
  170. }
  171. ALCcontext_DecRef(Context);
  172. }
  173. AL_API ALvoid AL_APIENTRY alFilterfv(ALuint filter, ALenum param, const ALfloat *values)
  174. {
  175. ALCcontext *Context;
  176. ALCdevice *Device;
  177. ALfilter *ALFilter;
  178. Context = GetContextRef();
  179. if(!Context) return;
  180. Device = Context->Device;
  181. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  182. alSetError(Context, AL_INVALID_NAME);
  183. else
  184. {
  185. /* Call the appropriate handler */
  186. ALfilter_SetParamfv(ALFilter, Context, param, values);
  187. }
  188. ALCcontext_DecRef(Context);
  189. }
  190. AL_API ALvoid AL_APIENTRY alGetFilteri(ALuint filter, ALenum param, ALint *value)
  191. {
  192. ALCcontext *Context;
  193. ALCdevice *Device;
  194. ALfilter *ALFilter;
  195. Context = GetContextRef();
  196. if(!Context) return;
  197. Device = Context->Device;
  198. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  199. alSetError(Context, AL_INVALID_NAME);
  200. else
  201. {
  202. if(param == AL_FILTER_TYPE)
  203. *value = ALFilter->type;
  204. else
  205. {
  206. /* Call the appropriate handler */
  207. ALfilter_GetParami(ALFilter, Context, param, value);
  208. }
  209. }
  210. ALCcontext_DecRef(Context);
  211. }
  212. AL_API ALvoid AL_APIENTRY alGetFilteriv(ALuint filter, ALenum param, ALint *values)
  213. {
  214. ALCcontext *Context;
  215. ALCdevice *Device;
  216. ALfilter *ALFilter;
  217. switch(param)
  218. {
  219. case AL_FILTER_TYPE:
  220. alGetFilteri(filter, param, values);
  221. return;
  222. }
  223. Context = GetContextRef();
  224. if(!Context) return;
  225. Device = Context->Device;
  226. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  227. alSetError(Context, AL_INVALID_NAME);
  228. else
  229. {
  230. /* Call the appropriate handler */
  231. ALfilter_GetParamiv(ALFilter, Context, param, values);
  232. }
  233. ALCcontext_DecRef(Context);
  234. }
  235. AL_API ALvoid AL_APIENTRY alGetFilterf(ALuint filter, ALenum param, ALfloat *value)
  236. {
  237. ALCcontext *Context;
  238. ALCdevice *Device;
  239. ALfilter *ALFilter;
  240. Context = GetContextRef();
  241. if(!Context) return;
  242. Device = Context->Device;
  243. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  244. alSetError(Context, AL_INVALID_NAME);
  245. else
  246. {
  247. /* Call the appropriate handler */
  248. ALfilter_GetParamf(ALFilter, Context, param, value);
  249. }
  250. ALCcontext_DecRef(Context);
  251. }
  252. AL_API ALvoid AL_APIENTRY alGetFilterfv(ALuint filter, ALenum param, ALfloat *values)
  253. {
  254. ALCcontext *Context;
  255. ALCdevice *Device;
  256. ALfilter *ALFilter;
  257. Context = GetContextRef();
  258. if(!Context) return;
  259. Device = Context->Device;
  260. if((ALFilter=LookupFilter(Device, filter)) == NULL)
  261. alSetError(Context, AL_INVALID_NAME);
  262. else
  263. {
  264. /* Call the appropriate handler */
  265. ALfilter_GetParamfv(ALFilter, Context, param, values);
  266. }
  267. ALCcontext_DecRef(Context);
  268. }
  269. void ALfilterState_clear(ALfilterState *filter)
  270. {
  271. filter->x[0] = 0.0f;
  272. filter->x[1] = 0.0f;
  273. filter->y[0] = 0.0f;
  274. filter->y[1] = 0.0f;
  275. }
  276. void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat gain, ALfloat freq_scale, ALfloat bandwidth)
  277. {
  278. ALfloat alpha;
  279. ALfloat w0;
  280. // Limit gain to -100dB
  281. gain = maxf(gain, 0.00001f);
  282. w0 = F_2PI * freq_scale;
  283. /* Calculate filter coefficients depending on filter type */
  284. switch(type)
  285. {
  286. case ALfilterType_HighShelf:
  287. alpha = sinf(w0) / 2.0f * sqrtf((gain + 1.0f/gain) *
  288. (1.0f/0.75f - 1.0f) + 2.0f);
  289. filter->b[0] = gain * ((gain + 1.0f) +
  290. (gain - 1.0f) * cosf(w0) +
  291. 2.0f * sqrtf(gain) * alpha);
  292. filter->b[1] = -2.0f * gain * ((gain - 1.0f) +
  293. (gain + 1.0f) * cosf(w0));
  294. filter->b[2] = gain * ((gain + 1.0f) +
  295. (gain - 1.0f) * cosf(w0) -
  296. 2.0f * sqrtf(gain) * alpha);
  297. filter->a[0] = (gain + 1.0f) -
  298. (gain - 1.0f) * cosf(w0) +
  299. 2.0f * sqrtf(gain) * alpha;
  300. filter->a[1] = 2.0f * ((gain - 1.0f) -
  301. (gain + 1.0f) * cosf(w0));
  302. filter->a[2] = (gain + 1.0f) -
  303. (gain - 1.0f) * cosf(w0) -
  304. 2.0f * sqrtf(gain) * alpha;
  305. break;
  306. case ALfilterType_LowShelf:
  307. alpha = sinf(w0) / 2.0f * sqrtf((gain + 1.0f / gain) *
  308. (1.0f / 0.75f - 1.0f) + 2.0f);
  309. filter->b[0] = gain * ((gain + 1.0f) -
  310. (gain - 1.0f) * cosf(w0) +
  311. 2.0f * sqrtf(gain) * alpha);
  312. filter->b[1] = 2.0f * gain * ((gain - 1.0f) -
  313. (gain + 1.0f) * cosf(w0));
  314. filter->b[2] = gain * ((gain + 1.0f) -
  315. (gain - 1.0f) * cosf(w0) -
  316. 2.0f * sqrtf(gain) * alpha);
  317. filter->a[0] = (gain + 1.0f) +
  318. (gain - 1.0f) * cosf(w0) +
  319. 2.0f * sqrtf(gain) * alpha;
  320. filter->a[1] = -2.0f * ((gain - 1.0f) +
  321. (gain + 1.0f) * cosf(w0));
  322. filter->a[2] = (gain + 1.0f) +
  323. (gain - 1.0f) * cosf(w0) -
  324. 2.0f * sqrtf(gain) * alpha;
  325. break;
  326. case ALfilterType_Peaking:
  327. alpha = sinf(w0) * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sinf(w0));
  328. filter->b[0] = 1.0f + alpha * gain;
  329. filter->b[1] = -2.0f * cosf(w0);
  330. filter->b[2] = 1.0f - alpha * gain;
  331. filter->a[0] = 1.0f + alpha / gain;
  332. filter->a[1] = -2.0f * cosf(w0);
  333. filter->a[2] = 1.0f - alpha / gain;
  334. break;
  335. case ALfilterType_LowPass:
  336. alpha = sinf(w0) * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sinf(w0));
  337. filter->b[0] = (1.0f - cosf(w0)) / 2.0f;
  338. filter->b[1] = 1.0f - cosf(w0);
  339. filter->b[2] = (1.0f - cosf(w0)) / 2.0f;
  340. filter->a[0] = 1.0f + alpha;
  341. filter->a[1] = -2.0f * cosf(w0);
  342. filter->a[2] = 1.0f - alpha;
  343. break;
  344. case ALfilterType_HighPass:
  345. alpha = sinf(w0) * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sinf(w0));
  346. filter->b[0] = (1.0f + cosf(w0)) / 2.0f;
  347. filter->b[1] = 1.0f + cosf(w0);
  348. filter->b[2] = (1.0f + cosf(w0)) / 2.0f;
  349. filter->a[0] = 1.0f + alpha;
  350. filter->a[1] = -2.0f * cosf(w0);
  351. filter->a[2] = 1.0f - alpha;
  352. break;
  353. case ALfilterType_BandPass:
  354. alpha = sinf(w0) * sinhf(logf(2.0f) / 2.0f * bandwidth * w0 / sinf(w0));
  355. filter->b[0] = alpha;
  356. filter->b[1] = 0;
  357. filter->b[2] = -alpha;
  358. filter->a[0] = 1.0f + alpha;
  359. filter->a[1] = -2.0f * cosf(w0);
  360. filter->a[2] = 1.0f - alpha;
  361. break;
  362. }
  363. filter->b[2] /= filter->a[0];
  364. filter->b[1] /= filter->a[0];
  365. filter->b[0] /= filter->a[0];
  366. filter->a[2] /= filter->a[0];
  367. filter->a[1] /= filter->a[0];
  368. filter->a[0] /= filter->a[0];
  369. }
  370. static void lp_SetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  371. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  372. static void lp_SetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), const ALint *UNUSED(vals))
  373. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  374. static void lp_SetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val)
  375. {
  376. switch(param)
  377. {
  378. case AL_LOWPASS_GAIN:
  379. if(!(val >= AL_LOWPASS_MIN_GAIN && val <= AL_LOWPASS_MAX_GAIN))
  380. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  381. filter->Gain = val;
  382. break;
  383. case AL_LOWPASS_GAINHF:
  384. if(!(val >= AL_LOWPASS_MIN_GAINHF && val <= AL_LOWPASS_MAX_GAINHF))
  385. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  386. filter->GainHF = val;
  387. break;
  388. default:
  389. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  390. }
  391. }
  392. static void lp_SetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals)
  393. {
  394. lp_SetParamf(filter, context, param, vals[0]);
  395. }
  396. static void lp_GetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  397. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  398. static void lp_GetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(vals))
  399. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  400. static void lp_GetParamf(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val)
  401. {
  402. switch(param)
  403. {
  404. case AL_LOWPASS_GAIN:
  405. *val = filter->Gain;
  406. break;
  407. case AL_LOWPASS_GAINHF:
  408. *val = filter->GainHF;
  409. break;
  410. default:
  411. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  412. }
  413. }
  414. static void lp_GetParamfv(ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals)
  415. {
  416. lp_GetParamf(filter, context, param, vals);
  417. }
  418. static void null_SetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  419. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  420. static void null_SetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), const ALint *UNUSED(vals))
  421. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  422. static void null_SetParamf(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
  423. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  424. static void null_SetParamfv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), const ALfloat *UNUSED(vals))
  425. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  426. static void null_GetParami(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  427. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  428. static void null_GetParamiv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(vals))
  429. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  430. static void null_GetParamf(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
  431. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  432. static void null_GetParamfv(ALfilter *UNUSED(filter), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(vals))
  433. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  434. ALvoid ReleaseALFilters(ALCdevice *device)
  435. {
  436. ALsizei i;
  437. for(i = 0;i < device->FilterMap.size;i++)
  438. {
  439. ALfilter *temp = device->FilterMap.array[i].value;
  440. device->FilterMap.array[i].value = NULL;
  441. // Release filter structure
  442. FreeThunkEntry(temp->id);
  443. memset(temp, 0, sizeof(ALfilter));
  444. free(temp);
  445. }
  446. }
  447. static void InitFilterParams(ALfilter *filter, ALenum type)
  448. {
  449. if(type == AL_FILTER_LOWPASS)
  450. {
  451. filter->Gain = AL_LOWPASS_DEFAULT_GAIN;
  452. filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF;
  453. filter->SetParami = lp_SetParami;
  454. filter->SetParamiv = lp_SetParamiv;
  455. filter->SetParamf = lp_SetParamf;
  456. filter->SetParamfv = lp_SetParamfv;
  457. filter->GetParami = lp_GetParami;
  458. filter->GetParamiv = lp_GetParamiv;
  459. filter->GetParamf = lp_GetParamf;
  460. filter->GetParamfv = lp_GetParamfv;
  461. }
  462. else
  463. {
  464. filter->SetParami = null_SetParami;
  465. filter->SetParamiv = null_SetParamiv;
  466. filter->SetParamf = null_SetParamf;
  467. filter->SetParamfv = null_SetParamfv;
  468. filter->GetParami = null_GetParami;
  469. filter->GetParamiv = null_GetParamiv;
  470. filter->GetParamf = null_GetParamf;
  471. filter->GetParamfv = null_GetParamfv;
  472. }
  473. filter->type = type;
  474. }