ALu.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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 <unistd.h>
  27. #include "alMain.h"
  28. #include "AL/al.h"
  29. #include "AL/alc.h"
  30. #include "alSource.h"
  31. #include "alBuffer.h"
  32. #include "alListener.h"
  33. #include "alAuxEffectSlot.h"
  34. #include "alu.h"
  35. #include "bs2b.h"
  36. #ifdef MAX_SOURCES_LOW
  37. // For throttling AlSource.c
  38. int alc_max_sources = MAX_SOURCES_LOW;
  39. int alc_active_sources = 0;
  40. int alc_num_cores = 0;
  41. #endif
  42. static __inline ALvoid aluCrossproduct(const ALfp *inVector1, const ALfp *inVector2, ALfp *outVector)
  43. {
  44. outVector[0] = (ALfpMult(inVector1[1],inVector2[2]) - ALfpMult(inVector1[2],inVector2[1]));
  45. outVector[1] = (ALfpMult(inVector1[2],inVector2[0]) - ALfpMult(inVector1[0],inVector2[2]));
  46. outVector[2] = (ALfpMult(inVector1[0],inVector2[1]) - ALfpMult(inVector1[1],inVector2[0]));
  47. }
  48. static __inline ALfp aluDotproduct(const ALfp *inVector1, const ALfp *inVector2)
  49. {
  50. return (ALfpMult(inVector1[0],inVector2[0]) + ALfpMult(inVector1[1],inVector2[1]) +
  51. ALfpMult(inVector1[2],inVector2[2]));
  52. }
  53. static __inline ALvoid aluNormalize(ALfp *inVector)
  54. {
  55. ALfp length, inverse_length;
  56. length = aluSqrt(aluDotproduct(inVector, inVector));
  57. if(length != int2ALfp(0))
  58. {
  59. inverse_length = ALfpDiv(int2ALfp(1),length);
  60. inVector[0] = ALfpMult(inVector[0], inverse_length);
  61. inVector[1] = ALfpMult(inVector[1], inverse_length);
  62. inVector[2] = ALfpMult(inVector[2], inverse_length);
  63. }
  64. }
  65. static __inline ALvoid aluMatrixVector(ALfp *vector,ALfp w,ALfp matrix[4][4])
  66. {
  67. ALfp temp[4] = {
  68. vector[0], vector[1], vector[2], w
  69. };
  70. vector[0] = ALfpMult(temp[0],matrix[0][0]) + ALfpMult(temp[1],matrix[1][0]) + ALfpMult(temp[2],matrix[2][0]) + ALfpMult(temp[3],matrix[3][0]);
  71. vector[1] = ALfpMult(temp[0],matrix[0][1]) + ALfpMult(temp[1],matrix[1][1]) + ALfpMult(temp[2],matrix[2][1]) + ALfpMult(temp[3],matrix[3][1]);
  72. vector[2] = ALfpMult(temp[0],matrix[0][2]) + ALfpMult(temp[1],matrix[1][2]) + ALfpMult(temp[2],matrix[2][2]) + ALfpMult(temp[3],matrix[3][2]);
  73. }
  74. ALvoid CalcNonAttnSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
  75. {
  76. ALfp SourceVolume,ListenerGain,MinVolume,MaxVolume;
  77. ALbufferlistitem *BufferListItem;
  78. enum DevFmtChannels DevChans;
  79. enum FmtChannels Channels;
  80. ALfp DryGain, DryGainHF;
  81. ALfp WetGain[MAX_SENDS];
  82. ALfp WetGainHF[MAX_SENDS];
  83. ALint NumSends, Frequency;
  84. ALboolean DupStereo;
  85. ALfp Pitch;
  86. ALfp cw;
  87. ALint i;
  88. /* Get device properties */
  89. DevChans = ALContext->Device->FmtChans;
  90. DupStereo = ALContext->Device->DuplicateStereo;
  91. NumSends = ALContext->Device->NumAuxSends;
  92. Frequency = ALContext->Device->Frequency;
  93. /* Get listener properties */
  94. ListenerGain = ALContext->Listener.Gain;
  95. /* Get source properties */
  96. SourceVolume = ALSource->flGain;
  97. MinVolume = ALSource->flMinGain;
  98. MaxVolume = ALSource->flMaxGain;
  99. Pitch = ALSource->flPitch;
  100. /* Calculate the stepping value */
  101. Channels = FmtMono;
  102. BufferListItem = ALSource->queue;
  103. while(BufferListItem != NULL)
  104. {
  105. ALbuffer *ALBuffer;
  106. if((ALBuffer=BufferListItem->buffer) != NULL)
  107. {
  108. ALint maxstep = STACK_DATA_SIZE / FrameSizeFromFmt(ALBuffer->FmtChannels,
  109. ALBuffer->FmtType);
  110. maxstep -= ResamplerPadding[ALSource->Resampler] +
  111. ResamplerPrePadding[ALSource->Resampler] + 1;
  112. maxstep = min(maxstep, INT_MAX>>FRACTIONBITS);
  113. Pitch = ALfpDiv(ALfpMult(Pitch, int2ALfp(ALBuffer->Frequency)), int2ALfp(Frequency));
  114. if(Pitch > int2ALfp(maxstep))
  115. ALSource->Params.Step = maxstep<<FRACTIONBITS;
  116. else
  117. {
  118. ALSource->Params.Step = ALfp2int(ALfpMult(Pitch, int2ALfp(FRACTIONONE)));
  119. if(ALSource->Params.Step == 0)
  120. ALSource->Params.Step = 1;
  121. }
  122. Channels = ALBuffer->FmtChannels;
  123. break;
  124. }
  125. BufferListItem = BufferListItem->next;
  126. }
  127. /* Calculate gains */
  128. DryGain = SourceVolume;
  129. DryGain = __min(DryGain,MaxVolume);
  130. DryGain = __max(DryGain,MinVolume);
  131. DryGainHF = int2ALfp(1);
  132. switch(ALSource->DirectFilter.type)
  133. {
  134. case AL_FILTER_LOWPASS:
  135. DryGain = ALfpMult(DryGain, ALSource->DirectFilter.Gain);
  136. DryGainHF = ALfpMult(DryGainHF, ALSource->DirectFilter.GainHF);
  137. break;
  138. }
  139. for(i = 0;i < MAXCHANNELS;i++)
  140. {
  141. ALuint i2;
  142. for(i2 = 0;i2 < MAXCHANNELS;i2++)
  143. ALSource->Params.DryGains[i][i2] = int2ALfp(0);
  144. }
  145. switch(Channels)
  146. {
  147. case FmtMono:
  148. ALSource->Params.DryGains[0][FRONT_CENTER] = ALfpMult(DryGain, ListenerGain);
  149. break;
  150. case FmtStereo:
  151. if(DupStereo == AL_FALSE)
  152. {
  153. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  154. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  155. }
  156. else
  157. {
  158. switch(DevChans)
  159. {
  160. case DevFmtMono:
  161. case DevFmtStereo:
  162. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  163. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  164. break;
  165. #ifdef STEREO_ONLY
  166. case DevFmtQuad:
  167. case DevFmtX51:
  168. case DevFmtX61:
  169. case DevFmtX71:
  170. break;
  171. #else
  172. case DevFmtQuad:
  173. case DevFmtX51:
  174. DryGain = ALfpMult(DryGain, aluSqrt(float2ALfp(2.0f/4.0f)));
  175. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  176. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  177. ALSource->Params.DryGains[0][BACK_LEFT] = ALfpMult(DryGain, ListenerGain);
  178. ALSource->Params.DryGains[1][BACK_RIGHT] = ALfpMult(DryGain, ListenerGain);
  179. break;
  180. case DevFmtX61:
  181. DryGain = ALfpMult(DryGain, aluSqrt(float2ALfp(2.0f/4.0f)));
  182. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  183. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  184. ALSource->Params.DryGains[0][SIDE_LEFT] = ALfpMult(DryGain, ListenerGain);
  185. ALSource->Params.DryGains[1][SIDE_RIGHT] = ALfpMult(DryGain, ListenerGain);
  186. break;
  187. case DevFmtX71:
  188. DryGain = ALfpMult(DryGain, aluSqrt(float2ALfp(2.0f/6.0f)));
  189. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  190. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  191. ALSource->Params.DryGains[0][BACK_LEFT] = ALfpMult(DryGain, ListenerGain);
  192. ALSource->Params.DryGains[1][BACK_RIGHT] = ALfpMult(DryGain, ListenerGain);
  193. ALSource->Params.DryGains[0][SIDE_LEFT] = ALfpMult(DryGain, ListenerGain);
  194. ALSource->Params.DryGains[1][SIDE_RIGHT] = ALfpMult(DryGain, ListenerGain);
  195. break;
  196. #endif
  197. }
  198. }
  199. break;
  200. case FmtRear:
  201. #ifndef STEREO_ONLY
  202. ALSource->Params.DryGains[0][BACK_LEFT] = ALfpMult(DryGain, ListenerGain);
  203. ALSource->Params.DryGains[1][BACK_RIGHT] = ALfpMult(DryGain, ListenerGain);
  204. #endif
  205. break;
  206. case FmtQuad:
  207. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  208. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  209. #ifndef STEREO_ONLY
  210. ALSource->Params.DryGains[2][BACK_LEFT] = ALfpMult(DryGain, ListenerGain);
  211. ALSource->Params.DryGains[3][BACK_RIGHT] = ALfpMult(DryGain, ListenerGain);
  212. #endif
  213. break;
  214. case FmtX51:
  215. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  216. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  217. #ifndef STEREO_ONLY
  218. ALSource->Params.DryGains[2][FRONT_CENTER] = ALfpMult(DryGain, ListenerGain);
  219. ALSource->Params.DryGains[3][LFE] = ALfpMult(DryGain, ListenerGain);
  220. ALSource->Params.DryGains[4][BACK_LEFT] = ALfpMult(DryGain, ListenerGain);
  221. ALSource->Params.DryGains[5][BACK_RIGHT] = ALfpMult(DryGain, ListenerGain);
  222. #endif
  223. break;
  224. case FmtX61:
  225. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  226. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  227. #ifndef STEREO_ONLY
  228. ALSource->Params.DryGains[2][FRONT_CENTER] = ALfpMult(DryGain, ListenerGain);
  229. ALSource->Params.DryGains[3][LFE] = ALfpMult(DryGain, ListenerGain);
  230. ALSource->Params.DryGains[4][BACK_CENTER] = ALfpMult(DryGain, ListenerGain);
  231. ALSource->Params.DryGains[5][SIDE_LEFT] = ALfpMult(DryGain, ListenerGain);
  232. ALSource->Params.DryGains[6][SIDE_RIGHT] = ALfpMult(DryGain, ListenerGain);
  233. #endif
  234. break;
  235. case FmtX71:
  236. ALSource->Params.DryGains[0][FRONT_LEFT] = ALfpMult(DryGain, ListenerGain);
  237. ALSource->Params.DryGains[1][FRONT_RIGHT] = ALfpMult(DryGain, ListenerGain);
  238. #ifndef STEREO_ONLY
  239. ALSource->Params.DryGains[2][FRONT_CENTER] = ALfpMult(DryGain, ListenerGain);
  240. ALSource->Params.DryGains[3][LFE] = ALfpMult(DryGain, ListenerGain);
  241. ALSource->Params.DryGains[4][BACK_LEFT] = ALfpMult(DryGain, ListenerGain);
  242. ALSource->Params.DryGains[5][BACK_RIGHT] = ALfpMult(DryGain, ListenerGain);
  243. ALSource->Params.DryGains[6][SIDE_LEFT] = ALfpMult(DryGain, ListenerGain);
  244. ALSource->Params.DryGains[7][SIDE_RIGHT] = ALfpMult(DryGain, ListenerGain);
  245. #endif
  246. break;
  247. }
  248. for(i = 0;i < NumSends;i++)
  249. {
  250. WetGain[i] = SourceVolume;
  251. WetGain[i] = __min(WetGain[i],MaxVolume);
  252. WetGain[i] = __max(WetGain[i],MinVolume);
  253. WetGainHF[i] = int2ALfp(1);
  254. switch(ALSource->Send[i].WetFilter.type)
  255. {
  256. case AL_FILTER_LOWPASS:
  257. WetGain[i] = ALfpMult(WetGain[i], ALSource->Send[i].WetFilter.Gain);
  258. WetGainHF[i] = ALfpMult(WetGainHF[i], ALSource->Send[i].WetFilter.GainHF);
  259. break;
  260. }
  261. ALSource->Params.Send[i].WetGain = ALfpMult(WetGain[i], ListenerGain);
  262. }
  263. /* Update filter coefficients. Calculations based on the I3DL2
  264. * spec. */
  265. cw = float2ALfp(cos(2.0*M_PI * LOWPASSFREQCUTOFF / Frequency));
  266. /* We use two chained one-pole filters, so we need to take the
  267. * square root of the squared gain, which is the same as the base
  268. * gain. */
  269. ALSource->Params.iirFilter.coeff = lpCoeffCalc(DryGainHF, cw);
  270. for(i = 0;i < NumSends;i++)
  271. {
  272. /* We use a one-pole filter, so we need to take the squared gain */
  273. ALfp a = lpCoeffCalc(ALfpMult(WetGainHF[i],WetGainHF[i]), cw);
  274. ALSource->Params.Send[i].iirFilter.coeff = a;
  275. }
  276. }
  277. ALvoid CalcSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
  278. {
  279. const ALCdevice *Device = ALContext->Device;
  280. ALfp InnerAngle,OuterAngle,Angle,Distance,OrigDist;
  281. ALfp Direction[3],Position[3],SourceToListener[3];
  282. ALfp Velocity[3],ListenerVel[3];
  283. ALfp MinVolume,MaxVolume,MinDist,MaxDist,Rolloff,OuterGainHF;
  284. ALfp ConeVolume,ConeHF,SourceVolume,ListenerGain;
  285. ALfp DopplerFactor, DopplerVelocity, SpeedOfSound;
  286. ALfp AirAbsorptionFactor;
  287. ALbufferlistitem *BufferListItem;
  288. ALfp Attenuation, EffectiveDist;
  289. ALfp RoomAttenuation[MAX_SENDS];
  290. ALfp MetersPerUnit;
  291. ALfp RoomRolloff[MAX_SENDS];
  292. ALfp DryGain;
  293. ALfp DryGainHF;
  294. ALfp WetGain[MAX_SENDS];
  295. ALfp WetGainHF[MAX_SENDS];
  296. ALfp DirGain, AmbientGain;
  297. const ALfp *SpeakerGain;
  298. ALfp Pitch;
  299. ALfp length;
  300. ALuint Frequency;
  301. ALint NumSends;
  302. ALint pos, s, i;
  303. ALfp cw;
  304. DryGainHF = int2ALfp(1);
  305. for(i = 0;i < MAX_SENDS;i++)
  306. WetGainHF[i] = int2ALfp(1);
  307. //Get context properties
  308. DopplerFactor = ALfpMult(ALContext->DopplerFactor, ALSource->DopplerFactor);
  309. DopplerVelocity = ALContext->DopplerVelocity;
  310. SpeedOfSound = ALContext->flSpeedOfSound;
  311. NumSends = Device->NumAuxSends;
  312. Frequency = Device->Frequency;
  313. //Get listener properties
  314. ListenerGain = ALContext->Listener.Gain;
  315. MetersPerUnit = ALContext->Listener.MetersPerUnit;
  316. memcpy(ListenerVel, ALContext->Listener.Velocity, sizeof(ALContext->Listener.Velocity));
  317. //Get source properties
  318. SourceVolume = ALSource->flGain;
  319. memcpy(Position, ALSource->vPosition, sizeof(ALSource->vPosition));
  320. memcpy(Direction, ALSource->vOrientation, sizeof(ALSource->vOrientation));
  321. memcpy(Velocity, ALSource->vVelocity, sizeof(ALSource->vVelocity));
  322. MinVolume = ALSource->flMinGain;
  323. MaxVolume = ALSource->flMaxGain;
  324. MinDist = ALSource->flRefDistance;
  325. MaxDist = ALSource->flMaxDistance;
  326. Rolloff = ALSource->flRollOffFactor;
  327. InnerAngle = ALSource->flInnerAngle;
  328. OuterAngle = ALSource->flOuterAngle;
  329. OuterGainHF = ALSource->OuterGainHF;
  330. AirAbsorptionFactor = ALSource->AirAbsorptionFactor;
  331. //1. Translate Listener to origin (convert to head relative)
  332. if(ALSource->bHeadRelative == AL_FALSE)
  333. {
  334. ALfp U[3],V[3],N[3];
  335. ALfp Matrix[4][4];
  336. // Build transform matrix
  337. memcpy(N, ALContext->Listener.Forward, sizeof(N)); // At-vector
  338. aluNormalize(N); // Normalized At-vector
  339. memcpy(V, ALContext->Listener.Up, sizeof(V)); // Up-vector
  340. aluNormalize(V); // Normalized Up-vector
  341. aluCrossproduct(N, V, U); // Right-vector
  342. aluNormalize(U); // Normalized Right-vector
  343. Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -1*N[0]; Matrix[0][3] = int2ALfp(0);
  344. Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -1*N[1]; Matrix[1][3] = int2ALfp(0);
  345. Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -1*N[2]; Matrix[2][3] = int2ALfp(0);
  346. Matrix[3][0] = int2ALfp(0); Matrix[3][1] = int2ALfp(0); Matrix[3][2] = int2ALfp(0); Matrix[3][3] = int2ALfp(1);
  347. // Translate position
  348. Position[0] -= ALContext->Listener.Position[0];
  349. Position[1] -= ALContext->Listener.Position[1];
  350. Position[2] -= ALContext->Listener.Position[2];
  351. // Transform source position and direction into listener space
  352. aluMatrixVector(Position, int2ALfp(1), Matrix);
  353. aluMatrixVector(Direction, int2ALfp(0), Matrix);
  354. // Transform source and listener velocity into listener space
  355. aluMatrixVector(Velocity, int2ALfp(0), Matrix);
  356. aluMatrixVector(ListenerVel, int2ALfp(0), Matrix);
  357. }
  358. else
  359. ListenerVel[0] = ListenerVel[1] = ListenerVel[2] = int2ALfp(0);
  360. SourceToListener[0] = -1*Position[0];
  361. SourceToListener[1] = -1*Position[1];
  362. SourceToListener[2] = -1*Position[2];
  363. aluNormalize(SourceToListener);
  364. aluNormalize(Direction);
  365. //2. Calculate distance attenuation
  366. Distance = aluSqrt(aluDotproduct(Position, Position));
  367. OrigDist = Distance;
  368. Attenuation = int2ALfp(1);
  369. for(i = 0;i < NumSends;i++)
  370. {
  371. RoomAttenuation[i] = int2ALfp(1);
  372. RoomRolloff[i] = ALSource->RoomRolloffFactor;
  373. if(ALSource->Send[i].Slot &&
  374. (ALSource->Send[i].Slot->effect.type == AL_EFFECT_REVERB ||
  375. ALSource->Send[i].Slot->effect.type == AL_EFFECT_EAXREVERB))
  376. RoomRolloff[i] += ALSource->Send[i].Slot->effect.Reverb.RoomRolloffFactor;
  377. }
  378. switch(ALContext->SourceDistanceModel ? ALSource->DistanceModel :
  379. ALContext->DistanceModel)
  380. {
  381. case AL_INVERSE_DISTANCE_CLAMPED:
  382. Distance=__max(Distance,MinDist);
  383. Distance=__min(Distance,MaxDist);
  384. if(MaxDist < MinDist)
  385. break;
  386. //fall-through
  387. case AL_INVERSE_DISTANCE:
  388. if(MinDist > int2ALfp(0))
  389. {
  390. if((MinDist + ALfpMult(Rolloff, (Distance - MinDist))) > int2ALfp(0))
  391. Attenuation = ALfpDiv(MinDist, (MinDist + ALfpMult(Rolloff, (Distance - MinDist))));
  392. for(i = 0;i < NumSends;i++)
  393. {
  394. if((MinDist + ALfpMult(RoomRolloff[i], (Distance - MinDist))) > int2ALfp(0))
  395. RoomAttenuation[i] = ALfpDiv(MinDist, (MinDist + ALfpMult(RoomRolloff[i], (Distance - MinDist))));
  396. }
  397. }
  398. break;
  399. case AL_LINEAR_DISTANCE_CLAMPED:
  400. Distance=__max(Distance,MinDist);
  401. Distance=__min(Distance,MaxDist);
  402. if(MaxDist < MinDist)
  403. break;
  404. //fall-through
  405. case AL_LINEAR_DISTANCE:
  406. if(MaxDist != MinDist)
  407. {
  408. Attenuation = int2ALfp(1) - ALfpDiv(ALfpMult(Rolloff,(Distance-MinDist)), (MaxDist - MinDist));
  409. Attenuation = __max(Attenuation, int2ALfp(0));
  410. for(i = 0;i < NumSends;i++)
  411. {
  412. RoomAttenuation[i] = int2ALfp(1) - ALfpDiv(ALfpMult(RoomRolloff[i],(Distance-MinDist)),(MaxDist - MinDist));
  413. RoomAttenuation[i] = __max(RoomAttenuation[i], int2ALfp(0));
  414. }
  415. }
  416. break;
  417. case AL_EXPONENT_DISTANCE_CLAMPED:
  418. Distance=__max(Distance,MinDist);
  419. Distance=__min(Distance,MaxDist);
  420. if(MaxDist < MinDist)
  421. break;
  422. //fall-through
  423. case AL_EXPONENT_DISTANCE:
  424. if(Distance > int2ALfp(0) && MinDist > int2ALfp(0))
  425. {
  426. Attenuation = aluPow(ALfpDiv(Distance,MinDist), (-1*Rolloff));
  427. for(i = 0;i < NumSends;i++)
  428. RoomAttenuation[i] = aluPow(ALfpDiv(Distance,MinDist), (-1*RoomRolloff[i]));
  429. }
  430. break;
  431. case AL_NONE:
  432. break;
  433. }
  434. // Source Gain + Attenuation
  435. DryGain = ALfpMult(SourceVolume, Attenuation);
  436. for(i = 0;i < NumSends;i++)
  437. WetGain[i] = ALfpMult(SourceVolume, RoomAttenuation[i]);
  438. EffectiveDist = int2ALfp(0);
  439. if(MinDist > int2ALfp(0) && Attenuation < int2ALfp(1))
  440. EffectiveDist = ALfpMult((ALfpDiv(MinDist,Attenuation) - MinDist),MetersPerUnit);
  441. // Distance-based air absorption
  442. if(AirAbsorptionFactor > int2ALfp(0) && EffectiveDist > int2ALfp(0))
  443. {
  444. ALfp absorb;
  445. // Absorption calculation is done in dB
  446. absorb = ALfpMult(ALfpMult(AirAbsorptionFactor,float2ALfp(AIRABSORBGAINDBHF)),
  447. EffectiveDist);
  448. // Convert dB to linear gain before applying
  449. absorb = aluPow(int2ALfp(10), ALfpDiv(absorb,int2ALfp(20)));
  450. DryGainHF = ALfpMult(DryGainHF,absorb);
  451. }
  452. //3. Apply directional soundcones
  453. Angle = ALfpMult(aluAcos(aluDotproduct(Direction,SourceToListener)), float2ALfp(180.0f/M_PI));
  454. if(Angle >= InnerAngle && Angle <= OuterAngle)
  455. {
  456. ALfp scale; scale = ALfpDiv((Angle-InnerAngle), (OuterAngle-InnerAngle));
  457. ConeVolume = int2ALfp(1) + ALfpMult((ALSource->flOuterGain - int2ALfp(1)),scale);
  458. ConeHF = (int2ALfp(1)+ALfpMult((OuterGainHF-int2ALfp(1)),scale));
  459. }
  460. else if(Angle > OuterAngle)
  461. {
  462. ConeVolume = (int2ALfp(1)+(ALSource->flOuterGain-int2ALfp(1)));
  463. ConeHF = (int2ALfp(1)+(OuterGainHF-int2ALfp(1)));
  464. }
  465. else
  466. {
  467. ConeVolume = int2ALfp(1);
  468. ConeHF = int2ALfp(1);
  469. }
  470. // Apply some high-frequency attenuation for sources behind the listener
  471. // NOTE: This should be aluDotproduct({0,0,-1}, ListenerToSource), however
  472. // that is equivalent to aluDotproduct({0,0,1}, SourceToListener), which is
  473. // the same as SourceToListener[2]
  474. Angle = ALfpMult(aluAcos(SourceToListener[2]), float2ALfp(180.0f/M_PI));
  475. // Sources within the minimum distance attenuate less
  476. if(OrigDist < MinDist)
  477. Angle = ALfpMult(Angle, ALfpDiv(OrigDist,MinDist));
  478. if(Angle > int2ALfp(90))
  479. {
  480. ALfp scale; scale = ALfpDiv((Angle-int2ALfp(90)), float2ALfp(180.1f-90.0f)); // .1 to account for fp errors
  481. ConeHF = ALfpMult(ConeHF, (int2ALfp(1) - ALfpMult(Device->HeadDampen,scale)));
  482. }
  483. DryGain = ALfpMult(DryGain, ConeVolume);
  484. if(ALSource->DryGainHFAuto)
  485. DryGainHF = ALfpMult(DryGainHF, ConeHF);
  486. // Clamp to Min/Max Gain
  487. DryGain = __min(DryGain,MaxVolume);
  488. DryGain = __max(DryGain,MinVolume);
  489. for(i = 0;i < NumSends;i++)
  490. {
  491. ALeffectslot *Slot = ALSource->Send[i].Slot;
  492. if(!Slot || Slot->effect.type == AL_EFFECT_NULL)
  493. {
  494. ALSource->Params.Send[i].WetGain = int2ALfp(0);
  495. WetGainHF[i] = int2ALfp(1);
  496. continue;
  497. }
  498. if(Slot->AuxSendAuto)
  499. {
  500. if(ALSource->WetGainAuto)
  501. WetGain[i] = ALfpMult(WetGain[i], ConeVolume);
  502. if(ALSource->WetGainHFAuto)
  503. WetGainHF[i] = ALfpMult(WetGainHF[i], ConeHF);
  504. // Clamp to Min/Max Gain
  505. WetGain[i] = __min(WetGain[i],MaxVolume);
  506. WetGain[i] = __max(WetGain[i],MinVolume);
  507. if(Slot->effect.type == AL_EFFECT_REVERB ||
  508. Slot->effect.type == AL_EFFECT_EAXREVERB)
  509. {
  510. /* Apply a decay-time transformation to the wet path, based on
  511. * the attenuation of the dry path.
  512. *
  513. * Using the approximate (effective) source to listener
  514. * distance, the initial decay of the reverb effect is
  515. * calculated and applied to the wet path.
  516. */
  517. WetGain[i] = ALfpMult(WetGain[i],
  518. aluPow(int2ALfp(10),
  519. ALfpDiv(ALfpMult(ALfpDiv(EffectiveDist,
  520. ALfpMult(float2ALfp(SPEEDOFSOUNDMETRESPERSEC), Slot->effect.Reverb.DecayTime)),
  521. int2ALfp(-60)),
  522. int2ALfp(20))));
  523. WetGainHF[i] = ALfpMult(WetGainHF[i],
  524. aluPow(Slot->effect.Reverb.AirAbsorptionGainHF,
  525. ALfpMult(AirAbsorptionFactor, EffectiveDist)));
  526. }
  527. }
  528. else
  529. {
  530. /* If the slot's auxiliary send auto is off, the data sent to the
  531. * effect slot is the same as the dry path, sans filter effects */
  532. WetGain[i] = DryGain;
  533. WetGainHF[i] = DryGainHF;
  534. }
  535. switch(ALSource->Send[i].WetFilter.type)
  536. {
  537. case AL_FILTER_LOWPASS:
  538. WetGain[i] = ALfpMult(WetGain[i], ALSource->Send[i].WetFilter.Gain);
  539. WetGainHF[i] = ALfpMult(WetGainHF[i], ALSource->Send[i].WetFilter.GainHF);
  540. break;
  541. }
  542. ALSource->Params.Send[i].WetGain = ALfpMult(WetGain[i], ListenerGain);
  543. }
  544. // Apply filter gains and filters
  545. switch(ALSource->DirectFilter.type)
  546. {
  547. case AL_FILTER_LOWPASS:
  548. DryGain = ALfpMult(DryGain, ALSource->DirectFilter.Gain);
  549. DryGainHF = ALfpMult(DryGainHF, ALSource->DirectFilter.GainHF);
  550. break;
  551. }
  552. DryGain = ALfpMult(DryGain, ListenerGain);
  553. // Calculate Velocity
  554. Pitch = ALSource->flPitch;
  555. if(DopplerFactor != int2ALfp(0))
  556. {
  557. ALfp VSS, VLS;
  558. ALfp MaxVelocity; MaxVelocity = ALfpDiv(ALfpMult(SpeedOfSound,DopplerVelocity),
  559. DopplerFactor);
  560. VSS = aluDotproduct(Velocity, SourceToListener);
  561. if(VSS >= MaxVelocity)
  562. VSS = (MaxVelocity - int2ALfp(1));
  563. else if(VSS <= -MaxVelocity)
  564. VSS = (-MaxVelocity + int2ALfp(1));
  565. VLS = aluDotproduct(ListenerVel, SourceToListener);
  566. if(VLS >= MaxVelocity)
  567. VLS = (MaxVelocity - int2ALfp(1));
  568. else if(VLS <= -MaxVelocity)
  569. VLS = -MaxVelocity + int2ALfp(1);
  570. Pitch = ALfpMult(Pitch,
  571. ALfpDiv((ALfpMult(SpeedOfSound,DopplerVelocity) - ALfpMult(DopplerFactor,VLS)),
  572. (ALfpMult(SpeedOfSound,DopplerVelocity) - ALfpMult(DopplerFactor,VSS))));
  573. }
  574. BufferListItem = ALSource->queue;
  575. while(BufferListItem != NULL)
  576. {
  577. ALbuffer *ALBuffer;
  578. if((ALBuffer=BufferListItem->buffer) != NULL)
  579. {
  580. ALint maxstep = STACK_DATA_SIZE / FrameSizeFromFmt(ALBuffer->FmtChannels,
  581. ALBuffer->FmtType);
  582. maxstep -= ResamplerPadding[ALSource->Resampler] +
  583. ResamplerPrePadding[ALSource->Resampler] + 1;
  584. maxstep = min(maxstep, INT_MAX>>FRACTIONBITS);
  585. Pitch = ALfpDiv(ALfpMult(Pitch, int2ALfp(ALBuffer->Frequency)), int2ALfp(Frequency));
  586. if(Pitch > int2ALfp(maxstep))
  587. ALSource->Params.Step = maxstep<<FRACTIONBITS;
  588. else
  589. {
  590. ALSource->Params.Step = ALfp2int(ALfpMult(Pitch,float2ALfp(FRACTIONONE)));
  591. if(ALSource->Params.Step == 0)
  592. ALSource->Params.Step = 1;
  593. }
  594. break;
  595. }
  596. BufferListItem = BufferListItem->next;
  597. }
  598. // Use energy-preserving panning algorithm for multi-speaker playback
  599. length = __max(OrigDist, MinDist);
  600. if(length > int2ALfp(0))
  601. {
  602. ALfp invlen = ALfpDiv(int2ALfp(1), length);
  603. Position[0] = ALfpMult(Position[0],invlen);
  604. Position[1] = ALfpMult(Position[1],invlen);
  605. Position[2] = ALfpMult(Position[2],invlen);
  606. }
  607. pos = aluCart2LUTpos((-1*Position[2]), Position[0]);
  608. SpeakerGain = &Device->PanningLUT[MAXCHANNELS * pos];
  609. DirGain = aluSqrt((ALfpMult(Position[0],Position[0]) + ALfpMult(Position[2],Position[2])));
  610. // elevation adjustment for directional gain. this sucks, but
  611. // has low complexity
  612. AmbientGain = aluSqrt(float2ALfp(1.0f/Device->NumChan));
  613. for(s = 0;s < MAXCHANNELS;s++)
  614. {
  615. ALuint s2;
  616. for(s2 = 0;s2 < MAXCHANNELS;s2++)
  617. ALSource->Params.DryGains[s][s2] = int2ALfp(0);
  618. }
  619. for(s = 0;s < (ALsizei)Device->NumChan;s++)
  620. {
  621. Channel chan = Device->Speaker2Chan[s];
  622. ALfp gain; gain = AmbientGain + ALfpMult((SpeakerGain[chan]-AmbientGain),DirGain);
  623. ALSource->Params.DryGains[0][chan] = ALfpMult(DryGain, gain);
  624. }
  625. /* Update filter coefficients. */
  626. cw = __cos(ALfpDiv(float2ALfp(2.0*M_PI*LOWPASSFREQCUTOFF), int2ALfp(Frequency)));
  627. /* Spatialized sources use four chained one-pole filters, so we need to
  628. * take the fourth root of the squared gain, which is the same as the
  629. * square root of the base gain. */
  630. ALSource->Params.iirFilter.coeff = lpCoeffCalc(aluSqrt(DryGainHF), cw);
  631. for(i = 0;i < NumSends;i++)
  632. {
  633. /* The wet path uses two chained one-pole filters, so take the
  634. * base gain (square root of the squared gain) */
  635. ALSource->Params.Send[i].iirFilter.coeff = lpCoeffCalc(WetGainHF[i], cw);
  636. }
  637. }
  638. static __inline ALfloat aluF2F(ALfp val)
  639. {
  640. return ALfp2float(val);
  641. }
  642. static __inline ALushort aluF2US(ALfp val)
  643. {
  644. if(val > int2ALfp(1)) return 65535;
  645. if(val < int2ALfp(-1)) return 0;
  646. return (ALushort)(ALfp2int(ALfpMult(val,int2ALfp(32767))) + 32768);
  647. }
  648. static __inline ALshort aluF2S(ALfp val)
  649. {
  650. if(val > int2ALfp(1)) return 32767;
  651. if(val < int2ALfp(-1)) return -32768;
  652. return (ALshort)(ALfp2int(ALfpMult(val,int2ALfp(32767))));
  653. }
  654. static __inline ALubyte aluF2UB(ALfp val)
  655. {
  656. ALushort i = aluF2US(val);
  657. return i>>8;
  658. }
  659. static __inline ALbyte aluF2B(ALfp val)
  660. {
  661. ALshort i = aluF2S(val);
  662. return i>>8;
  663. }
  664. static const Channel MonoChans[] = { FRONT_CENTER };
  665. static const Channel StereoChans[] = { FRONT_LEFT, FRONT_RIGHT };
  666. static const Channel QuadChans[] = { FRONT_LEFT, FRONT_RIGHT,
  667. BACK_LEFT, BACK_RIGHT };
  668. static const Channel X51Chans[] = { FRONT_LEFT, FRONT_RIGHT,
  669. FRONT_CENTER, LFE,
  670. BACK_LEFT, BACK_RIGHT };
  671. static const Channel X61Chans[] = { FRONT_LEFT, FRONT_LEFT,
  672. FRONT_CENTER, LFE, BACK_CENTER,
  673. SIDE_LEFT, SIDE_RIGHT };
  674. static const Channel X71Chans[] = { FRONT_LEFT, FRONT_RIGHT,
  675. FRONT_CENTER, LFE,
  676. BACK_LEFT, BACK_RIGHT,
  677. SIDE_LEFT, SIDE_RIGHT };
  678. #define DECL_TEMPLATE(T, chans,N, func) \
  679. static void Write_##T##_##chans(ALCdevice *device, T *buffer, ALuint SamplesToDo)\
  680. { \
  681. ALfp (*DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
  682. ALfp (*Matrix)[MAXCHANNELS] = device->ChannelMatrix; \
  683. const ALuint *ChanMap = device->DevChannels; \
  684. ALuint i, j, c; \
  685. \
  686. for(i = 0;i < SamplesToDo;i++) \
  687. { \
  688. for(j = 0;j < N;j++) \
  689. { \
  690. ALfp samp; samp = int2ALfp(0); \
  691. for(c = 0;c < MAXCHANNELS;c++) { \
  692. ALfp m = Matrix[c][chans[j]]; \
  693. if (m != 0) \
  694. samp += ALfpMult(DryBuffer[i][c], m); \
  695. } \
  696. ((T*)buffer)[ChanMap[chans[j]]] = func(samp); \
  697. } \
  698. buffer = ((T*)buffer) + N; \
  699. } \
  700. }
  701. DECL_TEMPLATE(ALfloat, MonoChans,1, aluF2F)
  702. DECL_TEMPLATE(ALfloat, QuadChans,4, aluF2F)
  703. DECL_TEMPLATE(ALfloat, X51Chans,6, aluF2F)
  704. DECL_TEMPLATE(ALfloat, X61Chans,7, aluF2F)
  705. DECL_TEMPLATE(ALfloat, X71Chans,8, aluF2F)
  706. DECL_TEMPLATE(ALushort, MonoChans,1, aluF2US)
  707. DECL_TEMPLATE(ALushort, QuadChans,4, aluF2US)
  708. DECL_TEMPLATE(ALushort, X51Chans,6, aluF2US)
  709. DECL_TEMPLATE(ALushort, X61Chans,7, aluF2US)
  710. DECL_TEMPLATE(ALushort, X71Chans,8, aluF2US)
  711. DECL_TEMPLATE(ALshort, MonoChans,1, aluF2S)
  712. DECL_TEMPLATE(ALshort, QuadChans,4, aluF2S)
  713. DECL_TEMPLATE(ALshort, X51Chans,6, aluF2S)
  714. DECL_TEMPLATE(ALshort, X61Chans,7, aluF2S)
  715. DECL_TEMPLATE(ALshort, X71Chans,8, aluF2S)
  716. DECL_TEMPLATE(ALubyte, MonoChans,1, aluF2UB)
  717. DECL_TEMPLATE(ALubyte, QuadChans,4, aluF2UB)
  718. DECL_TEMPLATE(ALubyte, X51Chans,6, aluF2UB)
  719. DECL_TEMPLATE(ALubyte, X61Chans,7, aluF2UB)
  720. DECL_TEMPLATE(ALubyte, X71Chans,8, aluF2UB)
  721. DECL_TEMPLATE(ALbyte, MonoChans,1, aluF2B)
  722. DECL_TEMPLATE(ALbyte, QuadChans,4, aluF2B)
  723. DECL_TEMPLATE(ALbyte, X51Chans,6, aluF2B)
  724. DECL_TEMPLATE(ALbyte, X61Chans,7, aluF2B)
  725. DECL_TEMPLATE(ALbyte, X71Chans,8, aluF2B)
  726. #undef DECL_TEMPLATE
  727. #define DECL_TEMPLATE(T, chans,N, func) \
  728. static void Write_##T##_##chans(ALCdevice *device, T *buffer, ALuint SamplesToDo)\
  729. { \
  730. ALfp (*DryBuffer)[MAXCHANNELS] = device->DryBuffer; \
  731. ALfp (*Matrix)[MAXCHANNELS] = device->ChannelMatrix; \
  732. const ALuint *ChanMap = device->DevChannels; \
  733. ALuint i, j, c; \
  734. \
  735. if(device->Bs2b) \
  736. { \
  737. for(i = 0;i < SamplesToDo;i++) \
  738. { \
  739. ALfp samples[2] = { int2ALfp(0), int2ALfp(0) }; \
  740. for(c = 0;c < MAXCHANNELS;c++) \
  741. { \
  742. samples[0] += ALfpMult(DryBuffer[i][c],Matrix[c][FRONT_LEFT]); \
  743. samples[1] += ALfpMult(DryBuffer[i][c],Matrix[c][FRONT_RIGHT]); \
  744. } \
  745. bs2b_cross_feed(device->Bs2b, samples); \
  746. ((T*)buffer)[ChanMap[FRONT_LEFT]] = func(samples[0]); \
  747. ((T*)buffer)[ChanMap[FRONT_RIGHT]] = func(samples[1]); \
  748. buffer = ((T*)buffer) + 2; \
  749. } \
  750. } \
  751. else \
  752. { \
  753. for(i = 0;i < SamplesToDo;i++) \
  754. { \
  755. for(j = 0;j < N;j++) \
  756. { \
  757. ALfp samp = int2ALfp(0); \
  758. for(c = 0;c < MAXCHANNELS;c++) \
  759. samp += ALfpMult(DryBuffer[i][c], Matrix[c][chans[j]]); \
  760. ((T*)buffer)[ChanMap[chans[j]]] = func(samp); \
  761. } \
  762. buffer = ((T*)buffer) + N; \
  763. } \
  764. } \
  765. }
  766. DECL_TEMPLATE(ALfloat, StereoChans,2, aluF2F)
  767. DECL_TEMPLATE(ALushort, StereoChans,2, aluF2US)
  768. DECL_TEMPLATE(ALshort, StereoChans,2, aluF2S)
  769. DECL_TEMPLATE(ALubyte, StereoChans,2, aluF2UB)
  770. DECL_TEMPLATE(ALbyte, StereoChans,2, aluF2B)
  771. #undef DECL_TEMPLATE
  772. #define DECL_TEMPLATE(T, func) \
  773. static void Write_##T(ALCdevice *device, T *buffer, ALuint SamplesToDo) \
  774. { \
  775. switch(device->FmtChans) \
  776. { \
  777. case DevFmtMono: \
  778. Write_##T##_MonoChans(device, buffer, SamplesToDo); \
  779. break; \
  780. case DevFmtStereo: \
  781. Write_##T##_StereoChans(device, buffer, SamplesToDo); \
  782. break; \
  783. case DevFmtQuad: \
  784. Write_##T##_QuadChans(device, buffer, SamplesToDo); \
  785. break; \
  786. case DevFmtX51: \
  787. Write_##T##_X51Chans(device, buffer, SamplesToDo); \
  788. break; \
  789. case DevFmtX61: \
  790. Write_##T##_X61Chans(device, buffer, SamplesToDo); \
  791. break; \
  792. case DevFmtX71: \
  793. Write_##T##_X71Chans(device, buffer, SamplesToDo); \
  794. break; \
  795. } \
  796. }
  797. DECL_TEMPLATE(ALfloat, aluF2F)
  798. DECL_TEMPLATE(ALushort, aluF2US)
  799. DECL_TEMPLATE(ALshort, aluF2S)
  800. DECL_TEMPLATE(ALubyte, aluF2UB)
  801. DECL_TEMPLATE(ALbyte, aluF2B)
  802. #undef DECL_TEMPLATE
  803. static __inline ALvoid aluMixDataPrivate(ALCdevice *device, ALvoid *buffer, ALsizei size)
  804. {
  805. ALuint SamplesToDo;
  806. ALeffectslot *ALEffectSlot;
  807. ALCcontext **ctx, **ctx_end;
  808. ALsource **src, **src_end;
  809. int fpuState;
  810. ALuint i, c;
  811. ALsizei e;
  812. #if defined(HAVE_FESETROUND)
  813. fpuState = fegetround();
  814. fesetround(FE_TOWARDZERO);
  815. #elif defined(HAVE__CONTROLFP)
  816. fpuState = _controlfp(_RC_CHOP, _MCW_RC);
  817. #else
  818. (void)fpuState;
  819. #endif
  820. while(size > 0)
  821. {
  822. /* Setup variables */
  823. SamplesToDo = min(size, BUFFERSIZE);
  824. /* Clear mixing buffer */
  825. memset(device->DryBuffer, 0, SamplesToDo*MAXCHANNELS*sizeof(ALfp));
  826. SuspendContext(NULL);
  827. ctx = device->Contexts;
  828. ctx_end = ctx + device->NumContexts;
  829. while(ctx != ctx_end)
  830. {
  831. SuspendContext(*ctx);
  832. src = (*ctx)->ActiveSources;
  833. src_end = src + (*ctx)->ActiveSourceCount;
  834. while(src != src_end)
  835. {
  836. if((*src)->state != AL_PLAYING)
  837. {
  838. --((*ctx)->ActiveSourceCount);
  839. *src = *(--src_end);
  840. continue;
  841. }
  842. if((*src)->NeedsUpdate)
  843. {
  844. ALsource_Update(*src, *ctx);
  845. (*src)->NeedsUpdate = AL_FALSE;
  846. }
  847. MixSource(*src, device, SamplesToDo);
  848. src++;
  849. }
  850. /* effect slot processing */
  851. for(e = 0;e < (*ctx)->EffectSlotMap.size;e++)
  852. {
  853. ALEffectSlot = (*ctx)->EffectSlotMap.array[e].value;
  854. for(i = 0;i < SamplesToDo;i++)
  855. {
  856. ALEffectSlot->ClickRemoval[0] -= ALfpDiv(ALEffectSlot->ClickRemoval[0], int2ALfp(256));
  857. ALEffectSlot->WetBuffer[i] += ALEffectSlot->ClickRemoval[0];
  858. }
  859. for(i = 0;i < 1;i++)
  860. {
  861. ALEffectSlot->ClickRemoval[i] += ALEffectSlot->PendingClicks[i];
  862. ALEffectSlot->PendingClicks[i] = int2ALfp(0);
  863. }
  864. ALEffect_Process(ALEffectSlot->EffectState, ALEffectSlot,
  865. SamplesToDo, ALEffectSlot->WetBuffer,
  866. device->DryBuffer);
  867. for(i = 0;i < SamplesToDo;i++)
  868. ALEffectSlot->WetBuffer[i] = int2ALfp(0);
  869. }
  870. ProcessContext(*ctx);
  871. ctx++;
  872. }
  873. ProcessContext(NULL);
  874. //Post processing loop
  875. for(i = 0;i < SamplesToDo;i++)
  876. {
  877. for(c = 0;c < MAXCHANNELS;c++)
  878. {
  879. device->ClickRemoval[c] -= ALfpDiv(device->ClickRemoval[c], int2ALfp(256));
  880. device->DryBuffer[i][c] += device->ClickRemoval[c];
  881. }
  882. }
  883. for(i = 0;i < MAXCHANNELS;i++)
  884. {
  885. device->ClickRemoval[i] += device->PendingClicks[i];
  886. device->PendingClicks[i] = int2ALfp(0);
  887. }
  888. switch(device->FmtType)
  889. {
  890. case DevFmtByte:
  891. Write_ALbyte(device, buffer, SamplesToDo);
  892. break;
  893. case DevFmtUByte:
  894. Write_ALubyte(device, buffer, SamplesToDo);
  895. break;
  896. case DevFmtShort:
  897. Write_ALshort(device, buffer, SamplesToDo);
  898. break;
  899. case DevFmtUShort:
  900. Write_ALushort(device, buffer, SamplesToDo);
  901. break;
  902. case DevFmtFloat:
  903. Write_ALfloat(device, buffer, SamplesToDo);
  904. break;
  905. }
  906. size -= SamplesToDo;
  907. }
  908. #if defined(HAVE_FESETROUND)
  909. fesetround(fpuState);
  910. #elif defined(HAVE__CONTROLFP)
  911. _controlfp(fpuState, _MCW_RC);
  912. #endif
  913. }
  914. static inline long timespecdiff(struct timespec *starttime, struct timespec *finishtime)
  915. {
  916. long usec;
  917. usec=(finishtime->tv_sec-starttime->tv_sec)*1000000;
  918. usec+=(finishtime->tv_nsec-starttime->tv_nsec)/1000;
  919. return usec;
  920. }
  921. ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
  922. {
  923. #ifdef MAX_SOURCES_LOW
  924. // Profile aluMixDataPrivate to set admission control parameters
  925. static struct timespec ts_start;
  926. static struct timespec ts_end;
  927. long ts_diff;
  928. int time_per_source;
  929. int max_sources_within_deadline;
  930. int mix_deadline_usec;
  931. int max;
  932. if (alc_num_cores == 0) {
  933. // FIXME(Apportable) this is Linux specific
  934. alc_num_cores = sysconf( _SC_NPROCESSORS_ONLN );
  935. LOGI("_SC_NPROCESSORS_ONLN=%d", alc_num_cores);
  936. }
  937. if (alc_num_cores > 1) {
  938. // Allow OpenAL to monopolize one core
  939. mix_deadline_usec = ((size*1000000) / device->Frequency) / 2;
  940. } else {
  941. // Try to cap mixing at 20% CPU
  942. mix_deadline_usec = ((size*1000000) / device->Frequency) / 5;
  943. }
  944. clock_gettime(CLOCK_MONOTONIC, &ts_start);
  945. aluMixDataPrivate(device, buffer, size);
  946. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  947. // Time in micro-seconds that aluMixData has taken to run
  948. ts_diff = timespecdiff(&ts_start, &ts_end);
  949. // Try to adjust the max sources limit adaptively, within a range
  950. if (alc_active_sources > 0) {
  951. time_per_source = max(1, ts_diff / alc_active_sources);
  952. max_sources_within_deadline = mix_deadline_usec / time_per_source;
  953. max = min(max(max_sources_within_deadline, MAX_SOURCES_LOW), MAX_SOURCES_HIGH);
  954. if (max > alc_max_sources) {
  955. alc_max_sources++;
  956. } else if (max < alc_max_sources) {
  957. alc_max_sources = max;
  958. }
  959. } else {
  960. alc_max_sources = MAX_SOURCES_START;
  961. }
  962. #else
  963. aluMixDataPrivate(device, buffer, size);
  964. #endif
  965. }
  966. ALvoid aluHandleDisconnect(ALCdevice *device)
  967. {
  968. ALuint i;
  969. SuspendContext(NULL);
  970. for(i = 0;i < device->NumContexts;i++)
  971. {
  972. ALCcontext *Context = device->Contexts[i];
  973. ALsource *source;
  974. ALsizei pos;
  975. SuspendContext(Context);
  976. for(pos = 0;pos < Context->SourceMap.size;pos++)
  977. {
  978. source = Context->SourceMap.array[pos].value;
  979. if(source->state == AL_PLAYING)
  980. {
  981. source->state = AL_STOPPED;
  982. source->BuffersPlayed = source->BuffersInQueue;
  983. source->position = 0;
  984. source->position_fraction = 0;
  985. }
  986. }
  987. ProcessContext(Context);
  988. }
  989. device->Connected = ALC_FALSE;
  990. ProcessContext(NULL);
  991. }