alSource.c 62 KB

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