audio.cc 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "audio/audio.h"
  23. #include "audio/audioDataBlock.h"
  24. #include "collection/vector.h"
  25. #include "console/console.h"
  26. #include "console/consoleTypes.h"
  27. #include "game/gameConnection.h"
  28. #include "io/fileStream.h"
  29. #include "audio/audioStreamSourceFactory.h"
  30. #ifdef TORQUE_OS_IOS
  31. #include "platformiOS/SoundEngine.h"
  32. #endif
  33. #ifdef TORQUE_OS_OSX
  34. //#define REL_WORKAROUND
  35. #endif
  36. //-------------------------------------------------------------------------
  37. #ifdef TORQUE_OS_IOS
  38. extern ALvoid alcMacOSXMixerOutputRateProc(const ALdouble value);
  39. #endif
  40. #define MAX_AUDIOSOURCES 16 // maximum number of concurrent sources
  41. #define MIN_GAIN 0.05f // anything with lower gain will not be started
  42. #define MIN_UNCULL_PERIOD 500 // time before buffer is checked to be unculled
  43. #define MIN_UNCULL_GAIN 0.1f // min gain of source to be unculled
  44. #define ALX_DEF_SAMPLE_RATE 44100 // default values for mixer
  45. #define ALX_DEF_SAMPLE_BITS 16
  46. #define ALX_DEF_CHANNELS 2
  47. #define FORCED_OUTER_FALLOFF 10000.f // forced falloff distance
  48. #ifdef TORQUE_OS_OSX
  49. static ALCdevice *mDevice = NULL; // active OpenAL device
  50. static ALCcontext *mContext = NULL; // active OpenAL context
  51. #elif TORQUE_OS_IOS
  52. static ALCdevice *mDevice = NULL; // active OpenAL device
  53. static ALCcontext *mContext = NULL; // active OpenAL context
  54. #else
  55. static ALCvoid *mDevice = NULL; // active OpenAL device
  56. static ALCvoid *mContext = NULL; // active OpenAL context
  57. #endif
  58. F32 mAudioChannelVolumes[Audio::AudioVolumeChannels]; // the attenuation for each of the channel types
  59. //-------------------------------------------------------------------------
  60. struct LoopingImage
  61. {
  62. AUDIOHANDLE mHandle;
  63. Resource<AudioBuffer> mBuffer;
  64. Audio::Description mDescription;
  65. AudioSampleEnvironment *mEnvironment;
  66. Point3F mPosition;
  67. Point3F mDirection;
  68. F32 mPitch;
  69. F32 mScore;
  70. U32 mCullTime;
  71. LoopingImage() { clear(); }
  72. void clear()
  73. {
  74. mHandle = NULL_AUDIOHANDLE;
  75. mBuffer = NULL;
  76. dMemset(&mDescription, 0, sizeof(Audio::Description));
  77. mEnvironment = 0;
  78. mPosition.set(0.f,0.f,0.f);
  79. mDirection.set(0.f,1.f,0.f);
  80. mPitch = 1.f;
  81. mScore = 0.f;
  82. mCullTime = 0;
  83. }
  84. };
  85. //-------------------------------------------------------------------------
  86. static F32 mMasterVolume = 1.f; // traped from AL_LISTENER gain (miles has difficulties with 3d sources)
  87. static ALuint mSource[MAX_AUDIOSOURCES]; // ALSources
  88. static AUDIOHANDLE mHandle[MAX_AUDIOSOURCES]; // unique handles
  89. static Resource<AudioBuffer> mBuffer[MAX_AUDIOSOURCES]; // each of the playing buffers (needed for AudioThread)
  90. static F32 mScore[MAX_AUDIOSOURCES]; // for figuring out which sources to cull/uncull
  91. static F32 mSourceVolume[MAX_AUDIOSOURCES]; // the samples current un-attenuated gain (not scaled by master/channel gains)
  92. static U32 mType[MAX_AUDIOSOURCES]; // the channel which this source belongs
  93. static AudioSampleEnvironment* mSampleEnvironment[MAX_AUDIOSOURCES]; // currently playing sample environments
  94. static bool mEnvironmentEnabled = false; // environment enabled?
  95. static SimObjectPtr<AudioEnvironment> mCurrentEnvironment; // the last environment set
  96. struct LoopingList : VectorPtr<LoopingImage*>
  97. {
  98. LoopingList() : VectorPtr<LoopingImage*>(__FILE__, __LINE__) { }
  99. LoopingList::iterator findImage(AUDIOHANDLE handle);
  100. void sort();
  101. };
  102. struct StreamingList : VectorPtr<AudioStreamSource*>
  103. {
  104. StreamingList() : VectorPtr<AudioStreamSource*>(__FILE__, __LINE__) { }
  105. StreamingList::iterator findImage(AUDIOHANDLE handle);
  106. void sort();
  107. };
  108. // LoopingList and LoopingFreeList own the images
  109. static LoopingList mLoopingList; // all the looping sources
  110. static LoopingList mLoopingFreeList; // free store
  111. static LoopingList mLoopingInactiveList; // sources which have not been played yet
  112. static LoopingList mLoopingCulledList; // sources which have been culled (alxPlay called)
  113. // StreamingList and StreamingFreeList own the images
  114. static StreamingList mStreamingList; // all the streaming sources
  115. //static StreamingList mStreamingFreeList; // free store
  116. static StreamingList mStreamingInactiveList; // sources which have not been played yet
  117. static StreamingList mStreamingCulledList; // sources which have been culled (alxPlay called)
  118. #define AUDIOHANDLE_LOOPING_BIT (0x80000000)
  119. #define AUDIOHANDLE_STREAMING_BIT (0x40000000)
  120. #define AUDIOHANDLE_INACTIVE_BIT (0x20000000)
  121. #define AUDIOHANDLE_LOADING_BIT (0x10000000)
  122. #define HANDLE_MASK ~(AUDIOHANDLE_LOOPING_BIT | AUDIOHANDLE_INACTIVE_BIT | AUDIOHANDLE_LOADING_BIT)
  123. // keep the 'AUDIOHANDLE_LOOPING_BIT' on the handle returned to the caller so that
  124. // the handle can quickly be rejected from looping list queries
  125. #define RETURN_MASK ~(AUDIOHANDLE_INACTIVE_BIT | AUDIOHANDLE_LOADING_BIT)
  126. static AUDIOHANDLE mLastHandle = NULL_AUDIOHANDLE;
  127. // force gain setting for 3d distances
  128. static U32 mNumSources = 0; // total number of sources to work with
  129. static U32 mRequestSources = MAX_AUDIOSOURCES; // number of sources to request from openAL
  130. #define INVALID_SOURCE 0xffffffff
  131. inline bool areEqualHandles(AUDIOHANDLE a, AUDIOHANDLE b)
  132. {
  133. return((a & HANDLE_MASK) == (b & HANDLE_MASK));
  134. }
  135. //-------------------------------------------------------------------------
  136. // Looping image
  137. //-------------------------------------------------------------------------
  138. inline LoopingList::iterator LoopingList::findImage(AUDIOHANDLE handle)
  139. {
  140. if(handle & AUDIOHANDLE_LOOPING_BIT)
  141. {
  142. LoopingList::iterator itr = begin();
  143. while(itr != end())
  144. {
  145. if(areEqualHandles((*itr)->mHandle, handle))
  146. return(itr);
  147. itr++;
  148. }
  149. }
  150. return(0);
  151. }
  152. inline S32 QSORT_CALLBACK loopingImageSort(const void * p1, const void * p2)
  153. {
  154. const LoopingImage * ip1 = *(const LoopingImage**)p1;
  155. const LoopingImage * ip2 = *(const LoopingImage**)p2;
  156. // min->max
  157. return (S32)(ip2->mScore - ip1->mScore);
  158. }
  159. void LoopingList::sort()
  160. {
  161. dQsort(address(), size(), sizeof(LoopingImage*), loopingImageSort);
  162. }
  163. //-------------------------------------------------------------------------
  164. // StreamingList
  165. //-------------------------------------------------------------------------
  166. inline StreamingList::iterator StreamingList::findImage(AUDIOHANDLE handle)
  167. {
  168. if(handle & AUDIOHANDLE_STREAMING_BIT)
  169. {
  170. StreamingList::iterator itr = begin();
  171. while(itr != end())
  172. {
  173. if(areEqualHandles((*itr)->mHandle, handle))
  174. return(itr);
  175. itr++;
  176. }
  177. }
  178. return(0);
  179. }
  180. inline S32 QSORT_CALLBACK streamingSourceSort(const void * p1, const void * p2)
  181. {
  182. const AudioStreamSource * ip1 = *(const AudioStreamSource**)p1;
  183. const AudioStreamSource * ip2 = *(const AudioStreamSource**)p2;
  184. // min->max
  185. return (S32)(ip2->mScore - ip1->mScore);
  186. }
  187. void StreamingList::sort()
  188. {
  189. dQsort(address(), size(), sizeof(AudioStreamSource*), streamingSourceSort);
  190. }
  191. //-------------------------------------------------------------------------
  192. LoopingImage * createLoopingImage()
  193. {
  194. LoopingImage *image;
  195. if (mLoopingFreeList.size())
  196. {
  197. image = mLoopingFreeList.last();
  198. mLoopingFreeList.pop_back();
  199. }
  200. else
  201. image = new LoopingImage;
  202. return(image);
  203. }
  204. //-------------------------------------------------------------------------
  205. AudioStreamSource * createStreamingSource(const char* filename)
  206. {
  207. AudioStreamSource *streamSource = AudioStreamSourceFactory::getNewInstance(filename);
  208. return(streamSource);
  209. }
  210. //-------------------------------------------------------------------------
  211. static AUDIOHANDLE getNewHandle()
  212. {
  213. mLastHandle++;
  214. mLastHandle &= HANDLE_MASK;
  215. if (mLastHandle == NULL_AUDIOHANDLE)
  216. mLastHandle++;
  217. return mLastHandle;
  218. }
  219. //-------------------------------------------------------------------------
  220. // function declarations
  221. void alxLoopingUpdate();
  222. void alxStreamingUpdate();
  223. void alxUpdateScores(bool);
  224. static bool findFreeSource(U32 *index)
  225. {
  226. for(U32 i = 0; i < mNumSources; i++)
  227. if(mHandle[i] == NULL_AUDIOHANDLE)
  228. {
  229. *index = i;
  230. return(true);
  231. }
  232. return(false);
  233. }
  234. //--------------------------------------------------------------------------
  235. // - cull out the min source that is below volume
  236. // - streams/voice/loading streams are all scored > 2
  237. // - volumes are attenuated by channel only
  238. static bool cullSource(U32 *index, F32 volume)
  239. {
  240. alGetError();
  241. F32 minVolume = volume;
  242. S32 best = -1;
  243. for(U32 i = 0; i < mNumSources; i++)
  244. {
  245. if(mScore[i] < minVolume)
  246. {
  247. minVolume = mScore[i];
  248. best = i;
  249. }
  250. }
  251. if(best == -1)
  252. return(false);
  253. // check if culling a looper
  254. LoopingList::iterator itr = mLoopingList.findImage(mHandle[best]);
  255. if(itr)
  256. {
  257. // check if culling an inactive looper
  258. if(mHandle[best] & AUDIOHANDLE_INACTIVE_BIT)
  259. {
  260. AssertFatal(!mLoopingInactiveList.findImage(mHandle[best]), "cullSource: image already in inactive list");
  261. AssertFatal(!mLoopingCulledList.findImage(mHandle[best]), "cullSource: image should not be in culled list");
  262. mLoopingInactiveList.push_back(*itr);
  263. }
  264. else
  265. {
  266. (*itr)->mHandle |= AUDIOHANDLE_INACTIVE_BIT;
  267. AssertFatal(!mLoopingCulledList.findImage(mHandle[best]), "cullSource: image already in culled list");
  268. AssertFatal(!mLoopingInactiveList.findImage(mHandle[best]), "cullSource: image should no be in inactive list");
  269. (*itr)->mCullTime = Platform::getRealMilliseconds();
  270. mLoopingCulledList.push_back(*itr);
  271. }
  272. }
  273. // check if culling a streamer
  274. StreamingList::iterator itr2 = mStreamingList.findImage(mHandle[best]);
  275. if(itr2)
  276. {
  277. // check if culling an inactive streamer
  278. if(mHandle[best] & AUDIOHANDLE_INACTIVE_BIT)
  279. {
  280. AssertFatal(!mStreamingInactiveList.findImage(mHandle[best]), "cullSource: image already in inactive list");
  281. AssertFatal(!mStreamingCulledList.findImage(mHandle[best]), "cullSource: image should not be in culled list");
  282. mStreamingInactiveList.push_back(*itr2);
  283. }
  284. else
  285. {
  286. (*itr2)->mHandle |= AUDIOHANDLE_INACTIVE_BIT;
  287. AssertFatal(!mStreamingCulledList.findImage(mHandle[best]), "cullSource: image already in culled list");
  288. AssertFatal(!mStreamingInactiveList.findImage(mHandle[best]), "cullSource: image should no be in inactive list");
  289. (*itr2)->freeStream();
  290. (*itr2)->mCullTime = Platform::getRealMilliseconds();
  291. mStreamingCulledList.push_back(*itr2);
  292. }
  293. }
  294. alSourceStop(mSource[best]);
  295. mHandle[best] = NULL_AUDIOHANDLE;
  296. mBuffer[best] = 0;
  297. *index = best;
  298. return(true);
  299. }
  300. //--------------------------------------------------------------------------
  301. /** Compute approximate max volume at a particular distance
  302. ignore cone volume influnces
  303. */
  304. static F32 approximate3DVolume(const Audio::Description& desc, const Point3F &position)
  305. {
  306. Point3F p1;
  307. alGetListener3f(AL_POSITION, &p1.x, &p1.y, &p1.z);
  308. p1 -= position;
  309. F32 distance = p1.magnitudeSafe();
  310. if(distance >= desc.mMaxDistance)
  311. return(0.f);
  312. else if(distance < desc.mReferenceDistance)
  313. return 1.0f;
  314. else
  315. return 1.0f - (distance - desc.mReferenceDistance) / (desc.mMaxDistance - desc.mReferenceDistance);
  316. }
  317. //--------------------------------------------------------------------------
  318. inline U32 alxFindIndex(AUDIOHANDLE handle)
  319. {
  320. for (U32 i=0; i<mNumSources; i++)
  321. if(mHandle[i] && areEqualHandles(mHandle[i], handle))
  322. return i;
  323. return MAX_AUDIOSOURCES;
  324. }
  325. //--------------------------------------------------------------------------
  326. ALuint alxFindSource(AUDIOHANDLE handle)
  327. {
  328. for (U32 i=0; i<mNumSources; i++)
  329. if(mHandle[i] && areEqualHandles(mHandle[i], handle))
  330. return mSource[i];
  331. return(INVALID_SOURCE);
  332. }
  333. //--------------------------------------------------------------------------
  334. /** Determmine if an AUDIOHANDLE is valid.
  335. An AUDIOHANDLE is valid if it is a currently playing source, inactive source,
  336. or a looping source (basically anything where a alxSource??? call will succeed)
  337. */
  338. bool alxIsValidHandle(AUDIOHANDLE handle)
  339. {
  340. if(handle == NULL_AUDIOHANDLE)
  341. return(false);
  342. // inactive sources are valid
  343. U32 idx = alxFindIndex(handle);
  344. if(idx != MAX_AUDIOSOURCES)
  345. {
  346. if(mHandle[idx] & AUDIOHANDLE_INACTIVE_BIT)
  347. return(true);
  348. // if it is active but not playing then it has stopped...
  349. ALint state = AL_STOPPED;
  350. alGetSourcei(mSource[idx], AL_SOURCE_STATE, &state);
  351. return(state == AL_PLAYING);
  352. }
  353. if(mLoopingList.findImage(handle))
  354. return(true);
  355. if(mStreamingList.findImage(handle))
  356. return(true);
  357. return(false);
  358. }
  359. //--------------------------------------------------------------------------
  360. /** Determmine if an AUDIOHANDLE is currently playing
  361. */
  362. bool alxIsPlaying(AUDIOHANDLE handle)
  363. {
  364. if(handle == NULL_AUDIOHANDLE)
  365. return(false);
  366. U32 idx = alxFindIndex(handle);
  367. if(idx == MAX_AUDIOSOURCES)
  368. return(false);
  369. ALint state = 0;
  370. alGetSourcei(mSource[idx], AL_SOURCE_STATE, &state);
  371. return(state == AL_PLAYING);
  372. }
  373. //--------------------------------------------------------------------------
  374. void alxEnvironmentDestroy()
  375. {
  376. /* todo
  377. if(mEnvironment)
  378. {
  379. alDeleteEnvironmentIASIG(1, &mEnvironment);
  380. mEnvironment = 0;
  381. }
  382. */
  383. }
  384. void alxEnvironmentInit()
  385. {
  386. /* todo
  387. alxEnvironmentDestroy();
  388. if(alIsExtensionPresent((const ALubyte *)"AL_EXT_IASIG"))
  389. {
  390. alGenEnvironmentIASIG(1, &mEnvironment);
  391. if(alGetError() != AL_NO_ERROR)
  392. mEnvironment = 0;
  393. }
  394. */
  395. }
  396. //--------------------------------------------------------------------------
  397. // - setup a sources environmental effect settings
  398. static void alxSourceEnvironment(ALuint source, F32 environmentLevel, AudioSampleEnvironment * env)
  399. {
  400. // environment level is on the AudioDatablock
  401. /* todo
  402. alSourcef(source, AL_ENV_SAMPLE_REVERB_MIX_EXT, environmentLevel);
  403. */
  404. if(!env)
  405. return;
  406. /* todo
  407. alSourcei(source, AL_ENV_SAMPLE_DIRECT_EXT, env->mDirect);
  408. alSourcei(source, AL_ENV_SAMPLE_DIRECT_HF_EXT, env->mDirectHF);
  409. alSourcei(source, AL_ENV_SAMPLE_ROOM_EXT, env->mRoom);
  410. alSourcei(source, AL_ENV_SAMPLE_ROOM_HF_EXT, env->mRoomHF);
  411. alSourcei(source, AL_ENV_SAMPLE_OUTSIDE_VOLUME_HF_EXT, env->mOutsideVolumeHF);
  412. alSourcei(source, AL_ENV_SAMPLE_FLAGS_EXT, env->mFlags);
  413. alSourcef(source, AL_ENV_SAMPLE_OBSTRUCTION_EXT, env->mObstruction);
  414. alSourcef(source, AL_ENV_SAMPLE_OBSTRUCTION_LF_RATIO_EXT, env->mObstructionLFRatio);
  415. alSourcef(source, AL_ENV_SAMPLE_OCCLUSION_EXT, env->mOcclusion);
  416. alSourcef(source, AL_ENV_SAMPLE_OCCLUSION_LF_RATIO_EXT, env->mOcclusionLFRatio);
  417. alSourcef(source, AL_ENV_SAMPLE_OCCLUSION_ROOM_RATIO_EXT, env->mOcclusionRoomRatio);
  418. alSourcef(source, AL_ENV_SAMPLE_ROOM_ROLLOFF_EXT, env->mRoomRolloff);
  419. alSourcef(source, AL_ENV_SAMPLE_AIR_ABSORPTION_EXT, env->mAirAbsorption);
  420. */
  421. }
  422. static void alxSourceEnvironment(ALuint source, LoopingImage * image)
  423. {
  424. AssertFatal(image, "alxSourceEnvironment: invalid looping image");
  425. if(image->mDescription.mIs3D)
  426. alxSourceEnvironment(source, image->mDescription.mEnvironmentLevel, image->mEnvironment);
  427. }
  428. static void alxSourceEnvironment(ALuint source, AudioStreamSource * image)
  429. {
  430. AssertFatal(image, "alxSourceEnvironment: invalid looping image");
  431. if(image->mDescription.mIs3D)
  432. alxSourceEnvironment(source, image->mDescription.mEnvironmentLevel, image->mEnvironment);
  433. }
  434. //--------------------------------------------------------------------------
  435. // setup a source to play... loopers have pitch cached
  436. // - by default, pitch is 1x (settings not defined in description)
  437. // - all the settings are cached by openAL (miles version), so no worries setting them here
  438. static void alxSourcePlay(ALuint source, Resource<AudioBuffer> buffer, const Audio::Description& desc, const MatrixF *transform)
  439. {
  440. alSourcei(source, AL_BUFFER, buffer->getALBuffer());
  441. alSourcef(source, AL_GAIN, Audio::linearToDB(desc.mVolume * mAudioChannelVolumes[desc.mVolumeChannel] * mMasterVolume));
  442. alSourcei(source, AL_LOOPING, desc.mIsLooping ? AL_TRUE : AL_FALSE);
  443. alSourcef(source, AL_PITCH, 1.f);
  444. alSourcei(source, AL_CONE_INNER_ANGLE, desc.mConeInsideAngle);
  445. alSourcei(source, AL_CONE_OUTER_ANGLE, desc.mConeOutsideAngle);
  446. alSourcef(source, AL_CONE_OUTER_GAIN, desc.mConeOutsideVolume);
  447. if(transform != NULL)
  448. {
  449. #ifdef REL_WORKAROUND
  450. alSourcei(source, AL_SOURCE_ABSOLUTE, AL_TRUE);
  451. #else
  452. alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
  453. #endif
  454. Point3F p;
  455. transform->getColumn(3, &p);
  456. alSource3f(source, AL_POSITION, p.x, p.y, p.z);
  457. //Always use ConeVector (which is tied to transform)
  458. alSource3f(source, AL_DIRECTION, desc.mConeVector.x, desc.mConeVector.y, desc.mConeVector.z);
  459. }
  460. else
  461. {
  462. // 2D sound
  463. alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
  464. alSource3f(source, AL_POSITION, 0.0f, 0.0f, 1.0f);
  465. }
  466. alSourcef(source, AL_REFERENCE_DISTANCE, desc.mReferenceDistance);
  467. alSourcef(source, AL_MAX_DISTANCE, desc.mMaxDistance);
  468. /* todo
  469. // environmental audio stuff:
  470. alSourcef(source, AL_ENV_SAMPLE_REVERB_MIX_EXT, desc.mEnvironmentLevel);
  471. if(desc.mEnvironmentLevel != 0.f)
  472. alSourceResetEnvironment_EXT(source);
  473. */
  474. }
  475. // helper for looping images
  476. static void alxSourcePlay(ALuint source, LoopingImage * image)
  477. {
  478. AssertFatal(image, "alxSourcePlay: invalid looping image");
  479. // 3d source? need position/direction
  480. if(image->mDescription.mIs3D)
  481. {
  482. MatrixF transform(true);
  483. transform.setColumn(3, image->mPosition);
  484. transform.setRow(1, image->mDirection);
  485. alxSourcePlay(source, image->mBuffer, image->mDescription, &transform);
  486. }
  487. else
  488. {
  489. // 2d source
  490. alxSourcePlay(source, image->mBuffer, image->mDescription, 0);
  491. }
  492. }
  493. //--------------------------------------------------------------------------
  494. // setup a streaming source to play
  495. static void alxSourcePlay(AudioStreamSource *streamSource)
  496. {
  497. ALuint source = streamSource->mSource;
  498. Audio::Description& desc = streamSource->mDescription;
  499. alSourcef(source, AL_GAIN, Audio::linearToDB(desc.mVolume * mAudioChannelVolumes[desc.mVolumeChannel] * mMasterVolume));
  500. // alSourcei(source, AL_LOOPING, AL_FALSE);
  501. alSourcef(source, AL_PITCH, 1.f);
  502. alSourcei(source, AL_CONE_INNER_ANGLE, desc.mConeInsideAngle);
  503. alSourcei(source, AL_CONE_OUTER_ANGLE, desc.mConeOutsideAngle);
  504. alSourcef(source, AL_CONE_OUTER_GAIN, desc.mConeOutsideVolume);
  505. if(streamSource->mDescription.mIs3D)
  506. {
  507. MatrixF transform(true);
  508. transform.setColumn(3, streamSource->mPosition);
  509. transform.setRow(1, streamSource->mDirection);
  510. #ifdef REL_WORKAROUND
  511. alSourcei(source, AL_SOURCE_ABSOLUTE, AL_TRUE);
  512. #else
  513. alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
  514. #endif
  515. Point3F p;
  516. transform.getColumn(3, &p);
  517. alSource3f(source, AL_POSITION, p.x, p.y, p.z);
  518. //Always use ConeVector (which is tied to transform)
  519. alSource3f(source, AL_DIRECTION, desc.mConeVector.x, desc.mConeVector.y, desc.mConeVector.z);
  520. }
  521. else
  522. {
  523. // 2D sound
  524. // JMQ: slam the stream source's position to our desired value
  525. streamSource->mPosition = Point3F(0.0f, 0.0f, 1.0f);
  526. alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
  527. alSource3f(source, AL_POSITION,
  528. streamSource->mPosition.x,
  529. streamSource->mPosition.y,
  530. streamSource->mPosition.z);
  531. }
  532. alSourcef(source, AL_REFERENCE_DISTANCE, desc.mReferenceDistance);
  533. alSourcef(source, AL_MAX_DISTANCE, desc.mMaxDistance);
  534. /* todo
  535. // environmental audio stuff:
  536. alSourcef(source, AL_ENV_SAMPLE_REVERB_MIX_EXT, desc.mEnvironmentLevel);
  537. if(desc.mEnvironmentLevel != 0.f)
  538. alSourceResetEnvironment_EXT(source);
  539. */
  540. }
  541. //--------------------------------------------------------------------------
  542. AUDIOHANDLE alxCreateSource(const Audio::Description& desc,
  543. const char *filename,
  544. const MatrixF *transform,
  545. AudioSampleEnvironment *sampleEnvironment)
  546. {
  547. if (!mContext)
  548. return NULL_AUDIOHANDLE;
  549. if( filename == NULL || filename == StringTable->EmptyString )
  550. return NULL_AUDIOHANDLE;
  551. F32 volume = desc.mVolume;
  552. // calculate an approximate attenuation for 3d sounds
  553. if(transform && desc.mIs3D)
  554. {
  555. Point3F position;
  556. transform->getColumn(3, &position);
  557. volume *= approximate3DVolume(desc, position);
  558. }
  559. // check the type specific volume
  560. AssertFatal(desc.mVolumeChannel < Audio::AudioVolumeChannels, "alxCreateSource: invalid volume channel for source");
  561. if(desc.mVolumeChannel >= Audio::AudioVolumeChannels)
  562. return(NULL_AUDIOHANDLE);
  563. // done if channel is muted (and not a looper)
  564. if(!desc.mIsLooping && !desc.mIsStreaming && (mAudioChannelVolumes[desc.mVolumeChannel] == 0.f))
  565. return(NULL_AUDIOHANDLE);
  566. // scale volume by channel attenuation
  567. volume *= mAudioChannelVolumes[desc.mVolumeChannel];
  568. // non-loopers don't add if < minvolume
  569. if(!desc.mIsLooping && !desc.mIsStreaming && (volume <= MIN_GAIN))
  570. return(NULL_AUDIOHANDLE);
  571. U32 index = MAX_AUDIOSOURCES;
  572. // try and find an available source: 0 volume loopers get added to inactive list
  573. if(volume > MIN_GAIN)
  574. {
  575. if(!findFreeSource(&index))
  576. {
  577. alxUpdateScores(true);
  578. // scores do not include master volume
  579. if(!cullSource(&index, volume))
  580. index = MAX_AUDIOSOURCES;
  581. }
  582. }
  583. // make sure that loopers are added
  584. if(index == MAX_AUDIOSOURCES)
  585. {
  586. if(desc.mIsLooping && !(desc.mIsStreaming))
  587. {
  588. Resource<AudioBuffer> buffer = AudioBuffer::find(filename);
  589. if(!(bool)buffer)
  590. return(NULL_AUDIOHANDLE);
  591. // create the inactive looping image
  592. LoopingImage * image = createLoopingImage();
  593. image->mHandle = getNewHandle() | AUDIOHANDLE_LOOPING_BIT | AUDIOHANDLE_INACTIVE_BIT;
  594. image->mBuffer = buffer;
  595. image->mDescription = desc;
  596. image->mScore = volume;
  597. image->mEnvironment = sampleEnvironment;
  598. // grab position/direction if 3d source
  599. if(transform)
  600. {
  601. transform->getColumn(3, &image->mPosition);
  602. transform->getColumn(1, &image->mDirection);
  603. }
  604. AssertFatal(!mLoopingInactiveList.findImage(image->mHandle), "alxCreateSource: handle in inactive list");
  605. AssertFatal(!mLoopingCulledList.findImage(image->mHandle), "alxCreateSource: handle in culled list");
  606. // add to the looping and inactive lists
  607. mLoopingList.push_back(image);
  608. mLoopingInactiveList.push_back(image);
  609. return(image->mHandle & RETURN_MASK);
  610. }
  611. else
  612. return(NULL_AUDIOHANDLE);
  613. }
  614. // make sure that streamers are added
  615. if(index == MAX_AUDIOSOURCES)
  616. {
  617. if(desc.mIsStreaming)
  618. {
  619. // create the inactive audio stream
  620. AudioStreamSource * streamSource = createStreamingSource(filename);
  621. if (streamSource)
  622. {
  623. streamSource->mHandle = getNewHandle() | AUDIOHANDLE_STREAMING_BIT | AUDIOHANDLE_INACTIVE_BIT;
  624. streamSource->mSource = NULL;
  625. streamSource->mDescription = desc;
  626. streamSource->mScore = volume;
  627. streamSource->mEnvironment = sampleEnvironment;
  628. // grab position/direction if 3d source
  629. if(transform)
  630. {
  631. transform->getColumn(3, &streamSource->mPosition);
  632. transform->getColumn(1, &streamSource->mDirection);
  633. }
  634. AssertFatal(!mStreamingInactiveList.findImage(streamSource->mHandle), "alxCreateSource: handle in inactive list");
  635. AssertFatal(!mStreamingCulledList.findImage(streamSource->mHandle), "alxCreateSource: handle in culled list");
  636. // add to the streaming and inactive lists
  637. mStreamingList.push_back(streamSource);
  638. mStreamingInactiveList.push_back(streamSource);
  639. return(streamSource->mHandle & RETURN_MASK);
  640. }
  641. else
  642. return NULL_AUDIOHANDLE;
  643. }
  644. else
  645. return(NULL_AUDIOHANDLE);
  646. }
  647. // clear the error state
  648. alGetError();
  649. // grab the buffer
  650. Resource<AudioBuffer> buffer;
  651. if(!(desc.mIsStreaming)) {
  652. buffer = AudioBuffer::find(filename);
  653. if((bool)buffer == false)
  654. return NULL_AUDIOHANDLE;
  655. }
  656. // init the source (created inactive) and store needed values
  657. mHandle[index] = getNewHandle() | AUDIOHANDLE_INACTIVE_BIT;
  658. mType[index] = desc.mVolumeChannel;
  659. if(!(desc.mIsStreaming)) {
  660. mBuffer[index] = buffer;
  661. }
  662. mScore[index] = volume;
  663. mSourceVolume[index] = desc.mVolume;
  664. mSampleEnvironment[index] = sampleEnvironment;
  665. ALuint source = mSource[index];
  666. // setup play info
  667. if(!desc.mIsStreaming)
  668. alxSourcePlay(source, buffer, desc, desc.mIs3D ? transform : 0);
  669. if(mEnvironmentEnabled)
  670. alxSourceEnvironment(source, desc.mEnvironmentLevel, sampleEnvironment);
  671. // setup a LoopingImage ONLY if the sound is a looper:
  672. if(desc.mIsLooping && !(desc.mIsStreaming))
  673. {
  674. mHandle[index] |= AUDIOHANDLE_LOOPING_BIT;
  675. LoopingImage * image = createLoopingImage();
  676. image->mHandle = mHandle[index];
  677. image->mBuffer = buffer;
  678. image->mDescription = desc;
  679. image->mScore = volume;
  680. image->mEnvironment = sampleEnvironment;
  681. // grab position/direction
  682. if(transform)
  683. {
  684. transform->getColumn(3, &image->mPosition);
  685. transform->getColumn(1, &image->mDirection);
  686. }
  687. AssertFatal(!mLoopingInactiveList.findImage(image->mHandle), "alxCreateSource: handle in inactive list");
  688. AssertFatal(!mLoopingCulledList.findImage(image->mHandle), "alxCreateSource: handle in culled list");
  689. // add to the looping list
  690. mLoopingList.push_back(image);
  691. }
  692. // setup a AudioStreamSource ONLY if the sound is a streamer:
  693. if(desc.mIsStreaming)
  694. {
  695. // Intangir> why is loading bit never used anywhere else?
  696. // comes in handy for my oggmixedstream
  697. // (prevents it from being deleted before it is loaded)
  698. mHandle[index] |= AUDIOHANDLE_STREAMING_BIT | AUDIOHANDLE_LOADING_BIT;
  699. AudioStreamSource * streamSource = createStreamingSource(filename);
  700. if (streamSource)
  701. {
  702. streamSource->mHandle = mHandle[index];
  703. streamSource->mSource = mSource[index];
  704. streamSource->mDescription = desc;
  705. streamSource->mScore = volume;
  706. streamSource->mEnvironment = sampleEnvironment;
  707. // grab position/direction
  708. if(transform)
  709. {
  710. transform->getColumn(3, &streamSource->mPosition);
  711. transform->getColumn(1, &streamSource->mDirection);
  712. }
  713. AssertFatal(!mStreamingInactiveList.findImage(streamSource->mHandle), "alxCreateSource: handle in inactive list");
  714. AssertFatal(!mStreamingCulledList.findImage(streamSource->mHandle), "alxCreateSource: handle in culled list");
  715. alxSourcePlay(streamSource);
  716. // add to the looping list
  717. mStreamingList.push_back(streamSource);
  718. }
  719. else
  720. {
  721. mSampleEnvironment[index] = 0;
  722. mHandle[index] = NULL_AUDIOHANDLE;
  723. mBuffer[index] = 0;
  724. return NULL_AUDIOHANDLE;
  725. }
  726. }
  727. // clear off all but looping bit
  728. return(mHandle[index] & RETURN_MASK);
  729. }
  730. //--------------------------------------------------------------------------
  731. AUDIOHANDLE alxCreateSource(const AudioAsset *profile, const MatrixF *transform)
  732. {
  733. if (profile == NULL)
  734. return NULL_AUDIOHANDLE;
  735. return alxCreateSource(profile->getAudioDescription(), profile->getAudioFile(), transform, NULL );
  736. }
  737. //--------------------------------------------------------------------------
  738. extern void threadPlay(AudioBuffer * buffer, AUDIOHANDLE handle);
  739. AUDIOHANDLE alxPlay(AUDIOHANDLE handle)
  740. {
  741. U32 index = alxFindIndex(handle);
  742. if(index != MAX_AUDIOSOURCES)
  743. {
  744. // play if not already playing
  745. if(mHandle[index] & AUDIOHANDLE_INACTIVE_BIT)
  746. {
  747. mHandle[index] &= ~(AUDIOHANDLE_INACTIVE_BIT | AUDIOHANDLE_LOADING_BIT);
  748. // make sure the looping image also clears it's inactive bit
  749. LoopingList::iterator itr = mLoopingList.findImage(handle);
  750. if(itr)
  751. (*itr)->mHandle &= ~(AUDIOHANDLE_INACTIVE_BIT | AUDIOHANDLE_LOADING_BIT);
  752. // make sure the streaming image also clears it's inactive bit
  753. StreamingList::iterator itr2 = mStreamingList.findImage(handle);
  754. if(itr2)
  755. (*itr2)->mHandle &= ~(AUDIOHANDLE_INACTIVE_BIT | AUDIOHANDLE_LOADING_BIT);
  756. alSourcePlay(mSource[index]);
  757. return(handle);
  758. }
  759. }
  760. else
  761. {
  762. // move inactive loopers to the culled list, try to start the sound
  763. LoopingList::iterator itr = mLoopingInactiveList.findImage(handle);
  764. if(itr)
  765. {
  766. AssertFatal(!mLoopingCulledList.findImage(handle), "alxPlay: image already in culled list");
  767. mLoopingCulledList.push_back(*itr);
  768. mLoopingInactiveList.erase_fast(itr);
  769. alxLoopingUpdate();
  770. return(handle);
  771. }
  772. else if(mLoopingCulledList.findImage(handle))
  773. {
  774. alxLoopingUpdate();
  775. return(handle);
  776. }
  777. else
  778. return(NULL_AUDIOHANDLE);
  779. #if 0
  780. // move inactive streamers to the culled list, try to start the sound
  781. StreamingList::iterator itr2 = mStreamingInactiveList.findImage(handle);
  782. if(itr2)
  783. {
  784. AssertFatal(!mStreamingCulledList.findImage(handle), "alxPlay: image already in culled list");
  785. (*itr2)->freeStream();
  786. mStreamingCulledList.push_back(*itr2);
  787. mStreamingInactiveList.erase_fast(itr2);
  788. alxStreamingUpdate();
  789. return(handle);
  790. }
  791. else if(mStreamingCulledList.findImage(handle))
  792. {
  793. alxStreamingUpdate();
  794. return(handle);
  795. }
  796. else
  797. return(NULL_AUDIOHANDLE);
  798. #endif
  799. }
  800. return(handle);
  801. }
  802. //--------------------------------------------------------------------------
  803. // helper function.. create a source and play it
  804. AUDIOHANDLE alxPlay(const AudioAsset *profile, const MatrixF *transform, const Point3F* /*velocity*/)
  805. {
  806. if(profile == NULL)
  807. return NULL_AUDIOHANDLE;
  808. AUDIOHANDLE handle = alxCreateSource(profile->getAudioDescription(), profile->getAudioFile(), transform, NULL);
  809. if(handle != NULL_AUDIOHANDLE)
  810. return(alxPlay(handle));
  811. return(handle);
  812. }
  813. bool alxPause( AUDIOHANDLE handle )
  814. {
  815. if(handle == NULL_AUDIOHANDLE)
  816. return false;
  817. U32 index = alxFindIndex( handle );
  818. alSourcePause( mSource[index] );
  819. ALenum error = 0;
  820. if ( (error = alGetError()) == AL_NO_ERROR)
  821. return true;
  822. switch (error)
  823. {
  824. case AL_INVALID_NAME:
  825. Con::errorf("alxPause - OpenAL AL_INVALID_NAME error code returned");
  826. break;
  827. case AL_INVALID_ENUM:
  828. Con::errorf("alxPause - OpenAL AL_INVALID_ENUM error code returned");
  829. break;
  830. case AL_INVALID_VALUE:
  831. Con::errorf("alxPause - OpenAL AL_INVALID_VALUE error code returned");
  832. break;
  833. case AL_INVALID_OPERATION:
  834. Con::errorf("alxPause - OpenAL AL_INVALID_OPERATION error code returned");
  835. break;
  836. case AL_OUT_OF_MEMORY:
  837. Con::errorf("alxPause - OpenAL AL_OUT_OF_MEMORY error code returned");
  838. break;
  839. default:
  840. Con::errorf("alxPause - OpenAL has encountered a problem and won't tell us what it is.");
  841. };
  842. return false;
  843. }
  844. void alxUnPause( AUDIOHANDLE handle )
  845. {
  846. if(handle == NULL_AUDIOHANDLE)
  847. return;
  848. alxPlay( handle );
  849. }
  850. //--------------------------------------------------------------------------
  851. void alxStop(AUDIOHANDLE handle)
  852. {
  853. U32 index = alxFindIndex(handle);
  854. // stop it
  855. if(index != MAX_AUDIOSOURCES)
  856. {
  857. if(!(mHandle[index] & AUDIOHANDLE_INACTIVE_BIT))
  858. {
  859. alSourceStop(mSource[index]);
  860. }
  861. alSourcei(mSource[index], AL_BUFFER, AL_NONE);
  862. mSampleEnvironment[index] = 0;
  863. mHandle[index] = NULL_AUDIOHANDLE;
  864. mBuffer[index] = 0;
  865. }
  866. // remove loopingImage and add it to the free list
  867. LoopingList::iterator itr = mLoopingList.findImage(handle);
  868. if(itr)
  869. {
  870. // remove from inactive/culled list
  871. if((*itr)->mHandle & AUDIOHANDLE_INACTIVE_BIT)
  872. {
  873. LoopingList::iterator tmp = mLoopingInactiveList.findImage(handle);
  874. // inactive?
  875. if(tmp)
  876. mLoopingInactiveList.erase_fast(tmp);
  877. else
  878. {
  879. //culled?
  880. tmp = mLoopingCulledList.findImage(handle);
  881. AssertFatal(tmp, "alxStop: failed to find inactive looping source");
  882. mLoopingCulledList.erase_fast(tmp);
  883. }
  884. }
  885. AssertFatal(!mLoopingInactiveList.findImage((*itr)->mHandle), "alxStop: handle in inactive list");
  886. AssertFatal(!mLoopingCulledList.findImage((*itr)->mHandle), "alxStop: handle in culled list");
  887. // remove it
  888. (*itr)->clear();
  889. mLoopingFreeList.push_back(*itr);
  890. mLoopingList.erase_fast(itr);
  891. }
  892. // remove streamingImage and add it to the free list
  893. StreamingList::iterator itr2 = mStreamingList.findImage(handle);
  894. if(itr2)
  895. {
  896. // remove from inactive/culled list
  897. if((*itr2)->mHandle & AUDIOHANDLE_INACTIVE_BIT)
  898. {
  899. StreamingList::iterator tmp = mStreamingInactiveList.findImage(handle);
  900. // inactive?
  901. if(tmp)
  902. mStreamingInactiveList.erase_fast(tmp);
  903. else
  904. {
  905. //culled?
  906. tmp = mStreamingCulledList.findImage(handle);
  907. AssertFatal(tmp, "alxStop: failed to find inactive looping source");
  908. mStreamingCulledList.erase_fast(tmp);
  909. }
  910. }
  911. AssertFatal(!mStreamingInactiveList.findImage((*itr2)->mHandle), "alxStop: handle in inactive list");
  912. AssertFatal(!mStreamingCulledList.findImage((*itr2)->mHandle), "alxStop: handle in culled list");
  913. // remove it
  914. (*itr2)->freeStream();
  915. delete(*itr2);
  916. mStreamingList.erase_fast(itr2);
  917. }
  918. }
  919. //--------------------------------------------------------------------------
  920. void alxStopAll()
  921. {
  922. // stop all open sources
  923. for(S32 i = mNumSources - 1; i >= 0; i--)
  924. if(mHandle[i] != NULL_AUDIOHANDLE)
  925. alxStop(mHandle[i]);
  926. // stop all looping sources
  927. while(mLoopingList.size())
  928. alxStop(mLoopingList.last()->mHandle);
  929. // stop all streaming sources
  930. while(mStreamingList.size())
  931. alxStop(mStreamingList.last()->mHandle);
  932. }
  933. void alxLoopSourcef(AUDIOHANDLE handle, ALenum pname, ALfloat value)
  934. {
  935. LoopingList::iterator itr = mLoopingList.findImage(handle);
  936. if(itr)
  937. {
  938. switch(pname)
  939. {
  940. case AL_GAIN:
  941. (*itr)->mDescription.mVolume = Audio::DBToLinear(value);
  942. break;
  943. case AL_GAIN_LINEAR:
  944. (*itr)->mDescription.mVolume = value;
  945. break;
  946. case AL_PITCH:
  947. (*itr)->mPitch = value;
  948. break;
  949. case AL_REFERENCE_DISTANCE:
  950. (*itr)->mDescription.mReferenceDistance = value;
  951. break;
  952. case AL_CONE_OUTER_GAIN:
  953. (*itr)->mDescription.mMaxDistance = value;
  954. break;
  955. }
  956. }
  957. }
  958. void alxLoopSource3f(AUDIOHANDLE handle, ALenum pname, ALfloat value1, ALfloat value2, ALfloat value3)
  959. {
  960. LoopingList::iterator itr = mLoopingList.findImage(handle);
  961. if(itr)
  962. {
  963. switch(pname)
  964. {
  965. case AL_POSITION:
  966. (*itr)->mPosition.x = value1;
  967. (*itr)->mPosition.y = value2;
  968. (*itr)->mPosition.z = value3;
  969. break;
  970. case AL_DIRECTION:
  971. (*itr)->mDirection.x = value1;
  972. (*itr)->mDirection.y = value2;
  973. (*itr)->mDirection.z = value3;
  974. break;
  975. }
  976. }
  977. }
  978. void alxLoopSourcei(AUDIOHANDLE handle, ALenum pname, ALint value)
  979. {
  980. LoopingList::iterator itr = mLoopingList.findImage(handle);
  981. if(itr)
  982. {
  983. switch(pname)
  984. {
  985. //case AL_SOURCE_AMBIENT:
  986. // (*itr)->mDescription.mIs3D = value;
  987. // break;
  988. case AL_CONE_INNER_ANGLE:
  989. (*itr)->mDescription.mConeInsideAngle = value;
  990. break;
  991. case AL_CONE_OUTER_ANGLE:
  992. (*itr)->mDescription.mConeOutsideAngle = value;
  993. break;
  994. }
  995. }
  996. }
  997. void alxLoopGetSourcef(AUDIOHANDLE handle, ALenum pname, ALfloat *value)
  998. {
  999. LoopingList::iterator itr = mLoopingList.findImage(handle);
  1000. if(itr)
  1001. {
  1002. switch(pname)
  1003. {
  1004. case AL_GAIN:
  1005. *value = Audio::linearToDB((*itr)->mDescription.mVolume);
  1006. break;
  1007. case AL_GAIN_LINEAR:
  1008. *value = (*itr)->mDescription.mVolume;
  1009. break;
  1010. case AL_PITCH:
  1011. *value = (*itr)->mPitch;
  1012. break;
  1013. case AL_REFERENCE_DISTANCE:
  1014. *value = (*itr)->mDescription.mReferenceDistance;
  1015. break;
  1016. case AL_CONE_OUTER_GAIN:
  1017. *value = (*itr)->mDescription.mMaxDistance;
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. void alxLoopGetSource3f(AUDIOHANDLE handle, ALenum pname, ALfloat *value1, ALfloat *value2, ALfloat *value3)
  1023. {
  1024. LoopingList::iterator itr = mLoopingList.findImage(handle);
  1025. if(itr)
  1026. {
  1027. switch(pname)
  1028. {
  1029. case AL_POSITION:
  1030. *value1 = (*itr)->mPosition.x;
  1031. *value2 = (*itr)->mPosition.y;
  1032. *value3 = (*itr)->mPosition.z;
  1033. break;
  1034. case AL_DIRECTION:
  1035. *value1 = (*itr)->mDirection.x;
  1036. *value2 = (*itr)->mDirection.y;
  1037. *value3 = (*itr)->mDirection.z;
  1038. break;
  1039. }
  1040. }
  1041. }
  1042. void alxLoopGetSourcei(AUDIOHANDLE handle, ALenum pname, ALint *value)
  1043. {
  1044. LoopingList::iterator itr = mLoopingList.findImage(handle);
  1045. if(itr)
  1046. {
  1047. switch(pname)
  1048. {
  1049. //case AL_SOURCE_AMBIENT:
  1050. // *value = (*itr)->mDescription.mIs3D;
  1051. // break;
  1052. case AL_LOOPING:
  1053. *value = true;
  1054. break;
  1055. case AL_CONE_INNER_ANGLE:
  1056. *value = (*itr)->mDescription.mConeInsideAngle;
  1057. break;
  1058. case AL_CONE_OUTER_ANGLE:
  1059. *value = (*itr)->mDescription.mConeOutsideAngle;
  1060. break;
  1061. }
  1062. }
  1063. }
  1064. //------------------------------------------------------
  1065. void alxStreamSourcef(AUDIOHANDLE handle, ALenum pname, ALfloat value)
  1066. {
  1067. StreamingList::iterator itr = mStreamingList.findImage(handle);
  1068. if(itr)
  1069. {
  1070. switch(pname)
  1071. {
  1072. case AL_GAIN:
  1073. (*itr)->mDescription.mVolume = Audio::DBToLinear(value);
  1074. break;
  1075. case AL_GAIN_LINEAR:
  1076. (*itr)->mDescription.mVolume = value;
  1077. break;
  1078. case AL_PITCH:
  1079. (*itr)->mPitch = value;
  1080. break;
  1081. case AL_REFERENCE_DISTANCE:
  1082. (*itr)->mDescription.mReferenceDistance = value;
  1083. break;
  1084. case AL_CONE_OUTER_GAIN:
  1085. (*itr)->mDescription.mMaxDistance = value;
  1086. break;
  1087. }
  1088. }
  1089. }
  1090. void alxStreamSource3f(AUDIOHANDLE handle, ALenum pname, ALfloat value1, ALfloat value2, ALfloat value3)
  1091. {
  1092. StreamingList::iterator itr = mStreamingList.findImage(handle);
  1093. if(itr)
  1094. {
  1095. switch(pname)
  1096. {
  1097. case AL_POSITION:
  1098. (*itr)->mPosition.x = value1;
  1099. (*itr)->mPosition.y = value2;
  1100. (*itr)->mPosition.z = value3;
  1101. break;
  1102. case AL_DIRECTION:
  1103. (*itr)->mDirection.x = value1;
  1104. (*itr)->mDirection.y = value2;
  1105. (*itr)->mDirection.z = value3;
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. void alxStreamSourcei(AUDIOHANDLE handle, ALenum pname, ALint value)
  1111. {
  1112. StreamingList::iterator itr = mStreamingList.findImage(handle);
  1113. if(itr)
  1114. {
  1115. switch(pname)
  1116. {
  1117. //case AL_SOURCE_AMBIENT:
  1118. // (*itr)->mDescription.mIs3D = value;
  1119. // break;
  1120. case AL_CONE_INNER_ANGLE:
  1121. (*itr)->mDescription.mConeInsideAngle = value;
  1122. break;
  1123. case AL_CONE_OUTER_ANGLE:
  1124. (*itr)->mDescription.mConeOutsideAngle = value;
  1125. break;
  1126. }
  1127. }
  1128. }
  1129. void alxStreamGetSourcef(AUDIOHANDLE handle, ALenum pname, ALfloat *value)
  1130. {
  1131. StreamingList::iterator itr = mStreamingList.findImage(handle);
  1132. if(itr)
  1133. {
  1134. switch(pname)
  1135. {
  1136. case AL_GAIN:
  1137. *value = Audio::linearToDB((*itr)->mDescription.mVolume);
  1138. break;
  1139. case AL_GAIN_LINEAR:
  1140. *value = (*itr)->mDescription.mVolume;
  1141. break;
  1142. case AL_PITCH:
  1143. *value = (*itr)->mPitch;
  1144. break;
  1145. case AL_REFERENCE_DISTANCE:
  1146. *value = (*itr)->mDescription.mReferenceDistance;
  1147. break;
  1148. case AL_CONE_OUTER_GAIN:
  1149. *value = (*itr)->mDescription.mMaxDistance;
  1150. break;
  1151. }
  1152. }
  1153. }
  1154. void alxStreamGetSource3f(AUDIOHANDLE handle, ALenum pname, ALfloat *value1, ALfloat *value2, ALfloat *value3)
  1155. {
  1156. StreamingList::iterator itr = mStreamingList.findImage(handle);
  1157. if(itr)
  1158. {
  1159. switch(pname)
  1160. {
  1161. case AL_POSITION:
  1162. *value1 = (*itr)->mPosition.x;
  1163. *value2 = (*itr)->mPosition.y;
  1164. *value3 = (*itr)->mPosition.z;
  1165. break;
  1166. case AL_DIRECTION:
  1167. *value1 = (*itr)->mDirection.x;
  1168. *value2 = (*itr)->mDirection.y;
  1169. *value3 = (*itr)->mDirection.z;
  1170. break;
  1171. }
  1172. }
  1173. }
  1174. void alxStreamGetSourcei(AUDIOHANDLE handle, ALenum pname, ALint *value)
  1175. {
  1176. StreamingList::iterator itr = mStreamingList.findImage(handle);
  1177. if(itr)
  1178. {
  1179. switch(pname)
  1180. {
  1181. //case AL_SOURCE_AMBIENT:
  1182. // *value = (*itr)->mDescription.mIs3D;
  1183. // break;
  1184. case AL_LOOPING:
  1185. *value = true;
  1186. break;
  1187. case AL_CONE_INNER_ANGLE:
  1188. *value = (*itr)->mDescription.mConeInsideAngle;
  1189. break;
  1190. case AL_CONE_OUTER_ANGLE:
  1191. *value = (*itr)->mDescription.mConeOutsideAngle;
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. //--------------------------------------------------------------------------
  1197. // AL get/set methods: Source
  1198. //--------------------------------------------------------------------------
  1199. // - only need to worry about playing sources.. proper volume gets set on
  1200. // create source (so, could get out of sync if someone changes volume between
  1201. // a createSource and playSource call...)
  1202. void alxUpdateTypeGain(U32 type)
  1203. {
  1204. for(U32 i = 0; i < mNumSources; i++)
  1205. {
  1206. if(mHandle[i] == NULL_AUDIOHANDLE)
  1207. continue;
  1208. if(type != mType[i])
  1209. continue;
  1210. ALint state = AL_STOPPED;
  1211. alGetSourcei(mSource[i], AL_SOURCE_STATE, &state);
  1212. if(state == AL_PLAYING)
  1213. {
  1214. // volume = SourceVolume * ChannelVolume * MasterVolume
  1215. F32 vol = mClampF(mSourceVolume[i] * mAudioChannelVolumes[mType[i]] * mMasterVolume, 0.f, 1.f);
  1216. alSourcef(mSource[i], AL_GAIN, Audio::linearToDB(vol) );
  1217. }
  1218. }
  1219. }
  1220. void alxSourcef(AUDIOHANDLE handle, ALenum pname, ALfloat value)
  1221. {
  1222. ALuint source = alxFindSource(handle);
  1223. if(source != INVALID_SOURCE)
  1224. {
  1225. // ensure gain_linear
  1226. if(pname == AL_GAIN)
  1227. {
  1228. value = Audio::DBToLinear(value);
  1229. pname = AL_GAIN_LINEAR;
  1230. }
  1231. // need to process gain settings (so source can be affected by channel/master gains)
  1232. if(pname == AL_GAIN_LINEAR)
  1233. {
  1234. U32 idx = alxFindIndex(handle);
  1235. AssertFatal(idx != MAX_AUDIOSOURCES, "alxSourcef: handle not located for found source");
  1236. if(idx == MAX_AUDIOSOURCES)
  1237. return;
  1238. // update the stored value
  1239. mSourceVolume[idx] = value;
  1240. // volume = SourceVolume * ChannelVolume * MasterVolume
  1241. // #ifdef REL_WORKAROUND
  1242. // ALint val = AL_TRUE;
  1243. // alGetSourcei(source, AL_SOURCE_ABSOLUTE, &val);
  1244. // if(val == AL_FALSE)
  1245. // #else
  1246. // ALint val = AL_FALSE;
  1247. // alGetSourcei(source, AL_SOURCE_RELATIVE, &val);
  1248. // if(val == AL_TRUE)
  1249. // #endif
  1250. {
  1251. F32 vol = mClampF(mSourceVolume[idx] * mAudioChannelVolumes[mType[idx]] * mMasterVolume, 0.f, 1.f);
  1252. alSourcef(source, AL_GAIN, Audio::linearToDB(vol) );
  1253. }
  1254. }
  1255. else
  1256. alSourcef(source, pname, value);
  1257. }
  1258. alxLoopSourcef(handle, pname, value);
  1259. alxStreamSourcef(handle, pname, value);
  1260. }
  1261. void alxSourcefv(AUDIOHANDLE handle, ALenum pname, ALfloat *values)
  1262. {
  1263. ALuint source = alxFindSource(handle);
  1264. if(source != INVALID_SOURCE)
  1265. alSourcefv(source, pname, values);
  1266. if((pname == AL_POSITION) || (pname == AL_DIRECTION) || (pname == AL_VELOCITY)) {
  1267. alxLoopSource3f(handle, pname, values[0], values[1], values[2]);
  1268. alxStreamSource3f(handle, pname, values[0], values[1], values[2]);
  1269. }
  1270. }
  1271. void alxSource3f(AUDIOHANDLE handle, ALenum pname, ALfloat value1, ALfloat value2, ALfloat value3)
  1272. {
  1273. ALuint source = alxFindSource(handle);
  1274. if(source != INVALID_SOURCE)
  1275. {
  1276. ALfloat values[3];
  1277. values[0] = value1;
  1278. values[1] = value2;
  1279. values[2] = value3;
  1280. alSourcefv(source, pname, values);
  1281. }
  1282. alxLoopSource3f(handle, pname, value1, value2, value3);
  1283. alxStreamSource3f(handle, pname, value1, value2, value3);
  1284. }
  1285. void alxSourcei(AUDIOHANDLE handle, ALenum pname, ALint value)
  1286. {
  1287. ALuint source = alxFindSource(handle);
  1288. if(source != INVALID_SOURCE)
  1289. alSourcei(source, pname, value);
  1290. alxLoopSourcei(handle, pname, value);
  1291. alxStreamSourcei(handle, pname, value);
  1292. }
  1293. // sets the position and direction of the source
  1294. void alxSourceMatrixF(AUDIOHANDLE handle, const MatrixF *transform)
  1295. {
  1296. ALuint source = alxFindSource(handle);
  1297. Point3F pos;
  1298. transform->getColumn(3, &pos);
  1299. Point3F dir;
  1300. transform->getColumn(1, &dir);
  1301. if(source != INVALID_SOURCE)
  1302. {
  1303. // OpenAL uses a Right-Handed corrdinate system so flip the orientation vector
  1304. alSource3f(source, AL_POSITION, pos.x, pos.y, pos.z);
  1305. alSource3f(source, AL_DIRECTION, -dir.x, -dir.y, -dir.z);
  1306. }
  1307. alxLoopSource3f(handle, AL_POSITION, pos.x, pos.y, pos.z);
  1308. alxLoopSource3f(handle, AL_DIRECTION, dir.x, dir.y, dir.z);
  1309. alxStreamSource3f(handle, AL_POSITION, pos.x, pos.y, pos.z);
  1310. alxStreamSource3f(handle, AL_DIRECTION, dir.x, dir.y, dir.z);
  1311. }
  1312. //--------------------------------------------------------------------------
  1313. void alxGetSourcef(AUDIOHANDLE handle, ALenum pname, ALfloat *value)
  1314. {
  1315. ALuint source = alxFindSource(handle);
  1316. if(source != INVALID_SOURCE)
  1317. {
  1318. // gain queries return unattenuated values
  1319. if((pname == AL_GAIN) || (pname == AL_GAIN_LINEAR))
  1320. {
  1321. U32 idx = alxFindIndex(handle);
  1322. AssertFatal(idx != MAX_AUDIOSOURCES, "alxGetSourcef: found source but handle is invalid");
  1323. if(idx == MAX_AUDIOSOURCES)
  1324. {
  1325. *value = 0.f;
  1326. return;
  1327. }
  1328. if(pname == AL_GAIN)
  1329. *value = Audio::linearToDB(mSourceVolume[idx]);
  1330. else
  1331. *value = mSourceVolume[idx];
  1332. }
  1333. else
  1334. alGetSourcef(source, pname, value);
  1335. }
  1336. else if(handle & AUDIOHANDLE_LOOPING_BIT)
  1337. alxLoopGetSourcef(handle, pname, value);
  1338. else
  1339. alxStreamGetSourcef(handle, pname, value);
  1340. }
  1341. void alxGetSourcefv(AUDIOHANDLE handle, ALenum pname, ALfloat *values)
  1342. {
  1343. if((pname == AL_POSITION) || (pname == AL_DIRECTION) || (pname == AL_VELOCITY))
  1344. alxGetSource3f(handle, pname, &values[0], &values[1], &values[2]);
  1345. }
  1346. void alxGetSource3f(AUDIOHANDLE handle, ALenum pname, ALfloat *value1, ALfloat *value2, ALfloat *value3)
  1347. {
  1348. ALuint source = alxFindSource(handle);
  1349. if(source != INVALID_SOURCE)
  1350. {
  1351. ALfloat values[3];
  1352. alGetSourcefv(source, pname, values);
  1353. *value1 = values[0];
  1354. *value2 = values[1];
  1355. *value3 = values[2];
  1356. }
  1357. else if(handle & AUDIOHANDLE_LOOPING_BIT)
  1358. alxLoopGetSource3f(handle, pname, value1, value2, value3);
  1359. else
  1360. alxStreamGetSource3f(handle, pname, value1, value2, value3);
  1361. }
  1362. void alxGetSourcei(AUDIOHANDLE handle, ALenum pname, ALint *value)
  1363. {
  1364. ALuint source = alxFindSource(handle);
  1365. if(source != INVALID_SOURCE)
  1366. alGetSourcei(source, pname, value);
  1367. else if(handle & AUDIOHANDLE_LOOPING_BIT)
  1368. alxLoopGetSourcei(handle, pname, value);
  1369. else
  1370. alxStreamGetSourcei(handle, pname, value);
  1371. }
  1372. //--------------------------------------------------------------------------
  1373. /** alListenerfv extension for use with MatrixF's
  1374. Set the listener's position and orientation using a matrix
  1375. */
  1376. void alxListenerMatrixF(const MatrixF *transform)
  1377. {
  1378. Point3F p1, p2;
  1379. transform->getColumn(3, &p1);
  1380. alListener3f(AL_POSITION, p1.x, p1.y, p1.z);
  1381. transform->getColumn(2, &p1); // Up Vector
  1382. transform->getColumn(1, &p2); // Forward Vector
  1383. F32 orientation[6];
  1384. orientation[0] = -p1.x;
  1385. orientation[1] = -p1.y;
  1386. orientation[2] = -p1.z;
  1387. orientation[3] = p2.x;
  1388. orientation[4] = p2.y;
  1389. orientation[5] = p2.z;
  1390. alListenerfv(AL_ORIENTATION, orientation);
  1391. }
  1392. //--------------------------------------------------------------------------
  1393. /** alListenerf extension supporting linear gain
  1394. */
  1395. void alxListenerf(ALenum param, ALfloat value)
  1396. {
  1397. if (param == AL_GAIN_LINEAR)
  1398. {
  1399. value = Audio::linearToDB(value);
  1400. param = AL_GAIN;
  1401. }
  1402. alListenerf(param, value);
  1403. }
  1404. //--------------------------------------------------------------------------
  1405. /** alGetListenerf extension supporting linear gain
  1406. */
  1407. void alxGetListenerf(ALenum param, ALfloat *value)
  1408. {
  1409. if (param == AL_GAIN_LINEAR)
  1410. {
  1411. alGetListenerf(AL_GAIN, value);
  1412. *value = Audio::DBToLinear(*value);
  1413. }
  1414. else
  1415. alGetListenerf(param, value);
  1416. }
  1417. //--------------------------------------------------------------------------
  1418. // Simple metrics
  1419. //--------------------------------------------------------------------------
  1420. #ifdef TORQUE_GATHER_METRICS
  1421. static void alxGatherMetrics()
  1422. {
  1423. S32 mNumOpenHandles = 0;
  1424. S32 mNumOpenLoopingHandles = 0;
  1425. S32 mNumOpenStreamingHandles = 0;
  1426. S32 mNumActiveStreams = 0;
  1427. S32 mNumNullActiveStreams = 0;
  1428. S32 mNumActiveLoopingStreams = 0;
  1429. S32 mNumActiveStreamingStreams = 0;
  1430. S32 mNumLoopingStreams = 0;
  1431. S32 mNumInactiveLoopingStreams = 0;
  1432. S32 mNumCulledLoopingStreams = 0;
  1433. S32 mNumStreamingStreams = 0;
  1434. S32 mNumInactiveStreamingStreams = 0;
  1435. S32 mNumCulledStreamingStreams = 0;
  1436. // count installed streams and open handles
  1437. for(U32 i = 0; i < mNumSources; i++)
  1438. {
  1439. if(mHandle[i] != NULL_AUDIOHANDLE)
  1440. {
  1441. mNumOpenHandles++;
  1442. if(mHandle[i] & AUDIOHANDLE_LOOPING_BIT)
  1443. mNumOpenLoopingHandles++;
  1444. if(mHandle[i] & AUDIOHANDLE_STREAMING_BIT)
  1445. mNumOpenStreamingHandles++;
  1446. }
  1447. ALint state = AL_STOPPED;
  1448. alGetSourcei(mSource[i], AL_SOURCE_STATE, &state);
  1449. if(state == AL_PLAYING)
  1450. {
  1451. mNumActiveStreams++;
  1452. if(mHandle[i] == NULL_AUDIOHANDLE)
  1453. mNumNullActiveStreams++;
  1454. if(mHandle[i] & AUDIOHANDLE_LOOPING_BIT)
  1455. mNumActiveLoopingStreams++;
  1456. if(mHandle[i] & AUDIOHANDLE_STREAMING_BIT)
  1457. mNumActiveStreamingStreams++;
  1458. }
  1459. }
  1460. for(LoopingList::iterator itr = mLoopingList.begin(); itr != mLoopingList.end(); itr++)
  1461. mNumLoopingStreams++;
  1462. for(LoopingList::iterator itr = mLoopingInactiveList.begin(); itr != mLoopingInactiveList.end(); itr++)
  1463. mNumInactiveLoopingStreams++;
  1464. for(LoopingList::iterator itr = mLoopingCulledList.begin(); itr != mLoopingCulledList.end(); itr++)
  1465. mNumCulledLoopingStreams++;
  1466. for(StreamingList::iterator itr = mStreamingList.begin(); itr != mStreamingList.end(); itr++)
  1467. mNumStreamingStreams++;
  1468. for(StreamingList::iterator itr = mStreamingInactiveList.begin(); itr != mStreamingInactiveList.end(); itr++)
  1469. mNumInactiveStreamingStreams++;
  1470. for(StreamingList::iterator itr = mStreamingCulledList.begin(); itr != mStreamingCulledList.end(); itr++)
  1471. mNumCulledStreamingStreams++;
  1472. Con::setIntVariable("Audio::numOpenHandles", mNumOpenHandles);
  1473. Con::setIntVariable("Audio::numOpenLoopingHandles", mNumOpenLoopingHandles);
  1474. Con::setIntVariable("Audio::numOpenStreamingHandles", mNumOpenStreamingHandles);
  1475. Con::setIntVariable("Audio::numActiveStreams", mNumActiveStreams);
  1476. Con::setIntVariable("Audio::numNullActiveStreams", mNumNullActiveStreams);
  1477. Con::setIntVariable("Audio::numActiveLoopingStreams", mNumActiveLoopingStreams);
  1478. Con::setIntVariable("Audio::numActiveStreamingStreams", mNumActiveStreamingStreams);
  1479. Con::setIntVariable("Audio::numLoopingStreams", mNumLoopingStreams);
  1480. Con::setIntVariable("Audio::numInactiveLoopingStreams", mNumInactiveLoopingStreams);
  1481. Con::setIntVariable("Audio::numCulledLoopingStreams", mNumCulledLoopingStreams);
  1482. Con::setIntVariable("Audio::numStreamingStreams", mNumStreamingStreams);
  1483. Con::setIntVariable("Audio::numInactiveStreamingStreams", mNumInactiveStreamingStreams);
  1484. Con::setIntVariable("Audio::numCulledStreamingStreams", mNumCulledStreamingStreams);
  1485. }
  1486. #endif
  1487. //--------------------------------------------------------------------------
  1488. // Audio Update...
  1489. //--------------------------------------------------------------------------
  1490. void alxLoopingUpdate()
  1491. {
  1492. static LoopingList culledList;
  1493. U32 updateTime = Platform::getRealMilliseconds();
  1494. // check if can wakeup the inactive loopers
  1495. if(mLoopingCulledList.size())
  1496. {
  1497. Point3F listener;
  1498. alxGetListenerPoint3F(AL_POSITION, &listener);
  1499. // get the 'sort' value for this sound (could be based on time played...),
  1500. // and add to the culled list
  1501. LoopingList::iterator itr;
  1502. culledList.clear();
  1503. for(itr = mLoopingCulledList.begin(); itr != mLoopingCulledList.end(); itr++)
  1504. {
  1505. if((*itr)->mScore <= MIN_UNCULL_GAIN)
  1506. continue;
  1507. if((updateTime - (*itr)->mCullTime) < MIN_UNCULL_PERIOD)
  1508. continue;
  1509. culledList.push_back(*itr);
  1510. }
  1511. if(!culledList.size())
  1512. return;
  1513. U32 index = MAX_AUDIOSOURCES;
  1514. if(culledList.size() > 1)
  1515. culledList.sort();
  1516. for(itr = culledList.begin(); itr != culledList.end(); itr++)
  1517. {
  1518. if(!findFreeSource(&index))
  1519. {
  1520. // score does not include master volume
  1521. if(!cullSource(&index, (*itr)->mScore))
  1522. break;
  1523. // check buffer
  1524. if(!bool((*itr)->mBuffer))
  1525. {
  1526. // remove from culled list
  1527. LoopingList::iterator tmp;
  1528. tmp = mLoopingCulledList.findImage((*itr)->mHandle);
  1529. AssertFatal(tmp, "alxLoopingUpdate: failed to find culled source");
  1530. mLoopingCulledList.erase_fast(tmp);
  1531. // remove from looping list (and free)
  1532. tmp = mLoopingList.findImage((*itr)->mHandle);
  1533. if(tmp)
  1534. {
  1535. (*tmp)->clear();
  1536. mLoopingFreeList.push_back(*tmp);
  1537. mLoopingList.erase_fast(tmp);
  1538. }
  1539. continue;
  1540. }
  1541. }
  1542. // remove from culled list
  1543. LoopingList::iterator tmp = mLoopingCulledList.findImage((*itr)->mHandle);
  1544. AssertFatal(tmp, "alxLoopingUpdate: failed to find culled source");
  1545. mLoopingCulledList.erase_fast(tmp);
  1546. // restore all state data
  1547. mHandle[index] = (*itr)->mHandle;
  1548. mBuffer[index] = (*itr)->mBuffer;
  1549. mScore[index] = (*itr)->mScore;
  1550. mSourceVolume[index] = (*itr)->mDescription.mVolume;
  1551. mType[index] = (*itr)->mDescription.mVolumeChannel;
  1552. mSampleEnvironment[index] = (*itr)->mEnvironment;
  1553. ALuint source = mSource[index];
  1554. // setup play info
  1555. alGetError();
  1556. alxSourcePlay(source, *itr);
  1557. if(mEnvironmentEnabled)
  1558. alxSourceEnvironment(source, *itr);
  1559. alxPlay(mHandle[index]);
  1560. }
  1561. }
  1562. }
  1563. void alxStreamingUpdate()
  1564. {
  1565. // update buffer queues on active streamers
  1566. // update the loopers
  1567. for(StreamingList::iterator itr = mStreamingList.begin(); itr != mStreamingList.end(); itr++)
  1568. {
  1569. if((*itr)->mHandle & AUDIOHANDLE_INACTIVE_BIT)
  1570. continue;
  1571. (*itr)->updateBuffers();
  1572. }
  1573. static StreamingList culledList;
  1574. U32 updateTime = Platform::getRealMilliseconds();
  1575. // check if can wakeup the inactive loopers
  1576. if(mStreamingCulledList.size())
  1577. {
  1578. Point3F listener;
  1579. alxGetListenerPoint3F(AL_POSITION, &listener);
  1580. // get the 'sort' value for this sound (could be based on time played...),
  1581. // and add to the culled list
  1582. StreamingList::iterator itr;
  1583. culledList.clear();
  1584. for(itr = mStreamingCulledList.begin(); itr != mStreamingCulledList.end(); itr++)
  1585. {
  1586. if((*itr)->mScore <= MIN_UNCULL_GAIN)
  1587. continue;
  1588. if((updateTime - (*itr)->mCullTime) < MIN_UNCULL_PERIOD)
  1589. continue;
  1590. culledList.push_back(*itr);
  1591. }
  1592. if(!culledList.size())
  1593. return;
  1594. U32 index = MAX_AUDIOSOURCES;
  1595. if(culledList.size() > 1)
  1596. culledList.sort();
  1597. for(itr = culledList.begin(); itr != culledList.end(); itr++)
  1598. {
  1599. if(!findFreeSource(&index))
  1600. {
  1601. // score does not include master volume
  1602. if(!cullSource(&index, (*itr)->mScore))
  1603. break;
  1604. // check buffer
  1605. //if(!bool((*itr)->mBuffer))
  1606. //{
  1607. // remove from culled list
  1608. StreamingList::iterator tmp;
  1609. tmp = mStreamingCulledList.findImage((*itr)->mHandle);
  1610. AssertFatal(tmp, "alxStreamingUpdate: failed to find culled source");
  1611. mStreamingCulledList.erase_fast(tmp);
  1612. // remove from streaming list (and free)
  1613. tmp = mStreamingList.findImage((*itr)->mHandle);
  1614. if(tmp)
  1615. {
  1616. delete(*tmp);
  1617. mStreamingList.erase_fast(tmp);
  1618. }
  1619. continue;
  1620. //}
  1621. }
  1622. // remove from culled list
  1623. StreamingList::iterator tmp = mStreamingCulledList.findImage((*itr)->mHandle);
  1624. AssertFatal(tmp, "alxStreamingUpdate: failed to find culled source");
  1625. mStreamingCulledList.erase_fast(tmp);
  1626. alxSourcePlay(*itr);
  1627. // restore all state data
  1628. mHandle[index] = (*itr)->mHandle;
  1629. mScore[index] = (*itr)->mScore;
  1630. mSourceVolume[index] = (*itr)->mDescription.mVolume;
  1631. mType[index] = (*itr)->mDescription.mVolumeChannel;
  1632. mSampleEnvironment[index] = (*itr)->mEnvironment;
  1633. ALuint source = mSource[index];
  1634. (*itr)->mSource = mSource[index];
  1635. // setup play info
  1636. alGetError();
  1637. if(mEnvironmentEnabled)
  1638. alxSourceEnvironment(source, *itr);
  1639. alxPlay(mHandle[index]);
  1640. }
  1641. }
  1642. }
  1643. //--------------------------------------------------------------------------
  1644. void alxCloseHandles()
  1645. {
  1646. for(U32 i = 0; i < mNumSources; i++)
  1647. {
  1648. if(mHandle[i] & AUDIOHANDLE_LOADING_BIT)
  1649. continue;
  1650. if(mHandle[i] == NULL_AUDIOHANDLE)
  1651. continue;
  1652. ALint state = 0;
  1653. alGetSourcei(mSource[i], AL_SOURCE_STATE, &state);
  1654. if(state == AL_PLAYING)
  1655. continue;
  1656. if(!(mHandle[i] & AUDIOHANDLE_INACTIVE_BIT))
  1657. {
  1658. // should be playing? must have encounted an error.. remove
  1659. LoopingList::iterator itr = mLoopingList.findImage(mHandle[i]);
  1660. if(itr && !((*itr)->mHandle & AUDIOHANDLE_INACTIVE_BIT))
  1661. {
  1662. AssertFatal(!mLoopingInactiveList.findImage((*itr)->mHandle), "alxCloseHandles: image incorrectly in inactive list");
  1663. AssertFatal(!mLoopingCulledList.findImage((*itr)->mHandle), "alxCloseHandles: image already in culled list");
  1664. mLoopingCulledList.push_back(*itr);
  1665. (*itr)->mHandle |= AUDIOHANDLE_INACTIVE_BIT;
  1666. mHandle[i] = NULL_AUDIOHANDLE;
  1667. mBuffer[i] = 0;
  1668. }
  1669. // should be playing? must have encounted an error.. remove
  1670. // StreamingList::iterator itr2 = mStreamingList.findImage(mHandle[i]);
  1671. // if(itr2 && !((*itr2)->mHandle & AUDIOHANDLE_INACTIVE_BIT))
  1672. // {
  1673. // AssertFatal(!mStreamingInactiveList.findImage((*itr2)->mHandle), "alxCloseHandles: image incorrectly in inactive list");
  1674. // AssertFatal(!mStreamingCulledList.findImage((*itr2)->mHandle), "alxCloseHandles: image already in culled list");
  1675. // (*itr2)->freeStream();
  1676. //
  1677. // mStreamingCulledList.push_back(*itr2);
  1678. // (*itr2)->mHandle |= AUDIOHANDLE_INACTIVE_BIT;
  1679. //
  1680. // mHandle[i] = NULL_AUDIOHANDLE;
  1681. // mBuffer[i] = 0;
  1682. // }
  1683. }
  1684. alSourcei(mSource[i], AL_BUFFER, AL_NONE);
  1685. mHandle[i] = NULL_AUDIOHANDLE;
  1686. mBuffer[i] = 0;
  1687. }
  1688. }
  1689. //----------------------------------------------------------------------------------
  1690. // - update the score for each audio source. this is used for culing sources.
  1691. // normal ranges are between 0.f->1.f, voice/loading/music streams are scored
  1692. // outside this range so that they will not be culled
  1693. // - does not scale by attenuated volumes
  1694. void alxUpdateScores(bool sourcesOnly)
  1695. {
  1696. Point3F listener;
  1697. alGetListener3f(AL_POSITION, &listener.x, &listener.y, &listener.z);
  1698. // do the base sources
  1699. for(U32 i = 0; i < mNumSources; i++)
  1700. {
  1701. if(mHandle[i] == NULL_AUDIOHANDLE)
  1702. {
  1703. mScore[i] = 0.f;
  1704. continue;
  1705. }
  1706. // grab the volume.. (not attenuated by master for score)
  1707. F32 volume = mSourceVolume[i] * mAudioChannelVolumes[mType[i]];
  1708. // 3d?
  1709. mScore[i] = volume;
  1710. #ifdef REL_WORKAROUND
  1711. ALint val = AL_FALSE;
  1712. alGetSourcei(mSource[i], AL_SOURCE_ABSOLUTE, &val);
  1713. if(val == AL_TRUE)
  1714. #else
  1715. ALint val = AL_FALSE;
  1716. alGetSourcei(mSource[i], AL_SOURCE_RELATIVE, &val);
  1717. if(val == AL_FALSE)
  1718. #endif
  1719. {
  1720. // approximate 3d volume
  1721. Point3F pos;
  1722. alGetSourcefv(mSource[i], AL_POSITION, (ALfloat*)((F32*)pos) );
  1723. ALfloat min=0, max=1;
  1724. alGetSourcef(mSource[i], AL_REFERENCE_DISTANCE, &min);
  1725. alGetSourcef(mSource[i], AL_MAX_DISTANCE, &max);
  1726. pos -= listener;
  1727. F32 dist = pos.magnitudeSafe();
  1728. if(dist >= max)
  1729. mScore[i] = 0.f;
  1730. else if(dist > min)
  1731. mScore[i] *= (max-dist) / (max-min);
  1732. }
  1733. }
  1734. if(sourcesOnly)
  1735. return;
  1736. U32 updateTime = Platform::getRealMilliseconds();
  1737. // update the loopers
  1738. for(LoopingList::iterator itr = mLoopingList.begin(); itr != mLoopingList.end(); itr++)
  1739. {
  1740. if(!((*itr)->mHandle & AUDIOHANDLE_INACTIVE_BIT))
  1741. continue;
  1742. if((updateTime - (*itr)->mCullTime) < MIN_UNCULL_PERIOD)
  1743. continue;
  1744. (*itr)->mScore = (*itr)->mDescription.mVolume;
  1745. if((*itr)->mDescription.mIs3D)
  1746. {
  1747. Point3F pos = (*itr)->mPosition - listener;
  1748. F32 dist = pos.magnitudeSafe();
  1749. F32 min = (*itr)->mDescription.mReferenceDistance;
  1750. F32 max = (*itr)->mDescription.mMaxDistance;
  1751. if(dist >= max)
  1752. (*itr)->mScore = 0.f;
  1753. else if(dist > min)
  1754. (*itr)->mScore *= (max-dist) / (max-min);
  1755. }
  1756. // attenuate by the channel gain
  1757. (*itr)->mScore *= mAudioChannelVolumes[(*itr)->mDescription.mVolumeChannel];
  1758. }
  1759. // update the streamers
  1760. for(StreamingList::iterator itr = mStreamingList.begin(); itr != mStreamingList.end(); itr++)
  1761. {
  1762. if(!((*itr)->mHandle & AUDIOHANDLE_INACTIVE_BIT))
  1763. continue;
  1764. if((updateTime - (*itr)->mCullTime) < MIN_UNCULL_PERIOD)
  1765. continue;
  1766. (*itr)->mScore = (*itr)->mDescription.mVolume;
  1767. if((*itr)->mDescription.mIs3D)
  1768. {
  1769. Point3F pos = (*itr)->mPosition - listener;
  1770. F32 dist = pos.magnitudeSafe();
  1771. F32 min = (*itr)->mDescription.mReferenceDistance;
  1772. F32 max = (*itr)->mDescription.mMaxDistance;
  1773. if(dist >= max)
  1774. (*itr)->mScore = 0.f;
  1775. else if(dist > min)
  1776. (*itr)->mScore *= (max-dist) / (max-min);
  1777. }
  1778. // attenuate by the channel gain
  1779. (*itr)->mScore *= mAudioChannelVolumes[(*itr)->mDescription.mVolumeChannel];
  1780. }
  1781. }
  1782. // the directx buffers are set to mute at max distance, but many of the providers seem to
  1783. // ignore this flag... that is why this is here
  1784. void alxUpdateMaxDistance()
  1785. {
  1786. Point3F listener;
  1787. alGetListener3f(AL_POSITION, &listener.x, &listener.y, &listener.z);
  1788. for(U32 i = 0; i < mNumSources; i++)
  1789. {
  1790. if(mHandle[i] == NULL_AUDIOHANDLE)
  1791. continue;
  1792. #ifdef REL_WORKAROUND
  1793. ALint val = AL_FALSE;
  1794. alGetSourcei(mSource[i], AL_SOURCE_ABSOLUTE, &val);
  1795. if(val == AL_FALSE)
  1796. #else
  1797. ALint val = AL_FALSE;
  1798. alGetSourcei(mSource[i], AL_SOURCE_RELATIVE, &val);
  1799. if(val == AL_TRUE)
  1800. #endif
  1801. continue;
  1802. Point3F pos;
  1803. alGetSourcefv(mSource[i], AL_POSITION, (F32*)pos);
  1804. F32 dist = 0.f;
  1805. alGetSourcef(mSource[i], AL_MAX_DISTANCE, &dist);
  1806. pos -= listener;
  1807. dist -= pos.len();
  1808. F32 gain = (dist < 0.f) ? 0.f : mSourceVolume[i] * mAudioChannelVolumes[mType[i]] * mMasterVolume;
  1809. alSourcef(mSource[i], AL_GAIN, Audio::linearToDB(gain));
  1810. }
  1811. }
  1812. //--------------------------------------------------------------------------
  1813. // Called to update alx system
  1814. //--------------------------------------------------------------------------
  1815. void alxUpdate()
  1816. {
  1817. //if(mForceMaxDistanceUpdate)
  1818. alxUpdateMaxDistance();
  1819. alxCloseHandles();
  1820. alxUpdateScores(false);
  1821. alxLoopingUpdate();
  1822. alxStreamingUpdate();
  1823. #ifdef TORQUE_GATHER_METRICS
  1824. alxGatherMetrics();
  1825. #endif
  1826. }
  1827. //--------------------------------------------------------------------------
  1828. // Misc
  1829. //--------------------------------------------------------------------------
  1830. // client-side function only
  1831. ALuint alxGetWaveLen(ALuint buffer)
  1832. {
  1833. if(buffer == AL_INVALID)
  1834. return(0);
  1835. ALint frequency = 0;
  1836. ALint bits = 0;
  1837. ALint channels = 0;
  1838. ALint size;
  1839. alGetBufferi(buffer, AL_FREQUENCY, &frequency);
  1840. alGetBufferi(buffer, AL_BITS, &bits);
  1841. alGetBufferi(buffer, AL_CHANNELS, &channels);
  1842. alGetBufferi(buffer, AL_SIZE, &size);
  1843. if(!frequency || !bits || !channels)
  1844. {
  1845. Con::errorf(ConsoleLogEntry::General, "alxGetWaveLen: invalid buffer");
  1846. return(0);
  1847. }
  1848. ALuint len = (ALuint)((F64(size) * 8000.f) / F64(frequency * bits * channels));
  1849. return(len);
  1850. }
  1851. //--------------------------------------------------------------------------
  1852. // Environment:
  1853. //--------------------------------------------------------------------------
  1854. void alxEnvironmenti(ALenum pname, ALint value)
  1855. {
  1856. /* todo
  1857. alEnvironmentiIASIG(mEnvironment, pname, value);
  1858. */
  1859. }
  1860. void alxEnvironmentf(ALenum pname, ALfloat value)
  1861. {
  1862. /* todo
  1863. alEnvironmentfIASIG(mEnvironment, pname, value);
  1864. */
  1865. }
  1866. void alxGetEnvironmenti(ALenum pname, ALint * value)
  1867. {
  1868. /* todo
  1869. alGetEnvironmentiIASIG_EXT(mEnvironment, pname, value);
  1870. */
  1871. }
  1872. void alxGetEnvironmentf(ALenum pname, ALfloat * value)
  1873. {
  1874. /* todo
  1875. alGetEnvironmentfIASIG_EXT(mEnvironment, pname, value);
  1876. */
  1877. }
  1878. void alxEnableEnvironmental(bool enable)
  1879. {
  1880. if(mEnvironmentEnabled == enable)
  1881. return;
  1882. // go through the playing sources and update their reverb mix
  1883. // - only 3d samples get environmental fx
  1884. // - only loopers can reenable fx
  1885. for(U32 i = 0; i < mNumSources; i++)
  1886. {
  1887. if(mHandle[i] == NULL_AUDIOHANDLE)
  1888. continue;
  1889. ALint val = AL_FALSE;
  1890. // 3d?
  1891. #ifdef REL_WORKAROUND
  1892. alGetSourcei(mSource[i], AL_SOURCE_ABSOLUTE, &val);
  1893. if(val == AL_FALSE)
  1894. #else
  1895. alGetSourcei(mSource[i], AL_SOURCE_RELATIVE, &val);
  1896. if(val == AL_TRUE)
  1897. #endif
  1898. continue;
  1899. // stopped?
  1900. val = AL_STOPPED;
  1901. alGetSourcei(mSource[i], AL_SOURCE_STATE, &val);
  1902. // only looping sources can reenable environmental effects (no description around
  1903. // for the non-loopers)
  1904. if(enable)
  1905. {
  1906. LoopingList::iterator itr = mLoopingList.findImage(mHandle[i]);
  1907. if(!itr)
  1908. continue;
  1909. /* todo
  1910. alSourcef(mSource[i], AL_ENV_SAMPLE_REVERB_MIX_EXT, (*itr)->mDescription.mEnvironmentLevel);
  1911. */
  1912. }
  1913. /* todo
  1914. else
  1915. alSourcef(mSource[i], AL_ENV_SAMPLE_REVERB_MIX_EXT, 0.f);
  1916. */
  1917. }
  1918. mEnvironmentEnabled = enable;
  1919. }
  1920. void alxSetEnvironment(const AudioEnvironment * env)
  1921. {
  1922. /* todo
  1923. mCurrentEnvironment = const_cast<AudioEnvironment*>(env);
  1924. // reset environmental audio?
  1925. if(!env)
  1926. {
  1927. alxEnvironmenti(AL_ENV_ROOM_IASIG, AL_ENVIRONMENT_GENERIC);
  1928. return;
  1929. }
  1930. // room trashes all the values
  1931. if(env->mUseRoom)
  1932. {
  1933. alxEnvironmenti(AL_ENV_ROOM_IASIG, env->mRoom);
  1934. return;
  1935. }
  1936. // set all the params
  1937. alxEnvironmenti(AL_ENV_ROOM_HIGH_FREQUENCY_IASIG, env->mRoomHF);
  1938. alxEnvironmenti(AL_ENV_REFLECTIONS_IASIG, env->mReflections);
  1939. alxEnvironmenti(AL_ENV_REVERB_IASIG, env->mReverb);
  1940. alxEnvironmentf(AL_ENV_ROOM_ROLLOFF_FACTOR_IASIG, env->mRoomRolloffFactor);
  1941. alxEnvironmentf(AL_ENV_DECAY_TIME_IASIG, env->mDecayTime);
  1942. alxEnvironmentf(AL_ENV_DECAY_HIGH_FREQUENCY_RATIO_IASIG, env->mDecayTime);
  1943. alxEnvironmentf(AL_ENV_REFLECTIONS_DELAY_IASIG, env->mReflectionsDelay);
  1944. alxEnvironmentf(AL_ENV_REVERB_DELAY_IASIG, env->mReverbDelay);
  1945. alxEnvironmentf(AL_ENV_DENSITY_IASIG, env->mAirAbsorption);
  1946. alxEnvironmentf(AL_ENV_DIFFUSION_IASIG, env->mEnvironmentDiffusion);
  1947. // ext:
  1948. alxEnvironmenti(AL_ENV_ROOM_VOLUME_EXT, env->mRoomVolume);
  1949. alxEnvironmenti(AL_ENV_FLAGS_EXT, env->mFlags);
  1950. alxEnvironmentf(AL_ENV_EFFECT_VOLUME_EXT, env->mEffectVolume);
  1951. alxEnvironmentf(AL_ENV_DAMPING_EXT, env->mDamping);
  1952. alxEnvironmentf(AL_ENV_ENVIRONMENT_SIZE_EXT, env->mEnvironmentSize);
  1953. */
  1954. }
  1955. const AudioEnvironment * alxGetEnvironment()
  1956. {
  1957. return(mCurrentEnvironment);
  1958. }
  1959. F32 alxGetStreamPosition( AUDIOHANDLE handle )
  1960. {
  1961. StreamingList::iterator itr = mStreamingList.findImage(handle);
  1962. if( !itr )
  1963. return -1.f;
  1964. return (*itr)->getElapsedTime();
  1965. }
  1966. F32 alxGetStreamDuration( AUDIOHANDLE handle )
  1967. {
  1968. StreamingList::iterator itr = mStreamingList.findImage(handle);
  1969. if( !itr )
  1970. return -1.f;
  1971. return (*itr)->getTotalTime();
  1972. }
  1973. // Namespace: Audio ---------------------------------------------------------
  1974. namespace Audio
  1975. {
  1976. //---------------------------------------------------------------------------
  1977. // the following db<->linear conversion functions come from Loki openAL linux driver
  1978. // code, here more for completeness than anything else (all current audio code
  1979. // uses AL_GAIN_LINEAR)... in Audio:: so that looping updates and audio channel updates
  1980. // can convert gain types and to give the miles driver access
  1981. static const F32 logtab[] = {
  1982. 0.00f, 0.001f, 0.002f, 0.003f, 0.004f,
  1983. 0.005f, 0.01f, 0.011f, 0.012f, 0.013f,
  1984. 0.014f, 0.015f, 0.016f, 0.02f, 0.021f,
  1985. 0.022f, 0.023f, 0.024f, 0.025f, 0.03f,
  1986. 0.031f, 0.032f, 0.033f, 0.034f, 0.04f,
  1987. 0.041f, 0.042f, 0.043f, 0.044f, 0.05f,
  1988. 0.051f, 0.052f, 0.053f, 0.054f, 0.06f,
  1989. 0.061f, 0.062f, 0.063f, 0.064f, 0.07f,
  1990. 0.071f, 0.072f, 0.073f, 0.08f, 0.081f,
  1991. 0.082f, 0.083f, 0.084f, 0.09f, 0.091f,
  1992. 0.092f, 0.093f, 0.094f, 0.10f, 0.101f,
  1993. 0.102f, 0.103f, 0.11f, 0.111f, 0.112f,
  1994. 0.113f, 0.12f, 0.121f, 0.122f, 0.123f,
  1995. 0.124f, 0.13f, 0.131f, 0.132f, 0.14f,
  1996. 0.141f, 0.142f, 0.143f, 0.15f, 0.151f,
  1997. 0.152f, 0.16f, 0.161f, 0.162f, 0.17f,
  1998. 0.171f, 0.172f, 0.18f, 0.181f, 0.19f,
  1999. 0.191f, 0.192f, 0.20f, 0.201f, 0.21f,
  2000. 0.211f, 0.22f, 0.221f, 0.23f, 0.231f,
  2001. 0.24f, 0.25f, 0.251f, 0.26f, 0.27f,
  2002. 0.271f, 0.28f, 0.29f, 0.30f, 0.301f,
  2003. 0.31f, 0.32f, 0.33f, 0.34f, 0.35f,
  2004. 0.36f, 0.37f, 0.38f, 0.39f, 0.40f,
  2005. 0.41f, 0.43f, 0.50f, 0.60f, 0.65f,
  2006. 0.70f, 0.75f, 0.80f, 0.85f, 0.90f,
  2007. 0.95f, 0.97f, 0.99f };
  2008. const int logmax = sizeof logtab / sizeof *logtab;
  2009. F32 DBToLinear(F32 value)
  2010. {
  2011. if(value <= 0.f)
  2012. return(0.f);
  2013. if(value >= 1.f)
  2014. return(1.f);
  2015. S32 max = logmax;
  2016. S32 min = 0;
  2017. S32 mid;
  2018. S32 last = -1;
  2019. mid = (max - min) / 2;
  2020. do {
  2021. last = mid;
  2022. if(logtab[mid] == value)
  2023. break;
  2024. if(logtab[mid] < value)
  2025. min = mid;
  2026. else
  2027. max = mid;
  2028. mid = min + ((max - min) / 2);
  2029. } while(last != mid);
  2030. return((F32)mid / logmax);
  2031. }
  2032. F32 linearToDB(F32 value)
  2033. {
  2034. if(value <= 0.f)
  2035. return(0.f);
  2036. if(value >= 1.f)
  2037. return(1.f);
  2038. return(logtab[(U32)(logmax * value)]);
  2039. }
  2040. //---------------------------------------------------------------------------
  2041. static ALvoid errorCallback(ALbyte *msg)
  2042. {
  2043. // used to allow our OpenAL implementation to display info on the console
  2044. Con::errorf(ConsoleLogEntry::General, (const char *)msg);
  2045. }
  2046. void shutdownContext()
  2047. {
  2048. // invalidate active handles
  2049. dMemset(mSource, 0, sizeof(mSource));
  2050. }
  2051. //--------------------------------------------------------------------------
  2052. bool OpenALInit()
  2053. {
  2054. OpenALShutdown();
  2055. if(!OpenALDLLInit())
  2056. return false;
  2057. // Open a device
  2058. #ifdef TORQUE_OS_IOS
  2059. ALenum result = AL_NO_ERROR;
  2060. mDevice = alcOpenDevice(NULL);
  2061. result = alGetError();
  2062. AssertNoOALError("Error opening output device");
  2063. //-Mat for streaming mp3
  2064. //-Sven Moved up to allow for music and audio to run with iPod Music
  2065. SoundEngine::SoundEngine_Initialize( DEFAULT_SOUND_OUTPUT_RATE );
  2066. // PUAP -Mat output rate must be set before calling alcCreateContext()
  2067. alcMacOSXMixerOutputRateProc(DEFAULT_SOUND_OUTPUT_RATE);
  2068. // Create an openAL context
  2069. mContext = alcCreateContext(mDevice,NULL);
  2070. // Make this context the active context
  2071. alcMakeContextCurrent(mContext);
  2072. AssertNoOALError("Error setting current OpenAL context");
  2073. //now request the number of audio sources we want, if we can't get that many decrement until we have a number we can get
  2074. #elif defined(TORQUE_OS_LINUX)
  2075. const char* deviceSpecifier =
  2076. Con::getVariable("Pref::Unix::OpenALSpecifier");
  2077. if (dStrlen(deviceSpecifier) == 0)
  2078. // use SDL for audio output by default
  2079. deviceSpecifier = "'((devices '(sdl)))";
  2080. mDevice = (ALCvoid *)alcOpenDevice(deviceSpecifier);
  2081. #elif defined(TORQUE_OS_OSX)
  2082. mDevice = alcOpenDevice((const ALCchar*)NULL);
  2083. #else
  2084. mDevice = (ALCvoid *)alcOpenDevice((const ALCchar*)NULL);
  2085. #endif
  2086. if (mDevice == (ALCvoid *)NULL)
  2087. return false;
  2088. // Create an openAL context
  2089. #ifdef TORQUE_OS_LINUX
  2090. int freq = Con::getIntVariable("Pref::Unix::OpenALFrequency");
  2091. if (freq == 0)
  2092. freq = 22050;
  2093. Con::printf(" Setting OpenAL output frequency to %d", freq);
  2094. // some versions of openal have bugs converting between 22050 and 44100
  2095. // samples when the lib is in 44100 mode.
  2096. int attrlist[] = {
  2097. // this 0x100 is "ALC_FREQUENCY" in the linux openal implementation.
  2098. // it doesn't match the value of the creative headers, so we can't use
  2099. // that define. seems like linux torque really shouldn't be using the
  2100. // creative headers.
  2101. 0x100, freq,
  2102. 0
  2103. };
  2104. mContext = alcCreateContext(mDevice,attrlist);
  2105. #else
  2106. mContext = alcCreateContext(mDevice,NULL);
  2107. #endif
  2108. if (mContext == NULL)
  2109. return false;
  2110. // Make this context the active context
  2111. alcMakeContextCurrent(mContext);
  2112. ALenum err = alGetError();
  2113. mRequestSources = MAX_AUDIOSOURCES;
  2114. while(true)
  2115. {
  2116. alGenSources(mRequestSources, mSource);
  2117. err = alGetError();
  2118. if (err == AL_NO_ERROR)
  2119. break;
  2120. mRequestSources--;
  2121. if (mRequestSources == 0)
  2122. {
  2123. OpenALShutdown();
  2124. return (false);
  2125. }
  2126. }
  2127. mNumSources = mRequestSources;
  2128. // invalidate all existing handles
  2129. dMemset(mHandle, NULL_AUDIOHANDLE, sizeof(mHandle));
  2130. // default all channels to full gain
  2131. for(U32 i = 0; i < Audio::AudioVolumeChannels; i++)
  2132. mAudioChannelVolumes[i] = 1.0f;
  2133. // Clear Error Code
  2134. alGetError();
  2135. // Similiar to DSound Model w/o min distance clamping
  2136. alEnable(AL_DISTANCE_MODEL);
  2137. alDistanceModel(AL_INVERSE_DISTANCE);
  2138. alListenerf(AL_GAIN_LINEAR, 1.f);
  2139. return true;
  2140. }
  2141. //--------------------------------------------------------------------------
  2142. void OpenALShutdown()
  2143. {
  2144. alxStopAll();
  2145. //if(mInitialized)
  2146. {
  2147. alxEnvironmentDestroy();
  2148. }
  2149. while(mLoopingList.size())
  2150. {
  2151. mLoopingList.last()->mBuffer.purge();
  2152. delete mLoopingList.last();
  2153. mLoopingList.pop_back();
  2154. }
  2155. while(mLoopingFreeList.size())
  2156. {
  2157. mLoopingFreeList.last()->mBuffer.purge();
  2158. delete mLoopingFreeList.last();
  2159. mLoopingFreeList.pop_back();
  2160. }
  2161. for(U32 i = 0; i < MAX_AUDIOSOURCES; i++)
  2162. mBuffer[i] = 0;
  2163. alDeleteSources(mNumSources, mSource);
  2164. if (mContext)
  2165. {
  2166. alcDestroyContext(mContext);
  2167. mContext = NULL;
  2168. }
  2169. if (mDevice)
  2170. {
  2171. alcCloseDevice(mDevice);
  2172. mDevice = NULL;
  2173. }
  2174. OpenALDLLShutdown();
  2175. }
  2176. } // end OpenAL namespace
  2177. AudioStreamSource* alxFindAudioStreamSource(AUDIOHANDLE handle)
  2178. {
  2179. StreamingList::iterator itr2 = mStreamingList.findImage(handle);
  2180. if(itr2)
  2181. return *itr2;
  2182. return NULL;
  2183. }