mixer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 <math.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <assert.h>
  26. #include "alMain.h"
  27. #include "AL/al.h"
  28. #include "AL/alc.h"
  29. #include "alSource.h"
  30. #include "alBuffer.h"
  31. #include "alListener.h"
  32. #include "alAuxEffectSlot.h"
  33. #include "alu.h"
  34. #include "mixer_defs.h"
  35. static_assert((INT_MAX>>FRACTIONBITS)/MAX_PITCH > BUFFERSIZE,
  36. "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
  37. extern inline void InitiatePositionArrays(ALuint frac, ALuint increment, ALuint *restrict frac_arr, ALuint *restrict pos_arr, ALuint size);
  38. alignas(16) union ResamplerCoeffs ResampleCoeffs;
  39. enum Resampler {
  40. PointResampler,
  41. LinearResampler,
  42. FIR4Resampler,
  43. FIR8Resampler,
  44. BSincResampler,
  45. ResamplerDefault = LinearResampler
  46. };
  47. /* FIR8 requires 3 extra samples before the current position, and 4 after. */
  48. static_assert(MAX_PRE_SAMPLES >= 3, "MAX_PRE_SAMPLES must be at least 3!");
  49. static_assert(MAX_POST_SAMPLES >= 4, "MAX_POST_SAMPLES must be at least 4!");
  50. static MixerFunc MixSamples = Mix_C;
  51. static HrtfMixerFunc MixHrtfSamples = MixHrtf_C;
  52. static ResamplerFunc ResampleSamples = Resample_point32_C;
  53. MixerFunc SelectMixer(void)
  54. {
  55. #ifdef HAVE_SSE
  56. if((CPUCapFlags&CPU_CAP_SSE))
  57. return Mix_SSE;
  58. #endif
  59. #ifdef HAVE_NEON
  60. if((CPUCapFlags&CPU_CAP_NEON))
  61. return Mix_Neon;
  62. #endif
  63. return Mix_C;
  64. }
  65. RowMixerFunc SelectRowMixer(void)
  66. {
  67. #ifdef HAVE_SSE
  68. if((CPUCapFlags&CPU_CAP_SSE))
  69. return MixRow_SSE;
  70. #endif
  71. #ifdef HAVE_NEON
  72. if((CPUCapFlags&CPU_CAP_NEON))
  73. return MixRow_Neon;
  74. #endif
  75. return MixRow_C;
  76. }
  77. static inline HrtfMixerFunc SelectHrtfMixer(void)
  78. {
  79. #ifdef HAVE_SSE
  80. if((CPUCapFlags&CPU_CAP_SSE))
  81. return MixHrtf_SSE;
  82. #endif
  83. #ifdef HAVE_NEON
  84. if((CPUCapFlags&CPU_CAP_NEON))
  85. return MixHrtf_Neon;
  86. #endif
  87. return MixHrtf_C;
  88. }
  89. static inline ResamplerFunc SelectResampler(enum Resampler resampler)
  90. {
  91. switch(resampler)
  92. {
  93. case PointResampler:
  94. return Resample_point32_C;
  95. case LinearResampler:
  96. #ifdef HAVE_SSE4_1
  97. if((CPUCapFlags&CPU_CAP_SSE4_1))
  98. return Resample_lerp32_SSE41;
  99. #endif
  100. #ifdef HAVE_SSE2
  101. if((CPUCapFlags&CPU_CAP_SSE2))
  102. return Resample_lerp32_SSE2;
  103. #endif
  104. return Resample_lerp32_C;
  105. case FIR4Resampler:
  106. #ifdef HAVE_SSE4_1
  107. if((CPUCapFlags&CPU_CAP_SSE4_1))
  108. return Resample_fir4_32_SSE41;
  109. #endif
  110. #ifdef HAVE_SSE3
  111. if((CPUCapFlags&CPU_CAP_SSE3))
  112. return Resample_fir4_32_SSE3;
  113. #endif
  114. return Resample_fir4_32_C;
  115. case FIR8Resampler:
  116. #ifdef HAVE_SSE4_1
  117. if((CPUCapFlags&CPU_CAP_SSE4_1))
  118. return Resample_fir8_32_SSE41;
  119. #endif
  120. #ifdef HAVE_SSE3
  121. if((CPUCapFlags&CPU_CAP_SSE3))
  122. return Resample_fir8_32_SSE3;
  123. #endif
  124. return Resample_fir8_32_C;
  125. case BSincResampler:
  126. #ifdef HAVE_SSE
  127. if((CPUCapFlags&CPU_CAP_SSE))
  128. return Resample_bsinc32_SSE;
  129. #endif
  130. return Resample_bsinc32_C;
  131. }
  132. return Resample_point32_C;
  133. }
  134. /* The sinc resampler makes use of a Kaiser window to limit the needed sample
  135. * points to 4 and 8, respectively.
  136. */
  137. #ifndef M_PI
  138. #define M_PI (3.14159265358979323846)
  139. #endif
  140. static inline double Sinc(double x)
  141. {
  142. if(x == 0.0) return 1.0;
  143. return sin(x*M_PI) / (x*M_PI);
  144. }
  145. /* The zero-order modified Bessel function of the first kind, used for the
  146. * Kaiser window.
  147. *
  148. * I_0(x) = sum_{k=0}^inf (1 / k!)^2 (x / 2)^(2 k)
  149. * = sum_{k=0}^inf ((x / 2)^k / k!)^2
  150. */
  151. static double BesselI_0(double x)
  152. {
  153. double term, sum, x2, y, last_sum;
  154. int k;
  155. /* Start at k=1 since k=0 is trivial. */
  156. term = 1.0;
  157. sum = 1.0;
  158. x2 = x / 2.0;
  159. k = 1;
  160. /* Let the integration converge until the term of the sum is no longer
  161. * significant.
  162. */
  163. do {
  164. y = x2 / k;
  165. k ++;
  166. last_sum = sum;
  167. term *= y * y;
  168. sum += term;
  169. } while(sum != last_sum);
  170. return sum;
  171. }
  172. /* Calculate a Kaiser window from the given beta value and a normalized k
  173. * [-1, 1].
  174. *
  175. * w(k) = { I_0(B sqrt(1 - k^2)) / I_0(B), -1 <= k <= 1
  176. * { 0, elsewhere.
  177. *
  178. * Where k can be calculated as:
  179. *
  180. * k = i / l, where -l <= i <= l.
  181. *
  182. * or:
  183. *
  184. * k = 2 i / M - 1, where 0 <= i <= M.
  185. */
  186. static inline double Kaiser(double b, double k)
  187. {
  188. if(k <= -1.0 || k >= 1.0) return 0.0;
  189. return BesselI_0(b * sqrt(1.0 - (k*k))) / BesselI_0(b);
  190. }
  191. static inline double CalcKaiserBeta(double rejection)
  192. {
  193. if(rejection > 50.0)
  194. return 0.1102 * (rejection - 8.7);
  195. if(rejection >= 21.0)
  196. return (0.5842 * pow(rejection - 21.0, 0.4)) +
  197. (0.07886 * (rejection - 21.0));
  198. return 0.0;
  199. }
  200. static float SincKaiser(double r, double x)
  201. {
  202. /* Limit rippling to -60dB. */
  203. return (float)(Kaiser(CalcKaiserBeta(60.0), x / r) * Sinc(x));
  204. }
  205. void aluInitMixer(void)
  206. {
  207. enum Resampler resampler = ResamplerDefault;
  208. const char *str;
  209. ALuint i;
  210. if(ConfigValueStr(NULL, NULL, "resampler", &str))
  211. {
  212. if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
  213. resampler = PointResampler;
  214. else if(strcasecmp(str, "linear") == 0)
  215. resampler = LinearResampler;
  216. else if(strcasecmp(str, "sinc4") == 0)
  217. resampler = FIR4Resampler;
  218. else if(strcasecmp(str, "sinc8") == 0)
  219. resampler = FIR8Resampler;
  220. else if(strcasecmp(str, "bsinc") == 0)
  221. resampler = BSincResampler;
  222. else if(strcasecmp(str, "cubic") == 0)
  223. {
  224. WARN("Resampler option \"cubic\" is deprecated, using sinc4\n");
  225. resampler = FIR4Resampler;
  226. }
  227. else
  228. {
  229. char *end;
  230. long n = strtol(str, &end, 0);
  231. if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == FIR4Resampler))
  232. resampler = n;
  233. else
  234. WARN("Invalid resampler: %s\n", str);
  235. }
  236. }
  237. if(resampler == FIR8Resampler)
  238. for(i = 0;i < FRACTIONONE;i++)
  239. {
  240. ALdouble mu = (ALdouble)i / FRACTIONONE;
  241. ResampleCoeffs.FIR8[i][0] = SincKaiser(4.0, mu - -3.0);
  242. ResampleCoeffs.FIR8[i][1] = SincKaiser(4.0, mu - -2.0);
  243. ResampleCoeffs.FIR8[i][2] = SincKaiser(4.0, mu - -1.0);
  244. ResampleCoeffs.FIR8[i][3] = SincKaiser(4.0, mu - 0.0);
  245. ResampleCoeffs.FIR8[i][4] = SincKaiser(4.0, mu - 1.0);
  246. ResampleCoeffs.FIR8[i][5] = SincKaiser(4.0, mu - 2.0);
  247. ResampleCoeffs.FIR8[i][6] = SincKaiser(4.0, mu - 3.0);
  248. ResampleCoeffs.FIR8[i][7] = SincKaiser(4.0, mu - 4.0);
  249. }
  250. else if(resampler == FIR4Resampler)
  251. for(i = 0;i < FRACTIONONE;i++)
  252. {
  253. ALdouble mu = (ALdouble)i / FRACTIONONE;
  254. ResampleCoeffs.FIR4[i][0] = SincKaiser(2.0, mu - -1.0);
  255. ResampleCoeffs.FIR4[i][1] = SincKaiser(2.0, mu - 0.0);
  256. ResampleCoeffs.FIR4[i][2] = SincKaiser(2.0, mu - 1.0);
  257. ResampleCoeffs.FIR4[i][3] = SincKaiser(2.0, mu - 2.0);
  258. }
  259. MixHrtfSamples = SelectHrtfMixer();
  260. MixSamples = SelectMixer();
  261. ResampleSamples = SelectResampler(resampler);
  262. }
  263. static inline ALfloat Sample_ALbyte(ALbyte val)
  264. { return val * (1.0f/127.0f); }
  265. static inline ALfloat Sample_ALshort(ALshort val)
  266. { return val * (1.0f/32767.0f); }
  267. static inline ALfloat Sample_ALfloat(ALfloat val)
  268. { return val; }
  269. #define DECL_TEMPLATE(T) \
  270. static inline void Load_##T(ALfloat *dst, const T *src, ALuint srcstep, ALuint samples)\
  271. { \
  272. ALuint i; \
  273. for(i = 0;i < samples;i++) \
  274. dst[i] = Sample_##T(src[i*srcstep]); \
  275. }
  276. DECL_TEMPLATE(ALbyte)
  277. DECL_TEMPLATE(ALshort)
  278. DECL_TEMPLATE(ALfloat)
  279. #undef DECL_TEMPLATE
  280. static void LoadSamples(ALfloat *dst, const ALvoid *src, ALuint srcstep, enum FmtType srctype, ALuint samples)
  281. {
  282. switch(srctype)
  283. {
  284. case FmtByte:
  285. Load_ALbyte(dst, src, srcstep, samples);
  286. break;
  287. case FmtShort:
  288. Load_ALshort(dst, src, srcstep, samples);
  289. break;
  290. case FmtFloat:
  291. Load_ALfloat(dst, src, srcstep, samples);
  292. break;
  293. }
  294. }
  295. static inline void SilenceSamples(ALfloat *dst, ALuint samples)
  296. {
  297. ALuint i;
  298. for(i = 0;i < samples;i++)
  299. dst[i] = 0.0f;
  300. }
  301. static const ALfloat *DoFilters(ALfilterState *lpfilter, ALfilterState *hpfilter,
  302. ALfloat *restrict dst, const ALfloat *restrict src,
  303. ALuint numsamples, enum ActiveFilters type)
  304. {
  305. ALuint i;
  306. switch(type)
  307. {
  308. case AF_None:
  309. ALfilterState_processPassthru(lpfilter, src, numsamples);
  310. ALfilterState_processPassthru(hpfilter, src, numsamples);
  311. break;
  312. case AF_LowPass:
  313. ALfilterState_process(lpfilter, dst, src, numsamples);
  314. ALfilterState_processPassthru(hpfilter, dst, numsamples);
  315. return dst;
  316. case AF_HighPass:
  317. ALfilterState_processPassthru(lpfilter, src, numsamples);
  318. ALfilterState_process(hpfilter, dst, src, numsamples);
  319. return dst;
  320. case AF_BandPass:
  321. for(i = 0;i < numsamples;)
  322. {
  323. ALfloat temp[256];
  324. ALuint todo = minu(256, numsamples-i);
  325. ALfilterState_process(lpfilter, temp, src+i, todo);
  326. ALfilterState_process(hpfilter, dst+i, temp, todo);
  327. i += todo;
  328. }
  329. return dst;
  330. }
  331. return src;
  332. }
  333. ALvoid MixSource(ALvoice *voice, ALsource *Source, ALCdevice *Device, ALuint SamplesToDo)
  334. {
  335. ResamplerFunc Resample;
  336. ALbufferlistitem *BufferListItem;
  337. ALuint DataPosInt, DataPosFrac;
  338. ALboolean Looping;
  339. ALuint increment;
  340. ALenum State;
  341. ALuint OutPos;
  342. ALuint NumChannels;
  343. ALuint SampleSize;
  344. ALint64 DataSize64;
  345. ALuint Counter;
  346. ALuint IrSize;
  347. ALuint chan, send, j;
  348. /* Get source info */
  349. State = AL_PLAYING; /* Only called while playing. */
  350. BufferListItem = ATOMIC_LOAD(&Source->current_buffer);
  351. DataPosInt = ATOMIC_LOAD(&Source->position, almemory_order_relaxed);
  352. DataPosFrac = ATOMIC_LOAD(&Source->position_fraction, almemory_order_relaxed);
  353. Looping = ATOMIC_LOAD(&Source->looping, almemory_order_relaxed);
  354. NumChannels = Source->NumChannels;
  355. SampleSize = Source->SampleSize;
  356. increment = voice->Step;
  357. IrSize = (Device->Hrtf.Handle ? Device->Hrtf.Handle->irSize : 0);
  358. Resample = ((increment == FRACTIONONE && DataPosFrac == 0) ?
  359. Resample_copy32_C : ResampleSamples);
  360. Counter = voice->Moving ? SamplesToDo : 0;
  361. OutPos = 0;
  362. do {
  363. ALuint SrcBufferSize, DstBufferSize;
  364. /* Figure out how many buffer samples will be needed */
  365. DataSize64 = SamplesToDo-OutPos;
  366. DataSize64 *= increment;
  367. DataSize64 += DataPosFrac+FRACTIONMASK;
  368. DataSize64 >>= FRACTIONBITS;
  369. DataSize64 += MAX_POST_SAMPLES+MAX_PRE_SAMPLES;
  370. SrcBufferSize = (ALuint)mini64(DataSize64, BUFFERSIZE);
  371. /* Figure out how many samples we can actually mix from this. */
  372. DataSize64 = SrcBufferSize;
  373. DataSize64 -= MAX_POST_SAMPLES+MAX_PRE_SAMPLES;
  374. DataSize64 <<= FRACTIONBITS;
  375. DataSize64 -= DataPosFrac;
  376. DstBufferSize = (ALuint)((DataSize64+(increment-1)) / increment);
  377. DstBufferSize = minu(DstBufferSize, (SamplesToDo-OutPos));
  378. /* Some mixers like having a multiple of 4, so try to give that unless
  379. * this is the last update. */
  380. if(OutPos+DstBufferSize < SamplesToDo)
  381. DstBufferSize &= ~3;
  382. for(chan = 0;chan < NumChannels;chan++)
  383. {
  384. const ALfloat *ResampledData;
  385. ALfloat *SrcData = Device->SourceData;
  386. ALuint SrcDataSize;
  387. /* Load the previous samples into the source data first. */
  388. memcpy(SrcData, voice->PrevSamples[chan], MAX_PRE_SAMPLES*sizeof(ALfloat));
  389. SrcDataSize = MAX_PRE_SAMPLES;
  390. if(Source->SourceType == AL_STATIC)
  391. {
  392. const ALbuffer *ALBuffer = BufferListItem->buffer;
  393. const ALubyte *Data = ALBuffer->data;
  394. ALuint DataSize;
  395. ALuint pos;
  396. /* Offset buffer data to current channel */
  397. Data += chan*SampleSize;
  398. /* If current pos is beyond the loop range, do not loop */
  399. if(Looping == AL_FALSE || DataPosInt >= (ALuint)ALBuffer->LoopEnd)
  400. {
  401. Looping = AL_FALSE;
  402. /* Load what's left to play from the source buffer, and
  403. * clear the rest of the temp buffer */
  404. pos = DataPosInt;
  405. DataSize = minu(SrcBufferSize - SrcDataSize, ALBuffer->SampleLen - pos);
  406. LoadSamples(&SrcData[SrcDataSize], &Data[pos * NumChannels*SampleSize],
  407. NumChannels, ALBuffer->FmtType, DataSize);
  408. SrcDataSize += DataSize;
  409. SilenceSamples(&SrcData[SrcDataSize], SrcBufferSize - SrcDataSize);
  410. SrcDataSize += SrcBufferSize - SrcDataSize;
  411. }
  412. else
  413. {
  414. ALuint LoopStart = ALBuffer->LoopStart;
  415. ALuint LoopEnd = ALBuffer->LoopEnd;
  416. /* Load what's left of this loop iteration, then load
  417. * repeats of the loop section */
  418. pos = DataPosInt;
  419. DataSize = LoopEnd - pos;
  420. DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
  421. LoadSamples(&SrcData[SrcDataSize], &Data[pos * NumChannels*SampleSize],
  422. NumChannels, ALBuffer->FmtType, DataSize);
  423. SrcDataSize += DataSize;
  424. DataSize = LoopEnd-LoopStart;
  425. while(SrcBufferSize > SrcDataSize)
  426. {
  427. DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
  428. LoadSamples(&SrcData[SrcDataSize], &Data[LoopStart * NumChannels*SampleSize],
  429. NumChannels, ALBuffer->FmtType, DataSize);
  430. SrcDataSize += DataSize;
  431. }
  432. }
  433. }
  434. else
  435. {
  436. /* Crawl the buffer queue to fill in the temp buffer */
  437. ALbufferlistitem *tmpiter = BufferListItem;
  438. ALuint pos = DataPosInt;
  439. while(tmpiter && SrcBufferSize > SrcDataSize)
  440. {
  441. const ALbuffer *ALBuffer;
  442. if((ALBuffer=tmpiter->buffer) != NULL)
  443. {
  444. const ALubyte *Data = ALBuffer->data;
  445. ALuint DataSize = ALBuffer->SampleLen;
  446. /* Skip the data already played */
  447. if(DataSize <= pos)
  448. pos -= DataSize;
  449. else
  450. {
  451. Data += (pos*NumChannels + chan)*SampleSize;
  452. DataSize -= pos;
  453. pos -= pos;
  454. DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
  455. LoadSamples(&SrcData[SrcDataSize], Data, NumChannels,
  456. ALBuffer->FmtType, DataSize);
  457. SrcDataSize += DataSize;
  458. }
  459. }
  460. tmpiter = tmpiter->next;
  461. if(!tmpiter && Looping)
  462. tmpiter = ATOMIC_LOAD(&Source->queue);
  463. else if(!tmpiter)
  464. {
  465. SilenceSamples(&SrcData[SrcDataSize], SrcBufferSize - SrcDataSize);
  466. SrcDataSize += SrcBufferSize - SrcDataSize;
  467. }
  468. }
  469. }
  470. /* Store the last source samples used for next time. */
  471. memcpy(voice->PrevSamples[chan],
  472. &SrcData[(increment*DstBufferSize + DataPosFrac)>>FRACTIONBITS],
  473. MAX_PRE_SAMPLES*sizeof(ALfloat)
  474. );
  475. /* Now resample, then filter and mix to the appropriate outputs. */
  476. ResampledData = Resample(&voice->SincState,
  477. &SrcData[MAX_PRE_SAMPLES], DataPosFrac, increment,
  478. Device->ResampledData, DstBufferSize
  479. );
  480. {
  481. DirectParams *parms = &voice->Chan[chan].Direct;
  482. const ALfloat *samples;
  483. samples = DoFilters(
  484. &parms->LowPass, &parms->HighPass, Device->FilteredData,
  485. ResampledData, DstBufferSize, parms->FilterType
  486. );
  487. if(!voice->IsHrtf)
  488. {
  489. if(!Counter)
  490. memcpy(parms->Gains.Current, parms->Gains.Target,
  491. sizeof(parms->Gains.Current));
  492. MixSamples(samples, voice->DirectOut.Channels, voice->DirectOut.Buffer,
  493. parms->Gains.Current, parms->Gains.Target, Counter, OutPos, DstBufferSize
  494. );
  495. }
  496. else
  497. {
  498. MixHrtfParams hrtfparams;
  499. int lidx, ridx;
  500. if(!Counter)
  501. {
  502. parms->Hrtf.Current = parms->Hrtf.Target;
  503. for(j = 0;j < HRIR_LENGTH;j++)
  504. {
  505. hrtfparams.Steps.Coeffs[j][0] = 0.0f;
  506. hrtfparams.Steps.Coeffs[j][1] = 0.0f;
  507. }
  508. hrtfparams.Steps.Delay[0] = 0;
  509. hrtfparams.Steps.Delay[1] = 0;
  510. }
  511. else
  512. {
  513. ALfloat delta = 1.0f / (ALfloat)Counter;
  514. ALfloat coeffdiff;
  515. ALint delaydiff;
  516. for(j = 0;j < IrSize;j++)
  517. {
  518. coeffdiff = parms->Hrtf.Target.Coeffs[j][0] - parms->Hrtf.Current.Coeffs[j][0];
  519. hrtfparams.Steps.Coeffs[j][0] = coeffdiff * delta;
  520. coeffdiff = parms->Hrtf.Target.Coeffs[j][1] - parms->Hrtf.Current.Coeffs[j][1];
  521. hrtfparams.Steps.Coeffs[j][1] = coeffdiff * delta;
  522. }
  523. delaydiff = (ALint)(parms->Hrtf.Target.Delay[0] - parms->Hrtf.Current.Delay[0]);
  524. hrtfparams.Steps.Delay[0] = fastf2i((ALfloat)delaydiff * delta);
  525. delaydiff = (ALint)(parms->Hrtf.Target.Delay[1] - parms->Hrtf.Current.Delay[1]);
  526. hrtfparams.Steps.Delay[1] = fastf2i((ALfloat)delaydiff * delta);
  527. }
  528. hrtfparams.Target = &parms->Hrtf.Target;
  529. hrtfparams.Current = &parms->Hrtf.Current;
  530. lidx = GetChannelIdxByName(Device->RealOut, FrontLeft);
  531. ridx = GetChannelIdxByName(Device->RealOut, FrontRight);
  532. assert(lidx != -1 && ridx != -1);
  533. MixHrtfSamples(voice->DirectOut.Buffer, lidx, ridx, samples, Counter,
  534. voice->Offset, OutPos, IrSize, &hrtfparams,
  535. &parms->Hrtf.State, DstBufferSize);
  536. }
  537. }
  538. for(send = 0;send < Device->NumAuxSends;send++)
  539. {
  540. SendParams *parms = &voice->Chan[chan].Send[send];
  541. const ALfloat *samples;
  542. if(!voice->SendOut[send].Buffer)
  543. continue;
  544. samples = DoFilters(
  545. &parms->LowPass, &parms->HighPass, Device->FilteredData,
  546. ResampledData, DstBufferSize, parms->FilterType
  547. );
  548. if(!Counter)
  549. memcpy(parms->Gains.Current, parms->Gains.Target,
  550. sizeof(parms->Gains.Current));
  551. MixSamples(samples, voice->SendOut[send].Channels, voice->SendOut[send].Buffer,
  552. parms->Gains.Current, parms->Gains.Target, Counter, OutPos, DstBufferSize
  553. );
  554. }
  555. }
  556. /* Update positions */
  557. DataPosFrac += increment*DstBufferSize;
  558. DataPosInt += DataPosFrac>>FRACTIONBITS;
  559. DataPosFrac &= FRACTIONMASK;
  560. OutPos += DstBufferSize;
  561. voice->Offset += DstBufferSize;
  562. Counter = maxu(DstBufferSize, Counter) - DstBufferSize;
  563. /* Handle looping sources */
  564. while(1)
  565. {
  566. const ALbuffer *ALBuffer;
  567. ALuint DataSize = 0;
  568. ALuint LoopStart = 0;
  569. ALuint LoopEnd = 0;
  570. if((ALBuffer=BufferListItem->buffer) != NULL)
  571. {
  572. DataSize = ALBuffer->SampleLen;
  573. LoopStart = ALBuffer->LoopStart;
  574. LoopEnd = ALBuffer->LoopEnd;
  575. if(LoopEnd > DataPosInt)
  576. break;
  577. }
  578. if(Looping && Source->SourceType == AL_STATIC)
  579. {
  580. assert(LoopEnd > LoopStart);
  581. DataPosInt = ((DataPosInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
  582. break;
  583. }
  584. if(DataSize > DataPosInt)
  585. break;
  586. if(!(BufferListItem=BufferListItem->next))
  587. {
  588. if(Looping)
  589. BufferListItem = ATOMIC_LOAD(&Source->queue);
  590. else
  591. {
  592. State = AL_STOPPED;
  593. BufferListItem = NULL;
  594. DataPosInt = 0;
  595. DataPosFrac = 0;
  596. break;
  597. }
  598. }
  599. DataPosInt -= DataSize;
  600. }
  601. } while(State == AL_PLAYING && OutPos < SamplesToDo);
  602. voice->Moving = AL_TRUE;
  603. /* Update source info */
  604. Source->state = State;
  605. ATOMIC_STORE(&Source->current_buffer, BufferListItem, almemory_order_relaxed);
  606. ATOMIC_STORE(&Source->position, DataPosInt, almemory_order_relaxed);
  607. ATOMIC_STORE(&Source->position_fraction, DataPosFrac);
  608. }