alSource.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708
  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 "AL/al.h"
  25. #include "AL/alc.h"
  26. #include "alMain.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 = LinearResampler;
  33. const ALsizei ResamplerPadding[ResamplerMax] = {
  34. 0, /* Point */
  35. 1, /* Linear */
  36. 2, /* Cubic */
  37. };
  38. const ALsizei ResamplerPrePadding[ResamplerMax] = {
  39. 0, /* Point */
  40. 0, /* Linear */
  41. 1, /* Cubic */
  42. };
  43. extern inline struct ALsource *LookupSource(ALCcontext *context, ALuint id);
  44. extern inline struct ALsource *RemoveSource(ALCcontext *context, ALuint id);
  45. static ALvoid InitSourceParams(ALsource *Source);
  46. static ALint64 GetSourceOffset(const ALsource *Source);
  47. static ALdouble GetSourceSecOffset(const ALsource *Source);
  48. static ALvoid GetSourceOffsets(const ALsource *Source, ALenum name, ALdouble *offsets, ALdouble updateLen);
  49. static ALint GetSampleOffset(ALsource *Source);
  50. typedef enum SrcFloatProp {
  51. sfPitch = AL_PITCH,
  52. sfGain = AL_GAIN,
  53. sfMinGain = AL_MIN_GAIN,
  54. sfMaxGain = AL_MAX_GAIN,
  55. sfMaxDistance = AL_MAX_DISTANCE,
  56. sfRolloffFactor = AL_ROLLOFF_FACTOR,
  57. sfDopplerFactor = AL_DOPPLER_FACTOR,
  58. sfConeOuterGain = AL_CONE_OUTER_GAIN,
  59. sfSecOffset = AL_SEC_OFFSET,
  60. sfSampleOffset = AL_SAMPLE_OFFSET,
  61. sfByteOffset = AL_BYTE_OFFSET,
  62. sfConeInnerAngle = AL_CONE_INNER_ANGLE,
  63. sfConeOuterAngle = AL_CONE_OUTER_ANGLE,
  64. sfRefDistance = AL_REFERENCE_DISTANCE,
  65. sfPosition = AL_POSITION,
  66. sfVelocity = AL_VELOCITY,
  67. sfDirection = AL_DIRECTION,
  68. sfSourceRelative = AL_SOURCE_RELATIVE,
  69. sfLooping = AL_LOOPING,
  70. sfBuffer = AL_BUFFER,
  71. sfSourceState = AL_SOURCE_STATE,
  72. sfBuffersQueued = AL_BUFFERS_QUEUED,
  73. sfBuffersProcessed = AL_BUFFERS_PROCESSED,
  74. sfSourceType = AL_SOURCE_TYPE,
  75. /* ALC_EXT_EFX */
  76. sfConeOuterGainHF = AL_CONE_OUTER_GAINHF,
  77. sfAirAbsorptionFactor = AL_AIR_ABSORPTION_FACTOR,
  78. sfRoomRolloffFactor = AL_ROOM_ROLLOFF_FACTOR,
  79. sfDirectFilterGainHFAuto = AL_DIRECT_FILTER_GAINHF_AUTO,
  80. sfAuxSendFilterGainAuto = AL_AUXILIARY_SEND_FILTER_GAIN_AUTO,
  81. sfAuxSendFilterGainHFAuto = AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO,
  82. /* AL_SOFT_direct_channels */
  83. sfDirectChannelsSOFT = AL_DIRECT_CHANNELS_SOFT,
  84. /* AL_EXT_source_distance_model */
  85. sfDistanceModel = AL_DISTANCE_MODEL,
  86. /* AL_SOFT_buffer_sub_data / AL_SOFT_buffer_samples */
  87. sfSampleRWOffsetsSOFT = AL_SAMPLE_RW_OFFSETS_SOFT,
  88. sfByteRWOffsetsSOFT = AL_BYTE_RW_OFFSETS_SOFT,
  89. /* AL_SOFT_source_latency */
  90. sfSecOffsetLatencySOFT = AL_SEC_OFFSET_LATENCY_SOFT,
  91. } SrcFloatProp;
  92. typedef enum SrcIntProp {
  93. siMaxDistance = AL_MAX_DISTANCE,
  94. siRolloffFactor = AL_ROLLOFF_FACTOR,
  95. siRefDistance = AL_REFERENCE_DISTANCE,
  96. siSourceRelative = AL_SOURCE_RELATIVE,
  97. siConeInnerAngle = AL_CONE_INNER_ANGLE,
  98. siConeOuterAngle = AL_CONE_OUTER_ANGLE,
  99. siLooping = AL_LOOPING,
  100. siBuffer = AL_BUFFER,
  101. siSourceState = AL_SOURCE_STATE,
  102. siBuffersQueued = AL_BUFFERS_QUEUED,
  103. siBuffersProcessed = AL_BUFFERS_PROCESSED,
  104. siSourceType = AL_SOURCE_TYPE,
  105. siSecOffset = AL_SEC_OFFSET,
  106. siSampleOffset = AL_SAMPLE_OFFSET,
  107. siByteOffset = AL_BYTE_OFFSET,
  108. siDopplerFactor = AL_DOPPLER_FACTOR,
  109. siPosition = AL_POSITION,
  110. siVelocity = AL_VELOCITY,
  111. siDirection = AL_DIRECTION,
  112. /* ALC_EXT_EFX */
  113. siDirectFilterGainHFAuto = AL_DIRECT_FILTER_GAINHF_AUTO,
  114. siAuxSendFilterGainAutio = AL_AUXILIARY_SEND_FILTER_GAIN_AUTO,
  115. siAuxSendFilterGainHFAuto = AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO,
  116. siDirectFilter = AL_DIRECT_FILTER,
  117. siAuxSendFilter = AL_AUXILIARY_SEND_FILTER,
  118. /* AL_SOFT_direct_channels */
  119. siDirectChannelsSOFT = AL_DIRECT_CHANNELS_SOFT,
  120. /* AL_EXT_source_distance_model */
  121. siDistanceModel = AL_DISTANCE_MODEL,
  122. /* AL_SOFT_buffer_sub_data / AL_SOFT_buffer_samples */
  123. siSampleRWOffsetsSOFT = AL_SAMPLE_RW_OFFSETS_SOFT,
  124. siByteRWOffsetsSOFT = AL_BYTE_RW_OFFSETS_SOFT,
  125. /* AL_SOFT_source_latency */
  126. siSampleOffsetLatencySOFT = AL_SAMPLE_OFFSET_LATENCY_SOFT,
  127. } SrcIntProp;
  128. static ALboolean SetSourcefv(ALsource *Source, ALCcontext *Context, SrcFloatProp prop, const ALfloat *values);
  129. static ALboolean SetSourceiv(ALsource *Source, ALCcontext *Context, SrcIntProp prop, const ALint *values);
  130. static ALboolean SetSourcei64v(ALsource *Source, ALCcontext *Context, SrcIntProp prop, const ALint64SOFT *values);
  131. static ALboolean GetSourcedv(const ALsource *Source, ALCcontext *Context, SrcFloatProp prop, ALdouble *values);
  132. static ALboolean GetSourceiv(const ALsource *Source, ALCcontext *Context, SrcIntProp prop, ALint *values);
  133. static ALboolean GetSourcei64v(const ALsource *Source, ALCcontext *Context, SrcIntProp prop, ALint64 *values);
  134. static ALint FloatValsByProp(ALenum prop)
  135. {
  136. if(prop != (ALenum)((SrcFloatProp)prop))
  137. return 0;
  138. switch((SrcFloatProp)prop)
  139. {
  140. case sfPitch:
  141. case sfGain:
  142. case sfMinGain:
  143. case sfMaxGain:
  144. case sfMaxDistance:
  145. case sfRolloffFactor:
  146. case sfDopplerFactor:
  147. case sfConeOuterGain:
  148. case sfSecOffset:
  149. case sfSampleOffset:
  150. case sfByteOffset:
  151. case sfConeInnerAngle:
  152. case sfConeOuterAngle:
  153. case sfRefDistance:
  154. case sfConeOuterGainHF:
  155. case sfAirAbsorptionFactor:
  156. case sfRoomRolloffFactor:
  157. case sfDirectFilterGainHFAuto:
  158. case sfAuxSendFilterGainAuto:
  159. case sfAuxSendFilterGainHFAuto:
  160. case sfDirectChannelsSOFT:
  161. case sfDistanceModel:
  162. case sfSourceRelative:
  163. case sfLooping:
  164. case sfBuffer:
  165. case sfSourceState:
  166. case sfBuffersQueued:
  167. case sfBuffersProcessed:
  168. case sfSourceType:
  169. return 1;
  170. case sfSampleRWOffsetsSOFT:
  171. case sfByteRWOffsetsSOFT:
  172. return 2;
  173. case sfPosition:
  174. case sfVelocity:
  175. case sfDirection:
  176. return 3;
  177. case sfSecOffsetLatencySOFT:
  178. break; /* Double only */
  179. }
  180. return 0;
  181. }
  182. static ALint DoubleValsByProp(ALenum prop)
  183. {
  184. if(prop != (ALenum)((SrcFloatProp)prop))
  185. return 0;
  186. switch((SrcFloatProp)prop)
  187. {
  188. case sfPitch:
  189. case sfGain:
  190. case sfMinGain:
  191. case sfMaxGain:
  192. case sfMaxDistance:
  193. case sfRolloffFactor:
  194. case sfDopplerFactor:
  195. case sfConeOuterGain:
  196. case sfSecOffset:
  197. case sfSampleOffset:
  198. case sfByteOffset:
  199. case sfConeInnerAngle:
  200. case sfConeOuterAngle:
  201. case sfRefDistance:
  202. case sfConeOuterGainHF:
  203. case sfAirAbsorptionFactor:
  204. case sfRoomRolloffFactor:
  205. case sfDirectFilterGainHFAuto:
  206. case sfAuxSendFilterGainAuto:
  207. case sfAuxSendFilterGainHFAuto:
  208. case sfDirectChannelsSOFT:
  209. case sfDistanceModel:
  210. case sfSourceRelative:
  211. case sfLooping:
  212. case sfBuffer:
  213. case sfSourceState:
  214. case sfBuffersQueued:
  215. case sfBuffersProcessed:
  216. case sfSourceType:
  217. return 1;
  218. case sfSampleRWOffsetsSOFT:
  219. case sfByteRWOffsetsSOFT:
  220. case sfSecOffsetLatencySOFT:
  221. return 2;
  222. case sfPosition:
  223. case sfVelocity:
  224. case sfDirection:
  225. return 3;
  226. }
  227. return 0;
  228. }
  229. static ALint IntValsByProp(ALenum prop)
  230. {
  231. if(prop != (ALenum)((SrcIntProp)prop))
  232. return 0;
  233. switch((SrcIntProp)prop)
  234. {
  235. case siMaxDistance:
  236. case siRolloffFactor:
  237. case siRefDistance:
  238. case siSourceRelative:
  239. case siConeInnerAngle:
  240. case siConeOuterAngle:
  241. case siLooping:
  242. case siBuffer:
  243. case siSourceState:
  244. case siBuffersQueued:
  245. case siBuffersProcessed:
  246. case siSourceType:
  247. case siSecOffset:
  248. case siSampleOffset:
  249. case siByteOffset:
  250. case siDopplerFactor:
  251. case siDirectFilterGainHFAuto:
  252. case siAuxSendFilterGainAutio:
  253. case siAuxSendFilterGainHFAuto:
  254. case siDirectFilter:
  255. case siDirectChannelsSOFT:
  256. case siDistanceModel:
  257. return 1;
  258. case siSampleRWOffsetsSOFT:
  259. case siByteRWOffsetsSOFT:
  260. return 2;
  261. case siPosition:
  262. case siVelocity:
  263. case siDirection:
  264. case siAuxSendFilter:
  265. return 3;
  266. case siSampleOffsetLatencySOFT:
  267. break; /* i64 only */
  268. }
  269. return 0;
  270. }
  271. static ALint Int64ValsByProp(ALenum prop)
  272. {
  273. if(prop != (ALenum)((SrcIntProp)prop))
  274. return 0;
  275. switch((SrcIntProp)prop)
  276. {
  277. case siMaxDistance:
  278. case siRolloffFactor:
  279. case siRefDistance:
  280. case siSourceRelative:
  281. case siConeInnerAngle:
  282. case siConeOuterAngle:
  283. case siLooping:
  284. case siBuffer:
  285. case siSourceState:
  286. case siBuffersQueued:
  287. case siBuffersProcessed:
  288. case siSourceType:
  289. case siSecOffset:
  290. case siSampleOffset:
  291. case siByteOffset:
  292. case siDopplerFactor:
  293. case siDirectFilterGainHFAuto:
  294. case siAuxSendFilterGainAutio:
  295. case siAuxSendFilterGainHFAuto:
  296. case siDirectFilter:
  297. case siDirectChannelsSOFT:
  298. case siDistanceModel:
  299. return 1;
  300. case siSampleRWOffsetsSOFT:
  301. case siByteRWOffsetsSOFT:
  302. case siSampleOffsetLatencySOFT:
  303. return 2;
  304. case siPosition:
  305. case siVelocity:
  306. case siDirection:
  307. case siAuxSendFilter:
  308. return 3;
  309. }
  310. return 0;
  311. }
  312. #define CHECKVAL(x) do { \
  313. if(!(x)) \
  314. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_VALUE, AL_FALSE); \
  315. } while(0)
  316. static ALboolean SetSourcefv(ALsource *Source, ALCcontext *Context, SrcFloatProp prop, const ALfloat *values)
  317. {
  318. ALint ival;
  319. switch(prop)
  320. {
  321. case AL_PITCH:
  322. CHECKVAL(*values >= 0.0f);
  323. Source->Pitch = *values;
  324. Source->NeedsUpdate = AL_TRUE;
  325. return AL_TRUE;
  326. case AL_CONE_INNER_ANGLE:
  327. CHECKVAL(*values >= 0.0f && *values <= 360.0f);
  328. Source->InnerAngle = *values;
  329. Source->NeedsUpdate = AL_TRUE;
  330. return AL_TRUE;
  331. case AL_CONE_OUTER_ANGLE:
  332. CHECKVAL(*values >= 0.0f && *values <= 360.0f);
  333. Source->OuterAngle = *values;
  334. Source->NeedsUpdate = AL_TRUE;
  335. return AL_TRUE;
  336. case AL_GAIN:
  337. CHECKVAL(*values >= 0.0f);
  338. Source->Gain = *values;
  339. Source->NeedsUpdate = AL_TRUE;
  340. return AL_TRUE;
  341. case AL_MAX_DISTANCE:
  342. CHECKVAL(*values >= 0.0f);
  343. Source->MaxDistance = *values;
  344. Source->NeedsUpdate = AL_TRUE;
  345. return AL_TRUE;
  346. case AL_ROLLOFF_FACTOR:
  347. CHECKVAL(*values >= 0.0f);
  348. Source->RollOffFactor = *values;
  349. Source->NeedsUpdate = AL_TRUE;
  350. return AL_TRUE;
  351. case AL_REFERENCE_DISTANCE:
  352. CHECKVAL(*values >= 0.0f);
  353. Source->RefDistance = *values;
  354. Source->NeedsUpdate = AL_TRUE;
  355. return AL_TRUE;
  356. case AL_MIN_GAIN:
  357. CHECKVAL(*values >= 0.0f && *values <= 1.0f);
  358. Source->MinGain = *values;
  359. Source->NeedsUpdate = AL_TRUE;
  360. return AL_TRUE;
  361. case AL_MAX_GAIN:
  362. CHECKVAL(*values >= 0.0f && *values <= 1.0f);
  363. Source->MaxGain = *values;
  364. Source->NeedsUpdate = AL_TRUE;
  365. return AL_TRUE;
  366. case AL_CONE_OUTER_GAIN:
  367. CHECKVAL(*values >= 0.0f && *values <= 1.0f);
  368. Source->OuterGain = *values;
  369. Source->NeedsUpdate = AL_TRUE;
  370. return AL_TRUE;
  371. case AL_CONE_OUTER_GAINHF:
  372. CHECKVAL(*values >= 0.0f && *values <= 1.0f);
  373. Source->OuterGainHF = *values;
  374. Source->NeedsUpdate = AL_TRUE;
  375. return AL_TRUE;
  376. case AL_AIR_ABSORPTION_FACTOR:
  377. CHECKVAL(*values >= 0.0f && *values <= 10.0f);
  378. Source->AirAbsorptionFactor = *values;
  379. Source->NeedsUpdate = AL_TRUE;
  380. return AL_TRUE;
  381. case AL_ROOM_ROLLOFF_FACTOR:
  382. CHECKVAL(*values >= 0.0f && *values <= 10.0f);
  383. Source->RoomRolloffFactor = *values;
  384. Source->NeedsUpdate = AL_TRUE;
  385. return AL_TRUE;
  386. case AL_DOPPLER_FACTOR:
  387. CHECKVAL(*values >= 0.0f && *values <= 1.0f);
  388. Source->DopplerFactor = *values;
  389. Source->NeedsUpdate = AL_TRUE;
  390. return AL_TRUE;
  391. case AL_SEC_OFFSET:
  392. case AL_SAMPLE_OFFSET:
  393. case AL_BYTE_OFFSET:
  394. CHECKVAL(*values >= 0.0f);
  395. LockContext(Context);
  396. Source->OffsetType = prop;
  397. Source->Offset = *values;
  398. if((Source->state == AL_PLAYING || Source->state == AL_PAUSED) &&
  399. !Context->DeferUpdates)
  400. {
  401. if(ApplyOffset(Source) == AL_FALSE)
  402. {
  403. UnlockContext(Context);
  404. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_VALUE, AL_FALSE);
  405. }
  406. }
  407. UnlockContext(Context);
  408. return AL_TRUE;
  409. case AL_SEC_OFFSET_LATENCY_SOFT:
  410. /* Query only */
  411. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_OPERATION, AL_FALSE);
  412. case AL_POSITION:
  413. CHECKVAL(isfinite(values[0]) && isfinite(values[1]) && isfinite(values[2]));
  414. LockContext(Context);
  415. Source->Position[0] = values[0];
  416. Source->Position[1] = values[1];
  417. Source->Position[2] = values[2];
  418. UnlockContext(Context);
  419. Source->NeedsUpdate = AL_TRUE;
  420. return AL_TRUE;
  421. case AL_VELOCITY:
  422. CHECKVAL(isfinite(values[0]) && isfinite(values[1]) && isfinite(values[2]));
  423. LockContext(Context);
  424. Source->Velocity[0] = values[0];
  425. Source->Velocity[1] = values[1];
  426. Source->Velocity[2] = values[2];
  427. UnlockContext(Context);
  428. Source->NeedsUpdate = AL_TRUE;
  429. return AL_TRUE;
  430. case AL_DIRECTION:
  431. CHECKVAL(isfinite(values[0]) && isfinite(values[1]) && isfinite(values[2]));
  432. LockContext(Context);
  433. Source->Orientation[0] = values[0];
  434. Source->Orientation[1] = values[1];
  435. Source->Orientation[2] = values[2];
  436. UnlockContext(Context);
  437. Source->NeedsUpdate = AL_TRUE;
  438. return AL_TRUE;
  439. case sfSampleRWOffsetsSOFT:
  440. case sfByteRWOffsetsSOFT:
  441. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_OPERATION, AL_FALSE);
  442. case sfSourceRelative:
  443. case sfLooping:
  444. case sfSourceState:
  445. case sfSourceType:
  446. case sfDistanceModel:
  447. case sfDirectFilterGainHFAuto:
  448. case sfAuxSendFilterGainAuto:
  449. case sfAuxSendFilterGainHFAuto:
  450. case sfDirectChannelsSOFT:
  451. ival = (ALint)values[0];
  452. return SetSourceiv(Source, Context, (SrcIntProp)prop, &ival);
  453. case sfBuffer:
  454. case sfBuffersQueued:
  455. case sfBuffersProcessed:
  456. ival = (ALint)((ALuint)values[0]);
  457. return SetSourceiv(Source, Context, (SrcIntProp)prop, &ival);
  458. }
  459. ERR("Unexpected property: 0x%04x\n", prop);
  460. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_ENUM, AL_FALSE);
  461. }
  462. static ALboolean SetSourceiv(ALsource *Source, ALCcontext *Context, SrcIntProp prop, const ALint *values)
  463. {
  464. ALCdevice *device = Context->Device;
  465. ALbuffer *buffer = NULL;
  466. ALfilter *filter = NULL;
  467. ALeffectslot *slot = NULL;
  468. ALbufferlistitem *oldlist;
  469. ALfloat fvals[3];
  470. switch(prop)
  471. {
  472. case AL_SOURCE_RELATIVE:
  473. CHECKVAL(*values == AL_FALSE || *values == AL_TRUE);
  474. Source->HeadRelative = (ALboolean)*values;
  475. Source->NeedsUpdate = AL_TRUE;
  476. return AL_TRUE;
  477. case AL_LOOPING:
  478. CHECKVAL(*values == AL_FALSE || *values == AL_TRUE);
  479. Source->Looping = (ALboolean)*values;
  480. return AL_TRUE;
  481. case AL_BUFFER:
  482. CHECKVAL(*values == 0 || (buffer=LookupBuffer(device, *values)) != NULL);
  483. LockContext(Context);
  484. if(!(Source->state == AL_STOPPED || Source->state == AL_INITIAL))
  485. {
  486. UnlockContext(Context);
  487. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_OPERATION, AL_FALSE);
  488. }
  489. Source->BuffersInQueue = 0;
  490. Source->BuffersPlayed = 0;
  491. if(buffer != NULL)
  492. {
  493. ALbufferlistitem *BufferListItem;
  494. /* Source is now Static */
  495. Source->SourceType = AL_STATIC;
  496. /* Add the selected buffer to a one-item queue */
  497. BufferListItem = malloc(sizeof(ALbufferlistitem));
  498. BufferListItem->buffer = buffer;
  499. BufferListItem->next = NULL;
  500. BufferListItem->prev = NULL;
  501. IncrementRef(&buffer->ref);
  502. oldlist = ExchangePtr((XchgPtr*)&Source->queue, BufferListItem);
  503. Source->BuffersInQueue = 1;
  504. ReadLock(&buffer->lock);
  505. Source->NumChannels = ChannelsFromFmt(buffer->FmtChannels);
  506. Source->SampleSize = BytesFromFmt(buffer->FmtType);
  507. ReadUnlock(&buffer->lock);
  508. if(buffer->FmtChannels == FmtMono)
  509. Source->Update = CalcSourceParams;
  510. else
  511. Source->Update = CalcNonAttnSourceParams;
  512. Source->NeedsUpdate = AL_TRUE;
  513. }
  514. else
  515. {
  516. /* Source is now Undetermined */
  517. Source->SourceType = AL_UNDETERMINED;
  518. oldlist = ExchangePtr((XchgPtr*)&Source->queue, NULL);
  519. }
  520. /* Delete all elements in the previous queue */
  521. while(oldlist != NULL)
  522. {
  523. ALbufferlistitem *temp = oldlist;
  524. oldlist = temp->next;
  525. if(temp->buffer)
  526. DecrementRef(&temp->buffer->ref);
  527. free(temp);
  528. }
  529. UnlockContext(Context);
  530. return AL_TRUE;
  531. case siSourceState:
  532. case siSourceType:
  533. case siBuffersQueued:
  534. case siBuffersProcessed:
  535. /* Query only */
  536. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_OPERATION, AL_FALSE);
  537. case AL_SEC_OFFSET:
  538. case AL_SAMPLE_OFFSET:
  539. case AL_BYTE_OFFSET:
  540. CHECKVAL(*values >= 0);
  541. LockContext(Context);
  542. Source->OffsetType = prop;
  543. Source->Offset = *values;
  544. if((Source->state == AL_PLAYING || Source->state == AL_PAUSED) &&
  545. !Context->DeferUpdates)
  546. {
  547. if(ApplyOffset(Source) == AL_FALSE)
  548. {
  549. UnlockContext(Context);
  550. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_VALUE, AL_FALSE);
  551. }
  552. }
  553. UnlockContext(Context);
  554. return AL_TRUE;
  555. case siSampleRWOffsetsSOFT:
  556. case siByteRWOffsetsSOFT:
  557. /* Query only */
  558. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_OPERATION, AL_FALSE);
  559. case AL_DIRECT_FILTER:
  560. CHECKVAL(*values == 0 || (filter=LookupFilter(device, *values)) != NULL);
  561. LockContext(Context);
  562. if(!filter)
  563. {
  564. Source->DirectGain = 1.0f;
  565. Source->DirectGainHF = 1.0f;
  566. }
  567. else
  568. {
  569. Source->DirectGain = filter->Gain;
  570. Source->DirectGainHF = filter->GainHF;
  571. }
  572. UnlockContext(Context);
  573. Source->NeedsUpdate = AL_TRUE;
  574. return AL_TRUE;
  575. case AL_DIRECT_FILTER_GAINHF_AUTO:
  576. CHECKVAL(*values == AL_FALSE || *values == AL_TRUE);
  577. Source->DryGainHFAuto = *values;
  578. Source->NeedsUpdate = AL_TRUE;
  579. return AL_TRUE;
  580. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  581. CHECKVAL(*values == AL_FALSE || *values == AL_TRUE);
  582. Source->WetGainAuto = *values;
  583. Source->NeedsUpdate = AL_TRUE;
  584. return AL_TRUE;
  585. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  586. CHECKVAL(*values == AL_FALSE || *values == AL_TRUE);
  587. Source->WetGainHFAuto = *values;
  588. Source->NeedsUpdate = AL_TRUE;
  589. return AL_TRUE;
  590. case AL_DIRECT_CHANNELS_SOFT:
  591. CHECKVAL(*values == AL_FALSE || *values == AL_TRUE);
  592. Source->DirectChannels = *values;
  593. Source->NeedsUpdate = AL_TRUE;
  594. return AL_TRUE;
  595. case AL_DISTANCE_MODEL:
  596. CHECKVAL(*values == AL_NONE ||
  597. *values == AL_INVERSE_DISTANCE ||
  598. *values == AL_INVERSE_DISTANCE_CLAMPED ||
  599. *values == AL_LINEAR_DISTANCE ||
  600. *values == AL_LINEAR_DISTANCE_CLAMPED ||
  601. *values == AL_EXPONENT_DISTANCE ||
  602. *values == AL_EXPONENT_DISTANCE_CLAMPED);
  603. Source->DistanceModel = *values;
  604. if(Context->SourceDistanceModel)
  605. Source->NeedsUpdate = AL_TRUE;
  606. return AL_TRUE;
  607. case AL_AUXILIARY_SEND_FILTER:
  608. LockContext(Context);
  609. if(!((ALuint)values[1] < device->NumAuxSends &&
  610. (values[0] == 0 || (slot=LookupEffectSlot(Context, values[0])) != NULL) &&
  611. (values[2] == 0 || (filter=LookupFilter(device, values[2])) != NULL)))
  612. {
  613. UnlockContext(Context);
  614. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_VALUE, AL_FALSE);
  615. }
  616. /* Add refcount on the new slot, and release the previous slot */
  617. if(slot) IncrementRef(&slot->ref);
  618. slot = ExchangePtr((XchgPtr*)&Source->Send[values[1]].Slot, slot);
  619. if(slot) DecrementRef(&slot->ref);
  620. if(!filter)
  621. {
  622. /* Disable filter */
  623. Source->Send[values[1]].Gain = 1.0f;
  624. Source->Send[values[1]].GainHF = 1.0f;
  625. }
  626. else
  627. {
  628. Source->Send[values[1]].Gain = filter->Gain;
  629. Source->Send[values[1]].GainHF = filter->GainHF;
  630. }
  631. Source->NeedsUpdate = AL_TRUE;
  632. UnlockContext(Context);
  633. return AL_TRUE;
  634. case AL_MAX_DISTANCE:
  635. case AL_ROLLOFF_FACTOR:
  636. case AL_CONE_INNER_ANGLE:
  637. case AL_CONE_OUTER_ANGLE:
  638. case AL_REFERENCE_DISTANCE:
  639. case siDopplerFactor:
  640. fvals[0] = (ALfloat)*values;
  641. return SetSourcefv(Source, Context, (int)prop, fvals);
  642. case AL_POSITION:
  643. case AL_VELOCITY:
  644. case AL_DIRECTION:
  645. fvals[0] = (ALfloat)values[0];
  646. fvals[1] = (ALfloat)values[1];
  647. fvals[2] = (ALfloat)values[2];
  648. return SetSourcefv(Source, Context, (int)prop, fvals);
  649. case siSampleOffsetLatencySOFT:
  650. /* i64 only */
  651. break;
  652. }
  653. ERR("Unexpected property: 0x%04x\n", prop);
  654. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_ENUM, AL_FALSE);
  655. }
  656. static ALboolean SetSourcei64v(ALsource *Source, ALCcontext *Context, SrcIntProp prop, const ALint64SOFT *values)
  657. {
  658. ALfloat fvals[3];
  659. ALint ivals[3];
  660. switch(prop)
  661. {
  662. case siSampleRWOffsetsSOFT:
  663. case siByteRWOffsetsSOFT:
  664. case siSampleOffsetLatencySOFT:
  665. /* Query only */
  666. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_OPERATION, AL_FALSE);
  667. /* 1x int */
  668. case AL_SOURCE_RELATIVE:
  669. case AL_LOOPING:
  670. case AL_SOURCE_STATE:
  671. case AL_BYTE_OFFSET:
  672. case AL_SAMPLE_OFFSET:
  673. case siSourceType:
  674. case siBuffersQueued:
  675. case siBuffersProcessed:
  676. case AL_DIRECT_FILTER_GAINHF_AUTO:
  677. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  678. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  679. case AL_DIRECT_CHANNELS_SOFT:
  680. case AL_DISTANCE_MODEL:
  681. CHECKVAL(*values <= INT_MAX && *values >= INT_MIN);
  682. ivals[0] = (ALint)*values;
  683. return SetSourceiv(Source, Context, (int)prop, ivals);
  684. /* 1x uint */
  685. case AL_BUFFER:
  686. case AL_DIRECT_FILTER:
  687. CHECKVAL(*values <= UINT_MAX && *values >= 0);
  688. ivals[0] = (ALuint)*values;
  689. return SetSourceiv(Source, Context, (int)prop, ivals);
  690. /* 3x uint */
  691. case AL_AUXILIARY_SEND_FILTER:
  692. CHECKVAL(values[0] <= UINT_MAX && values[0] >= 0 &&
  693. values[1] <= UINT_MAX && values[1] >= 0 &&
  694. values[2] <= UINT_MAX && values[2] >= 0);
  695. ivals[0] = (ALuint)values[0];
  696. ivals[1] = (ALuint)values[1];
  697. ivals[2] = (ALuint)values[2];
  698. return SetSourceiv(Source, Context, (int)prop, ivals);
  699. /* 1x float */
  700. case AL_MAX_DISTANCE:
  701. case AL_ROLLOFF_FACTOR:
  702. case AL_CONE_INNER_ANGLE:
  703. case AL_CONE_OUTER_ANGLE:
  704. case AL_REFERENCE_DISTANCE:
  705. case AL_SEC_OFFSET:
  706. case siDopplerFactor:
  707. fvals[0] = (ALfloat)*values;
  708. return SetSourcefv(Source, Context, (int)prop, fvals);
  709. /* 3x float */
  710. case AL_POSITION:
  711. case AL_VELOCITY:
  712. case AL_DIRECTION:
  713. fvals[0] = (ALfloat)values[0];
  714. fvals[1] = (ALfloat)values[1];
  715. fvals[2] = (ALfloat)values[2];
  716. return SetSourcefv(Source, Context, (int)prop, fvals);
  717. }
  718. ERR("Unexpected property: 0x%04x\n", prop);
  719. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_ENUM, AL_FALSE);
  720. }
  721. #undef CHECKVAL
  722. static ALboolean GetSourcedv(const ALsource *Source, ALCcontext *Context, SrcFloatProp prop, ALdouble *values)
  723. {
  724. ALdouble offsets[2];
  725. ALdouble updateLen;
  726. ALint ivals[3];
  727. ALboolean err;
  728. switch(prop)
  729. {
  730. case AL_GAIN:
  731. *values = Source->Gain;
  732. return AL_TRUE;
  733. case AL_PITCH:
  734. *values = Source->Pitch;
  735. return AL_TRUE;
  736. case AL_MAX_DISTANCE:
  737. *values = Source->MaxDistance;
  738. return AL_TRUE;
  739. case AL_ROLLOFF_FACTOR:
  740. *values = Source->RollOffFactor;
  741. return AL_TRUE;
  742. case AL_REFERENCE_DISTANCE:
  743. *values = Source->RefDistance;
  744. return AL_TRUE;
  745. case AL_CONE_INNER_ANGLE:
  746. *values = Source->InnerAngle;
  747. return AL_TRUE;
  748. case AL_CONE_OUTER_ANGLE:
  749. *values = Source->OuterAngle;
  750. return AL_TRUE;
  751. case AL_MIN_GAIN:
  752. *values = Source->MinGain;
  753. return AL_TRUE;
  754. case AL_MAX_GAIN:
  755. *values = Source->MaxGain;
  756. return AL_TRUE;
  757. case AL_CONE_OUTER_GAIN:
  758. *values = Source->OuterGain;
  759. return AL_TRUE;
  760. case AL_SEC_OFFSET:
  761. case AL_SAMPLE_OFFSET:
  762. case AL_BYTE_OFFSET:
  763. LockContext(Context);
  764. updateLen = (ALdouble)Context->Device->UpdateSize /
  765. Context->Device->Frequency;
  766. GetSourceOffsets(Source, prop, offsets, updateLen);
  767. UnlockContext(Context);
  768. *values = offsets[0];
  769. return AL_TRUE;
  770. case AL_CONE_OUTER_GAINHF:
  771. *values = Source->OuterGainHF;
  772. return AL_TRUE;
  773. case AL_AIR_ABSORPTION_FACTOR:
  774. *values = Source->AirAbsorptionFactor;
  775. return AL_TRUE;
  776. case AL_ROOM_ROLLOFF_FACTOR:
  777. *values = Source->RoomRolloffFactor;
  778. return AL_TRUE;
  779. case AL_DOPPLER_FACTOR:
  780. *values = Source->DopplerFactor;
  781. return AL_TRUE;
  782. case AL_SAMPLE_RW_OFFSETS_SOFT:
  783. case AL_BYTE_RW_OFFSETS_SOFT:
  784. LockContext(Context);
  785. updateLen = (ALdouble)Context->Device->UpdateSize /
  786. Context->Device->Frequency;
  787. GetSourceOffsets(Source, prop, values, updateLen);
  788. UnlockContext(Context);
  789. return AL_TRUE;
  790. case AL_SEC_OFFSET_LATENCY_SOFT:
  791. LockContext(Context);
  792. values[0] = GetSourceSecOffset(Source);
  793. values[1] = (ALdouble)ALCdevice_GetLatency(Context->Device) /
  794. 1000000000.0;
  795. UnlockContext(Context);
  796. return AL_TRUE;
  797. case AL_POSITION:
  798. LockContext(Context);
  799. values[0] = Source->Position[0];
  800. values[1] = Source->Position[1];
  801. values[2] = Source->Position[2];
  802. UnlockContext(Context);
  803. return AL_TRUE;
  804. case AL_VELOCITY:
  805. LockContext(Context);
  806. values[0] = Source->Velocity[0];
  807. values[1] = Source->Velocity[1];
  808. values[2] = Source->Velocity[2];
  809. UnlockContext(Context);
  810. return AL_TRUE;
  811. case AL_DIRECTION:
  812. LockContext(Context);
  813. values[0] = Source->Orientation[0];
  814. values[1] = Source->Orientation[1];
  815. values[2] = Source->Orientation[2];
  816. UnlockContext(Context);
  817. return AL_TRUE;
  818. case AL_SOURCE_RELATIVE:
  819. case AL_LOOPING:
  820. case AL_BUFFER:
  821. case AL_SOURCE_STATE:
  822. case AL_BUFFERS_QUEUED:
  823. case AL_BUFFERS_PROCESSED:
  824. case AL_SOURCE_TYPE:
  825. case AL_DIRECT_FILTER_GAINHF_AUTO:
  826. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  827. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  828. case AL_DIRECT_CHANNELS_SOFT:
  829. case AL_DISTANCE_MODEL:
  830. if((err=GetSourceiv(Source, Context, (int)prop, ivals)) != AL_FALSE)
  831. *values = (ALdouble)ivals[0];
  832. return err;
  833. }
  834. ERR("Unexpected property: 0x%04x\n", prop);
  835. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_ENUM, AL_FALSE);
  836. }
  837. static ALboolean GetSourceiv(const ALsource *Source, ALCcontext *Context, SrcIntProp prop, ALint *values)
  838. {
  839. ALbufferlistitem *BufferList;
  840. ALdouble dvals[3];
  841. ALboolean err;
  842. switch(prop)
  843. {
  844. case AL_SOURCE_RELATIVE:
  845. *values = Source->HeadRelative;
  846. return AL_TRUE;
  847. case AL_LOOPING:
  848. *values = Source->Looping;
  849. return AL_TRUE;
  850. case AL_BUFFER:
  851. LockContext(Context);
  852. BufferList = Source->queue;
  853. if(Source->SourceType != AL_STATIC)
  854. {
  855. ALuint i = Source->BuffersPlayed;
  856. while(i > 0)
  857. {
  858. BufferList = BufferList->next;
  859. i--;
  860. }
  861. }
  862. *values = ((BufferList && BufferList->buffer) ?
  863. BufferList->buffer->id : 0);
  864. UnlockContext(Context);
  865. return AL_TRUE;
  866. case AL_SOURCE_STATE:
  867. *values = Source->state;
  868. return AL_TRUE;
  869. case AL_BUFFERS_QUEUED:
  870. *values = Source->BuffersInQueue;
  871. return AL_TRUE;
  872. case AL_BUFFERS_PROCESSED:
  873. LockContext(Context);
  874. if(Source->Looping || Source->SourceType != AL_STREAMING)
  875. {
  876. /* Buffers on a looping source are in a perpetual state of
  877. * PENDING, so don't report any as PROCESSED */
  878. *values = 0;
  879. }
  880. else
  881. *values = Source->BuffersPlayed;
  882. UnlockContext(Context);
  883. return AL_TRUE;
  884. case AL_SOURCE_TYPE:
  885. *values = Source->SourceType;
  886. return AL_TRUE;
  887. case AL_DIRECT_FILTER_GAINHF_AUTO:
  888. *values = Source->DryGainHFAuto;
  889. return AL_TRUE;
  890. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  891. *values = Source->WetGainAuto;
  892. return AL_TRUE;
  893. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  894. *values = Source->WetGainHFAuto;
  895. return AL_TRUE;
  896. case AL_DIRECT_CHANNELS_SOFT:
  897. *values = Source->DirectChannels;
  898. return AL_TRUE;
  899. case AL_DISTANCE_MODEL:
  900. *values = Source->DistanceModel;
  901. return AL_TRUE;
  902. case AL_MAX_DISTANCE:
  903. case AL_ROLLOFF_FACTOR:
  904. case AL_REFERENCE_DISTANCE:
  905. case AL_CONE_INNER_ANGLE:
  906. case AL_CONE_OUTER_ANGLE:
  907. case AL_SEC_OFFSET:
  908. case AL_SAMPLE_OFFSET:
  909. case AL_BYTE_OFFSET:
  910. case AL_DOPPLER_FACTOR:
  911. if((err=GetSourcedv(Source, Context, (int)prop, dvals)) != AL_FALSE)
  912. *values = (ALint)dvals[0];
  913. return err;
  914. case AL_SAMPLE_RW_OFFSETS_SOFT:
  915. case AL_BYTE_RW_OFFSETS_SOFT:
  916. if((err=GetSourcedv(Source, Context, (int)prop, dvals)) != AL_FALSE)
  917. {
  918. values[0] = (ALint)dvals[0];
  919. values[1] = (ALint)dvals[1];
  920. }
  921. return err;
  922. case AL_POSITION:
  923. case AL_VELOCITY:
  924. case AL_DIRECTION:
  925. if((err=GetSourcedv(Source, Context, (int)prop, dvals)) != AL_FALSE)
  926. {
  927. values[0] = (ALint)dvals[0];
  928. values[1] = (ALint)dvals[1];
  929. values[2] = (ALint)dvals[2];
  930. }
  931. return err;
  932. case siSampleOffsetLatencySOFT:
  933. /* i64 only */
  934. break;
  935. case siDirectFilter:
  936. case siAuxSendFilter:
  937. /* ??? */
  938. break;
  939. }
  940. ERR("Unexpected property: 0x%04x\n", prop);
  941. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_ENUM, AL_FALSE);
  942. }
  943. static ALboolean GetSourcei64v(const ALsource *Source, ALCcontext *Context, SrcIntProp prop, ALint64 *values)
  944. {
  945. ALdouble dvals[3];
  946. ALint ivals[3];
  947. ALboolean err;
  948. switch(prop)
  949. {
  950. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  951. LockContext(Context);
  952. values[0] = GetSourceOffset(Source);
  953. values[1] = ALCdevice_GetLatency(Context->Device);
  954. UnlockContext(Context);
  955. return AL_TRUE;
  956. case AL_MAX_DISTANCE:
  957. case AL_ROLLOFF_FACTOR:
  958. case AL_REFERENCE_DISTANCE:
  959. case AL_CONE_INNER_ANGLE:
  960. case AL_CONE_OUTER_ANGLE:
  961. case AL_SEC_OFFSET:
  962. case AL_SAMPLE_OFFSET:
  963. case AL_BYTE_OFFSET:
  964. case AL_DOPPLER_FACTOR:
  965. if((err=GetSourcedv(Source, Context, (int)prop, dvals)) != AL_FALSE)
  966. *values = (ALint64)dvals[0];
  967. return err;
  968. case AL_SAMPLE_RW_OFFSETS_SOFT:
  969. case AL_BYTE_RW_OFFSETS_SOFT:
  970. if((err=GetSourcedv(Source, Context, (int)prop, dvals)) != AL_FALSE)
  971. {
  972. values[0] = (ALint64)dvals[0];
  973. values[1] = (ALint64)dvals[1];
  974. }
  975. return err;
  976. case AL_POSITION:
  977. case AL_VELOCITY:
  978. case AL_DIRECTION:
  979. if((err=GetSourcedv(Source, Context, (int)prop, dvals)) != AL_FALSE)
  980. {
  981. values[0] = (ALint64)dvals[0];
  982. values[1] = (ALint64)dvals[1];
  983. values[2] = (ALint64)dvals[2];
  984. }
  985. return err;
  986. case AL_SOURCE_RELATIVE:
  987. case AL_LOOPING:
  988. case AL_SOURCE_STATE:
  989. case AL_BUFFERS_QUEUED:
  990. case AL_BUFFERS_PROCESSED:
  991. case AL_SOURCE_TYPE:
  992. case AL_DIRECT_FILTER_GAINHF_AUTO:
  993. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  994. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  995. case AL_DIRECT_CHANNELS_SOFT:
  996. case AL_DISTANCE_MODEL:
  997. if((err=GetSourceiv(Source, Context, (int)prop, ivals)) != AL_FALSE)
  998. *values = ivals[0];
  999. return err;
  1000. case siBuffer:
  1001. case siDirectFilter:
  1002. if((err=GetSourceiv(Source, Context, (int)prop, ivals)) != AL_FALSE)
  1003. *values = ((ALuint*)ivals)[0];
  1004. return err;
  1005. case siAuxSendFilter:
  1006. if((err=GetSourceiv(Source, Context, (int)prop, ivals)) != AL_FALSE)
  1007. {
  1008. values[0] = ((ALuint*)ivals)[0];
  1009. values[1] = ((ALuint*)ivals)[1];
  1010. values[2] = ((ALuint*)ivals)[2];
  1011. }
  1012. return err;
  1013. }
  1014. ERR("Unexpected property: 0x%04x\n", prop);
  1015. SET_ERROR_AND_RETURN_VALUE(Context, AL_INVALID_ENUM, AL_FALSE);
  1016. }
  1017. AL_API ALvoid AL_APIENTRY alGenSources(ALsizei n, ALuint *sources)
  1018. {
  1019. ALCcontext *context;
  1020. ALsizei cur = 0;
  1021. ALenum err;
  1022. context = GetContextRef();
  1023. if(!context) return;
  1024. if(!(n >= 0))
  1025. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1026. for(cur = 0;cur < n;cur++)
  1027. {
  1028. ALsource *source = al_calloc(16, sizeof(ALsource));
  1029. if(!source)
  1030. {
  1031. alDeleteSources(cur, sources);
  1032. SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done);
  1033. }
  1034. InitSourceParams(source);
  1035. err = NewThunkEntry(&source->id);
  1036. if(err == AL_NO_ERROR)
  1037. err = InsertUIntMapEntry(&context->SourceMap, source->id, source);
  1038. if(err != AL_NO_ERROR)
  1039. {
  1040. FreeThunkEntry(source->id);
  1041. memset(source, 0, sizeof(ALsource));
  1042. al_free(source);
  1043. alDeleteSources(cur, sources);
  1044. SET_ERROR_AND_GOTO(context, err, done);
  1045. }
  1046. sources[cur] = source->id;
  1047. }
  1048. done:
  1049. ALCcontext_DecRef(context);
  1050. }
  1051. AL_API ALvoid AL_APIENTRY alDeleteSources(ALsizei n, const ALuint *sources)
  1052. {
  1053. ALCcontext *context;
  1054. ALbufferlistitem *BufferList;
  1055. ALsource *Source;
  1056. ALsizei i, j;
  1057. context = GetContextRef();
  1058. if(!context) return;
  1059. if(!(n >= 0))
  1060. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1061. /* Check that all Sources are valid */
  1062. for(i = 0;i < n;i++)
  1063. {
  1064. if(LookupSource(context, sources[i]) == NULL)
  1065. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  1066. }
  1067. for(i = 0;i < n;i++)
  1068. {
  1069. ALsource **srclist, **srclistend;
  1070. if((Source=RemoveSource(context, sources[i])) == NULL)
  1071. continue;
  1072. FreeThunkEntry(Source->id);
  1073. LockContext(context);
  1074. srclist = context->ActiveSources;
  1075. srclistend = srclist + context->ActiveSourceCount;
  1076. while(srclist != srclistend)
  1077. {
  1078. if(*srclist == Source)
  1079. {
  1080. context->ActiveSourceCount--;
  1081. *srclist = *(--srclistend);
  1082. break;
  1083. }
  1084. srclist++;
  1085. }
  1086. UnlockContext(context);
  1087. while(Source->queue != NULL)
  1088. {
  1089. BufferList = Source->queue;
  1090. Source->queue = BufferList->next;
  1091. if(BufferList->buffer != NULL)
  1092. DecrementRef(&BufferList->buffer->ref);
  1093. free(BufferList);
  1094. }
  1095. for(j = 0;j < MAX_SENDS;++j)
  1096. {
  1097. if(Source->Send[j].Slot)
  1098. DecrementRef(&Source->Send[j].Slot->ref);
  1099. Source->Send[j].Slot = NULL;
  1100. }
  1101. memset(Source, 0, sizeof(*Source));
  1102. al_free(Source);
  1103. }
  1104. done:
  1105. ALCcontext_DecRef(context);
  1106. }
  1107. AL_API ALboolean AL_APIENTRY alIsSource(ALuint source)
  1108. {
  1109. ALCcontext *context;
  1110. ALboolean ret;
  1111. context = GetContextRef();
  1112. if(!context) return AL_FALSE;
  1113. ret = (LookupSource(context, source) ? AL_TRUE : AL_FALSE);
  1114. ALCcontext_DecRef(context);
  1115. return ret;
  1116. }
  1117. AL_API ALvoid AL_APIENTRY alSourcef(ALuint source, ALenum param, ALfloat value)
  1118. {
  1119. ALCcontext *Context;
  1120. ALsource *Source;
  1121. Context = GetContextRef();
  1122. if(!Context) return;
  1123. if((Source=LookupSource(Context, source)) == NULL)
  1124. alSetError(Context, AL_INVALID_NAME);
  1125. else if(!(FloatValsByProp(param) == 1))
  1126. alSetError(Context, AL_INVALID_ENUM);
  1127. else
  1128. SetSourcefv(Source, Context, param, &value);
  1129. ALCcontext_DecRef(Context);
  1130. }
  1131. AL_API ALvoid AL_APIENTRY alSource3f(ALuint source, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3)
  1132. {
  1133. ALCcontext *Context;
  1134. ALsource *Source;
  1135. Context = GetContextRef();
  1136. if(!Context) return;
  1137. if((Source=LookupSource(Context, source)) == NULL)
  1138. alSetError(Context, AL_INVALID_NAME);
  1139. else if(!(FloatValsByProp(param) == 3))
  1140. alSetError(Context, AL_INVALID_ENUM);
  1141. else
  1142. {
  1143. ALfloat fvals[3] = { value1, value2, value3 };
  1144. SetSourcefv(Source, Context, param, fvals);
  1145. }
  1146. ALCcontext_DecRef(Context);
  1147. }
  1148. AL_API ALvoid AL_APIENTRY alSourcefv(ALuint source, ALenum param, const ALfloat *values)
  1149. {
  1150. ALCcontext *Context;
  1151. ALsource *Source;
  1152. Context = GetContextRef();
  1153. if(!Context) return;
  1154. if((Source=LookupSource(Context, source)) == NULL)
  1155. alSetError(Context, AL_INVALID_NAME);
  1156. else if(!values)
  1157. alSetError(Context, AL_INVALID_VALUE);
  1158. else if(!(FloatValsByProp(param) > 0))
  1159. alSetError(Context, AL_INVALID_ENUM);
  1160. else
  1161. SetSourcefv(Source, Context, param, values);
  1162. ALCcontext_DecRef(Context);
  1163. }
  1164. AL_API ALvoid AL_APIENTRY alSourcedSOFT(ALuint source, ALenum param, ALdouble value)
  1165. {
  1166. ALCcontext *Context;
  1167. ALsource *Source;
  1168. Context = GetContextRef();
  1169. if(!Context) return;
  1170. if((Source=LookupSource(Context, source)) == NULL)
  1171. alSetError(Context, AL_INVALID_NAME);
  1172. else if(!(DoubleValsByProp(param) == 1))
  1173. alSetError(Context, AL_INVALID_ENUM);
  1174. else
  1175. {
  1176. ALfloat fval = (ALfloat)value;
  1177. SetSourcefv(Source, Context, param, &fval);
  1178. }
  1179. ALCcontext_DecRef(Context);
  1180. }
  1181. AL_API ALvoid AL_APIENTRY alSource3dSOFT(ALuint source, ALenum param, ALdouble value1, ALdouble value2, ALdouble value3)
  1182. {
  1183. ALCcontext *Context;
  1184. ALsource *Source;
  1185. Context = GetContextRef();
  1186. if(!Context) return;
  1187. if((Source=LookupSource(Context, source)) == NULL)
  1188. alSetError(Context, AL_INVALID_NAME);
  1189. else if(!(DoubleValsByProp(param) == 3))
  1190. alSetError(Context, AL_INVALID_ENUM);
  1191. else
  1192. {
  1193. ALfloat fvals[3] = { (ALfloat)value1, (ALfloat)value2, (ALfloat)value3 };
  1194. SetSourcefv(Source, Context, param, fvals);
  1195. }
  1196. ALCcontext_DecRef(Context);
  1197. }
  1198. AL_API ALvoid AL_APIENTRY alSourcedvSOFT(ALuint source, ALenum param, const ALdouble *values)
  1199. {
  1200. ALCcontext *Context;
  1201. ALsource *Source;
  1202. ALint count;
  1203. Context = GetContextRef();
  1204. if(!Context) return;
  1205. if((Source=LookupSource(Context, source)) == NULL)
  1206. alSetError(Context, AL_INVALID_NAME);
  1207. else if(!values)
  1208. alSetError(Context, AL_INVALID_VALUE);
  1209. else if(!((count=DoubleValsByProp(param)) > 0 && count <= 3))
  1210. alSetError(Context, AL_INVALID_ENUM);
  1211. else
  1212. {
  1213. ALfloat fvals[3];
  1214. ALint i;
  1215. for(i = 0;i < count;i++)
  1216. fvals[i] = (ALfloat)values[i];
  1217. SetSourcefv(Source, Context, param, fvals);
  1218. }
  1219. ALCcontext_DecRef(Context);
  1220. }
  1221. AL_API ALvoid AL_APIENTRY alSourcei(ALuint source, ALenum param, ALint value)
  1222. {
  1223. ALCcontext *Context;
  1224. ALsource *Source;
  1225. Context = GetContextRef();
  1226. if(!Context) return;
  1227. if((Source=LookupSource(Context, source)) == NULL)
  1228. alSetError(Context, AL_INVALID_NAME);
  1229. else if(!(IntValsByProp(param) == 1))
  1230. alSetError(Context, AL_INVALID_ENUM);
  1231. else
  1232. SetSourceiv(Source, Context, param, &value);
  1233. ALCcontext_DecRef(Context);
  1234. }
  1235. AL_API void AL_APIENTRY alSource3i(ALuint source, ALenum param, ALint value1, ALint value2, ALint value3)
  1236. {
  1237. ALCcontext *Context;
  1238. ALsource *Source;
  1239. Context = GetContextRef();
  1240. if(!Context) return;
  1241. if((Source=LookupSource(Context, source)) == NULL)
  1242. alSetError(Context, AL_INVALID_NAME);
  1243. else if(!(IntValsByProp(param) == 3))
  1244. alSetError(Context, AL_INVALID_ENUM);
  1245. else
  1246. {
  1247. ALint ivals[3] = { value1, value2, value3 };
  1248. SetSourceiv(Source, Context, param, ivals);
  1249. }
  1250. ALCcontext_DecRef(Context);
  1251. }
  1252. AL_API void AL_APIENTRY alSourceiv(ALuint source, ALenum param, const ALint *values)
  1253. {
  1254. ALCcontext *Context;
  1255. ALsource *Source;
  1256. Context = GetContextRef();
  1257. if(!Context) return;
  1258. if((Source=LookupSource(Context, source)) == NULL)
  1259. alSetError(Context, AL_INVALID_NAME);
  1260. else if(!values)
  1261. alSetError(Context, AL_INVALID_VALUE);
  1262. else if(!(IntValsByProp(param) > 0))
  1263. alSetError(Context, AL_INVALID_ENUM);
  1264. else
  1265. SetSourceiv(Source, Context, param, values);
  1266. ALCcontext_DecRef(Context);
  1267. }
  1268. AL_API ALvoid AL_APIENTRY alSourcei64SOFT(ALuint source, ALenum param, ALint64SOFT value)
  1269. {
  1270. ALCcontext *Context;
  1271. ALsource *Source;
  1272. Context = GetContextRef();
  1273. if(!Context) return;
  1274. if((Source=LookupSource(Context, source)) == NULL)
  1275. alSetError(Context, AL_INVALID_NAME);
  1276. else if(!(Int64ValsByProp(param) == 1))
  1277. alSetError(Context, AL_INVALID_ENUM);
  1278. else
  1279. SetSourcei64v(Source, Context, param, &value);
  1280. ALCcontext_DecRef(Context);
  1281. }
  1282. AL_API void AL_APIENTRY alSource3i64SOFT(ALuint source, ALenum param, ALint64SOFT value1, ALint64SOFT value2, ALint64SOFT value3)
  1283. {
  1284. ALCcontext *Context;
  1285. ALsource *Source;
  1286. Context = GetContextRef();
  1287. if(!Context) return;
  1288. if((Source=LookupSource(Context, source)) == NULL)
  1289. alSetError(Context, AL_INVALID_NAME);
  1290. else if(!(Int64ValsByProp(param) == 3))
  1291. alSetError(Context, AL_INVALID_ENUM);
  1292. else
  1293. {
  1294. ALint64SOFT i64vals[3] = { value1, value2, value3 };
  1295. SetSourcei64v(Source, Context, param, i64vals);
  1296. }
  1297. ALCcontext_DecRef(Context);
  1298. }
  1299. AL_API void AL_APIENTRY alSourcei64vSOFT(ALuint source, ALenum param, const ALint64SOFT *values)
  1300. {
  1301. ALCcontext *Context;
  1302. ALsource *Source;
  1303. Context = GetContextRef();
  1304. if(!Context) return;
  1305. if((Source=LookupSource(Context, source)) == NULL)
  1306. alSetError(Context, AL_INVALID_NAME);
  1307. else if(!values)
  1308. alSetError(Context, AL_INVALID_VALUE);
  1309. else if(!(Int64ValsByProp(param) > 0))
  1310. alSetError(Context, AL_INVALID_ENUM);
  1311. else
  1312. SetSourcei64v(Source, Context, param, values);
  1313. ALCcontext_DecRef(Context);
  1314. }
  1315. AL_API ALvoid AL_APIENTRY alGetSourcef(ALuint source, ALenum param, ALfloat *value)
  1316. {
  1317. ALCcontext *Context;
  1318. ALsource *Source;
  1319. Context = GetContextRef();
  1320. if(!Context) return;
  1321. if((Source=LookupSource(Context, source)) == NULL)
  1322. alSetError(Context, AL_INVALID_NAME);
  1323. else if(!value)
  1324. alSetError(Context, AL_INVALID_VALUE);
  1325. else if(!(FloatValsByProp(param) == 1))
  1326. alSetError(Context, AL_INVALID_ENUM);
  1327. else
  1328. {
  1329. ALdouble dval;
  1330. if(GetSourcedv(Source, Context, param, &dval))
  1331. *value = (ALfloat)dval;
  1332. }
  1333. ALCcontext_DecRef(Context);
  1334. }
  1335. AL_API ALvoid AL_APIENTRY alGetSource3f(ALuint source, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
  1336. {
  1337. ALCcontext *Context;
  1338. ALsource *Source;
  1339. Context = GetContextRef();
  1340. if(!Context) return;
  1341. if((Source=LookupSource(Context, source)) == NULL)
  1342. alSetError(Context, AL_INVALID_NAME);
  1343. else if(!(value1 && value2 && value3))
  1344. alSetError(Context, AL_INVALID_VALUE);
  1345. else if(!(FloatValsByProp(param) == 3))
  1346. alSetError(Context, AL_INVALID_ENUM);
  1347. else
  1348. {
  1349. ALdouble dvals[3];
  1350. if(GetSourcedv(Source, Context, param, dvals))
  1351. {
  1352. *value1 = (ALfloat)dvals[0];
  1353. *value2 = (ALfloat)dvals[1];
  1354. *value3 = (ALfloat)dvals[2];
  1355. }
  1356. }
  1357. ALCcontext_DecRef(Context);
  1358. }
  1359. AL_API ALvoid AL_APIENTRY alGetSourcefv(ALuint source, ALenum param, ALfloat *values)
  1360. {
  1361. ALCcontext *Context;
  1362. ALsource *Source;
  1363. ALint count;
  1364. Context = GetContextRef();
  1365. if(!Context) return;
  1366. if((Source=LookupSource(Context, source)) == NULL)
  1367. alSetError(Context, AL_INVALID_NAME);
  1368. else if(!values)
  1369. alSetError(Context, AL_INVALID_VALUE);
  1370. else if(!((count=FloatValsByProp(param)) > 0 && count <= 3))
  1371. alSetError(Context, AL_INVALID_ENUM);
  1372. else
  1373. {
  1374. ALdouble dvals[3];
  1375. if(GetSourcedv(Source, Context, param, dvals))
  1376. {
  1377. ALint i;
  1378. for(i = 0;i < count;i++)
  1379. values[i] = (ALfloat)dvals[i];
  1380. }
  1381. }
  1382. ALCcontext_DecRef(Context);
  1383. }
  1384. AL_API void AL_APIENTRY alGetSourcedSOFT(ALuint source, ALenum param, ALdouble *value)
  1385. {
  1386. ALCcontext *Context;
  1387. ALsource *Source;
  1388. Context = GetContextRef();
  1389. if(!Context) return;
  1390. if((Source=LookupSource(Context, source)) == NULL)
  1391. alSetError(Context, AL_INVALID_NAME);
  1392. else if(!value)
  1393. alSetError(Context, AL_INVALID_VALUE);
  1394. else if(!(DoubleValsByProp(param) == 1))
  1395. alSetError(Context, AL_INVALID_ENUM);
  1396. else
  1397. GetSourcedv(Source, Context, param, value);
  1398. ALCcontext_DecRef(Context);
  1399. }
  1400. AL_API void AL_APIENTRY alGetSource3dSOFT(ALuint source, ALenum param, ALdouble *value1, ALdouble *value2, ALdouble *value3)
  1401. {
  1402. ALCcontext *Context;
  1403. ALsource *Source;
  1404. Context = GetContextRef();
  1405. if(!Context) return;
  1406. if((Source=LookupSource(Context, source)) == NULL)
  1407. alSetError(Context, AL_INVALID_NAME);
  1408. else if(!(value1 && value2 && value3))
  1409. alSetError(Context, AL_INVALID_VALUE);
  1410. else if(!(DoubleValsByProp(param) == 3))
  1411. alSetError(Context, AL_INVALID_ENUM);
  1412. else
  1413. {
  1414. ALdouble dvals[3];
  1415. if(GetSourcedv(Source, Context, param, dvals))
  1416. {
  1417. *value1 = dvals[0];
  1418. *value2 = dvals[1];
  1419. *value3 = dvals[2];
  1420. }
  1421. }
  1422. ALCcontext_DecRef(Context);
  1423. }
  1424. AL_API void AL_APIENTRY alGetSourcedvSOFT(ALuint source, ALenum param, ALdouble *values)
  1425. {
  1426. ALCcontext *Context;
  1427. ALsource *Source;
  1428. Context = GetContextRef();
  1429. if(!Context) return;
  1430. if((Source=LookupSource(Context, source)) == NULL)
  1431. alSetError(Context, AL_INVALID_NAME);
  1432. else if(!values)
  1433. alSetError(Context, AL_INVALID_VALUE);
  1434. else if(!(DoubleValsByProp(param) > 0))
  1435. alSetError(Context, AL_INVALID_ENUM);
  1436. else
  1437. GetSourcedv(Source, Context, param, values);
  1438. ALCcontext_DecRef(Context);
  1439. }
  1440. AL_API ALvoid AL_APIENTRY alGetSourcei(ALuint source, ALenum param, ALint *value)
  1441. {
  1442. ALCcontext *Context;
  1443. ALsource *Source;
  1444. Context = GetContextRef();
  1445. if(!Context) return;
  1446. if((Source=LookupSource(Context, source)) == NULL)
  1447. alSetError(Context, AL_INVALID_NAME);
  1448. else if(!value)
  1449. alSetError(Context, AL_INVALID_VALUE);
  1450. else if(!(IntValsByProp(param) == 1))
  1451. alSetError(Context, AL_INVALID_ENUM);
  1452. else
  1453. GetSourceiv(Source, Context, param, value);
  1454. ALCcontext_DecRef(Context);
  1455. }
  1456. AL_API void AL_APIENTRY alGetSource3i(ALuint source, ALenum param, ALint *value1, ALint *value2, ALint *value3)
  1457. {
  1458. ALCcontext *Context;
  1459. ALsource *Source;
  1460. Context = GetContextRef();
  1461. if(!Context) return;
  1462. if((Source=LookupSource(Context, source)) == NULL)
  1463. alSetError(Context, AL_INVALID_NAME);
  1464. else if(!(value1 && value2 && value3))
  1465. alSetError(Context, AL_INVALID_VALUE);
  1466. else if(!(IntValsByProp(param) == 3))
  1467. alSetError(Context, AL_INVALID_ENUM);
  1468. else
  1469. {
  1470. ALint ivals[3];
  1471. if(GetSourceiv(Source, Context, param, ivals))
  1472. {
  1473. *value1 = ivals[0];
  1474. *value2 = ivals[1];
  1475. *value3 = ivals[2];
  1476. }
  1477. }
  1478. ALCcontext_DecRef(Context);
  1479. }
  1480. AL_API void AL_APIENTRY alGetSourceiv(ALuint source, ALenum param, ALint *values)
  1481. {
  1482. ALCcontext *Context;
  1483. ALsource *Source;
  1484. Context = GetContextRef();
  1485. if(!Context) return;
  1486. if((Source=LookupSource(Context, source)) == NULL)
  1487. alSetError(Context, AL_INVALID_NAME);
  1488. else if(!values)
  1489. alSetError(Context, AL_INVALID_VALUE);
  1490. else if(!(IntValsByProp(param) > 0))
  1491. alSetError(Context, AL_INVALID_ENUM);
  1492. else
  1493. GetSourceiv(Source, Context, param, values);
  1494. ALCcontext_DecRef(Context);
  1495. }
  1496. AL_API void AL_APIENTRY alGetSourcei64SOFT(ALuint source, ALenum param, ALint64SOFT *value)
  1497. {
  1498. ALCcontext *Context;
  1499. ALsource *Source;
  1500. Context = GetContextRef();
  1501. if(!Context) return;
  1502. if((Source=LookupSource(Context, source)) == NULL)
  1503. alSetError(Context, AL_INVALID_NAME);
  1504. else if(!value)
  1505. alSetError(Context, AL_INVALID_VALUE);
  1506. else if(!(Int64ValsByProp(param) == 1))
  1507. alSetError(Context, AL_INVALID_ENUM);
  1508. else
  1509. GetSourcei64v(Source, Context, param, value);
  1510. ALCcontext_DecRef(Context);
  1511. }
  1512. AL_API void AL_APIENTRY alGetSource3i64SOFT(ALuint source, ALenum param, ALint64SOFT *value1, ALint64SOFT *value2, ALint64SOFT *value3)
  1513. {
  1514. ALCcontext *Context;
  1515. ALsource *Source;
  1516. Context = GetContextRef();
  1517. if(!Context) return;
  1518. if((Source=LookupSource(Context, source)) == NULL)
  1519. alSetError(Context, AL_INVALID_NAME);
  1520. else if(!(value1 && value2 && value3))
  1521. alSetError(Context, AL_INVALID_VALUE);
  1522. else if(!(Int64ValsByProp(param) == 3))
  1523. alSetError(Context, AL_INVALID_ENUM);
  1524. else
  1525. {
  1526. ALint64 i64vals[3];
  1527. if(GetSourcei64v(Source, Context, param, i64vals))
  1528. {
  1529. *value1 = i64vals[0];
  1530. *value2 = i64vals[1];
  1531. *value3 = i64vals[2];
  1532. }
  1533. }
  1534. ALCcontext_DecRef(Context);
  1535. }
  1536. AL_API void AL_APIENTRY alGetSourcei64vSOFT(ALuint source, ALenum param, ALint64SOFT *values)
  1537. {
  1538. ALCcontext *Context;
  1539. ALsource *Source;
  1540. Context = GetContextRef();
  1541. if(!Context) return;
  1542. if((Source=LookupSource(Context, source)) == NULL)
  1543. alSetError(Context, AL_INVALID_NAME);
  1544. else if(!values)
  1545. alSetError(Context, AL_INVALID_VALUE);
  1546. else if(!(Int64ValsByProp(param) > 0))
  1547. alSetError(Context, AL_INVALID_ENUM);
  1548. else
  1549. GetSourcei64v(Source, Context, param, values);
  1550. ALCcontext_DecRef(Context);
  1551. }
  1552. AL_API ALvoid AL_APIENTRY alSourcePlay(ALuint source)
  1553. {
  1554. alSourcePlayv(1, &source);
  1555. }
  1556. AL_API ALvoid AL_APIENTRY alSourcePlayv(ALsizei n, const ALuint *sources)
  1557. {
  1558. ALCcontext *context;
  1559. ALsource *source;
  1560. ALsizei i;
  1561. context = GetContextRef();
  1562. if(!context) return;
  1563. if(!(n >= 0))
  1564. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1565. for(i = 0;i < n;i++)
  1566. {
  1567. if(!LookupSource(context, sources[i]))
  1568. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  1569. }
  1570. LockContext(context);
  1571. while(n > context->MaxActiveSources-context->ActiveSourceCount)
  1572. {
  1573. void *temp = NULL;
  1574. ALsizei newcount;
  1575. newcount = context->MaxActiveSources << 1;
  1576. if(newcount > 0)
  1577. temp = realloc(context->ActiveSources,
  1578. sizeof(*context->ActiveSources) * newcount);
  1579. if(!temp)
  1580. {
  1581. UnlockContext(context);
  1582. SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done);
  1583. }
  1584. context->ActiveSources = temp;
  1585. context->MaxActiveSources = newcount;
  1586. }
  1587. for(i = 0;i < n;i++)
  1588. {
  1589. source = LookupSource(context, sources[i]);
  1590. if(context->DeferUpdates) source->new_state = AL_PLAYING;
  1591. else SetSourceState(source, context, AL_PLAYING);
  1592. }
  1593. UnlockContext(context);
  1594. done:
  1595. ALCcontext_DecRef(context);
  1596. }
  1597. AL_API ALvoid AL_APIENTRY alSourcePause(ALuint source)
  1598. {
  1599. alSourcePausev(1, &source);
  1600. }
  1601. AL_API ALvoid AL_APIENTRY alSourcePausev(ALsizei n, const ALuint *sources)
  1602. {
  1603. ALCcontext *context;
  1604. ALsource *source;
  1605. ALsizei i;
  1606. context = GetContextRef();
  1607. if(!context) return;
  1608. if(!(n >= 0))
  1609. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1610. for(i = 0;i < n;i++)
  1611. {
  1612. if(!LookupSource(context, sources[i]))
  1613. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  1614. }
  1615. LockContext(context);
  1616. for(i = 0;i < n;i++)
  1617. {
  1618. source = LookupSource(context, sources[i]);
  1619. if(context->DeferUpdates) source->new_state = AL_PAUSED;
  1620. else SetSourceState(source, context, AL_PAUSED);
  1621. }
  1622. UnlockContext(context);
  1623. done:
  1624. ALCcontext_DecRef(context);
  1625. }
  1626. AL_API ALvoid AL_APIENTRY alSourceStop(ALuint source)
  1627. {
  1628. alSourceStopv(1, &source);
  1629. }
  1630. AL_API ALvoid AL_APIENTRY alSourceStopv(ALsizei n, const ALuint *sources)
  1631. {
  1632. ALCcontext *context;
  1633. ALsource *source;
  1634. ALsizei i;
  1635. context = GetContextRef();
  1636. if(!context) return;
  1637. if(!(n >= 0))
  1638. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1639. for(i = 0;i < n;i++)
  1640. {
  1641. if(!LookupSource(context, sources[i]))
  1642. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  1643. }
  1644. LockContext(context);
  1645. for(i = 0;i < n;i++)
  1646. {
  1647. source = LookupSource(context, sources[i]);
  1648. source->new_state = AL_NONE;
  1649. SetSourceState(source, context, AL_STOPPED);
  1650. }
  1651. UnlockContext(context);
  1652. done:
  1653. ALCcontext_DecRef(context);
  1654. }
  1655. AL_API ALvoid AL_APIENTRY alSourceRewind(ALuint source)
  1656. {
  1657. alSourceRewindv(1, &source);
  1658. }
  1659. AL_API ALvoid AL_APIENTRY alSourceRewindv(ALsizei n, const ALuint *sources)
  1660. {
  1661. ALCcontext *context;
  1662. ALsource *source;
  1663. ALsizei i;
  1664. context = GetContextRef();
  1665. if(!context) return;
  1666. if(!(n >= 0))
  1667. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1668. for(i = 0;i < n;i++)
  1669. {
  1670. if(!LookupSource(context, sources[i]))
  1671. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  1672. }
  1673. LockContext(context);
  1674. for(i = 0;i < n;i++)
  1675. {
  1676. source = LookupSource(context, sources[i]);
  1677. source->new_state = AL_NONE;
  1678. SetSourceState(source, context, AL_INITIAL);
  1679. }
  1680. UnlockContext(context);
  1681. done:
  1682. ALCcontext_DecRef(context);
  1683. }
  1684. AL_API ALvoid AL_APIENTRY alSourceQueueBuffers(ALuint src, ALsizei nb, const ALuint *buffers)
  1685. {
  1686. ALCdevice *device;
  1687. ALCcontext *context;
  1688. ALsource *source;
  1689. ALsizei i;
  1690. ALbufferlistitem *BufferListStart = NULL;
  1691. ALbufferlistitem *BufferList;
  1692. ALbuffer *BufferFmt = NULL;
  1693. if(nb == 0)
  1694. return;
  1695. context = GetContextRef();
  1696. if(!context) return;
  1697. device = context->Device;
  1698. if(!(nb >= 0))
  1699. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1700. if((source=LookupSource(context, src)) == NULL)
  1701. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  1702. LockContext(context);
  1703. if(source->SourceType == AL_STATIC)
  1704. {
  1705. UnlockContext(context);
  1706. /* Can't queue on a Static Source */
  1707. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  1708. }
  1709. /* Check for a valid Buffer, for its frequency and format */
  1710. BufferList = source->queue;
  1711. while(BufferList)
  1712. {
  1713. if(BufferList->buffer)
  1714. {
  1715. BufferFmt = BufferList->buffer;
  1716. break;
  1717. }
  1718. BufferList = BufferList->next;
  1719. }
  1720. for(i = 0;i < nb;i++)
  1721. {
  1722. ALbuffer *buffer = NULL;
  1723. if(buffers[i] && (buffer=LookupBuffer(device, buffers[i])) == NULL)
  1724. {
  1725. UnlockContext(context);
  1726. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  1727. }
  1728. if(!BufferListStart)
  1729. {
  1730. BufferListStart = malloc(sizeof(ALbufferlistitem));
  1731. BufferListStart->buffer = buffer;
  1732. BufferListStart->next = NULL;
  1733. BufferListStart->prev = NULL;
  1734. BufferList = BufferListStart;
  1735. }
  1736. else
  1737. {
  1738. BufferList->next = malloc(sizeof(ALbufferlistitem));
  1739. BufferList->next->buffer = buffer;
  1740. BufferList->next->next = NULL;
  1741. BufferList->next->prev = BufferList;
  1742. BufferList = BufferList->next;
  1743. }
  1744. if(!buffer) continue;
  1745. IncrementRef(&buffer->ref);
  1746. ReadLock(&buffer->lock);
  1747. if(BufferFmt == NULL)
  1748. {
  1749. BufferFmt = buffer;
  1750. source->NumChannels = ChannelsFromFmt(buffer->FmtChannels);
  1751. source->SampleSize = BytesFromFmt(buffer->FmtType);
  1752. if(buffer->FmtChannels == FmtMono)
  1753. source->Update = CalcSourceParams;
  1754. else
  1755. source->Update = CalcNonAttnSourceParams;
  1756. source->NeedsUpdate = AL_TRUE;
  1757. }
  1758. else if(BufferFmt->Frequency != buffer->Frequency ||
  1759. BufferFmt->OriginalChannels != buffer->OriginalChannels ||
  1760. BufferFmt->OriginalType != buffer->OriginalType)
  1761. {
  1762. ReadUnlock(&buffer->lock);
  1763. UnlockContext(context);
  1764. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  1765. }
  1766. ReadUnlock(&buffer->lock);
  1767. }
  1768. /* Source is now streaming */
  1769. source->SourceType = AL_STREAMING;
  1770. if(source->queue == NULL)
  1771. source->queue = BufferListStart;
  1772. else
  1773. {
  1774. /* Append to the end of the queue */
  1775. BufferList = source->queue;
  1776. while(BufferList->next != NULL)
  1777. BufferList = BufferList->next;
  1778. BufferListStart->prev = BufferList;
  1779. BufferList->next = BufferListStart;
  1780. }
  1781. BufferListStart = NULL;
  1782. source->BuffersInQueue += nb;
  1783. UnlockContext(context);
  1784. done:
  1785. while(BufferListStart)
  1786. {
  1787. BufferList = BufferListStart;
  1788. BufferListStart = BufferList->next;
  1789. if(BufferList->buffer)
  1790. DecrementRef(&BufferList->buffer->ref);
  1791. free(BufferList);
  1792. }
  1793. ALCcontext_DecRef(context);
  1794. }
  1795. AL_API ALvoid AL_APIENTRY alSourceUnqueueBuffers(ALuint src, ALsizei nb, ALuint *buffers)
  1796. {
  1797. ALCcontext *context;
  1798. ALsource *source;
  1799. ALsizei i;
  1800. ALbufferlistitem *BufferList;
  1801. if(nb == 0)
  1802. return;
  1803. context = GetContextRef();
  1804. if(!context) return;
  1805. if(!(nb >= 0))
  1806. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1807. if((source=LookupSource(context, src)) == NULL)
  1808. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  1809. LockContext(context);
  1810. if(source->Looping || source->SourceType != AL_STREAMING ||
  1811. (ALuint)nb > source->BuffersPlayed)
  1812. {
  1813. UnlockContext(context);
  1814. /* Trying to unqueue pending buffers, or a buffer that wasn't queued. */
  1815. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  1816. }
  1817. for(i = 0;i < nb;i++)
  1818. {
  1819. BufferList = source->queue;
  1820. source->queue = BufferList->next;
  1821. source->BuffersInQueue--;
  1822. source->BuffersPlayed--;
  1823. if(BufferList->buffer)
  1824. {
  1825. buffers[i] = BufferList->buffer->id;
  1826. DecrementRef(&BufferList->buffer->ref);
  1827. }
  1828. else
  1829. buffers[i] = 0;
  1830. free(BufferList);
  1831. }
  1832. if(source->queue)
  1833. source->queue->prev = NULL;
  1834. UnlockContext(context);
  1835. done:
  1836. ALCcontext_DecRef(context);
  1837. }
  1838. static ALvoid InitSourceParams(ALsource *Source)
  1839. {
  1840. ALuint i;
  1841. Source->InnerAngle = 360.0f;
  1842. Source->OuterAngle = 360.0f;
  1843. Source->Pitch = 1.0f;
  1844. Source->Position[0] = 0.0f;
  1845. Source->Position[1] = 0.0f;
  1846. Source->Position[2] = 0.0f;
  1847. Source->Orientation[0] = 0.0f;
  1848. Source->Orientation[1] = 0.0f;
  1849. Source->Orientation[2] = 0.0f;
  1850. Source->Velocity[0] = 0.0f;
  1851. Source->Velocity[1] = 0.0f;
  1852. Source->Velocity[2] = 0.0f;
  1853. Source->RefDistance = 1.0f;
  1854. Source->MaxDistance = FLT_MAX;
  1855. Source->RollOffFactor = 1.0f;
  1856. Source->Looping = AL_FALSE;
  1857. Source->Gain = 1.0f;
  1858. Source->MinGain = 0.0f;
  1859. Source->MaxGain = 1.0f;
  1860. Source->OuterGain = 0.0f;
  1861. Source->OuterGainHF = 1.0f;
  1862. Source->DryGainHFAuto = AL_TRUE;
  1863. Source->WetGainAuto = AL_TRUE;
  1864. Source->WetGainHFAuto = AL_TRUE;
  1865. Source->AirAbsorptionFactor = 0.0f;
  1866. Source->RoomRolloffFactor = 0.0f;
  1867. Source->DopplerFactor = 1.0f;
  1868. Source->DirectChannels = AL_FALSE;
  1869. Source->DistanceModel = DefaultDistanceModel;
  1870. Source->Resampler = DefaultResampler;
  1871. Source->state = AL_INITIAL;
  1872. Source->new_state = AL_NONE;
  1873. Source->SourceType = AL_UNDETERMINED;
  1874. Source->Offset = -1.0;
  1875. Source->DirectGain = 1.0f;
  1876. Source->DirectGainHF = 1.0f;
  1877. for(i = 0;i < MAX_SENDS;i++)
  1878. {
  1879. Source->Send[i].Gain = 1.0f;
  1880. Source->Send[i].GainHF = 1.0f;
  1881. }
  1882. Source->NeedsUpdate = AL_TRUE;
  1883. Source->Hrtf.Moving = AL_FALSE;
  1884. Source->Hrtf.Counter = 0;
  1885. }
  1886. /* SetSourceState
  1887. *
  1888. * Sets the source's new play state given its current state.
  1889. */
  1890. ALvoid SetSourceState(ALsource *Source, ALCcontext *Context, ALenum state)
  1891. {
  1892. if(state == AL_PLAYING)
  1893. {
  1894. ALbufferlistitem *BufferList;
  1895. ALsizei j, k;
  1896. /* Check that there is a queue containing at least one valid, non zero
  1897. * length Buffer. */
  1898. BufferList = Source->queue;
  1899. while(BufferList)
  1900. {
  1901. if(BufferList->buffer != NULL && BufferList->buffer->SampleLen)
  1902. break;
  1903. BufferList = BufferList->next;
  1904. }
  1905. if(Source->state != AL_PLAYING)
  1906. {
  1907. for(j = 0;j < MAX_INPUT_CHANNELS;j++)
  1908. {
  1909. for(k = 0;k < SRC_HISTORY_LENGTH;k++)
  1910. Source->Hrtf.History[j][k] = 0.0f;
  1911. for(k = 0;k < HRIR_LENGTH;k++)
  1912. {
  1913. Source->Hrtf.Values[j][k][0] = 0.0f;
  1914. Source->Hrtf.Values[j][k][1] = 0.0f;
  1915. }
  1916. }
  1917. }
  1918. if(Source->state != AL_PAUSED)
  1919. {
  1920. Source->state = AL_PLAYING;
  1921. Source->position = 0;
  1922. Source->position_fraction = 0;
  1923. Source->BuffersPlayed = 0;
  1924. }
  1925. else
  1926. Source->state = AL_PLAYING;
  1927. // Check if an Offset has been set
  1928. if(Source->Offset >= 0.0)
  1929. ApplyOffset(Source);
  1930. /* If there's nothing to play, or device is disconnected, go right to
  1931. * stopped */
  1932. if(!BufferList || !Context->Device->Connected)
  1933. {
  1934. SetSourceState(Source, Context, AL_STOPPED);
  1935. return;
  1936. }
  1937. for(j = 0;j < Context->ActiveSourceCount;j++)
  1938. {
  1939. if(Context->ActiveSources[j] == Source)
  1940. break;
  1941. }
  1942. if(j == Context->ActiveSourceCount)
  1943. Context->ActiveSources[Context->ActiveSourceCount++] = Source;
  1944. }
  1945. else if(state == AL_PAUSED)
  1946. {
  1947. if(Source->state == AL_PLAYING)
  1948. {
  1949. Source->state = AL_PAUSED;
  1950. Source->Hrtf.Moving = AL_FALSE;
  1951. Source->Hrtf.Counter = 0;
  1952. }
  1953. }
  1954. else if(state == AL_STOPPED)
  1955. {
  1956. if(Source->state != AL_INITIAL)
  1957. {
  1958. Source->state = AL_STOPPED;
  1959. Source->BuffersPlayed = Source->BuffersInQueue;
  1960. Source->Hrtf.Moving = AL_FALSE;
  1961. Source->Hrtf.Counter = 0;
  1962. }
  1963. Source->Offset = -1.0;
  1964. }
  1965. else if(state == AL_INITIAL)
  1966. {
  1967. if(Source->state != AL_INITIAL)
  1968. {
  1969. Source->state = AL_INITIAL;
  1970. Source->position = 0;
  1971. Source->position_fraction = 0;
  1972. Source->BuffersPlayed = 0;
  1973. Source->Hrtf.Moving = AL_FALSE;
  1974. Source->Hrtf.Counter = 0;
  1975. }
  1976. Source->Offset = -1.0;
  1977. }
  1978. }
  1979. /* GetSourceOffset
  1980. *
  1981. * Gets the current read offset for the given Source, in 32.32 fixed-point
  1982. * samples. The offset is relative to the start of the queue (not the start of
  1983. * the current buffer).
  1984. */
  1985. static ALint64 GetSourceOffset(const ALsource *Source)
  1986. {
  1987. const ALbufferlistitem *BufferList;
  1988. ALuint64 readPos;
  1989. ALuint i;
  1990. if(Source->state != AL_PLAYING && Source->state != AL_PAUSED)
  1991. return 0;
  1992. /* NOTE: This is the offset into the *current* buffer, so add the length of
  1993. * any played buffers */
  1994. readPos = (ALuint64)Source->position << 32;
  1995. readPos |= (ALuint64)Source->position_fraction << (32-FRACTIONBITS);
  1996. BufferList = Source->queue;
  1997. for(i = 0;i < Source->BuffersPlayed && BufferList;i++)
  1998. {
  1999. if(BufferList->buffer)
  2000. readPos += (ALuint64)BufferList->buffer->SampleLen << 32;
  2001. BufferList = BufferList->next;
  2002. }
  2003. return (ALint64)minu64(readPos, U64(0x7fffffffffffffff));
  2004. }
  2005. /* GetSourceSecOffset
  2006. *
  2007. * Gets the current read offset for the given Source, in seconds. The offset is
  2008. * relative to the start of the queue (not the start of the current buffer).
  2009. */
  2010. static ALdouble GetSourceSecOffset(const ALsource *Source)
  2011. {
  2012. const ALbufferlistitem *BufferList;
  2013. const ALbuffer *Buffer = NULL;
  2014. ALuint64 readPos;
  2015. ALuint i;
  2016. BufferList = Source->queue;
  2017. while(BufferList)
  2018. {
  2019. if(BufferList->buffer)
  2020. {
  2021. Buffer = BufferList->buffer;
  2022. break;
  2023. }
  2024. BufferList = BufferList->next;
  2025. }
  2026. if((Source->state != AL_PLAYING && Source->state != AL_PAUSED) || !Buffer)
  2027. return 0.0;
  2028. /* NOTE: This is the offset into the *current* buffer, so add the length of
  2029. * any played buffers */
  2030. readPos = (ALuint64)Source->position << FRACTIONBITS;
  2031. readPos |= (ALuint64)Source->position_fraction;
  2032. BufferList = Source->queue;
  2033. for(i = 0;i < Source->BuffersPlayed && BufferList;i++)
  2034. {
  2035. if(BufferList->buffer)
  2036. readPos += (ALuint64)BufferList->buffer->SampleLen << FRACTIONBITS;
  2037. BufferList = BufferList->next;
  2038. }
  2039. return (ALdouble)readPos / (ALdouble)FRACTIONONE / (ALdouble)Buffer->Frequency;
  2040. }
  2041. /* GetSourceOffsets
  2042. *
  2043. * Gets the current read and write offsets for the given Source, in the
  2044. * appropriate format (Bytes, Samples or Seconds). The offsets are relative to
  2045. * the start of the queue (not the start of the current buffer).
  2046. */
  2047. static ALvoid GetSourceOffsets(const ALsource *Source, ALenum name, ALdouble *offset, ALdouble updateLen)
  2048. {
  2049. const ALbufferlistitem *BufferList;
  2050. const ALbuffer *Buffer = NULL;
  2051. ALuint readPos, writePos;
  2052. ALuint totalBufferLen;
  2053. ALuint i;
  2054. // Find the first valid Buffer in the Queue
  2055. BufferList = Source->queue;
  2056. while(BufferList)
  2057. {
  2058. if(BufferList->buffer)
  2059. {
  2060. Buffer = BufferList->buffer;
  2061. break;
  2062. }
  2063. BufferList = BufferList->next;
  2064. }
  2065. if((Source->state != AL_PLAYING && Source->state != AL_PAUSED) || !Buffer)
  2066. {
  2067. offset[0] = 0.0;
  2068. offset[1] = 0.0;
  2069. return;
  2070. }
  2071. if(updateLen > 0.0 && updateLen < 0.015)
  2072. updateLen = 0.015;
  2073. /* NOTE: This is the offset into the *current* buffer, so add the length of
  2074. * any played buffers */
  2075. readPos = Source->position;
  2076. totalBufferLen = 0;
  2077. BufferList = Source->queue;
  2078. for(i = 0;BufferList;i++)
  2079. {
  2080. if(BufferList->buffer)
  2081. {
  2082. if(i < Source->BuffersPlayed)
  2083. readPos += BufferList->buffer->SampleLen;
  2084. totalBufferLen += BufferList->buffer->SampleLen;
  2085. }
  2086. BufferList = BufferList->next;
  2087. }
  2088. if(Source->state == AL_PLAYING)
  2089. writePos = readPos + (ALuint)(updateLen*Buffer->Frequency);
  2090. else
  2091. writePos = readPos;
  2092. if(Source->Looping)
  2093. {
  2094. readPos %= totalBufferLen;
  2095. writePos %= totalBufferLen;
  2096. }
  2097. else
  2098. {
  2099. /* Wrap positions back to 0 */
  2100. if(readPos >= totalBufferLen)
  2101. readPos = 0;
  2102. if(writePos >= totalBufferLen)
  2103. writePos = 0;
  2104. }
  2105. switch(name)
  2106. {
  2107. case AL_SEC_OFFSET:
  2108. offset[0] = (ALdouble)readPos / Buffer->Frequency;
  2109. offset[1] = (ALdouble)writePos / Buffer->Frequency;
  2110. break;
  2111. case AL_SAMPLE_OFFSET:
  2112. case AL_SAMPLE_RW_OFFSETS_SOFT:
  2113. offset[0] = (ALdouble)readPos;
  2114. offset[1] = (ALdouble)writePos;
  2115. break;
  2116. case AL_BYTE_OFFSET:
  2117. case AL_BYTE_RW_OFFSETS_SOFT:
  2118. if(Buffer->OriginalType == UserFmtIMA4)
  2119. {
  2120. ALuint BlockSize = 36 * ChannelsFromFmt(Buffer->FmtChannels);
  2121. ALuint FrameBlockSize = 65;
  2122. /* Round down to nearest ADPCM block */
  2123. offset[0] = (ALdouble)(readPos / FrameBlockSize * BlockSize);
  2124. if(Source->state != AL_PLAYING)
  2125. offset[1] = offset[0];
  2126. else
  2127. {
  2128. /* Round up to nearest ADPCM block */
  2129. offset[1] = (ALdouble)((writePos+FrameBlockSize-1) /
  2130. FrameBlockSize * BlockSize);
  2131. }
  2132. }
  2133. else
  2134. {
  2135. ALuint FrameSize = FrameSizeFromUserFmt(Buffer->OriginalChannels, Buffer->OriginalType);
  2136. offset[0] = (ALdouble)(readPos * FrameSize);
  2137. offset[1] = (ALdouble)(writePos * FrameSize);
  2138. }
  2139. break;
  2140. }
  2141. }
  2142. /* ApplyOffset
  2143. *
  2144. * Apply the stored playback offset to the Source. This function will update
  2145. * the number of buffers "played" given the stored offset.
  2146. */
  2147. ALboolean ApplyOffset(ALsource *Source)
  2148. {
  2149. const ALbufferlistitem *BufferList;
  2150. const ALbuffer *Buffer;
  2151. ALint bufferLen, totalBufferLen;
  2152. ALint buffersPlayed;
  2153. ALint offset;
  2154. /* Get sample frame offset */
  2155. offset = GetSampleOffset(Source);
  2156. if(offset == -1)
  2157. return AL_FALSE;
  2158. buffersPlayed = 0;
  2159. totalBufferLen = 0;
  2160. BufferList = Source->queue;
  2161. while(BufferList)
  2162. {
  2163. Buffer = BufferList->buffer;
  2164. bufferLen = Buffer ? Buffer->SampleLen : 0;
  2165. if(bufferLen <= offset-totalBufferLen)
  2166. {
  2167. /* Offset is past this buffer so increment to the next buffer */
  2168. buffersPlayed++;
  2169. }
  2170. else if(totalBufferLen <= offset)
  2171. {
  2172. /* Offset is in this buffer */
  2173. Source->BuffersPlayed = buffersPlayed;
  2174. Source->position = offset - totalBufferLen;
  2175. Source->position_fraction = 0;
  2176. return AL_TRUE;
  2177. }
  2178. totalBufferLen += bufferLen;
  2179. BufferList = BufferList->next;
  2180. }
  2181. /* Offset is out of range of the queue */
  2182. return AL_FALSE;
  2183. }
  2184. /* GetSampleOffset
  2185. *
  2186. * Returns the sample offset into the Source's queue (from the Sample, Byte or
  2187. * Second offset supplied by the application). This takes into account the fact
  2188. * that the buffer format may have been modifed since.
  2189. */
  2190. static ALint GetSampleOffset(ALsource *Source)
  2191. {
  2192. const ALbuffer *Buffer = NULL;
  2193. const ALbufferlistitem *BufferList;
  2194. ALint Offset = -1;
  2195. /* Find the first valid Buffer in the Queue */
  2196. BufferList = Source->queue;
  2197. while(BufferList)
  2198. {
  2199. if(BufferList->buffer)
  2200. {
  2201. Buffer = BufferList->buffer;
  2202. break;
  2203. }
  2204. BufferList = BufferList->next;
  2205. }
  2206. if(!Buffer)
  2207. {
  2208. Source->Offset = -1.0;
  2209. return -1;
  2210. }
  2211. switch(Source->OffsetType)
  2212. {
  2213. case AL_BYTE_OFFSET:
  2214. /* Determine the ByteOffset (and ensure it is block aligned) */
  2215. Offset = (ALint)Source->Offset;
  2216. if(Buffer->OriginalType == UserFmtIMA4)
  2217. {
  2218. Offset /= 36 * ChannelsFromUserFmt(Buffer->OriginalChannels);
  2219. Offset *= 65;
  2220. }
  2221. else
  2222. Offset /= FrameSizeFromUserFmt(Buffer->OriginalChannels, Buffer->OriginalType);
  2223. break;
  2224. case AL_SAMPLE_OFFSET:
  2225. Offset = (ALint)Source->Offset;
  2226. break;
  2227. case AL_SEC_OFFSET:
  2228. Offset = (ALint)(Source->Offset * Buffer->Frequency);
  2229. break;
  2230. }
  2231. Source->Offset = -1.0;
  2232. return Offset;
  2233. }
  2234. /* ReleaseALSources
  2235. *
  2236. * Destroys all sources in the source map.
  2237. */
  2238. ALvoid ReleaseALSources(ALCcontext *Context)
  2239. {
  2240. ALsizei pos;
  2241. ALuint j;
  2242. for(pos = 0;pos < Context->SourceMap.size;pos++)
  2243. {
  2244. ALsource *temp = Context->SourceMap.array[pos].value;
  2245. Context->SourceMap.array[pos].value = NULL;
  2246. while(temp->queue != NULL)
  2247. {
  2248. ALbufferlistitem *BufferList = temp->queue;
  2249. temp->queue = BufferList->next;
  2250. if(BufferList->buffer != NULL)
  2251. DecrementRef(&BufferList->buffer->ref);
  2252. free(BufferList);
  2253. }
  2254. for(j = 0;j < MAX_SENDS;++j)
  2255. {
  2256. if(temp->Send[j].Slot)
  2257. DecrementRef(&temp->Send[j].Slot->ref);
  2258. temp->Send[j].Slot = NULL;
  2259. }
  2260. FreeThunkEntry(temp->id);
  2261. memset(temp, 0, sizeof(*temp));
  2262. al_free(temp);
  2263. }
  2264. }