alSource.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <float.h>
  24. #include "alMain.h"
  25. #include "AL/al.h"
  26. #include "AL/alc.h"
  27. #include "alError.h"
  28. #include "alSource.h"
  29. #include "alBuffer.h"
  30. #include "alThunk.h"
  31. #include "alAuxEffectSlot.h"
  32. enum Resampler DefaultResampler = RESAMPLER_DEFAULT;
  33. const ALsizei ResamplerPadding[RESAMPLER_MAX] = {
  34. 0, /* Point */
  35. 1, /* Linear */
  36. 2, /* Cubic */
  37. };
  38. const ALsizei ResamplerPrePadding[RESAMPLER_MAX] = {
  39. 0, /* Point */
  40. 0, /* Linear */
  41. 1, /* Cubic */
  42. };
  43. static ALvoid InitSourceParams(ALsource *Source);
  44. static ALvoid GetSourceOffset(ALsource *Source, ALenum eName, ALdouble *Offsets, ALdouble updateLen);
  45. static ALint GetSampleOffset(ALsource *Source);
  46. AL_API ALvoid AL_APIENTRY alGenSources(ALsizei n,ALuint *sources)
  47. {
  48. ALCcontext *Context;
  49. ALCdevice *Device;
  50. Context = GetContextRef();
  51. if(!Context) return;
  52. Device = Context->Device;
  53. if(n < 0 || IsBadWritePtr((void*)sources, n * sizeof(ALuint)))
  54. alSetError(Context, AL_INVALID_VALUE);
  55. else
  56. {
  57. ALenum err;
  58. ALsizei i;
  59. // Add additional sources to the list
  60. i = 0;
  61. while(i < n)
  62. {
  63. ALsource *source = calloc(1, sizeof(ALsource));
  64. if(!source)
  65. {
  66. alSetError(Context, AL_OUT_OF_MEMORY);
  67. alDeleteSources(i, sources);
  68. break;
  69. }
  70. InitSourceParams(source);
  71. err = NewThunkEntry(&source->source);
  72. if(err == AL_NO_ERROR)
  73. err = InsertUIntMapEntry(&Context->SourceMap, source->source, source);
  74. if(err != AL_NO_ERROR)
  75. {
  76. FreeThunkEntry(source->source);
  77. memset(source, 0, sizeof(ALsource));
  78. free(source);
  79. alSetError(Context, err);
  80. alDeleteSources(i, sources);
  81. break;
  82. }
  83. sources[i++] = source->source;
  84. }
  85. }
  86. ALCcontext_DecRef(Context);
  87. }
  88. AL_API ALvoid AL_APIENTRY alDeleteSources(ALsizei n, const ALuint *sources)
  89. {
  90. ALCcontext *Context;
  91. ALsource *Source;
  92. ALsizei i, j;
  93. ALbufferlistitem *BufferList;
  94. Context = GetContextRef();
  95. if(!Context) return;
  96. if(n < 0)
  97. alSetError(Context, AL_INVALID_VALUE);
  98. else
  99. {
  100. // Check that all Sources are valid (and can therefore be deleted)
  101. for(i = 0;i < n;i++)
  102. {
  103. if(LookupSource(Context, sources[i]) == NULL)
  104. {
  105. alSetError(Context, AL_INVALID_NAME);
  106. n = 0;
  107. break;
  108. }
  109. }
  110. // All Sources are valid, and can be deleted
  111. for(i = 0;i < n;i++)
  112. {
  113. ALsource **srclist, **srclistend;
  114. // Remove Source from list of Sources
  115. if((Source=RemoveSource(Context, sources[i])) == NULL)
  116. continue;
  117. FreeThunkEntry(Source->source);
  118. LockContext(Context);
  119. srclist = Context->ActiveSources;
  120. srclistend = srclist + Context->ActiveSourceCount;
  121. while(srclist != srclistend)
  122. {
  123. if(*srclist == Source)
  124. {
  125. Context->ActiveSourceCount--;
  126. *srclist = *(--srclistend);
  127. break;
  128. }
  129. srclist++;
  130. }
  131. UnlockContext(Context);
  132. // For each buffer in the source's queue...
  133. while(Source->queue != NULL)
  134. {
  135. BufferList = Source->queue;
  136. Source->queue = BufferList->next;
  137. if(BufferList->buffer != NULL)
  138. DecrementRef(&BufferList->buffer->ref);
  139. free(BufferList);
  140. }
  141. for(j = 0;j < MAX_SENDS;++j)
  142. {
  143. if(Source->Send[j].Slot)
  144. DecrementRef(&Source->Send[j].Slot->ref);
  145. Source->Send[j].Slot = NULL;
  146. }
  147. memset(Source,0,sizeof(ALsource));
  148. free(Source);
  149. }
  150. }
  151. ALCcontext_DecRef(Context);
  152. }
  153. AL_API ALboolean AL_APIENTRY alIsSource(ALuint source)
  154. {
  155. ALCcontext *Context;
  156. ALboolean result;
  157. Context = GetContextRef();
  158. if(!Context) return AL_FALSE;
  159. result = (LookupSource(Context, source) ? AL_TRUE : AL_FALSE);
  160. ALCcontext_DecRef(Context);
  161. return result;
  162. }
  163. AL_API ALvoid AL_APIENTRY alSourcef(ALuint source, ALenum eParam, ALfloat flValue)
  164. {
  165. ALCcontext *pContext;
  166. ALsource *Source;
  167. pContext = GetContextRef();
  168. if(!pContext) return;
  169. if((Source=LookupSource(pContext, source)) != NULL)
  170. {
  171. switch(eParam)
  172. {
  173. case AL_PITCH:
  174. if(flValue >= 0.0f)
  175. {
  176. Source->flPitch = flValue;
  177. Source->NeedsUpdate = AL_TRUE;
  178. }
  179. else
  180. alSetError(pContext, AL_INVALID_VALUE);
  181. break;
  182. case AL_CONE_INNER_ANGLE:
  183. if(flValue >= 0.0f && flValue <= 360.0f)
  184. {
  185. Source->flInnerAngle = flValue;
  186. Source->NeedsUpdate = AL_TRUE;
  187. }
  188. else
  189. alSetError(pContext, AL_INVALID_VALUE);
  190. break;
  191. case AL_CONE_OUTER_ANGLE:
  192. if(flValue >= 0.0f && flValue <= 360.0f)
  193. {
  194. Source->flOuterAngle = flValue;
  195. Source->NeedsUpdate = AL_TRUE;
  196. }
  197. else
  198. alSetError(pContext, AL_INVALID_VALUE);
  199. break;
  200. case AL_GAIN:
  201. if(flValue >= 0.0f)
  202. {
  203. Source->flGain = flValue;
  204. Source->NeedsUpdate = AL_TRUE;
  205. }
  206. else
  207. alSetError(pContext, AL_INVALID_VALUE);
  208. break;
  209. case AL_MAX_DISTANCE:
  210. if(flValue >= 0.0f)
  211. {
  212. Source->flMaxDistance = flValue;
  213. Source->NeedsUpdate = AL_TRUE;
  214. }
  215. else
  216. alSetError(pContext, AL_INVALID_VALUE);
  217. break;
  218. case AL_ROLLOFF_FACTOR:
  219. if(flValue >= 0.0f)
  220. {
  221. Source->flRollOffFactor = flValue;
  222. Source->NeedsUpdate = AL_TRUE;
  223. }
  224. else
  225. alSetError(pContext, AL_INVALID_VALUE);
  226. break;
  227. case AL_REFERENCE_DISTANCE:
  228. if(flValue >= 0.0f)
  229. {
  230. Source->flRefDistance = flValue;
  231. Source->NeedsUpdate = AL_TRUE;
  232. }
  233. else
  234. alSetError(pContext, AL_INVALID_VALUE);
  235. break;
  236. case AL_MIN_GAIN:
  237. if(flValue >= 0.0f && flValue <= 1.0f)
  238. {
  239. Source->flMinGain = flValue;
  240. Source->NeedsUpdate = AL_TRUE;
  241. }
  242. else
  243. alSetError(pContext, AL_INVALID_VALUE);
  244. break;
  245. case AL_MAX_GAIN:
  246. if(flValue >= 0.0f && flValue <= 1.0f)
  247. {
  248. Source->flMaxGain = flValue;
  249. Source->NeedsUpdate = AL_TRUE;
  250. }
  251. else
  252. alSetError(pContext, AL_INVALID_VALUE);
  253. break;
  254. case AL_CONE_OUTER_GAIN:
  255. if(flValue >= 0.0f && flValue <= 1.0f)
  256. {
  257. Source->flOuterGain = flValue;
  258. Source->NeedsUpdate = AL_TRUE;
  259. }
  260. else
  261. alSetError(pContext, AL_INVALID_VALUE);
  262. break;
  263. case AL_CONE_OUTER_GAINHF:
  264. if(flValue >= 0.0f && flValue <= 1.0f)
  265. {
  266. Source->OuterGainHF = flValue;
  267. Source->NeedsUpdate = AL_TRUE;
  268. }
  269. else
  270. alSetError(pContext, AL_INVALID_VALUE);
  271. break;
  272. case AL_AIR_ABSORPTION_FACTOR:
  273. if(flValue >= 0.0f && flValue <= 10.0f)
  274. {
  275. Source->AirAbsorptionFactor = flValue;
  276. Source->NeedsUpdate = AL_TRUE;
  277. }
  278. else
  279. alSetError(pContext, AL_INVALID_VALUE);
  280. break;
  281. case AL_ROOM_ROLLOFF_FACTOR:
  282. if(flValue >= 0.0f && flValue <= 10.0f)
  283. {
  284. Source->RoomRolloffFactor = flValue;
  285. Source->NeedsUpdate = AL_TRUE;
  286. }
  287. else
  288. alSetError(pContext, AL_INVALID_VALUE);
  289. break;
  290. case AL_DOPPLER_FACTOR:
  291. if(flValue >= 0.0f && flValue <= 1.0f)
  292. {
  293. Source->DopplerFactor = flValue;
  294. Source->NeedsUpdate = AL_TRUE;
  295. }
  296. else
  297. alSetError(pContext, AL_INVALID_VALUE);
  298. break;
  299. case AL_SEC_OFFSET:
  300. case AL_SAMPLE_OFFSET:
  301. case AL_BYTE_OFFSET:
  302. if(flValue >= 0.0f)
  303. {
  304. LockContext(pContext);
  305. Source->lOffsetType = eParam;
  306. // Store Offset (convert Seconds into Milliseconds)
  307. if(eParam == AL_SEC_OFFSET)
  308. Source->lOffset = (ALint)(flValue * 1000.0f);
  309. else
  310. Source->lOffset = (ALint)flValue;
  311. if((Source->state == AL_PLAYING || Source->state == AL_PAUSED) &&
  312. !pContext->DeferUpdates)
  313. {
  314. if(ApplyOffset(Source) == AL_FALSE)
  315. alSetError(pContext, AL_INVALID_VALUE);
  316. }
  317. UnlockContext(pContext);
  318. }
  319. else
  320. alSetError(pContext, AL_INVALID_VALUE);
  321. break;
  322. default:
  323. alSetError(pContext, AL_INVALID_ENUM);
  324. break;
  325. }
  326. }
  327. else
  328. {
  329. // Invalid Source Name
  330. alSetError(pContext, AL_INVALID_NAME);
  331. }
  332. ALCcontext_DecRef(pContext);
  333. }
  334. AL_API ALvoid AL_APIENTRY alSource3f(ALuint source, ALenum eParam, ALfloat flValue1,ALfloat flValue2,ALfloat flValue3)
  335. {
  336. ALCcontext *pContext;
  337. ALsource *Source;
  338. pContext = GetContextRef();
  339. if(!pContext) return;
  340. if((Source=LookupSource(pContext, source)) != NULL)
  341. {
  342. switch(eParam)
  343. {
  344. case AL_POSITION:
  345. if(isfinite(flValue1) && isfinite(flValue2) && isfinite(flValue3))
  346. {
  347. LockContext(pContext);
  348. Source->vPosition[0] = flValue1;
  349. Source->vPosition[1] = flValue2;
  350. Source->vPosition[2] = flValue3;
  351. UnlockContext(pContext);
  352. Source->NeedsUpdate = AL_TRUE;
  353. }
  354. else
  355. alSetError(pContext, AL_INVALID_VALUE);
  356. break;
  357. case AL_VELOCITY:
  358. if(isfinite(flValue1) && isfinite(flValue2) && isfinite(flValue3))
  359. {
  360. LockContext(pContext);
  361. Source->vVelocity[0] = flValue1;
  362. Source->vVelocity[1] = flValue2;
  363. Source->vVelocity[2] = flValue3;
  364. UnlockContext(pContext);
  365. Source->NeedsUpdate = AL_TRUE;
  366. }
  367. else
  368. alSetError(pContext, AL_INVALID_VALUE);
  369. break;
  370. case AL_DIRECTION:
  371. if(isfinite(flValue1) && isfinite(flValue2) && isfinite(flValue3))
  372. {
  373. LockContext(pContext);
  374. Source->vOrientation[0] = flValue1;
  375. Source->vOrientation[1] = flValue2;
  376. Source->vOrientation[2] = flValue3;
  377. UnlockContext(pContext);
  378. Source->NeedsUpdate = AL_TRUE;
  379. }
  380. else
  381. alSetError(pContext, AL_INVALID_VALUE);
  382. break;
  383. default:
  384. alSetError(pContext, AL_INVALID_ENUM);
  385. break;
  386. }
  387. }
  388. else
  389. alSetError(pContext, AL_INVALID_NAME);
  390. ALCcontext_DecRef(pContext);
  391. }
  392. AL_API ALvoid AL_APIENTRY alSourcefv(ALuint source, ALenum eParam, const ALfloat *pflValues)
  393. {
  394. ALCcontext *pContext;
  395. if(pflValues)
  396. {
  397. switch(eParam)
  398. {
  399. case AL_PITCH:
  400. case AL_CONE_INNER_ANGLE:
  401. case AL_CONE_OUTER_ANGLE:
  402. case AL_GAIN:
  403. case AL_MAX_DISTANCE:
  404. case AL_ROLLOFF_FACTOR:
  405. case AL_REFERENCE_DISTANCE:
  406. case AL_MIN_GAIN:
  407. case AL_MAX_GAIN:
  408. case AL_CONE_OUTER_GAIN:
  409. case AL_CONE_OUTER_GAINHF:
  410. case AL_SEC_OFFSET:
  411. case AL_SAMPLE_OFFSET:
  412. case AL_BYTE_OFFSET:
  413. case AL_AIR_ABSORPTION_FACTOR:
  414. case AL_ROOM_ROLLOFF_FACTOR:
  415. alSourcef(source, eParam, pflValues[0]);
  416. return;
  417. case AL_POSITION:
  418. case AL_VELOCITY:
  419. case AL_DIRECTION:
  420. alSource3f(source, eParam, pflValues[0], pflValues[1], pflValues[2]);
  421. return;
  422. }
  423. }
  424. pContext = GetContextRef();
  425. if(!pContext) return;
  426. if(pflValues)
  427. {
  428. if(LookupSource(pContext, source) != NULL)
  429. {
  430. switch(eParam)
  431. {
  432. default:
  433. alSetError(pContext, AL_INVALID_ENUM);
  434. break;
  435. }
  436. }
  437. else
  438. alSetError(pContext, AL_INVALID_NAME);
  439. }
  440. else
  441. alSetError(pContext, AL_INVALID_VALUE);
  442. ALCcontext_DecRef(pContext);
  443. }
  444. AL_API ALvoid AL_APIENTRY alSourcei(ALuint source,ALenum eParam,ALint lValue)
  445. {
  446. ALCcontext *pContext;
  447. ALsource *Source;
  448. ALbufferlistitem *BufferListItem;
  449. switch(eParam)
  450. {
  451. case AL_MAX_DISTANCE:
  452. case AL_ROLLOFF_FACTOR:
  453. case AL_CONE_INNER_ANGLE:
  454. case AL_CONE_OUTER_ANGLE:
  455. case AL_REFERENCE_DISTANCE:
  456. alSourcef(source, eParam, (ALfloat)lValue);
  457. return;
  458. }
  459. pContext = GetContextRef();
  460. if(!pContext) return;
  461. if((Source=LookupSource(pContext, source)) != NULL)
  462. {
  463. ALCdevice *device = pContext->Device;
  464. switch(eParam)
  465. {
  466. case AL_SOURCE_RELATIVE:
  467. if(lValue == AL_FALSE || lValue == AL_TRUE)
  468. {
  469. Source->bHeadRelative = (ALboolean)lValue;
  470. Source->NeedsUpdate = AL_TRUE;
  471. }
  472. else
  473. alSetError(pContext, AL_INVALID_VALUE);
  474. break;
  475. case AL_LOOPING:
  476. if(lValue == AL_FALSE || lValue == AL_TRUE)
  477. Source->bLooping = (ALboolean)lValue;
  478. else
  479. alSetError(pContext, AL_INVALID_VALUE);
  480. break;
  481. case AL_BUFFER:
  482. LockContext(pContext);
  483. if(Source->state == AL_STOPPED || Source->state == AL_INITIAL)
  484. {
  485. ALbufferlistitem *oldlist;
  486. ALbuffer *buffer = NULL;
  487. if(lValue == 0 || (buffer=LookupBuffer(device, lValue)) != NULL)
  488. {
  489. Source->BuffersInQueue = 0;
  490. Source->BuffersPlayed = 0;
  491. // Add the buffer to the queue (as long as it is NOT the NULL buffer)
  492. if(buffer != NULL)
  493. {
  494. // Source is now in STATIC mode
  495. Source->lSourceType = AL_STATIC;
  496. // Add the selected buffer to the queue
  497. BufferListItem = malloc(sizeof(ALbufferlistitem));
  498. BufferListItem->buffer = buffer;
  499. BufferListItem->next = NULL;
  500. BufferListItem->prev = NULL;
  501. // Increment reference counter for buffer
  502. IncrementRef(&buffer->ref);
  503. oldlist = ExchangePtr((void**)&Source->queue, BufferListItem);
  504. Source->BuffersInQueue = 1;
  505. ReadLock(&buffer->lock);
  506. Source->NumChannels = ChannelsFromFmt(buffer->FmtChannels);
  507. Source->SampleSize = BytesFromFmt(buffer->FmtType);
  508. ReadUnlock(&buffer->lock);
  509. if(buffer->FmtChannels == FmtMono)
  510. Source->Update = CalcSourceParams;
  511. else
  512. Source->Update = CalcNonAttnSourceParams;
  513. Source->NeedsUpdate = AL_TRUE;
  514. }
  515. else
  516. {
  517. // Source is now in UNDETERMINED mode
  518. Source->lSourceType = AL_UNDETERMINED;
  519. oldlist = ExchangePtr((void**)&Source->queue, NULL);
  520. }
  521. // Delete all previous elements in the queue
  522. while(oldlist != NULL)
  523. {
  524. BufferListItem = oldlist;
  525. oldlist = BufferListItem->next;
  526. if(BufferListItem->buffer)
  527. DecrementRef(&BufferListItem->buffer->ref);
  528. free(BufferListItem);
  529. }
  530. }
  531. else
  532. alSetError(pContext, AL_INVALID_VALUE);
  533. }
  534. else
  535. alSetError(pContext, AL_INVALID_OPERATION);
  536. UnlockContext(pContext);
  537. break;
  538. case AL_SOURCE_STATE:
  539. // Query only
  540. alSetError(pContext, AL_INVALID_OPERATION);
  541. break;
  542. case AL_SEC_OFFSET:
  543. case AL_SAMPLE_OFFSET:
  544. case AL_BYTE_OFFSET:
  545. if(lValue >= 0)
  546. {
  547. LockContext(pContext);
  548. Source->lOffsetType = eParam;
  549. // Store Offset (convert Seconds into Milliseconds)
  550. if(eParam == AL_SEC_OFFSET)
  551. Source->lOffset = lValue * 1000;
  552. else
  553. Source->lOffset = lValue;
  554. if((Source->state == AL_PLAYING || Source->state == AL_PAUSED) &&
  555. !pContext->DeferUpdates)
  556. {
  557. if(ApplyOffset(Source) == AL_FALSE)
  558. alSetError(pContext, AL_INVALID_VALUE);
  559. }
  560. UnlockContext(pContext);
  561. }
  562. else
  563. alSetError(pContext, AL_INVALID_VALUE);
  564. break;
  565. case AL_DIRECT_FILTER: {
  566. ALfilter *filter = NULL;
  567. if(lValue == 0 || (filter=LookupFilter(pContext->Device, lValue)) != NULL)
  568. {
  569. LockContext(pContext);
  570. if(!filter)
  571. {
  572. Source->DirectGain = 1.0f;
  573. Source->DirectGainHF = 1.0f;
  574. }
  575. else
  576. {
  577. Source->DirectGain = filter->Gain;
  578. Source->DirectGainHF = filter->GainHF;
  579. }
  580. UnlockContext(pContext);
  581. Source->NeedsUpdate = AL_TRUE;
  582. }
  583. else
  584. alSetError(pContext, AL_INVALID_VALUE);
  585. } break;
  586. case AL_DIRECT_FILTER_GAINHF_AUTO:
  587. if(lValue == AL_TRUE || lValue == AL_FALSE)
  588. {
  589. Source->DryGainHFAuto = lValue;
  590. Source->NeedsUpdate = AL_TRUE;
  591. }
  592. else
  593. alSetError(pContext, AL_INVALID_VALUE);
  594. break;
  595. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  596. if(lValue == AL_TRUE || lValue == AL_FALSE)
  597. {
  598. Source->WetGainAuto = lValue;
  599. Source->NeedsUpdate = AL_TRUE;
  600. }
  601. else
  602. alSetError(pContext, AL_INVALID_VALUE);
  603. break;
  604. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  605. if(lValue == AL_TRUE || lValue == AL_FALSE)
  606. {
  607. Source->WetGainHFAuto = lValue;
  608. Source->NeedsUpdate = AL_TRUE;
  609. }
  610. else
  611. alSetError(pContext, AL_INVALID_VALUE);
  612. break;
  613. case AL_VIRTUAL_CHANNELS_SOFT:
  614. if(lValue == AL_TRUE || lValue == AL_FALSE)
  615. {
  616. Source->VirtualChannels = lValue;
  617. Source->NeedsUpdate = AL_TRUE;
  618. }
  619. else
  620. alSetError(pContext, AL_INVALID_VALUE);
  621. break;
  622. case AL_DISTANCE_MODEL:
  623. if(lValue == AL_NONE ||
  624. lValue == AL_INVERSE_DISTANCE ||
  625. lValue == AL_INVERSE_DISTANCE_CLAMPED ||
  626. lValue == AL_LINEAR_DISTANCE ||
  627. lValue == AL_LINEAR_DISTANCE_CLAMPED ||
  628. lValue == AL_EXPONENT_DISTANCE ||
  629. lValue == AL_EXPONENT_DISTANCE_CLAMPED)
  630. {
  631. Source->DistanceModel = lValue;
  632. if(pContext->SourceDistanceModel)
  633. Source->NeedsUpdate = AL_TRUE;
  634. }
  635. else
  636. alSetError(pContext, AL_INVALID_VALUE);
  637. break;
  638. default:
  639. alSetError(pContext, AL_INVALID_ENUM);
  640. break;
  641. }
  642. }
  643. else
  644. alSetError(pContext, AL_INVALID_NAME);
  645. ALCcontext_DecRef(pContext);
  646. }
  647. AL_API void AL_APIENTRY alSource3i(ALuint source, ALenum eParam, ALint lValue1, ALint lValue2, ALint lValue3)
  648. {
  649. ALCcontext *pContext;
  650. ALsource *Source;
  651. switch(eParam)
  652. {
  653. case AL_POSITION:
  654. case AL_VELOCITY:
  655. case AL_DIRECTION:
  656. alSource3f(source, eParam, (ALfloat)lValue1, (ALfloat)lValue2, (ALfloat)lValue3);
  657. return;
  658. }
  659. pContext = GetContextRef();
  660. if(!pContext) return;
  661. if((Source=LookupSource(pContext, source)) != NULL)
  662. {
  663. ALCdevice *device = pContext->Device;
  664. switch(eParam)
  665. {
  666. case AL_AUXILIARY_SEND_FILTER: {
  667. ALeffectslot *ALEffectSlot = NULL;
  668. ALfilter *ALFilter = NULL;
  669. LockContext(pContext);
  670. if((ALuint)lValue2 < device->NumAuxSends &&
  671. (lValue1 == 0 || (ALEffectSlot=LookupEffectSlot(pContext, lValue1)) != NULL) &&
  672. (lValue3 == 0 || (ALFilter=LookupFilter(device, lValue3)) != NULL))
  673. {
  674. /* Release refcount on the previous slot, and add one for
  675. * the new slot */
  676. if(ALEffectSlot) IncrementRef(&ALEffectSlot->ref);
  677. ALEffectSlot = ExchangePtr((void**)&Source->Send[lValue2].Slot, ALEffectSlot);
  678. if(ALEffectSlot) DecrementRef(&ALEffectSlot->ref);
  679. if(!ALFilter)
  680. {
  681. /* Disable filter */
  682. Source->Send[lValue2].WetGain = 1.0f;
  683. Source->Send[lValue2].WetGainHF = 1.0f;
  684. }
  685. else
  686. {
  687. Source->Send[lValue2].WetGain = ALFilter->Gain;
  688. Source->Send[lValue2].WetGainHF = ALFilter->GainHF;
  689. }
  690. Source->NeedsUpdate = AL_TRUE;
  691. }
  692. else
  693. alSetError(pContext, AL_INVALID_VALUE);
  694. UnlockContext(pContext);
  695. } break;
  696. default:
  697. alSetError(pContext, AL_INVALID_ENUM);
  698. break;
  699. }
  700. }
  701. else
  702. alSetError(pContext, AL_INVALID_NAME);
  703. ALCcontext_DecRef(pContext);
  704. }
  705. AL_API void AL_APIENTRY alSourceiv(ALuint source, ALenum eParam, const ALint* plValues)
  706. {
  707. ALCcontext *pContext;
  708. if(plValues)
  709. {
  710. switch(eParam)
  711. {
  712. case AL_SOURCE_RELATIVE:
  713. case AL_CONE_INNER_ANGLE:
  714. case AL_CONE_OUTER_ANGLE:
  715. case AL_LOOPING:
  716. case AL_BUFFER:
  717. case AL_SOURCE_STATE:
  718. case AL_SEC_OFFSET:
  719. case AL_SAMPLE_OFFSET:
  720. case AL_BYTE_OFFSET:
  721. case AL_MAX_DISTANCE:
  722. case AL_ROLLOFF_FACTOR:
  723. case AL_REFERENCE_DISTANCE:
  724. case AL_DIRECT_FILTER:
  725. case AL_DIRECT_FILTER_GAINHF_AUTO:
  726. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  727. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  728. case AL_DISTANCE_MODEL:
  729. case AL_VIRTUAL_CHANNELS_SOFT:
  730. alSourcei(source, eParam, plValues[0]);
  731. return;
  732. case AL_POSITION:
  733. case AL_VELOCITY:
  734. case AL_DIRECTION:
  735. case AL_AUXILIARY_SEND_FILTER:
  736. alSource3i(source, eParam, plValues[0], plValues[1], plValues[2]);
  737. return;
  738. }
  739. }
  740. pContext = GetContextRef();
  741. if(!pContext) return;
  742. if(plValues)
  743. {
  744. if(LookupSource(pContext, source) != NULL)
  745. {
  746. switch(eParam)
  747. {
  748. default:
  749. alSetError(pContext, AL_INVALID_ENUM);
  750. break;
  751. }
  752. }
  753. else
  754. alSetError(pContext, AL_INVALID_NAME);
  755. }
  756. else
  757. alSetError(pContext, AL_INVALID_VALUE);
  758. ALCcontext_DecRef(pContext);
  759. }
  760. AL_API ALvoid AL_APIENTRY alGetSourcef(ALuint source, ALenum eParam, ALfloat *pflValue)
  761. {
  762. ALCcontext *pContext;
  763. ALsource *Source;
  764. ALdouble Offsets[2];
  765. ALdouble updateLen;
  766. pContext = GetContextRef();
  767. if(!pContext) return;
  768. if(pflValue)
  769. {
  770. if((Source=LookupSource(pContext, source)) != NULL)
  771. {
  772. switch(eParam)
  773. {
  774. case AL_PITCH:
  775. *pflValue = Source->flPitch;
  776. break;
  777. case AL_GAIN:
  778. *pflValue = Source->flGain;
  779. break;
  780. case AL_MIN_GAIN:
  781. *pflValue = Source->flMinGain;
  782. break;
  783. case AL_MAX_GAIN:
  784. *pflValue = Source->flMaxGain;
  785. break;
  786. case AL_MAX_DISTANCE:
  787. *pflValue = Source->flMaxDistance;
  788. break;
  789. case AL_ROLLOFF_FACTOR:
  790. *pflValue = Source->flRollOffFactor;
  791. break;
  792. case AL_CONE_OUTER_GAIN:
  793. *pflValue = Source->flOuterGain;
  794. break;
  795. case AL_CONE_OUTER_GAINHF:
  796. *pflValue = Source->OuterGainHF;
  797. break;
  798. case AL_SEC_OFFSET:
  799. case AL_SAMPLE_OFFSET:
  800. case AL_BYTE_OFFSET:
  801. LockContext(pContext);
  802. updateLen = (ALdouble)pContext->Device->UpdateSize /
  803. pContext->Device->Frequency;
  804. GetSourceOffset(Source, eParam, Offsets, updateLen);
  805. UnlockContext(pContext);
  806. *pflValue = (ALfloat)Offsets[0];
  807. break;
  808. case AL_CONE_INNER_ANGLE:
  809. *pflValue = Source->flInnerAngle;
  810. break;
  811. case AL_CONE_OUTER_ANGLE:
  812. *pflValue = Source->flOuterAngle;
  813. break;
  814. case AL_REFERENCE_DISTANCE:
  815. *pflValue = Source->flRefDistance;
  816. break;
  817. case AL_AIR_ABSORPTION_FACTOR:
  818. *pflValue = Source->AirAbsorptionFactor;
  819. break;
  820. case AL_ROOM_ROLLOFF_FACTOR:
  821. *pflValue = Source->RoomRolloffFactor;
  822. break;
  823. case AL_DOPPLER_FACTOR:
  824. *pflValue = Source->DopplerFactor;
  825. break;
  826. default:
  827. alSetError(pContext, AL_INVALID_ENUM);
  828. break;
  829. }
  830. }
  831. else
  832. alSetError(pContext, AL_INVALID_NAME);
  833. }
  834. else
  835. alSetError(pContext, AL_INVALID_VALUE);
  836. ALCcontext_DecRef(pContext);
  837. }
  838. AL_API ALvoid AL_APIENTRY alGetSource3f(ALuint source, ALenum eParam, ALfloat* pflValue1, ALfloat* pflValue2, ALfloat* pflValue3)
  839. {
  840. ALCcontext *pContext;
  841. ALsource *Source;
  842. pContext = GetContextRef();
  843. if(!pContext) return;
  844. if(pflValue1 && pflValue2 && pflValue3)
  845. {
  846. if((Source=LookupSource(pContext, source)) != NULL)
  847. {
  848. switch(eParam)
  849. {
  850. case AL_POSITION:
  851. LockContext(pContext);
  852. *pflValue1 = Source->vPosition[0];
  853. *pflValue2 = Source->vPosition[1];
  854. *pflValue3 = Source->vPosition[2];
  855. UnlockContext(pContext);
  856. break;
  857. case AL_VELOCITY:
  858. LockContext(pContext);
  859. *pflValue1 = Source->vVelocity[0];
  860. *pflValue2 = Source->vVelocity[1];
  861. *pflValue3 = Source->vVelocity[2];
  862. UnlockContext(pContext);
  863. break;
  864. case AL_DIRECTION:
  865. LockContext(pContext);
  866. *pflValue1 = Source->vOrientation[0];
  867. *pflValue2 = Source->vOrientation[1];
  868. *pflValue3 = Source->vOrientation[2];
  869. UnlockContext(pContext);
  870. break;
  871. default:
  872. alSetError(pContext, AL_INVALID_ENUM);
  873. break;
  874. }
  875. }
  876. else
  877. alSetError(pContext, AL_INVALID_NAME);
  878. }
  879. else
  880. alSetError(pContext, AL_INVALID_VALUE);
  881. ALCcontext_DecRef(pContext);
  882. }
  883. AL_API ALvoid AL_APIENTRY alGetSourcefv(ALuint source, ALenum eParam, ALfloat *pflValues)
  884. {
  885. ALCcontext *pContext;
  886. ALsource *Source;
  887. ALdouble Offsets[2];
  888. ALdouble updateLen;
  889. switch(eParam)
  890. {
  891. case AL_PITCH:
  892. case AL_GAIN:
  893. case AL_MIN_GAIN:
  894. case AL_MAX_GAIN:
  895. case AL_MAX_DISTANCE:
  896. case AL_ROLLOFF_FACTOR:
  897. case AL_DOPPLER_FACTOR:
  898. case AL_CONE_OUTER_GAIN:
  899. case AL_SEC_OFFSET:
  900. case AL_SAMPLE_OFFSET:
  901. case AL_BYTE_OFFSET:
  902. case AL_CONE_INNER_ANGLE:
  903. case AL_CONE_OUTER_ANGLE:
  904. case AL_REFERENCE_DISTANCE:
  905. case AL_CONE_OUTER_GAINHF:
  906. case AL_AIR_ABSORPTION_FACTOR:
  907. case AL_ROOM_ROLLOFF_FACTOR:
  908. alGetSourcef(source, eParam, pflValues);
  909. return;
  910. case AL_POSITION:
  911. case AL_VELOCITY:
  912. case AL_DIRECTION:
  913. alGetSource3f(source, eParam, pflValues+0, pflValues+1, pflValues+2);
  914. return;
  915. }
  916. pContext = GetContextRef();
  917. if(!pContext) return;
  918. if(pflValues)
  919. {
  920. if((Source=LookupSource(pContext, source)) != NULL)
  921. {
  922. switch(eParam)
  923. {
  924. case AL_SAMPLE_RW_OFFSETS_SOFT:
  925. case AL_BYTE_RW_OFFSETS_SOFT:
  926. LockContext(pContext);
  927. updateLen = (ALdouble)pContext->Device->UpdateSize /
  928. pContext->Device->Frequency;
  929. GetSourceOffset(Source, eParam, Offsets, updateLen);
  930. UnlockContext(pContext);
  931. pflValues[0] = (ALfloat)Offsets[0];
  932. pflValues[1] = (ALfloat)Offsets[1];
  933. break;
  934. default:
  935. alSetError(pContext, AL_INVALID_ENUM);
  936. break;
  937. }
  938. }
  939. else
  940. alSetError(pContext, AL_INVALID_NAME);
  941. }
  942. else
  943. alSetError(pContext, AL_INVALID_VALUE);
  944. ALCcontext_DecRef(pContext);
  945. }
  946. AL_API ALvoid AL_APIENTRY alGetSourcei(ALuint source, ALenum eParam, ALint *plValue)
  947. {
  948. ALbufferlistitem *BufferList;
  949. ALCcontext *pContext;
  950. ALsource *Source;
  951. ALdouble Offsets[2];
  952. ALdouble updateLen;
  953. pContext = GetContextRef();
  954. if(!pContext) return;
  955. if(plValue)
  956. {
  957. if((Source=LookupSource(pContext, source)) != NULL)
  958. {
  959. switch(eParam)
  960. {
  961. case AL_MAX_DISTANCE:
  962. *plValue = (ALint)Source->flMaxDistance;
  963. break;
  964. case AL_ROLLOFF_FACTOR:
  965. *plValue = (ALint)Source->flRollOffFactor;
  966. break;
  967. case AL_REFERENCE_DISTANCE:
  968. *plValue = (ALint)Source->flRefDistance;
  969. break;
  970. case AL_SOURCE_RELATIVE:
  971. *plValue = Source->bHeadRelative;
  972. break;
  973. case AL_CONE_INNER_ANGLE:
  974. *plValue = (ALint)Source->flInnerAngle;
  975. break;
  976. case AL_CONE_OUTER_ANGLE:
  977. *plValue = (ALint)Source->flOuterAngle;
  978. break;
  979. case AL_LOOPING:
  980. *plValue = Source->bLooping;
  981. break;
  982. case AL_BUFFER:
  983. LockContext(pContext);
  984. BufferList = Source->queue;
  985. if(Source->lSourceType != AL_STATIC)
  986. {
  987. ALuint i = Source->BuffersPlayed;
  988. while(i > 0)
  989. {
  990. BufferList = BufferList->next;
  991. i--;
  992. }
  993. }
  994. *plValue = ((BufferList && BufferList->buffer) ?
  995. BufferList->buffer->buffer : 0);
  996. UnlockContext(pContext);
  997. break;
  998. case AL_SOURCE_STATE:
  999. *plValue = Source->state;
  1000. break;
  1001. case AL_BUFFERS_QUEUED:
  1002. *plValue = Source->BuffersInQueue;
  1003. break;
  1004. case AL_BUFFERS_PROCESSED:
  1005. LockContext(pContext);
  1006. if(Source->bLooping || Source->lSourceType != AL_STREAMING)
  1007. {
  1008. /* Buffers on a looping source are in a perpetual state
  1009. * of PENDING, so don't report any as PROCESSED */
  1010. *plValue = 0;
  1011. }
  1012. else
  1013. *plValue = Source->BuffersPlayed;
  1014. UnlockContext(pContext);
  1015. break;
  1016. case AL_SOURCE_TYPE:
  1017. *plValue = Source->lSourceType;
  1018. break;
  1019. case AL_SEC_OFFSET:
  1020. case AL_SAMPLE_OFFSET:
  1021. case AL_BYTE_OFFSET:
  1022. LockContext(pContext);
  1023. updateLen = (ALdouble)pContext->Device->UpdateSize /
  1024. pContext->Device->Frequency;
  1025. GetSourceOffset(Source, eParam, Offsets, updateLen);
  1026. UnlockContext(pContext);
  1027. *plValue = (ALint)Offsets[0];
  1028. break;
  1029. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1030. *plValue = Source->DryGainHFAuto;
  1031. break;
  1032. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1033. *plValue = Source->WetGainAuto;
  1034. break;
  1035. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1036. *plValue = Source->WetGainHFAuto;
  1037. break;
  1038. case AL_DOPPLER_FACTOR:
  1039. *plValue = (ALint)Source->DopplerFactor;
  1040. break;
  1041. case AL_VIRTUAL_CHANNELS_SOFT:
  1042. *plValue = Source->VirtualChannels;
  1043. break;
  1044. case AL_DISTANCE_MODEL:
  1045. *plValue = Source->DistanceModel;
  1046. break;
  1047. default:
  1048. alSetError(pContext, AL_INVALID_ENUM);
  1049. break;
  1050. }
  1051. }
  1052. else
  1053. alSetError(pContext, AL_INVALID_NAME);
  1054. }
  1055. else
  1056. alSetError(pContext, AL_INVALID_VALUE);
  1057. ALCcontext_DecRef(pContext);
  1058. }
  1059. AL_API void AL_APIENTRY alGetSource3i(ALuint source, ALenum eParam, ALint* plValue1, ALint* plValue2, ALint* plValue3)
  1060. {
  1061. ALCcontext *pContext;
  1062. ALsource *Source;
  1063. pContext = GetContextRef();
  1064. if(!pContext) return;
  1065. if(plValue1 && plValue2 && plValue3)
  1066. {
  1067. if((Source=LookupSource(pContext, source)) != NULL)
  1068. {
  1069. switch(eParam)
  1070. {
  1071. case AL_POSITION:
  1072. LockContext(pContext);
  1073. *plValue1 = (ALint)Source->vPosition[0];
  1074. *plValue2 = (ALint)Source->vPosition[1];
  1075. *plValue3 = (ALint)Source->vPosition[2];
  1076. UnlockContext(pContext);
  1077. break;
  1078. case AL_VELOCITY:
  1079. LockContext(pContext);
  1080. *plValue1 = (ALint)Source->vVelocity[0];
  1081. *plValue2 = (ALint)Source->vVelocity[1];
  1082. *plValue3 = (ALint)Source->vVelocity[2];
  1083. UnlockContext(pContext);
  1084. break;
  1085. case AL_DIRECTION:
  1086. LockContext(pContext);
  1087. *plValue1 = (ALint)Source->vOrientation[0];
  1088. *plValue2 = (ALint)Source->vOrientation[1];
  1089. *plValue3 = (ALint)Source->vOrientation[2];
  1090. UnlockContext(pContext);
  1091. break;
  1092. default:
  1093. alSetError(pContext, AL_INVALID_ENUM);
  1094. break;
  1095. }
  1096. }
  1097. else
  1098. alSetError(pContext, AL_INVALID_NAME);
  1099. }
  1100. else
  1101. alSetError(pContext, AL_INVALID_VALUE);
  1102. ALCcontext_DecRef(pContext);
  1103. }
  1104. AL_API void AL_APIENTRY alGetSourceiv(ALuint source, ALenum eParam, ALint* plValues)
  1105. {
  1106. ALCcontext *pContext;
  1107. ALsource *Source;
  1108. ALdouble Offsets[2];
  1109. ALdouble updateLen;
  1110. switch(eParam)
  1111. {
  1112. case AL_SOURCE_RELATIVE:
  1113. case AL_CONE_INNER_ANGLE:
  1114. case AL_CONE_OUTER_ANGLE:
  1115. case AL_LOOPING:
  1116. case AL_BUFFER:
  1117. case AL_SOURCE_STATE:
  1118. case AL_BUFFERS_QUEUED:
  1119. case AL_BUFFERS_PROCESSED:
  1120. case AL_SEC_OFFSET:
  1121. case AL_SAMPLE_OFFSET:
  1122. case AL_BYTE_OFFSET:
  1123. case AL_MAX_DISTANCE:
  1124. case AL_ROLLOFF_FACTOR:
  1125. case AL_DOPPLER_FACTOR:
  1126. case AL_REFERENCE_DISTANCE:
  1127. case AL_SOURCE_TYPE:
  1128. case AL_DIRECT_FILTER:
  1129. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1130. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1131. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1132. case AL_DISTANCE_MODEL:
  1133. case AL_VIRTUAL_CHANNELS_SOFT:
  1134. alGetSourcei(source, eParam, plValues);
  1135. return;
  1136. case AL_POSITION:
  1137. case AL_VELOCITY:
  1138. case AL_DIRECTION:
  1139. alGetSource3i(source, eParam, plValues+0, plValues+1, plValues+2);
  1140. return;
  1141. }
  1142. pContext = GetContextRef();
  1143. if(!pContext) return;
  1144. if(plValues)
  1145. {
  1146. if((Source=LookupSource(pContext, source)) != NULL)
  1147. {
  1148. switch(eParam)
  1149. {
  1150. case AL_SAMPLE_RW_OFFSETS_SOFT:
  1151. case AL_BYTE_RW_OFFSETS_SOFT:
  1152. LockContext(pContext);
  1153. updateLen = (ALdouble)pContext->Device->UpdateSize /
  1154. pContext->Device->Frequency;
  1155. GetSourceOffset(Source, eParam, Offsets, updateLen);
  1156. UnlockContext(pContext);
  1157. plValues[0] = (ALint)Offsets[0];
  1158. plValues[1] = (ALint)Offsets[1];
  1159. break;
  1160. default:
  1161. alSetError(pContext, AL_INVALID_ENUM);
  1162. break;
  1163. }
  1164. }
  1165. else
  1166. alSetError(pContext, AL_INVALID_NAME);
  1167. }
  1168. else
  1169. alSetError(pContext, AL_INVALID_VALUE);
  1170. ALCcontext_DecRef(pContext);
  1171. }
  1172. AL_API ALvoid AL_APIENTRY alSourcePlay(ALuint source)
  1173. {
  1174. alSourcePlayv(1, &source);
  1175. }
  1176. AL_API ALvoid AL_APIENTRY alSourcePlayv(ALsizei n, const ALuint *sources)
  1177. {
  1178. ALCcontext *Context;
  1179. ALsource *Source;
  1180. ALsizei i;
  1181. Context = GetContextRef();
  1182. if(!Context) return;
  1183. if(n < 0)
  1184. {
  1185. alSetError(Context, AL_INVALID_VALUE);
  1186. goto done;
  1187. }
  1188. if(n > 0 && !sources)
  1189. {
  1190. alSetError(Context, AL_INVALID_VALUE);
  1191. goto done;
  1192. }
  1193. // Check that all the Sources are valid
  1194. for(i = 0;i < n;i++)
  1195. {
  1196. if(!LookupSource(Context, sources[i]))
  1197. {
  1198. alSetError(Context, AL_INVALID_NAME);
  1199. goto done;
  1200. }
  1201. }
  1202. LockContext(Context);
  1203. while(Context->MaxActiveSources-Context->ActiveSourceCount < n)
  1204. {
  1205. void *temp = NULL;
  1206. ALsizei newcount;
  1207. newcount = Context->MaxActiveSources << 1;
  1208. if(newcount > 0)
  1209. temp = realloc(Context->ActiveSources,
  1210. sizeof(*Context->ActiveSources) * newcount);
  1211. if(!temp)
  1212. {
  1213. UnlockContext(Context);
  1214. alSetError(Context, AL_OUT_OF_MEMORY);
  1215. goto done;
  1216. }
  1217. Context->ActiveSources = temp;
  1218. Context->MaxActiveSources = newcount;
  1219. }
  1220. for(i = 0;i < n;i++)
  1221. {
  1222. Source = LookupSource(Context, sources[i]);
  1223. if(Context->DeferUpdates) Source->new_state = AL_PLAYING;
  1224. else SetSourceState(Source, Context, AL_PLAYING);
  1225. }
  1226. UnlockContext(Context);
  1227. done:
  1228. ALCcontext_DecRef(Context);
  1229. }
  1230. AL_API ALvoid AL_APIENTRY alSourcePause(ALuint source)
  1231. {
  1232. alSourcePausev(1, &source);
  1233. }
  1234. AL_API ALvoid AL_APIENTRY alSourcePausev(ALsizei n, const ALuint *sources)
  1235. {
  1236. ALCcontext *Context;
  1237. ALsource *Source;
  1238. ALsizei i;
  1239. Context = GetContextRef();
  1240. if(!Context) return;
  1241. if(n < 0)
  1242. {
  1243. alSetError(Context, AL_INVALID_VALUE);
  1244. goto done;
  1245. }
  1246. if(n > 0 && !sources)
  1247. {
  1248. alSetError(Context, AL_INVALID_VALUE);
  1249. goto done;
  1250. }
  1251. // Check all the Sources are valid
  1252. for(i = 0;i < n;i++)
  1253. {
  1254. if(!LookupSource(Context, sources[i]))
  1255. {
  1256. alSetError(Context, AL_INVALID_NAME);
  1257. goto done;
  1258. }
  1259. }
  1260. LockContext(Context);
  1261. for(i = 0;i < n;i++)
  1262. {
  1263. Source = LookupSource(Context, sources[i]);
  1264. if(Context->DeferUpdates) Source->new_state = AL_PAUSED;
  1265. else SetSourceState(Source, Context, AL_PAUSED);
  1266. }
  1267. UnlockContext(Context);
  1268. done:
  1269. ALCcontext_DecRef(Context);
  1270. }
  1271. AL_API ALvoid AL_APIENTRY alSourceStop(ALuint source)
  1272. {
  1273. alSourceStopv(1, &source);
  1274. }
  1275. AL_API ALvoid AL_APIENTRY alSourceStopv(ALsizei n, const ALuint *sources)
  1276. {
  1277. ALCcontext *Context;
  1278. ALsource *Source;
  1279. ALsizei i;
  1280. Context = GetContextRef();
  1281. if(!Context) return;
  1282. if(n < 0)
  1283. {
  1284. alSetError(Context, AL_INVALID_VALUE);
  1285. goto done;
  1286. }
  1287. if(n > 0 && !sources)
  1288. {
  1289. alSetError(Context, AL_INVALID_VALUE);
  1290. goto done;
  1291. }
  1292. // Check all the Sources are valid
  1293. for(i = 0;i < n;i++)
  1294. {
  1295. if(!LookupSource(Context, sources[i]))
  1296. {
  1297. alSetError(Context, AL_INVALID_NAME);
  1298. goto done;
  1299. }
  1300. }
  1301. LockContext(Context);
  1302. for(i = 0;i < n;i++)
  1303. {
  1304. Source = LookupSource(Context, sources[i]);
  1305. Source->new_state = AL_NONE;
  1306. SetSourceState(Source, Context, AL_STOPPED);
  1307. }
  1308. UnlockContext(Context);
  1309. done:
  1310. ALCcontext_DecRef(Context);
  1311. }
  1312. AL_API ALvoid AL_APIENTRY alSourceRewind(ALuint source)
  1313. {
  1314. alSourceRewindv(1, &source);
  1315. }
  1316. AL_API ALvoid AL_APIENTRY alSourceRewindv(ALsizei n, const ALuint *sources)
  1317. {
  1318. ALCcontext *Context;
  1319. ALsource *Source;
  1320. ALsizei i;
  1321. Context = GetContextRef();
  1322. if(!Context) return;
  1323. if(n < 0)
  1324. {
  1325. alSetError(Context, AL_INVALID_VALUE);
  1326. goto done;
  1327. }
  1328. if(n > 0 && !sources)
  1329. {
  1330. alSetError(Context, AL_INVALID_VALUE);
  1331. goto done;
  1332. }
  1333. // Check all the Sources are valid
  1334. for(i = 0;i < n;i++)
  1335. {
  1336. if(!LookupSource(Context, sources[i]))
  1337. {
  1338. alSetError(Context, AL_INVALID_NAME);
  1339. goto done;
  1340. }
  1341. }
  1342. LockContext(Context);
  1343. for(i = 0;i < n;i++)
  1344. {
  1345. Source = LookupSource(Context, sources[i]);
  1346. Source->new_state = AL_NONE;
  1347. SetSourceState(Source, Context, AL_INITIAL);
  1348. }
  1349. UnlockContext(Context);
  1350. done:
  1351. ALCcontext_DecRef(Context);
  1352. }
  1353. AL_API ALvoid AL_APIENTRY alSourceQueueBuffers(ALuint source, ALsizei n, const ALuint *buffers)
  1354. {
  1355. ALCcontext *Context;
  1356. ALCdevice *device;
  1357. ALsource *Source;
  1358. ALsizei i;
  1359. ALbufferlistitem *BufferListStart = NULL;
  1360. ALbufferlistitem *BufferList;
  1361. ALbuffer *BufferFmt;
  1362. if(n == 0)
  1363. return;
  1364. Context = GetContextRef();
  1365. if(!Context) return;
  1366. if(n < 0)
  1367. {
  1368. alSetError(Context, AL_INVALID_VALUE);
  1369. goto error;
  1370. }
  1371. // Check that all buffers are valid or zero and that the source is valid
  1372. // Check that this is a valid source
  1373. if((Source=LookupSource(Context, source)) == NULL)
  1374. {
  1375. alSetError(Context, AL_INVALID_NAME);
  1376. goto error;
  1377. }
  1378. LockContext(Context);
  1379. // Check that this is not a STATIC Source
  1380. if(Source->lSourceType == AL_STATIC)
  1381. {
  1382. UnlockContext(Context);
  1383. // Invalid Source Type (can't queue on a Static Source)
  1384. alSetError(Context, AL_INVALID_OPERATION);
  1385. goto error;
  1386. }
  1387. device = Context->Device;
  1388. BufferFmt = NULL;
  1389. // Check existing Queue (if any) for a valid Buffers and get its frequency and format
  1390. BufferList = Source->queue;
  1391. while(BufferList)
  1392. {
  1393. if(BufferList->buffer)
  1394. {
  1395. BufferFmt = BufferList->buffer;
  1396. break;
  1397. }
  1398. BufferList = BufferList->next;
  1399. }
  1400. for(i = 0;i < n;i++)
  1401. {
  1402. ALbuffer *buffer = NULL;
  1403. if(buffers[i] && (buffer=LookupBuffer(device, buffers[i])) == NULL)
  1404. {
  1405. UnlockContext(Context);
  1406. alSetError(Context, AL_INVALID_NAME);
  1407. goto error;
  1408. }
  1409. if(!BufferListStart)
  1410. {
  1411. BufferListStart = malloc(sizeof(ALbufferlistitem));
  1412. BufferListStart->buffer = buffer;
  1413. BufferListStart->next = NULL;
  1414. BufferListStart->prev = NULL;
  1415. BufferList = BufferListStart;
  1416. }
  1417. else
  1418. {
  1419. BufferList->next = malloc(sizeof(ALbufferlistitem));
  1420. BufferList->next->buffer = buffer;
  1421. BufferList->next->next = NULL;
  1422. BufferList->next->prev = BufferList;
  1423. BufferList = BufferList->next;
  1424. }
  1425. if(!buffer) continue;
  1426. // Increment reference counter for buffer
  1427. IncrementRef(&buffer->ref);
  1428. ReadLock(&buffer->lock);
  1429. if(BufferFmt == NULL)
  1430. {
  1431. BufferFmt = buffer;
  1432. Source->NumChannels = ChannelsFromFmt(buffer->FmtChannels);
  1433. Source->SampleSize = BytesFromFmt(buffer->FmtType);
  1434. if(buffer->FmtChannels == FmtMono)
  1435. Source->Update = CalcSourceParams;
  1436. else
  1437. Source->Update = CalcNonAttnSourceParams;
  1438. Source->NeedsUpdate = AL_TRUE;
  1439. }
  1440. else if(BufferFmt->Frequency != buffer->Frequency ||
  1441. BufferFmt->OriginalChannels != buffer->OriginalChannels ||
  1442. BufferFmt->OriginalType != buffer->OriginalType)
  1443. {
  1444. ReadUnlock(&buffer->lock);
  1445. UnlockContext(Context);
  1446. alSetError(Context, AL_INVALID_OPERATION);
  1447. goto error;
  1448. }
  1449. ReadUnlock(&buffer->lock);
  1450. }
  1451. // Change Source Type
  1452. Source->lSourceType = AL_STREAMING;
  1453. if(Source->queue == NULL)
  1454. Source->queue = BufferListStart;
  1455. else
  1456. {
  1457. // Find end of queue
  1458. BufferList = Source->queue;
  1459. while(BufferList->next != NULL)
  1460. BufferList = BufferList->next;
  1461. BufferListStart->prev = BufferList;
  1462. BufferList->next = BufferListStart;
  1463. }
  1464. // Update number of buffers in queue
  1465. Source->BuffersInQueue += n;
  1466. UnlockContext(Context);
  1467. ALCcontext_DecRef(Context);
  1468. return;
  1469. error:
  1470. while(BufferListStart)
  1471. {
  1472. BufferList = BufferListStart;
  1473. BufferListStart = BufferList->next;
  1474. if(BufferList->buffer)
  1475. DecrementRef(&BufferList->buffer->ref);
  1476. free(BufferList);
  1477. }
  1478. ALCcontext_DecRef(Context);
  1479. }
  1480. // Implementation assumes that n is the number of buffers to be removed from the queue and buffers is
  1481. // an array of buffer IDs that are to be filled with the names of the buffers removed
  1482. AL_API ALvoid AL_APIENTRY alSourceUnqueueBuffers( ALuint source, ALsizei n, ALuint* buffers )
  1483. {
  1484. ALCcontext *Context;
  1485. ALsource *Source;
  1486. ALsizei i;
  1487. ALbufferlistitem *BufferList;
  1488. if(n == 0)
  1489. return;
  1490. Context = GetContextRef();
  1491. if(!Context) return;
  1492. if(n < 0)
  1493. {
  1494. alSetError(Context, AL_INVALID_VALUE);
  1495. goto done;
  1496. }
  1497. if((Source=LookupSource(Context, source)) == NULL)
  1498. {
  1499. alSetError(Context, AL_INVALID_NAME);
  1500. goto done;
  1501. }
  1502. LockContext(Context);
  1503. if(Source->bLooping || Source->lSourceType != AL_STREAMING ||
  1504. (ALuint)n > Source->BuffersPlayed)
  1505. {
  1506. UnlockContext(Context);
  1507. // Some buffers can't be unqueue because they have not been processed
  1508. alSetError(Context, AL_INVALID_VALUE);
  1509. goto done;
  1510. }
  1511. for(i = 0;i < n;i++)
  1512. {
  1513. BufferList = Source->queue;
  1514. Source->queue = BufferList->next;
  1515. Source->BuffersInQueue--;
  1516. Source->BuffersPlayed--;
  1517. if(BufferList->buffer)
  1518. {
  1519. // Record name of buffer
  1520. buffers[i] = BufferList->buffer->buffer;
  1521. // Decrement buffer reference counter
  1522. DecrementRef(&BufferList->buffer->ref);
  1523. }
  1524. else
  1525. buffers[i] = 0;
  1526. // Release memory for buffer list item
  1527. free(BufferList);
  1528. }
  1529. if(Source->queue)
  1530. Source->queue->prev = NULL;
  1531. UnlockContext(Context);
  1532. done:
  1533. ALCcontext_DecRef(Context);
  1534. }
  1535. static ALvoid InitSourceParams(ALsource *Source)
  1536. {
  1537. ALuint i;
  1538. Source->flInnerAngle = 360.0f;
  1539. Source->flOuterAngle = 360.0f;
  1540. Source->flPitch = 1.0f;
  1541. Source->vPosition[0] = 0.0f;
  1542. Source->vPosition[1] = 0.0f;
  1543. Source->vPosition[2] = 0.0f;
  1544. Source->vOrientation[0] = 0.0f;
  1545. Source->vOrientation[1] = 0.0f;
  1546. Source->vOrientation[2] = 0.0f;
  1547. Source->vVelocity[0] = 0.0f;
  1548. Source->vVelocity[1] = 0.0f;
  1549. Source->vVelocity[2] = 0.0f;
  1550. Source->flRefDistance = 1.0f;
  1551. Source->flMaxDistance = FLT_MAX;
  1552. Source->flRollOffFactor = 1.0f;
  1553. Source->bLooping = AL_FALSE;
  1554. Source->flGain = 1.0f;
  1555. Source->flMinGain = 0.0f;
  1556. Source->flMaxGain = 1.0f;
  1557. Source->flOuterGain = 0.0f;
  1558. Source->OuterGainHF = 1.0f;
  1559. Source->DryGainHFAuto = AL_TRUE;
  1560. Source->WetGainAuto = AL_TRUE;
  1561. Source->WetGainHFAuto = AL_TRUE;
  1562. Source->AirAbsorptionFactor = 0.0f;
  1563. Source->RoomRolloffFactor = 0.0f;
  1564. Source->DopplerFactor = 1.0f;
  1565. Source->VirtualChannels = AL_TRUE;
  1566. Source->DistanceModel = AL_INVERSE_DISTANCE_CLAMPED;
  1567. Source->Resampler = DefaultResampler;
  1568. Source->state = AL_INITIAL;
  1569. Source->new_state = AL_NONE;
  1570. Source->lSourceType = AL_UNDETERMINED;
  1571. Source->lOffset = -1;
  1572. Source->DirectGain = 1.0f;
  1573. Source->DirectGainHF = 1.0f;
  1574. for(i = 0;i < MAX_SENDS;i++)
  1575. {
  1576. Source->Send[i].WetGain = 1.0f;
  1577. Source->Send[i].WetGainHF = 1.0f;
  1578. }
  1579. Source->NeedsUpdate = AL_TRUE;
  1580. Source->HrtfMoving = AL_FALSE;
  1581. Source->HrtfCounter = 0;
  1582. }
  1583. /*
  1584. * SetSourceState
  1585. *
  1586. * Sets the source's new play state given its current state
  1587. */
  1588. ALvoid SetSourceState(ALsource *Source, ALCcontext *Context, ALenum state)
  1589. {
  1590. if(state == AL_PLAYING)
  1591. {
  1592. ALbufferlistitem *BufferList;
  1593. ALsizei j, k;
  1594. /* Check that there is a queue containing at least one non-null, non zero length AL Buffer */
  1595. BufferList = Source->queue;
  1596. while(BufferList)
  1597. {
  1598. if(BufferList->buffer != NULL && BufferList->buffer->SampleLen)
  1599. break;
  1600. BufferList = BufferList->next;
  1601. }
  1602. if(Source->state != AL_PLAYING)
  1603. {
  1604. for(j = 0;j < MAXCHANNELS;j++)
  1605. {
  1606. for(k = 0;k < SRC_HISTORY_LENGTH;k++)
  1607. Source->HrtfHistory[j][k] = 0.0f;
  1608. for(k = 0;k < HRIR_LENGTH;k++)
  1609. {
  1610. Source->HrtfValues[j][k][0] = 0.0f;
  1611. Source->HrtfValues[j][k][1] = 0.0f;
  1612. }
  1613. }
  1614. }
  1615. if(Source->state != AL_PAUSED)
  1616. {
  1617. Source->state = AL_PLAYING;
  1618. Source->position = 0;
  1619. Source->position_fraction = 0;
  1620. Source->BuffersPlayed = 0;
  1621. }
  1622. else
  1623. Source->state = AL_PLAYING;
  1624. // Check if an Offset has been set
  1625. if(Source->lOffset != -1)
  1626. ApplyOffset(Source);
  1627. /* If there's nothing to play, or device is disconnected, go right to
  1628. * stopped */
  1629. if(!BufferList || !Context->Device->Connected)
  1630. {
  1631. SetSourceState(Source, Context, AL_STOPPED);
  1632. return;
  1633. }
  1634. for(j = 0;j < Context->ActiveSourceCount;j++)
  1635. {
  1636. if(Context->ActiveSources[j] == Source)
  1637. break;
  1638. }
  1639. if(j == Context->ActiveSourceCount)
  1640. Context->ActiveSources[Context->ActiveSourceCount++] = Source;
  1641. }
  1642. else if(state == AL_PAUSED)
  1643. {
  1644. if(Source->state == AL_PLAYING)
  1645. {
  1646. Source->state = AL_PAUSED;
  1647. Source->HrtfMoving = AL_FALSE;
  1648. Source->HrtfCounter = 0;
  1649. }
  1650. }
  1651. else if(state == AL_STOPPED)
  1652. {
  1653. if(Source->state != AL_INITIAL)
  1654. {
  1655. Source->state = AL_STOPPED;
  1656. Source->BuffersPlayed = Source->BuffersInQueue;
  1657. Source->HrtfMoving = AL_FALSE;
  1658. Source->HrtfCounter = 0;
  1659. }
  1660. Source->lOffset = -1;
  1661. }
  1662. else if(state == AL_INITIAL)
  1663. {
  1664. if(Source->state != AL_INITIAL)
  1665. {
  1666. Source->state = AL_INITIAL;
  1667. Source->position = 0;
  1668. Source->position_fraction = 0;
  1669. Source->BuffersPlayed = 0;
  1670. Source->HrtfMoving = AL_FALSE;
  1671. Source->HrtfCounter = 0;
  1672. }
  1673. Source->lOffset = -1;
  1674. }
  1675. }
  1676. /*
  1677. GetSourceOffset
  1678. Gets the current playback position in the given Source, in the appropriate format (Bytes, Samples or MilliSeconds)
  1679. The offset is relative to the start of the queue (not the start of the current buffer)
  1680. */
  1681. static ALvoid GetSourceOffset(ALsource *Source, ALenum name, ALdouble *offset, ALdouble updateLen)
  1682. {
  1683. const ALbufferlistitem *BufferList;
  1684. const ALbuffer *Buffer = NULL;
  1685. ALuint BufferFreq = 0;
  1686. ALuint readPos, writePos;
  1687. ALuint totalBufferLen;
  1688. ALuint i;
  1689. // Find the first non-NULL Buffer in the Queue
  1690. BufferList = Source->queue;
  1691. while(BufferList)
  1692. {
  1693. if(BufferList->buffer)
  1694. {
  1695. Buffer = BufferList->buffer;
  1696. BufferFreq = Buffer->Frequency;
  1697. break;
  1698. }
  1699. BufferList = BufferList->next;
  1700. }
  1701. if((Source->state != AL_PLAYING && Source->state != AL_PAUSED) || !Buffer)
  1702. {
  1703. offset[0] = 0.0;
  1704. offset[1] = 0.0;
  1705. return;
  1706. }
  1707. // Get Current SamplesPlayed (NOTE : This is the offset into the *current* buffer)
  1708. readPos = Source->position;
  1709. // Add length of any processed buffers in the queue
  1710. totalBufferLen = 0;
  1711. BufferList = Source->queue;
  1712. for(i = 0;BufferList;i++)
  1713. {
  1714. if(BufferList->buffer)
  1715. {
  1716. if(i < Source->BuffersPlayed)
  1717. readPos += BufferList->buffer->SampleLen;
  1718. totalBufferLen += BufferList->buffer->SampleLen;
  1719. }
  1720. BufferList = BufferList->next;
  1721. }
  1722. if(Source->state == AL_PLAYING)
  1723. writePos = readPos + (ALuint)(updateLen*BufferFreq);
  1724. else
  1725. writePos = readPos;
  1726. if(Source->bLooping)
  1727. {
  1728. readPos %= totalBufferLen;
  1729. writePos %= totalBufferLen;
  1730. }
  1731. else
  1732. {
  1733. // Wrap positions back to 0
  1734. if(readPos >= totalBufferLen)
  1735. readPos = 0;
  1736. if(writePos >= totalBufferLen)
  1737. writePos = 0;
  1738. }
  1739. switch(name)
  1740. {
  1741. case AL_SEC_OFFSET:
  1742. offset[0] = (ALdouble)readPos / Buffer->Frequency;
  1743. offset[1] = (ALdouble)writePos / Buffer->Frequency;
  1744. break;
  1745. case AL_SAMPLE_OFFSET:
  1746. case AL_SAMPLE_RW_OFFSETS_SOFT:
  1747. offset[0] = (ALdouble)readPos;
  1748. offset[1] = (ALdouble)writePos;
  1749. break;
  1750. case AL_BYTE_OFFSET:
  1751. case AL_BYTE_RW_OFFSETS_SOFT:
  1752. // Take into account the original format of the Buffer
  1753. if(Buffer->OriginalType == UserFmtIMA4)
  1754. {
  1755. ALuint BlockSize = 36 * ChannelsFromFmt(Buffer->FmtChannels);
  1756. ALuint FrameBlockSize = 65;
  1757. // Round down to nearest ADPCM block
  1758. offset[0] = (ALdouble)(readPos / FrameBlockSize * BlockSize);
  1759. if(Source->state != AL_PLAYING)
  1760. offset[1] = offset[0];
  1761. else
  1762. {
  1763. // Round up to nearest ADPCM block
  1764. offset[1] = (ALdouble)((writePos+FrameBlockSize-1) /
  1765. FrameBlockSize * BlockSize);
  1766. }
  1767. }
  1768. else
  1769. {
  1770. ALuint FrameSize = FrameSizeFromUserFmt(Buffer->OriginalChannels, Buffer->OriginalType);
  1771. offset[0] = (ALdouble)(readPos * FrameSize);
  1772. offset[1] = (ALdouble)(writePos * FrameSize);
  1773. }
  1774. break;
  1775. }
  1776. }
  1777. /*
  1778. ApplyOffset
  1779. Apply a playback offset to the Source. This function will update the queue (to correctly
  1780. mark buffers as 'pending' or 'processed' depending upon the new offset.
  1781. */
  1782. ALboolean ApplyOffset(ALsource *Source)
  1783. {
  1784. const ALbufferlistitem *BufferList;
  1785. const ALbuffer *Buffer;
  1786. ALint bufferLen, totalBufferLen;
  1787. ALint buffersPlayed;
  1788. ALint offset;
  1789. // Get true byte offset
  1790. offset = GetSampleOffset(Source);
  1791. // If the offset is invalid, don't apply it
  1792. if(offset == -1)
  1793. return AL_FALSE;
  1794. // Sort out the queue (pending and processed states)
  1795. BufferList = Source->queue;
  1796. totalBufferLen = 0;
  1797. buffersPlayed = 0;
  1798. while(BufferList)
  1799. {
  1800. Buffer = BufferList->buffer;
  1801. bufferLen = Buffer ? Buffer->SampleLen : 0;
  1802. if(bufferLen <= offset-totalBufferLen)
  1803. {
  1804. // Offset is past this buffer so increment BuffersPlayed
  1805. buffersPlayed++;
  1806. }
  1807. else if(totalBufferLen <= offset)
  1808. {
  1809. // Offset is within this buffer
  1810. Source->BuffersPlayed = buffersPlayed;
  1811. // SW Mixer Positions are in Samples
  1812. Source->position = offset - totalBufferLen;
  1813. return AL_TRUE;
  1814. }
  1815. // Increment the TotalBufferSize
  1816. totalBufferLen += bufferLen;
  1817. // Move on to next buffer in the Queue
  1818. BufferList = BufferList->next;
  1819. }
  1820. // Offset is out of range of the buffer queue
  1821. return AL_FALSE;
  1822. }
  1823. /*
  1824. GetSampleOffset
  1825. Returns the sample offset into the Source's queue (from the Sample, Byte or Millisecond offset
  1826. supplied by the application). This takes into account the fact that the buffer format may have
  1827. been modifed by AL
  1828. */
  1829. static ALint GetSampleOffset(ALsource *Source)
  1830. {
  1831. const ALbuffer *Buffer = NULL;
  1832. const ALbufferlistitem *BufferList;
  1833. ALint Offset = -1;
  1834. // Find the first non-NULL Buffer in the Queue
  1835. BufferList = Source->queue;
  1836. while(BufferList)
  1837. {
  1838. if(BufferList->buffer)
  1839. {
  1840. Buffer = BufferList->buffer;
  1841. break;
  1842. }
  1843. BufferList = BufferList->next;
  1844. }
  1845. if(!Buffer)
  1846. {
  1847. Source->lOffset = -1;
  1848. return -1;
  1849. }
  1850. // Determine the ByteOffset (and ensure it is block aligned)
  1851. switch(Source->lOffsetType)
  1852. {
  1853. case AL_BYTE_OFFSET:
  1854. // Take into consideration the original format
  1855. Offset = Source->lOffset;
  1856. if(Buffer->OriginalType == UserFmtIMA4)
  1857. {
  1858. // Round down to nearest ADPCM block
  1859. Offset /= 36 * ChannelsFromUserFmt(Buffer->OriginalChannels);
  1860. // Multiply by compression rate (65 sample frames per block)
  1861. Offset *= 65;
  1862. }
  1863. else
  1864. Offset /= FrameSizeFromUserFmt(Buffer->OriginalChannels, Buffer->OriginalType);
  1865. break;
  1866. case AL_SAMPLE_OFFSET:
  1867. Offset = Source->lOffset;
  1868. break;
  1869. case AL_SEC_OFFSET:
  1870. // Note - lOffset is internally stored as Milliseconds
  1871. Offset = (ALint)(Source->lOffset / 1000.0 * Buffer->Frequency);
  1872. break;
  1873. }
  1874. // Clear Offset
  1875. Source->lOffset = -1;
  1876. return Offset;
  1877. }
  1878. ALvoid ReleaseALSources(ALCcontext *Context)
  1879. {
  1880. ALsizei pos;
  1881. ALuint j;
  1882. for(pos = 0;pos < Context->SourceMap.size;pos++)
  1883. {
  1884. ALsource *temp = Context->SourceMap.array[pos].value;
  1885. Context->SourceMap.array[pos].value = NULL;
  1886. // For each buffer in the source's queue, decrement its reference counter and remove it
  1887. while(temp->queue != NULL)
  1888. {
  1889. ALbufferlistitem *BufferList = temp->queue;
  1890. temp->queue = BufferList->next;
  1891. if(BufferList->buffer != NULL)
  1892. DecrementRef(&BufferList->buffer->ref);
  1893. free(BufferList);
  1894. }
  1895. for(j = 0;j < MAX_SENDS;++j)
  1896. {
  1897. if(temp->Send[j].Slot)
  1898. DecrementRef(&temp->Send[j].Slot->ref);
  1899. temp->Send[j].Slot = NULL;
  1900. }
  1901. // Release source structure
  1902. FreeThunkEntry(temp->source);
  1903. memset(temp, 0, sizeof(ALsource));
  1904. free(temp);
  1905. }
  1906. }