alSource.c 91 KB

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