mixer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <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 "bs2b.h"
  35. static inline ALfloat Sample_ALbyte(ALbyte val)
  36. { return val * (1.0f/127.0f); }
  37. static inline ALfloat Sample_ALshort(ALshort val)
  38. { return val * (1.0f/32767.0f); }
  39. static inline ALfloat Sample_ALfloat(ALfloat val)
  40. { return val; }
  41. #define DECL_TEMPLATE(T) \
  42. static void Load_##T(ALfloat *dst, const T *src, ALuint srcstep, ALuint samples)\
  43. { \
  44. ALuint i; \
  45. for(i = 0;i < samples;i++) \
  46. dst[i] = Sample_##T(src[i*srcstep]); \
  47. }
  48. DECL_TEMPLATE(ALbyte)
  49. DECL_TEMPLATE(ALshort)
  50. DECL_TEMPLATE(ALfloat)
  51. #undef DECL_TEMPLATE
  52. static void LoadData(ALfloat *dst, const ALvoid *src, ALuint srcstep, enum FmtType srctype, ALuint samples)
  53. {
  54. switch(srctype)
  55. {
  56. case FmtByte:
  57. Load_ALbyte(dst, src, srcstep, samples);
  58. break;
  59. case FmtShort:
  60. Load_ALshort(dst, src, srcstep, samples);
  61. break;
  62. case FmtFloat:
  63. Load_ALfloat(dst, src, srcstep, samples);
  64. break;
  65. }
  66. }
  67. static void SilenceData(ALfloat *dst, ALuint samples)
  68. {
  69. ALuint i;
  70. for(i = 0;i < samples;i++)
  71. dst[i] = 0.0f;
  72. }
  73. static void DoFilter(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *restrict src,
  74. ALuint numsamples)
  75. {
  76. ALuint i;
  77. for(i = 0;i < numsamples;i++)
  78. dst[i] = ALfilterState_processSingle(filter, src[i]);
  79. dst[i] = ALfilterState_processSingleC(filter, src[i]);
  80. }
  81. ALvoid MixSource(ALsource *Source, ALCdevice *Device, ALuint SamplesToDo)
  82. {
  83. ALbufferlistitem *BufferListItem;
  84. ALuint DataPosInt, DataPosFrac;
  85. ALuint BuffersPlayed;
  86. ALboolean Looping;
  87. ALuint increment;
  88. enum Resampler Resampler;
  89. ALenum State;
  90. ALuint OutPos;
  91. ALuint NumChannels;
  92. ALuint SampleSize;
  93. ALint64 DataSize64;
  94. ALuint chan, j;
  95. /* Get source info */
  96. State = Source->state;
  97. BuffersPlayed = Source->BuffersPlayed;
  98. DataPosInt = Source->position;
  99. DataPosFrac = Source->position_fraction;
  100. Looping = Source->Looping;
  101. increment = Source->Params.Step;
  102. Resampler = (increment==FRACTIONONE) ? PointResampler : Source->Resampler;
  103. NumChannels = Source->NumChannels;
  104. SampleSize = Source->SampleSize;
  105. /* Get current buffer queue item */
  106. BufferListItem = Source->queue;
  107. for(j = 0;j < BuffersPlayed;j++)
  108. BufferListItem = BufferListItem->next;
  109. OutPos = 0;
  110. do {
  111. const ALuint BufferPrePadding = ResamplerPrePadding[Resampler];
  112. const ALuint BufferPadding = ResamplerPadding[Resampler];
  113. ALuint SrcBufferSize, DstBufferSize;
  114. /* Figure out how many buffer samples will be needed */
  115. DataSize64 = SamplesToDo-OutPos+1;
  116. DataSize64 *= increment;
  117. DataSize64 += DataPosFrac+FRACTIONMASK;
  118. DataSize64 >>= FRACTIONBITS;
  119. DataSize64 += BufferPadding+BufferPrePadding;
  120. SrcBufferSize = (ALuint)mini64(DataSize64, BUFFERSIZE);
  121. /* Figure out how many samples we can actually mix from this. */
  122. DataSize64 = SrcBufferSize;
  123. DataSize64 -= BufferPadding+BufferPrePadding;
  124. DataSize64 <<= FRACTIONBITS;
  125. DataSize64 -= increment;
  126. DataSize64 -= DataPosFrac;
  127. DstBufferSize = (ALuint)((DataSize64+(increment-1)) / increment);
  128. DstBufferSize = minu(DstBufferSize, (SamplesToDo-OutPos));
  129. /* Some mixers like having a multiple of 4, so try to give that unless
  130. * this is the last update. */
  131. if(OutPos+DstBufferSize < SamplesToDo)
  132. DstBufferSize &= ~3;
  133. for(chan = 0;chan < NumChannels;chan++)
  134. {
  135. ALfloat *SrcData = Device->SampleData1;
  136. ALfloat *ResampledData = Device->SampleData2;
  137. ALuint SrcDataSize = 0;
  138. if(Source->SourceType == AL_STATIC)
  139. {
  140. const ALbuffer *ALBuffer = Source->queue->buffer;
  141. const ALubyte *Data = ALBuffer->data;
  142. ALuint DataSize;
  143. ALuint pos;
  144. /* If current pos is beyond the loop range, do not loop */
  145. if(Looping == AL_FALSE || DataPosInt >= (ALuint)ALBuffer->LoopEnd)
  146. {
  147. Looping = AL_FALSE;
  148. if(DataPosInt >= BufferPrePadding)
  149. pos = DataPosInt - BufferPrePadding;
  150. else
  151. {
  152. DataSize = BufferPrePadding - DataPosInt;
  153. DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
  154. SilenceData(&SrcData[SrcDataSize], DataSize);
  155. SrcDataSize += DataSize;
  156. pos = 0;
  157. }
  158. /* Copy what's left to play in the source buffer, and clear the
  159. * rest of the temp buffer */
  160. DataSize = minu(SrcBufferSize - SrcDataSize, ALBuffer->SampleLen - pos);
  161. LoadData(&SrcData[SrcDataSize], &Data[(pos*NumChannels + chan)*SampleSize],
  162. NumChannels, ALBuffer->FmtType, DataSize);
  163. SrcDataSize += DataSize;
  164. SilenceData(&SrcData[SrcDataSize], SrcBufferSize - SrcDataSize);
  165. SrcDataSize += SrcBufferSize - SrcDataSize;
  166. }
  167. else
  168. {
  169. ALuint LoopStart = ALBuffer->LoopStart;
  170. ALuint LoopEnd = ALBuffer->LoopEnd;
  171. if(DataPosInt >= LoopStart)
  172. {
  173. pos = DataPosInt-LoopStart;
  174. while(pos < BufferPrePadding)
  175. pos += LoopEnd-LoopStart;
  176. pos -= BufferPrePadding;
  177. pos += LoopStart;
  178. }
  179. else if(DataPosInt >= BufferPrePadding)
  180. pos = DataPosInt - BufferPrePadding;
  181. else
  182. {
  183. DataSize = BufferPrePadding - DataPosInt;
  184. DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
  185. SilenceData(&SrcData[SrcDataSize], DataSize);
  186. SrcDataSize += DataSize;
  187. pos = 0;
  188. }
  189. /* Copy what's left of this loop iteration, then copy repeats
  190. * of the loop section */
  191. DataSize = LoopEnd - pos;
  192. DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
  193. LoadData(&SrcData[SrcDataSize], &Data[(pos*NumChannels + chan)*SampleSize],
  194. NumChannels, ALBuffer->FmtType, DataSize);
  195. SrcDataSize += DataSize;
  196. DataSize = LoopEnd-LoopStart;
  197. while(SrcBufferSize > SrcDataSize)
  198. {
  199. DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
  200. LoadData(&SrcData[SrcDataSize], &Data[(LoopStart*NumChannels + chan)*SampleSize],
  201. NumChannels, ALBuffer->FmtType, DataSize);
  202. SrcDataSize += DataSize;
  203. }
  204. }
  205. }
  206. else
  207. {
  208. /* Crawl the buffer queue to fill in the temp buffer */
  209. ALbufferlistitem *tmpiter = BufferListItem;
  210. ALuint pos;
  211. if(DataPosInt >= BufferPrePadding)
  212. pos = DataPosInt - BufferPrePadding;
  213. else
  214. {
  215. pos = BufferPrePadding - DataPosInt;
  216. while(pos > 0)
  217. {
  218. if(!tmpiter->prev && !Looping)
  219. {
  220. ALuint DataSize = minu(SrcBufferSize - SrcDataSize, pos);
  221. SilenceData(&SrcData[SrcDataSize], DataSize);
  222. SrcDataSize += DataSize;
  223. pos = 0;
  224. break;
  225. }
  226. if(tmpiter->prev)
  227. tmpiter = tmpiter->prev;
  228. else
  229. {
  230. while(tmpiter->next)
  231. tmpiter = tmpiter->next;
  232. }
  233. if(tmpiter->buffer)
  234. {
  235. if((ALuint)tmpiter->buffer->SampleLen > pos)
  236. {
  237. pos = tmpiter->buffer->SampleLen - pos;
  238. break;
  239. }
  240. pos -= tmpiter->buffer->SampleLen;
  241. }
  242. }
  243. }
  244. while(tmpiter && SrcBufferSize > SrcDataSize)
  245. {
  246. const ALbuffer *ALBuffer;
  247. if((ALBuffer=tmpiter->buffer) != NULL)
  248. {
  249. const ALubyte *Data = ALBuffer->data;
  250. ALuint DataSize = ALBuffer->SampleLen;
  251. /* Skip the data already played */
  252. if(DataSize <= pos)
  253. pos -= DataSize;
  254. else
  255. {
  256. Data += (pos*NumChannels + chan)*SampleSize;
  257. DataSize -= pos;
  258. pos -= pos;
  259. DataSize = minu(SrcBufferSize - SrcDataSize, DataSize);
  260. LoadData(&SrcData[SrcDataSize], Data, NumChannels,
  261. ALBuffer->FmtType, DataSize);
  262. SrcDataSize += DataSize;
  263. }
  264. }
  265. tmpiter = tmpiter->next;
  266. if(!tmpiter && Looping)
  267. tmpiter = Source->queue;
  268. else if(!tmpiter)
  269. {
  270. SilenceData(&SrcData[SrcDataSize], SrcBufferSize - SrcDataSize);
  271. SrcDataSize += SrcBufferSize - SrcDataSize;
  272. }
  273. }
  274. }
  275. /* Now resample, then filter and mix to the appropriate outputs. */
  276. Source->Params.Resample(&SrcData[BufferPrePadding], DataPosFrac,
  277. increment, ResampledData, DstBufferSize);
  278. {
  279. DirectParams *directparms = &Source->Params.Direct;
  280. DoFilter(&directparms->LpFilter[chan], SrcData, ResampledData,
  281. DstBufferSize);
  282. Source->Params.DryMix(directparms, SrcData, chan, OutPos,
  283. SamplesToDo, DstBufferSize);
  284. }
  285. for(j = 0;j < Device->NumAuxSends;j++)
  286. {
  287. SendParams *sendparms = &Source->Params.Send[j];
  288. if(!sendparms->OutBuffer)
  289. continue;
  290. DoFilter(&sendparms->LpFilter[chan], SrcData, ResampledData,
  291. DstBufferSize);
  292. Source->Params.WetMix(sendparms, SrcData, OutPos,
  293. SamplesToDo, DstBufferSize);
  294. }
  295. }
  296. /* Update positions */
  297. for(j = 0;j < DstBufferSize;j++)
  298. {
  299. DataPosFrac += increment;
  300. DataPosInt += DataPosFrac>>FRACTIONBITS;
  301. DataPosFrac &= FRACTIONMASK;
  302. }
  303. OutPos += DstBufferSize;
  304. /* Handle looping sources */
  305. while(1)
  306. {
  307. const ALbuffer *ALBuffer;
  308. ALuint DataSize = 0;
  309. ALuint LoopStart = 0;
  310. ALuint LoopEnd = 0;
  311. if((ALBuffer=BufferListItem->buffer) != NULL)
  312. {
  313. DataSize = ALBuffer->SampleLen;
  314. LoopStart = ALBuffer->LoopStart;
  315. LoopEnd = ALBuffer->LoopEnd;
  316. if(LoopEnd > DataPosInt)
  317. break;
  318. }
  319. if(Looping && Source->SourceType == AL_STATIC)
  320. {
  321. DataPosInt = ((DataPosInt-LoopStart)%(LoopEnd-LoopStart)) + LoopStart;
  322. break;
  323. }
  324. if(DataSize > DataPosInt)
  325. break;
  326. if(BufferListItem->next)
  327. {
  328. BufferListItem = BufferListItem->next;
  329. BuffersPlayed++;
  330. }
  331. else if(Looping)
  332. {
  333. BufferListItem = Source->queue;
  334. BuffersPlayed = 0;
  335. }
  336. else
  337. {
  338. State = AL_STOPPED;
  339. BufferListItem = Source->queue;
  340. BuffersPlayed = Source->BuffersInQueue;
  341. DataPosInt = 0;
  342. DataPosFrac = 0;
  343. break;
  344. }
  345. DataPosInt -= DataSize;
  346. }
  347. } while(State == AL_PLAYING && OutPos < SamplesToDo);
  348. /* Update source info */
  349. Source->state = State;
  350. Source->BuffersPlayed = BuffersPlayed;
  351. Source->position = DataPosInt;
  352. Source->position_fraction = DataPosFrac;
  353. Source->Hrtf.Offset += OutPos;
  354. if(State == AL_PLAYING)
  355. Source->Hrtf.Counter = maxu(Source->Hrtf.Counter, OutPos) - OutPos;
  356. else
  357. {
  358. Source->Hrtf.Counter = 0;
  359. Source->Hrtf.Moving = AL_FALSE;
  360. }
  361. }