audio.cc 85 KB

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