mixvoice.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 "sample_cvt.h"
  34. #include "alu.h"
  35. #include "alconfig.h"
  36. #include "ringbuffer.h"
  37. #include "cpu_caps.h"
  38. #include "mixer/defs.h"
  39. static_assert((INT_MAX>>FRACTIONBITS)/MAX_PITCH > BUFFERSIZE,
  40. "MAX_PITCH and/or BUFFERSIZE are too large for FRACTIONBITS!");
  41. extern inline void InitiatePositionArrays(ALsizei frac, ALint increment, ALsizei *restrict frac_arr, ALint *restrict pos_arr, ALsizei size);
  42. /* BSinc24 requires up to 23 extra samples before the current position, and 24 after. */
  43. static_assert(MAX_RESAMPLE_PADDING >= 24, "MAX_RESAMPLE_PADDING must be at least 24!");
  44. enum Resampler ResamplerDefault = LinearResampler;
  45. MixerFunc MixSamples = Mix_C;
  46. RowMixerFunc MixRowSamples = MixRow_C;
  47. static HrtfMixerFunc MixHrtfSamples = MixHrtf_C;
  48. static HrtfMixerBlendFunc MixHrtfBlendSamples = MixHrtfBlend_C;
  49. static MixerFunc SelectMixer(void)
  50. {
  51. #ifdef HAVE_NEON
  52. if((CPUCapFlags&CPU_CAP_NEON))
  53. return Mix_Neon;
  54. #endif
  55. #ifdef HAVE_SSE
  56. if((CPUCapFlags&CPU_CAP_SSE))
  57. return Mix_SSE;
  58. #endif
  59. return Mix_C;
  60. }
  61. static RowMixerFunc SelectRowMixer(void)
  62. {
  63. #ifdef HAVE_NEON
  64. if((CPUCapFlags&CPU_CAP_NEON))
  65. return MixRow_Neon;
  66. #endif
  67. #ifdef HAVE_SSE
  68. if((CPUCapFlags&CPU_CAP_SSE))
  69. return MixRow_SSE;
  70. #endif
  71. return MixRow_C;
  72. }
  73. static inline HrtfMixerFunc SelectHrtfMixer(void)
  74. {
  75. #ifdef HAVE_NEON
  76. if((CPUCapFlags&CPU_CAP_NEON))
  77. return MixHrtf_Neon;
  78. #endif
  79. #ifdef HAVE_SSE
  80. if((CPUCapFlags&CPU_CAP_SSE))
  81. return MixHrtf_SSE;
  82. #endif
  83. return MixHrtf_C;
  84. }
  85. static inline HrtfMixerBlendFunc SelectHrtfBlendMixer(void)
  86. {
  87. #ifdef HAVE_NEON
  88. if((CPUCapFlags&CPU_CAP_NEON))
  89. return MixHrtfBlend_Neon;
  90. #endif
  91. #ifdef HAVE_SSE
  92. if((CPUCapFlags&CPU_CAP_SSE))
  93. return MixHrtfBlend_SSE;
  94. #endif
  95. return MixHrtfBlend_C;
  96. }
  97. ResamplerFunc SelectResampler(enum Resampler resampler)
  98. {
  99. switch(resampler)
  100. {
  101. case PointResampler:
  102. return Resample_point_C;
  103. case LinearResampler:
  104. #ifdef HAVE_NEON
  105. if((CPUCapFlags&CPU_CAP_NEON))
  106. return Resample_lerp_Neon;
  107. #endif
  108. #ifdef HAVE_SSE4_1
  109. if((CPUCapFlags&CPU_CAP_SSE4_1))
  110. return Resample_lerp_SSE41;
  111. #endif
  112. #ifdef HAVE_SSE2
  113. if((CPUCapFlags&CPU_CAP_SSE2))
  114. return Resample_lerp_SSE2;
  115. #endif
  116. return Resample_lerp_C;
  117. case FIR4Resampler:
  118. return Resample_cubic_C;
  119. case BSinc12Resampler:
  120. case BSinc24Resampler:
  121. #ifdef HAVE_NEON
  122. if((CPUCapFlags&CPU_CAP_NEON))
  123. return Resample_bsinc_Neon;
  124. #endif
  125. #ifdef HAVE_SSE
  126. if((CPUCapFlags&CPU_CAP_SSE))
  127. return Resample_bsinc_SSE;
  128. #endif
  129. return Resample_bsinc_C;
  130. }
  131. return Resample_point_C;
  132. }
  133. void aluInitMixer(void)
  134. {
  135. const char *str;
  136. if(ConfigValueStr(NULL, NULL, "resampler", &str))
  137. {
  138. if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
  139. ResamplerDefault = PointResampler;
  140. else if(strcasecmp(str, "linear") == 0)
  141. ResamplerDefault = LinearResampler;
  142. else if(strcasecmp(str, "cubic") == 0)
  143. ResamplerDefault = FIR4Resampler;
  144. else if(strcasecmp(str, "bsinc12") == 0)
  145. ResamplerDefault = BSinc12Resampler;
  146. else if(strcasecmp(str, "bsinc24") == 0)
  147. ResamplerDefault = BSinc24Resampler;
  148. else if(strcasecmp(str, "bsinc") == 0)
  149. {
  150. WARN("Resampler option \"%s\" is deprecated, using bsinc12\n", str);
  151. ResamplerDefault = BSinc12Resampler;
  152. }
  153. else if(strcasecmp(str, "sinc4") == 0 || strcasecmp(str, "sinc8") == 0)
  154. {
  155. WARN("Resampler option \"%s\" is deprecated, using cubic\n", str);
  156. ResamplerDefault = FIR4Resampler;
  157. }
  158. else
  159. {
  160. char *end;
  161. long n = strtol(str, &end, 0);
  162. if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == FIR4Resampler))
  163. ResamplerDefault = n;
  164. else
  165. WARN("Invalid resampler: %s\n", str);
  166. }
  167. }
  168. MixHrtfBlendSamples = SelectHrtfBlendMixer();
  169. MixHrtfSamples = SelectHrtfMixer();
  170. MixSamples = SelectMixer();
  171. MixRowSamples = SelectRowMixer();
  172. }
  173. static void SendAsyncEvent(ALCcontext *context, ALuint enumtype, ALenum type,
  174. ALuint objid, ALuint param, const char *msg)
  175. {
  176. AsyncEvent evt;
  177. evt.EnumType = enumtype;
  178. evt.Type = type;
  179. evt.ObjectId = objid;
  180. evt.Param = param;
  181. strcpy(evt.Message, msg);
  182. if(ll_ringbuffer_write(context->AsyncEvents, (const char*)&evt, 1) == 1)
  183. alsem_post(&context->EventSem);
  184. }
  185. static inline ALfloat Sample_ALubyte(ALubyte val)
  186. { return (val-128) * (1.0f/128.0f); }
  187. static inline ALfloat Sample_ALshort(ALshort val)
  188. { return val * (1.0f/32768.0f); }
  189. static inline ALfloat Sample_ALfloat(ALfloat val)
  190. { return val; }
  191. static inline ALfloat Sample_ALdouble(ALdouble val)
  192. { return (ALfloat)val; }
  193. typedef ALubyte ALmulaw;
  194. static inline ALfloat Sample_ALmulaw(ALmulaw val)
  195. { return muLawDecompressionTable[val] * (1.0f/32768.0f); }
  196. typedef ALubyte ALalaw;
  197. static inline ALfloat Sample_ALalaw(ALalaw val)
  198. { return aLawDecompressionTable[val] * (1.0f/32768.0f); }
  199. #define DECL_TEMPLATE(T) \
  200. static inline void Load_##T(ALfloat *restrict dst, const T *restrict src, \
  201. ALint srcstep, ALsizei samples) \
  202. { \
  203. ALsizei i; \
  204. for(i = 0;i < samples;i++) \
  205. dst[i] += Sample_##T(src[i*srcstep]); \
  206. }
  207. DECL_TEMPLATE(ALubyte)
  208. DECL_TEMPLATE(ALshort)
  209. DECL_TEMPLATE(ALfloat)
  210. DECL_TEMPLATE(ALdouble)
  211. DECL_TEMPLATE(ALmulaw)
  212. DECL_TEMPLATE(ALalaw)
  213. #undef DECL_TEMPLATE
  214. static void LoadSamples(ALfloat *restrict dst, const ALvoid *restrict src, ALint srcstep,
  215. enum FmtType srctype, ALsizei samples)
  216. {
  217. #define HANDLE_FMT(ET, ST) case ET: Load_##ST(dst, src, srcstep, samples); break
  218. switch(srctype)
  219. {
  220. HANDLE_FMT(FmtUByte, ALubyte);
  221. HANDLE_FMT(FmtShort, ALshort);
  222. HANDLE_FMT(FmtFloat, ALfloat);
  223. HANDLE_FMT(FmtDouble, ALdouble);
  224. HANDLE_FMT(FmtMulaw, ALmulaw);
  225. HANDLE_FMT(FmtAlaw, ALalaw);
  226. }
  227. #undef HANDLE_FMT
  228. }
  229. static const ALfloat *DoFilters(BiquadFilter *lpfilter, BiquadFilter *hpfilter,
  230. ALfloat *restrict dst, const ALfloat *restrict src,
  231. ALsizei numsamples, enum ActiveFilters type)
  232. {
  233. ALsizei i;
  234. switch(type)
  235. {
  236. case AF_None:
  237. BiquadFilter_passthru(lpfilter, numsamples);
  238. BiquadFilter_passthru(hpfilter, numsamples);
  239. break;
  240. case AF_LowPass:
  241. BiquadFilter_process(lpfilter, dst, src, numsamples);
  242. BiquadFilter_passthru(hpfilter, numsamples);
  243. return dst;
  244. case AF_HighPass:
  245. BiquadFilter_passthru(lpfilter, numsamples);
  246. BiquadFilter_process(hpfilter, dst, src, numsamples);
  247. return dst;
  248. case AF_BandPass:
  249. for(i = 0;i < numsamples;)
  250. {
  251. ALfloat temp[256];
  252. ALsizei todo = mini(256, numsamples-i);
  253. BiquadFilter_process(lpfilter, temp, src+i, todo);
  254. BiquadFilter_process(hpfilter, dst+i, temp, todo);
  255. i += todo;
  256. }
  257. return dst;
  258. }
  259. return src;
  260. }
  261. /* This function uses these device temp buffers. */
  262. #define SOURCE_DATA_BUF 0
  263. #define RESAMPLED_BUF 1
  264. #define FILTERED_BUF 2
  265. #define NFC_DATA_BUF 3
  266. ALboolean MixSource(ALvoice *voice, ALuint SourceID, ALCcontext *Context, ALsizei SamplesToDo)
  267. {
  268. ALCdevice *Device = Context->Device;
  269. ALbufferlistitem *BufferListItem;
  270. ALbufferlistitem *BufferLoopItem;
  271. ALsizei NumChannels, SampleSize;
  272. ALbitfieldSOFT enabledevt;
  273. ALsizei buffers_done = 0;
  274. ResamplerFunc Resample;
  275. ALsizei DataPosInt;
  276. ALsizei DataPosFrac;
  277. ALint64 DataSize64;
  278. ALint increment;
  279. ALsizei Counter;
  280. ALsizei OutPos;
  281. ALsizei IrSize;
  282. bool isplaying;
  283. bool firstpass;
  284. bool isstatic;
  285. ALsizei chan;
  286. ALsizei send;
  287. /* Get source info */
  288. isplaying = true; /* Will only be called while playing. */
  289. isstatic = !!(voice->Flags&VOICE_IS_STATIC);
  290. DataPosInt = ATOMIC_LOAD(&voice->position, almemory_order_acquire);
  291. DataPosFrac = ATOMIC_LOAD(&voice->position_fraction, almemory_order_relaxed);
  292. BufferListItem = ATOMIC_LOAD(&voice->current_buffer, almemory_order_relaxed);
  293. BufferLoopItem = ATOMIC_LOAD(&voice->loop_buffer, almemory_order_relaxed);
  294. NumChannels = voice->NumChannels;
  295. SampleSize = voice->SampleSize;
  296. increment = voice->Step;
  297. IrSize = (Device->HrtfHandle ? Device->HrtfHandle->irSize : 0);
  298. Resample = ((increment == FRACTIONONE && DataPosFrac == 0) ?
  299. Resample_copy_C : voice->Resampler);
  300. Counter = (voice->Flags&VOICE_IS_FADING) ? SamplesToDo : 0;
  301. firstpass = true;
  302. OutPos = 0;
  303. do {
  304. ALsizei SrcBufferSize, DstBufferSize;
  305. /* Figure out how many buffer samples will be needed */
  306. DataSize64 = SamplesToDo-OutPos;
  307. DataSize64 *= increment;
  308. DataSize64 += DataPosFrac+FRACTIONMASK;
  309. DataSize64 >>= FRACTIONBITS;
  310. DataSize64 += MAX_RESAMPLE_PADDING*2;
  311. SrcBufferSize = (ALsizei)mini64(DataSize64, BUFFERSIZE);
  312. /* Figure out how many samples we can actually mix from this. */
  313. DataSize64 = SrcBufferSize;
  314. DataSize64 -= MAX_RESAMPLE_PADDING*2;
  315. DataSize64 <<= FRACTIONBITS;
  316. DataSize64 -= DataPosFrac;
  317. DstBufferSize = (ALsizei)mini64((DataSize64+(increment-1)) / increment,
  318. SamplesToDo - OutPos);
  319. /* Some mixers like having a multiple of 4, so try to give that unless
  320. * this is the last update. */
  321. if(DstBufferSize < SamplesToDo-OutPos)
  322. DstBufferSize &= ~3;
  323. /* It's impossible to have a buffer list item with no entries. */
  324. assert(BufferListItem->num_buffers > 0);
  325. for(chan = 0;chan < NumChannels;chan++)
  326. {
  327. const ALfloat *ResampledData;
  328. ALfloat *SrcData = Device->TempBuffer[SOURCE_DATA_BUF];
  329. ALsizei FilledAmt;
  330. /* Load the previous samples into the source data first, and clear the rest. */
  331. memcpy(SrcData, voice->PrevSamples[chan], MAX_RESAMPLE_PADDING*sizeof(ALfloat));
  332. memset(SrcData+MAX_RESAMPLE_PADDING, 0, (BUFFERSIZE-MAX_RESAMPLE_PADDING)*
  333. sizeof(ALfloat));
  334. FilledAmt = MAX_RESAMPLE_PADDING;
  335. if(isstatic)
  336. {
  337. /* TODO: For static sources, loop points are taken from the
  338. * first buffer (should be adjusted by any buffer offset, to
  339. * possibly be added later).
  340. */
  341. const ALbuffer *Buffer0 = BufferListItem->buffers[0];
  342. const ALsizei LoopStart = Buffer0->LoopStart;
  343. const ALsizei LoopEnd = Buffer0->LoopEnd;
  344. const ALsizei LoopSize = LoopEnd - LoopStart;
  345. /* If current pos is beyond the loop range, do not loop */
  346. if(!BufferLoopItem || DataPosInt >= LoopEnd)
  347. {
  348. ALsizei SizeToDo = SrcBufferSize - FilledAmt;
  349. ALsizei CompLen = 0;
  350. ALsizei i;
  351. BufferLoopItem = NULL;
  352. for(i = 0;i < BufferListItem->num_buffers;i++)
  353. {
  354. const ALbuffer *buffer = BufferListItem->buffers[i];
  355. const ALubyte *Data = buffer->data;
  356. ALsizei DataSize;
  357. if(DataPosInt >= buffer->SampleLen)
  358. continue;
  359. /* Load what's left to play from the buffer */
  360. DataSize = mini(SizeToDo, buffer->SampleLen - DataPosInt);
  361. CompLen = maxi(CompLen, DataSize);
  362. LoadSamples(&SrcData[FilledAmt],
  363. &Data[(DataPosInt*NumChannels + chan)*SampleSize],
  364. NumChannels, buffer->FmtType, DataSize
  365. );
  366. }
  367. FilledAmt += CompLen;
  368. }
  369. else
  370. {
  371. ALsizei SizeToDo = mini(SrcBufferSize - FilledAmt, LoopEnd - DataPosInt);
  372. ALsizei CompLen = 0;
  373. ALsizei i;
  374. for(i = 0;i < BufferListItem->num_buffers;i++)
  375. {
  376. const ALbuffer *buffer = BufferListItem->buffers[i];
  377. const ALubyte *Data = buffer->data;
  378. ALsizei DataSize;
  379. if(DataPosInt >= buffer->SampleLen)
  380. continue;
  381. /* Load what's left of this loop iteration */
  382. DataSize = mini(SizeToDo, buffer->SampleLen - DataPosInt);
  383. CompLen = maxi(CompLen, DataSize);
  384. LoadSamples(&SrcData[FilledAmt],
  385. &Data[(DataPosInt*NumChannels + chan)*SampleSize],
  386. NumChannels, buffer->FmtType, DataSize
  387. );
  388. }
  389. FilledAmt += CompLen;
  390. while(SrcBufferSize > FilledAmt)
  391. {
  392. const ALsizei SizeToDo = mini(SrcBufferSize - FilledAmt, LoopSize);
  393. CompLen = 0;
  394. for(i = 0;i < BufferListItem->num_buffers;i++)
  395. {
  396. const ALbuffer *buffer = BufferListItem->buffers[i];
  397. const ALubyte *Data = buffer->data;
  398. ALsizei DataSize;
  399. if(LoopStart >= buffer->SampleLen)
  400. continue;
  401. DataSize = mini(SizeToDo, buffer->SampleLen - LoopStart);
  402. CompLen = maxi(CompLen, DataSize);
  403. LoadSamples(&SrcData[FilledAmt],
  404. &Data[(LoopStart*NumChannels + chan)*SampleSize],
  405. NumChannels, buffer->FmtType, DataSize
  406. );
  407. }
  408. FilledAmt += CompLen;
  409. }
  410. }
  411. }
  412. else
  413. {
  414. /* Crawl the buffer queue to fill in the temp buffer */
  415. ALbufferlistitem *tmpiter = BufferListItem;
  416. ALsizei pos = DataPosInt;
  417. while(tmpiter && SrcBufferSize > FilledAmt)
  418. {
  419. ALsizei SizeToDo = SrcBufferSize - FilledAmt;
  420. ALsizei i;
  421. for(i = 0;i < tmpiter->num_buffers;i++)
  422. {
  423. const ALbuffer *ALBuffer = tmpiter->buffers[i];
  424. ALsizei DataSize = ALBuffer ? ALBuffer->SampleLen : 0;
  425. if(DataSize > pos)
  426. {
  427. const ALubyte *Data = ALBuffer->data;
  428. Data += (pos*NumChannels + chan)*SampleSize;
  429. DataSize = minu(SizeToDo, DataSize - pos);
  430. LoadSamples(&SrcData[FilledAmt], Data, NumChannels,
  431. ALBuffer->FmtType, DataSize);
  432. }
  433. }
  434. if(pos > tmpiter->max_samples)
  435. pos -= tmpiter->max_samples;
  436. else
  437. {
  438. FilledAmt += tmpiter->max_samples - pos;
  439. pos = 0;
  440. }
  441. if(SrcBufferSize > FilledAmt)
  442. {
  443. tmpiter = ATOMIC_LOAD(&tmpiter->next, almemory_order_acquire);
  444. if(!tmpiter) tmpiter = BufferLoopItem;
  445. }
  446. }
  447. }
  448. /* Store the last source samples used for next time. */
  449. memcpy(voice->PrevSamples[chan],
  450. &SrcData[(increment*DstBufferSize + DataPosFrac)>>FRACTIONBITS],
  451. MAX_RESAMPLE_PADDING*sizeof(ALfloat)
  452. );
  453. /* Now resample, then filter and mix to the appropriate outputs. */
  454. ResampledData = Resample(&voice->ResampleState,
  455. &SrcData[MAX_RESAMPLE_PADDING], DataPosFrac, increment,
  456. Device->TempBuffer[RESAMPLED_BUF], DstBufferSize
  457. );
  458. {
  459. DirectParams *parms = &voice->Direct.Params[chan];
  460. const ALfloat *samples;
  461. samples = DoFilters(
  462. &parms->LowPass, &parms->HighPass, Device->TempBuffer[FILTERED_BUF],
  463. ResampledData, DstBufferSize, voice->Direct.FilterType
  464. );
  465. if(!(voice->Flags&VOICE_HAS_HRTF))
  466. {
  467. if(!Counter)
  468. memcpy(parms->Gains.Current, parms->Gains.Target,
  469. sizeof(parms->Gains.Current));
  470. if(!(voice->Flags&VOICE_HAS_NFC))
  471. MixSamples(samples, voice->Direct.Channels, voice->Direct.Buffer,
  472. parms->Gains.Current, parms->Gains.Target, Counter, OutPos,
  473. DstBufferSize
  474. );
  475. else
  476. {
  477. ALfloat *nfcsamples = Device->TempBuffer[NFC_DATA_BUF];
  478. ALsizei chanoffset = 0;
  479. MixSamples(samples,
  480. voice->Direct.ChannelsPerOrder[0], voice->Direct.Buffer,
  481. parms->Gains.Current, parms->Gains.Target, Counter, OutPos,
  482. DstBufferSize
  483. );
  484. chanoffset += voice->Direct.ChannelsPerOrder[0];
  485. #define APPLY_NFC_MIX(order) \
  486. if(voice->Direct.ChannelsPerOrder[order] > 0) \
  487. { \
  488. NfcFilterProcess##order(&parms->NFCtrlFilter, nfcsamples, samples, \
  489. DstBufferSize); \
  490. MixSamples(nfcsamples, voice->Direct.ChannelsPerOrder[order], \
  491. voice->Direct.Buffer+chanoffset, parms->Gains.Current+chanoffset, \
  492. parms->Gains.Target+chanoffset, Counter, OutPos, DstBufferSize \
  493. ); \
  494. chanoffset += voice->Direct.ChannelsPerOrder[order]; \
  495. }
  496. APPLY_NFC_MIX(1)
  497. APPLY_NFC_MIX(2)
  498. APPLY_NFC_MIX(3)
  499. #undef APPLY_NFC_MIX
  500. }
  501. }
  502. else
  503. {
  504. MixHrtfParams hrtfparams;
  505. ALsizei fademix = 0;
  506. int lidx, ridx;
  507. lidx = GetChannelIdxByName(&Device->RealOut, FrontLeft);
  508. ridx = GetChannelIdxByName(&Device->RealOut, FrontRight);
  509. assert(lidx != -1 && ridx != -1);
  510. if(!Counter)
  511. {
  512. /* No fading, just overwrite the old HRTF params. */
  513. parms->Hrtf.Old = parms->Hrtf.Target;
  514. }
  515. else if(!(parms->Hrtf.Old.Gain > GAIN_SILENCE_THRESHOLD))
  516. {
  517. /* The old HRTF params are silent, so overwrite the old
  518. * coefficients with the new, and reset the old gain to
  519. * 0. The future mix will then fade from silence.
  520. */
  521. parms->Hrtf.Old = parms->Hrtf.Target;
  522. parms->Hrtf.Old.Gain = 0.0f;
  523. }
  524. else if(firstpass)
  525. {
  526. ALfloat gain;
  527. /* Fade between the coefficients over 128 samples. */
  528. fademix = mini(DstBufferSize, 128);
  529. /* The new coefficients need to fade in completely
  530. * since they're replacing the old ones. To keep the
  531. * gain fading consistent, interpolate between the old
  532. * and new target gains given how much of the fade time
  533. * this mix handles.
  534. */
  535. gain = lerp(parms->Hrtf.Old.Gain, parms->Hrtf.Target.Gain,
  536. minf(1.0f, (ALfloat)fademix/Counter));
  537. hrtfparams.Coeffs = parms->Hrtf.Target.Coeffs;
  538. hrtfparams.Delay[0] = parms->Hrtf.Target.Delay[0];
  539. hrtfparams.Delay[1] = parms->Hrtf.Target.Delay[1];
  540. hrtfparams.Gain = 0.0f;
  541. hrtfparams.GainStep = gain / (ALfloat)fademix;
  542. MixHrtfBlendSamples(
  543. voice->Direct.Buffer[lidx], voice->Direct.Buffer[ridx],
  544. samples, voice->Offset, OutPos, IrSize, &parms->Hrtf.Old,
  545. &hrtfparams, &parms->Hrtf.State, fademix
  546. );
  547. /* Update the old parameters with the result. */
  548. parms->Hrtf.Old = parms->Hrtf.Target;
  549. if(fademix < Counter)
  550. parms->Hrtf.Old.Gain = hrtfparams.Gain;
  551. }
  552. if(fademix < DstBufferSize)
  553. {
  554. ALsizei todo = DstBufferSize - fademix;
  555. ALfloat gain = parms->Hrtf.Target.Gain;
  556. /* Interpolate the target gain if the gain fading lasts
  557. * longer than this mix.
  558. */
  559. if(Counter > DstBufferSize)
  560. gain = lerp(parms->Hrtf.Old.Gain, gain,
  561. (ALfloat)todo/(Counter-fademix));
  562. hrtfparams.Coeffs = parms->Hrtf.Target.Coeffs;
  563. hrtfparams.Delay[0] = parms->Hrtf.Target.Delay[0];
  564. hrtfparams.Delay[1] = parms->Hrtf.Target.Delay[1];
  565. hrtfparams.Gain = parms->Hrtf.Old.Gain;
  566. hrtfparams.GainStep = (gain - parms->Hrtf.Old.Gain) / (ALfloat)todo;
  567. MixHrtfSamples(
  568. voice->Direct.Buffer[lidx], voice->Direct.Buffer[ridx],
  569. samples+fademix, voice->Offset+fademix, OutPos+fademix, IrSize,
  570. &hrtfparams, &parms->Hrtf.State, todo
  571. );
  572. /* Store the interpolated gain or the final target gain
  573. * depending if the fade is done.
  574. */
  575. if(DstBufferSize < Counter)
  576. parms->Hrtf.Old.Gain = gain;
  577. else
  578. parms->Hrtf.Old.Gain = parms->Hrtf.Target.Gain;
  579. }
  580. }
  581. }
  582. for(send = 0;send < Device->NumAuxSends;send++)
  583. {
  584. SendParams *parms = &voice->Send[send].Params[chan];
  585. const ALfloat *samples;
  586. if(!voice->Send[send].Buffer)
  587. continue;
  588. samples = DoFilters(
  589. &parms->LowPass, &parms->HighPass, Device->TempBuffer[FILTERED_BUF],
  590. ResampledData, DstBufferSize, voice->Send[send].FilterType
  591. );
  592. if(!Counter)
  593. memcpy(parms->Gains.Current, parms->Gains.Target,
  594. sizeof(parms->Gains.Current));
  595. MixSamples(samples, voice->Send[send].Channels, voice->Send[send].Buffer,
  596. parms->Gains.Current, parms->Gains.Target, Counter, OutPos, DstBufferSize
  597. );
  598. }
  599. }
  600. /* Update positions */
  601. DataPosFrac += increment*DstBufferSize;
  602. DataPosInt += DataPosFrac>>FRACTIONBITS;
  603. DataPosFrac &= FRACTIONMASK;
  604. OutPos += DstBufferSize;
  605. voice->Offset += DstBufferSize;
  606. Counter = maxi(DstBufferSize, Counter) - DstBufferSize;
  607. firstpass = false;
  608. if(isstatic)
  609. {
  610. if(BufferLoopItem)
  611. {
  612. /* Handle looping static source */
  613. const ALbuffer *Buffer = BufferListItem->buffers[0];
  614. ALsizei LoopStart = Buffer->LoopStart;
  615. ALsizei LoopEnd = Buffer->LoopEnd;
  616. if(DataPosInt >= LoopEnd)
  617. {
  618. assert(LoopEnd > LoopStart);
  619. DataPosInt = ((DataPosInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
  620. }
  621. }
  622. else
  623. {
  624. /* Handle non-looping static source */
  625. if(DataPosInt >= BufferListItem->max_samples)
  626. {
  627. isplaying = false;
  628. BufferListItem = NULL;
  629. DataPosInt = 0;
  630. DataPosFrac = 0;
  631. break;
  632. }
  633. }
  634. }
  635. else while(1)
  636. {
  637. /* Handle streaming source */
  638. if(BufferListItem->max_samples > DataPosInt)
  639. break;
  640. buffers_done += BufferListItem->num_buffers;
  641. BufferListItem = ATOMIC_LOAD(&BufferListItem->next, almemory_order_acquire);
  642. if(!BufferListItem && !(BufferListItem=BufferLoopItem))
  643. {
  644. isplaying = false;
  645. DataPosInt = 0;
  646. DataPosFrac = 0;
  647. break;
  648. }
  649. DataPosInt -= BufferListItem->max_samples;
  650. }
  651. } while(isplaying && OutPos < SamplesToDo);
  652. voice->Flags |= VOICE_IS_FADING;
  653. /* Update source info */
  654. ATOMIC_STORE(&voice->position, DataPosInt, almemory_order_relaxed);
  655. ATOMIC_STORE(&voice->position_fraction, DataPosFrac, almemory_order_relaxed);
  656. ATOMIC_STORE(&voice->current_buffer, BufferListItem, almemory_order_release);
  657. /* Send any events now, after the position/buffer info was updated. */
  658. enabledevt = ATOMIC_LOAD(&Context->EnabledEvts, almemory_order_acquire);
  659. if(buffers_done > 0 && (enabledevt&EventType_BufferCompleted))
  660. SendAsyncEvent(Context, EventType_BufferCompleted,
  661. AL_EVENT_TYPE_BUFFER_COMPLETED_SOFT, SourceID, buffers_done, "Buffer completed"
  662. );
  663. return isplaying;
  664. }