audio.cc 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666
  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. //if(mForceMaxDistanceUpdate)
  1841. alxUpdateMaxDistance();
  1842. alxCloseHandles();
  1843. alxUpdateScores(false);
  1844. alxLoopingUpdate();
  1845. alxStreamingUpdate();
  1846. #ifdef TORQUE_GATHER_METRICS
  1847. alxGatherMetrics();
  1848. #endif
  1849. }
  1850. //--------------------------------------------------------------------------
  1851. // Misc
  1852. //--------------------------------------------------------------------------
  1853. // client-side function only
  1854. ALuint alxGetWaveLen(ALuint buffer)
  1855. {
  1856. if(buffer == AL_INVALID)
  1857. return(0);
  1858. ALint frequency = 0;
  1859. ALint bits = 0;
  1860. ALint channels = 0;
  1861. ALint size;
  1862. alGetBufferi(buffer, AL_FREQUENCY, &frequency);
  1863. alGetBufferi(buffer, AL_BITS, &bits);
  1864. alGetBufferi(buffer, AL_CHANNELS, &channels);
  1865. alGetBufferi(buffer, AL_SIZE, &size);
  1866. if(!frequency || !bits || !channels)
  1867. {
  1868. Con::errorf(ConsoleLogEntry::General, "alxGetWaveLen: invalid buffer");
  1869. return(0);
  1870. }
  1871. ALuint len = (ALuint)((F64(size) * 8000.f) / F64(frequency * bits * channels));
  1872. return(len);
  1873. }
  1874. bool alxCheckError(const char* sourceFuncName, const char* alFuncName)
  1875. {
  1876. ALenum errorVal = alGetError();
  1877. switch (errorVal)
  1878. {
  1879. case AL_NO_ERROR:
  1880. break;
  1881. case AL_INVALID_NAME:
  1882. Con::errorf("%s - %s OpenAL AL_INVALID_NAME error code returned", sourceFuncName, alFuncName);
  1883. break;
  1884. case AL_INVALID_ENUM:
  1885. Con::errorf("%s - %s OpenAL AL_INVALID_ENUM error code returned", sourceFuncName, alFuncName);
  1886. break;
  1887. case AL_INVALID_VALUE:
  1888. Con::errorf("%s - %s OpenAL AL_INVALID_VALUE error code returned", sourceFuncName, alFuncName);
  1889. break;
  1890. case AL_INVALID_OPERATION:
  1891. Con::errorf("%s - %s OpenAL AL_INVALID_OPERATION error code returned", sourceFuncName, alFuncName);
  1892. break;
  1893. case AL_OUT_OF_MEMORY:
  1894. Con::errorf("%s - %s OpenAL AL_OUT_OF_MEMORY error code returned", sourceFuncName, alFuncName);
  1895. break;
  1896. default:
  1897. Con::errorf("%s - %s OpenAL has encountered a problem and won't tell us what it is. %d", sourceFuncName, alFuncName, errorVal);
  1898. };
  1899. if (errorVal == AL_NO_ERROR)
  1900. return true;
  1901. else
  1902. return false;
  1903. }
  1904. //--------------------------------------------------------------------------
  1905. // Environment:
  1906. //--------------------------------------------------------------------------
  1907. void alxEnvironmenti(ALenum pname, ALint value)
  1908. {
  1909. /* todo
  1910. alEnvironmentiIASIG(mEnvironment, pname, value);
  1911. */
  1912. }
  1913. void alxEnvironmentf(ALenum pname, ALfloat value)
  1914. {
  1915. /* todo
  1916. alEnvironmentfIASIG(mEnvironment, pname, value);
  1917. */
  1918. }
  1919. void alxGetEnvironmenti(ALenum pname, ALint * value)
  1920. {
  1921. /* todo
  1922. alGetEnvironmentiIASIG_EXT(mEnvironment, pname, value);
  1923. */
  1924. }
  1925. void alxGetEnvironmentf(ALenum pname, ALfloat * value)
  1926. {
  1927. /* todo
  1928. alGetEnvironmentfIASIG_EXT(mEnvironment, pname, value);
  1929. */
  1930. }
  1931. void alxEnableEnvironmental(bool enable)
  1932. {
  1933. if(mEnvironmentEnabled == enable)
  1934. return;
  1935. // go through the playing sources and update their reverb mix
  1936. // - only 3d samples get environmental fx
  1937. // - only loopers can reenable fx
  1938. for(U32 i = 0; i < mNumSources; i++)
  1939. {
  1940. if(mHandle[i] == NULL_AUDIOHANDLE)
  1941. continue;
  1942. ALint val = AL_FALSE;
  1943. // 3d?
  1944. #ifdef REL_WORKAROUND
  1945. alGetSourcei(mSource[i], AL_SOURCE_ABSOLUTE, &val);
  1946. if(val == AL_FALSE)
  1947. #else
  1948. alGetSourcei(mSource[i], AL_SOURCE_RELATIVE, &val);
  1949. if(val == AL_TRUE)
  1950. #endif
  1951. continue;
  1952. // stopped?
  1953. val = AL_STOPPED;
  1954. alGetSourcei(mSource[i], AL_SOURCE_STATE, &val);
  1955. // only looping sources can reenable environmental effects (no description around
  1956. // for the non-loopers)
  1957. if(enable)
  1958. {
  1959. LoopingList::iterator itr = mLoopingList.findImage(mHandle[i]);
  1960. if(!itr)
  1961. continue;
  1962. /* todo
  1963. alSourcef(mSource[i], AL_ENV_SAMPLE_REVERB_MIX_EXT, (*itr)->mDescription.mEnvironmentLevel);
  1964. */
  1965. }
  1966. /* todo
  1967. else
  1968. alSourcef(mSource[i], AL_ENV_SAMPLE_REVERB_MIX_EXT, 0.f);
  1969. */
  1970. }
  1971. mEnvironmentEnabled = enable;
  1972. }
  1973. void alxSetEnvironment(const AudioEnvironment * env)
  1974. {
  1975. /* todo
  1976. mCurrentEnvironment = const_cast<AudioEnvironment*>(env);
  1977. // reset environmental audio?
  1978. if(!env)
  1979. {
  1980. alxEnvironmenti(AL_ENV_ROOM_IASIG, AL_ENVIRONMENT_GENERIC);
  1981. return;
  1982. }
  1983. // room trashes all the values
  1984. if(env->mUseRoom)
  1985. {
  1986. alxEnvironmenti(AL_ENV_ROOM_IASIG, env->mRoom);
  1987. return;
  1988. }
  1989. // set all the params
  1990. alxEnvironmenti(AL_ENV_ROOM_HIGH_FREQUENCY_IASIG, env->mRoomHF);
  1991. alxEnvironmenti(AL_ENV_REFLECTIONS_IASIG, env->mReflections);
  1992. alxEnvironmenti(AL_ENV_REVERB_IASIG, env->mReverb);
  1993. alxEnvironmentf(AL_ENV_ROOM_ROLLOFF_FACTOR_IASIG, env->mRoomRolloffFactor);
  1994. alxEnvironmentf(AL_ENV_DECAY_TIME_IASIG, env->mDecayTime);
  1995. alxEnvironmentf(AL_ENV_DECAY_HIGH_FREQUENCY_RATIO_IASIG, env->mDecayTime);
  1996. alxEnvironmentf(AL_ENV_REFLECTIONS_DELAY_IASIG, env->mReflectionsDelay);
  1997. alxEnvironmentf(AL_ENV_REVERB_DELAY_IASIG, env->mReverbDelay);
  1998. alxEnvironmentf(AL_ENV_DENSITY_IASIG, env->mAirAbsorption);
  1999. alxEnvironmentf(AL_ENV_DIFFUSION_IASIG, env->mEnvironmentDiffusion);
  2000. // ext:
  2001. alxEnvironmenti(AL_ENV_ROOM_VOLUME_EXT, env->mRoomVolume);
  2002. alxEnvironmenti(AL_ENV_FLAGS_EXT, env->mFlags);
  2003. alxEnvironmentf(AL_ENV_EFFECT_VOLUME_EXT, env->mEffectVolume);
  2004. alxEnvironmentf(AL_ENV_DAMPING_EXT, env->mDamping);
  2005. alxEnvironmentf(AL_ENV_ENVIRONMENT_SIZE_EXT, env->mEnvironmentSize);
  2006. */
  2007. }
  2008. const AudioEnvironment * alxGetEnvironment()
  2009. {
  2010. return(mCurrentEnvironment);
  2011. }
  2012. F32 alxGetStreamPosition( AUDIOHANDLE handle )
  2013. {
  2014. StreamingList::iterator itr = mStreamingList.findImage(handle);
  2015. if( !itr )
  2016. return -1.f;
  2017. return (*itr)->getElapsedTime();
  2018. }
  2019. F32 alxGetStreamDuration( AUDIOHANDLE handle )
  2020. {
  2021. StreamingList::iterator itr = mStreamingList.findImage(handle);
  2022. if( !itr )
  2023. return -1.f;
  2024. return (*itr)->getTotalTime();
  2025. }
  2026. // Namespace: Audio ---------------------------------------------------------
  2027. namespace Audio
  2028. {
  2029. //---------------------------------------------------------------------------
  2030. // the following db<->linear conversion functions come from Loki openAL linux driver
  2031. // code, here more for completeness than anything else (all current audio code
  2032. // uses AL_GAIN_LINEAR)... in Audio:: so that looping updates and audio channel updates
  2033. // can convert gain types and to give the miles driver access
  2034. static const F32 logtab[] = {
  2035. 0.00f, 0.001f, 0.002f, 0.003f, 0.004f,
  2036. 0.005f, 0.01f, 0.011f, 0.012f, 0.013f,
  2037. 0.014f, 0.015f, 0.016f, 0.02f, 0.021f,
  2038. 0.022f, 0.023f, 0.024f, 0.025f, 0.03f,
  2039. 0.031f, 0.032f, 0.033f, 0.034f, 0.04f,
  2040. 0.041f, 0.042f, 0.043f, 0.044f, 0.05f,
  2041. 0.051f, 0.052f, 0.053f, 0.054f, 0.06f,
  2042. 0.061f, 0.062f, 0.063f, 0.064f, 0.07f,
  2043. 0.071f, 0.072f, 0.073f, 0.08f, 0.081f,
  2044. 0.082f, 0.083f, 0.084f, 0.09f, 0.091f,
  2045. 0.092f, 0.093f, 0.094f, 0.10f, 0.101f,
  2046. 0.102f, 0.103f, 0.11f, 0.111f, 0.112f,
  2047. 0.113f, 0.12f, 0.121f, 0.122f, 0.123f,
  2048. 0.124f, 0.13f, 0.131f, 0.132f, 0.14f,
  2049. 0.141f, 0.142f, 0.143f, 0.15f, 0.151f,
  2050. 0.152f, 0.16f, 0.161f, 0.162f, 0.17f,
  2051. 0.171f, 0.172f, 0.18f, 0.181f, 0.19f,
  2052. 0.191f, 0.192f, 0.20f, 0.201f, 0.21f,
  2053. 0.211f, 0.22f, 0.221f, 0.23f, 0.231f,
  2054. 0.24f, 0.25f, 0.251f, 0.26f, 0.27f,
  2055. 0.271f, 0.28f, 0.29f, 0.30f, 0.301f,
  2056. 0.31f, 0.32f, 0.33f, 0.34f, 0.35f,
  2057. 0.36f, 0.37f, 0.38f, 0.39f, 0.40f,
  2058. 0.41f, 0.43f, 0.50f, 0.60f, 0.65f,
  2059. 0.70f, 0.75f, 0.80f, 0.85f, 0.90f,
  2060. 0.95f, 0.97f, 0.99f };
  2061. const int logmax = sizeof logtab / sizeof *logtab;
  2062. F32 DBToLinear(F32 value)
  2063. {
  2064. if(value <= 0.f)
  2065. return(0.f);
  2066. if(value >= 1.f)
  2067. return(1.f);
  2068. S32 max = logmax;
  2069. S32 min = 0;
  2070. S32 mid;
  2071. S32 last = -1;
  2072. mid = (max - min) / 2;
  2073. do {
  2074. last = mid;
  2075. if(logtab[mid] == value)
  2076. break;
  2077. if(logtab[mid] < value)
  2078. min = mid;
  2079. else
  2080. max = mid;
  2081. mid = min + ((max - min) / 2);
  2082. } while(last != mid);
  2083. return((F32)mid / logmax);
  2084. }
  2085. F32 linearToDB(F32 value)
  2086. {
  2087. if(value <= 0.f)
  2088. return(0.f);
  2089. if(value >= 1.f)
  2090. return(1.f);
  2091. return(logtab[(U32)(logmax * value)]);
  2092. }
  2093. //---------------------------------------------------------------------------
  2094. static ALvoid errorCallback(ALbyte *msg)
  2095. {
  2096. // used to allow our OpenAL implementation to display info on the console
  2097. Con::errorf(ConsoleLogEntry::General, (const char *)msg);
  2098. }
  2099. void shutdownContext()
  2100. {
  2101. // invalidate active handles
  2102. dMemset(mSource, 0, sizeof(mSource));
  2103. }
  2104. //--------------------------------------------------------------------------
  2105. bool OpenALInit()
  2106. {
  2107. OpenALShutdown();
  2108. if(!OpenALDLLInit())
  2109. return false;
  2110. // Open a device
  2111. #ifdef TORQUE_OS_IOS
  2112. ALenum result = AL_NO_ERROR;
  2113. mDevice = alcOpenDevice(NULL);
  2114. result = alGetError();
  2115. AssertNoOALError("Error opening output device");
  2116. //-Mat for streaming mp3
  2117. //-Sven Moved up to allow for music and audio to run with iPod Music
  2118. SoundEngine::SoundEngine_Initialize( DEFAULT_SOUND_OUTPUT_RATE );
  2119. // PUAP -Mat output rate must be set before calling alcCreateContext()
  2120. alcMacOSXMixerOutputRateProc(DEFAULT_SOUND_OUTPUT_RATE);
  2121. // Create an openAL context
  2122. mContext = alcCreateContext(mDevice,NULL);
  2123. // Make this context the active context
  2124. alcMakeContextCurrent(mContext);
  2125. AssertNoOALError("Error setting current OpenAL context");
  2126. //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
  2127. #elif defined(TORQUE_OS_OSX)
  2128. mDevice = alcOpenDevice((const ALCchar*)NULL);
  2129. #elif defined(TORQUE_OS_ANDROID)
  2130. mDevice = alcOpenDevice("openal-soft");
  2131. #else
  2132. mDevice = (ALCvoid *)alcOpenDevice((const ALCchar*)NULL);
  2133. #endif
  2134. if (mDevice == (ALCvoid *)NULL)
  2135. return false;
  2136. // Create an openAL context
  2137. #if defined(TORQUE_OS_LINUX) || defined(TORQUE_OS_OPENBSD)
  2138. int freq = Con::getIntVariable("Pref::Unix::OpenALFrequency");
  2139. if (freq == 0)
  2140. freq = 22050;
  2141. Con::printf(" Setting OpenAL output frequency to %d", freq);
  2142. // some versions of openal have bugs converting between 22050 and 44100
  2143. // samples when the lib is in 44100 mode.
  2144. int attrlist[] = {
  2145. // this 0x100 is "ALC_FREQUENCY" in the linux openal implementation.
  2146. // it doesn't match the value of the creative headers, so we can't use
  2147. // that define. seems like linux torque really shouldn't be using the
  2148. // creative headers.
  2149. 0x100, freq,
  2150. 0
  2151. };
  2152. mContext = alcCreateContext((ALCdevice*)mDevice,attrlist);
  2153. #elif defined(TORQUE_OS_ANDROID)
  2154. mContext = alcCreateContext((ALCdevice*)mDevice, NULL);
  2155. #elif defined(TORQUE_OS_EMSCRIPTEN)
  2156. mContext = alcCreateContext((ALCdevice*)mDevice, NULL);;
  2157. #elif defined(TORQUE_OS_IOS)
  2158. #else
  2159. mContext = alcCreateContext(mDevice,NULL);
  2160. #endif
  2161. if (mContext == NULL)
  2162. return false;
  2163. // Make this context the active context
  2164. #if defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_LINUX) || defined(TORQUE_OS_EMSCRIPTEN) || defined(TORQUE_OS_OPENBSD)
  2165. alcMakeContextCurrent((ALCcontext*)mContext);
  2166. #else
  2167. alcMakeContextCurrent(mContext);
  2168. #endif
  2169. ALenum err = alGetError();
  2170. mRequestSources = MAX_AUDIOSOURCES;
  2171. while(true)
  2172. {
  2173. alGenSources(mRequestSources, mSource);
  2174. err = alGetError();
  2175. if (err == AL_NO_ERROR)
  2176. break;
  2177. mRequestSources--;
  2178. if (mRequestSources == 0)
  2179. {
  2180. OpenALShutdown();
  2181. return (false);
  2182. }
  2183. }
  2184. mNumSources = mRequestSources;
  2185. // invalidate all existing handles
  2186. dMemset(mHandle, NULL_AUDIOHANDLE, sizeof(mHandle));
  2187. // default all channels to full gain
  2188. for(U32 i = 0; i < Audio::AudioVolumeChannels; i++)
  2189. mAudioChannelVolumes[i] = 1.0f;
  2190. // Clear Error Code
  2191. alGetError();
  2192. // Similiar to DSound Model w/o min distance clamping
  2193. alEnable(AL_DISTANCE_MODEL);
  2194. alDistanceModel(AL_INVERSE_DISTANCE);
  2195. alDopplerFactor(1.f);
  2196. alListenerf(AL_GAIN_LINEAR, 1.f);
  2197. //The audio listener is initialized at position 0,0
  2198. //Listener orientation
  2199. //The first three values represent an "at" vector which points towards the screen.
  2200. //The second set of three values represent the "up" vector (Y+)
  2201. F32 listenerPos[] = { 0.f, 0.f, 0.f };
  2202. F32 listenerVel[] = { 0.f, 0.f, 0.f };
  2203. F32 listenerOrientation[] = { 0.f, 0.f, -1.0f, 0.f, 1.f, 0.f };
  2204. alListenerfv(AL_POSITION, listenerPos);
  2205. alListenerfv(AL_VELOCITY, listenerVel);
  2206. alListenerfv(AL_ORIENTATION, listenerOrientation);
  2207. return true;
  2208. }
  2209. //--------------------------------------------------------------------------
  2210. void OpenALShutdown()
  2211. {
  2212. alxStopAll();
  2213. //if(mInitialized)
  2214. {
  2215. alxEnvironmentDestroy();
  2216. }
  2217. while(mLoopingList.size())
  2218. {
  2219. mLoopingList.last()->mBuffer.purge();
  2220. delete mLoopingList.last();
  2221. mLoopingList.pop_back();
  2222. }
  2223. while(mLoopingFreeList.size())
  2224. {
  2225. mLoopingFreeList.last()->mBuffer.purge();
  2226. delete mLoopingFreeList.last();
  2227. mLoopingFreeList.pop_back();
  2228. }
  2229. //clear error buffer
  2230. alGetError();
  2231. for(U32 i = 0; i < MAX_AUDIOSOURCES; i++)
  2232. {
  2233. ALint tempbuff = 0;
  2234. alGetSourcei( mSource[i], AL_BUFFER, &tempbuff);
  2235. if (alIsBuffer(tempbuff) && tempbuff !=0)
  2236. {
  2237. ALuint buffer = tempbuff;
  2238. alSourceUnqueueBuffers( mSource[i], 1, &buffer);
  2239. alxCheckError("OpenALShutdown()","alSourceUnqueueBuffers");
  2240. }
  2241. }
  2242. alDeleteSources(mNumSources, mSource);
  2243. if (mContext)
  2244. {
  2245. #if defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_LINUX) || defined(TORQUE_OS_OPENBSD)
  2246. alcDestroyContext((ALCcontext*)mContext);
  2247. #elif defined(TORQUE_OS_EMSCRIPTEN)
  2248. alcDestroyContext((ALCcontext*)mContext);
  2249. #else
  2250. alcDestroyContext(mContext);
  2251. #endif
  2252. mContext = NULL;
  2253. }
  2254. if (mDevice)
  2255. {
  2256. #if defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_LINUX) || defined(TORQUE_OS_OPENBSD)
  2257. alcCloseDevice((ALCdevice*)mDevice);
  2258. #elif defined(TORQUE_OS_EMSCRIPTEN)
  2259. alcCloseDevice((ALCdevice*)mDevice);
  2260. #else
  2261. alcCloseDevice(mDevice);
  2262. #endif
  2263. mDevice = NULL;
  2264. }
  2265. OpenALDLLShutdown();
  2266. }
  2267. } // end OpenAL namespace
  2268. AudioStreamSource* alxFindAudioStreamSource(AUDIOHANDLE handle)
  2269. {
  2270. StreamingList::iterator itr2 = mStreamingList.findImage(handle);
  2271. if(itr2)
  2272. return *itr2;
  2273. return NULL;
  2274. }