llaudioengine.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  1. /**
  2. * @file llaudioengine.cpp
  3. * @brief implementation of LLAudioEngine class abstracting the Open
  4. * AL audio support
  5. *
  6. * $LicenseInfo:firstyear=2000&license=viewergpl$
  7. *
  8. * Copyright (c) 2000-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "linden_common.h"
  34. #include "llaudioengine.h"
  35. #include "llaudiodecodemgr.h"
  36. #include "llassetstorage.h"
  37. #include "lldir.h"
  38. #include "llfilesystem.h"
  39. #include "llmath.h"
  40. #include "llstreamingaudio.h"
  41. // Necessary for grabbing sounds from sim (implemented in viewer)
  42. extern void request_sound(const LLUUID& sound_guid);
  43. LLAudioEngine* gAudiop = NULL;
  44. //static
  45. uuid_list_t LLAudioData::sBlockedSounds;
  46. //
  47. // LLAudioEngine implementation
  48. //
  49. LLAudioEngine::LLAudioEngine()
  50. : mSourcesUpdated(false),
  51. mListenerp(NULL)
  52. {
  53. setDefaults();
  54. }
  55. LLStreamingAudioInterface* LLAudioEngine::getStreamingAudioImpl()
  56. {
  57. return mStreamingAudioImpl;
  58. }
  59. void LLAudioEngine::setStreamingAudioImpl(LLStreamingAudioInterface* impl)
  60. {
  61. mStreamingAudioImpl = impl;
  62. }
  63. void LLAudioEngine::setDefaults()
  64. {
  65. mMaxWindGain = 1.f;
  66. if (mListenerp)
  67. {
  68. delete mListenerp;
  69. mListenerp = NULL;
  70. }
  71. mMuted = false;
  72. mUserData = NULL;
  73. mLastStatus = 0;
  74. mEnableWind = false;
  75. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  76. {
  77. mChannels[i] = NULL;
  78. }
  79. for (S32 i = 0; i < MAX_AUDIO_BUFFERS; ++i)
  80. {
  81. mBuffers[i] = NULL;
  82. }
  83. mMasterGain = 1.f;
  84. // Setting mInternalGain to an out of range value fixes the issue reported
  85. // in STORM-830. There is an edge case in setMasterGain during startup
  86. // which prevents setInternalGain from being called if the master volume
  87. // setting and mInternalGain both equal 0, so using -1 forces the if
  88. // statement in setMasterGain to execute when the viewer starts up.
  89. mInternalGain = -1.f;
  90. mNextWindUpdate = 0.f;
  91. mStreamingAudioImpl = NULL;
  92. for (U32 i = 0; i < AUDIO_TYPE_COUNT; ++i)
  93. {
  94. mSecondaryGain[i] = 1.0f;
  95. }
  96. }
  97. //virtual
  98. bool LLAudioEngine::init(void* userdata)
  99. {
  100. setDefaults();
  101. mUserData = userdata;
  102. allocateListener();
  103. if (!gAudioDecodeMgrp)
  104. {
  105. // Initialize the decode manager
  106. gAudioDecodeMgrp = new LLAudioDecodeMgr;
  107. }
  108. llinfos << "Audio engine successfully created with " << MAX_AUDIO_CHANNELS
  109. << " channels." << llendl;
  110. return true;
  111. }
  112. //virtual
  113. void LLAudioEngine::shutdown()
  114. {
  115. // Clean up decode manager
  116. delete gAudioDecodeMgrp;
  117. gAudioDecodeMgrp = NULL;
  118. // Clean up wind source
  119. cleanupWind();
  120. // Clean up audio sources
  121. for (source_map_t::iterator it = mAllSources.begin(),
  122. end = mAllSources.end();
  123. it != end; ++it)
  124. {
  125. delete it->second;
  126. }
  127. // Clean up audio data
  128. for (data_map_t::iterator it = mAllData.begin(), end = mAllData.end();
  129. it != end; ++it)
  130. {
  131. delete it->second;
  132. }
  133. // Clean up channels
  134. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  135. {
  136. delete mChannels[i];
  137. mChannels[i] = NULL;
  138. }
  139. for (S32 i = 0; i < MAX_AUDIO_BUFFERS; ++i)
  140. {
  141. delete mBuffers[i];
  142. mBuffers[i] = NULL;
  143. }
  144. mSourcesUpdated = true;
  145. }
  146. //virtual
  147. void LLAudioEngine::startInternetStream(const std::string& url)
  148. {
  149. if (mStreamingAudioImpl)
  150. {
  151. mStreamingAudioImpl->start(url);
  152. }
  153. }
  154. //virtual
  155. void LLAudioEngine::stopInternetStream()
  156. {
  157. if (mStreamingAudioImpl)
  158. {
  159. mStreamingAudioImpl->stop();
  160. }
  161. }
  162. //virtual
  163. void LLAudioEngine::pauseInternetStream(S32 pause)
  164. {
  165. if (mStreamingAudioImpl)
  166. {
  167. mStreamingAudioImpl->pause(pause);
  168. }
  169. }
  170. //virtual
  171. void LLAudioEngine::updateInternetStream()
  172. {
  173. if (mStreamingAudioImpl)
  174. {
  175. mStreamingAudioImpl->update();
  176. }
  177. }
  178. //virtual
  179. int LLAudioEngine::isInternetStreamPlaying()
  180. {
  181. return mStreamingAudioImpl ? mStreamingAudioImpl->isPlaying() : 0;
  182. }
  183. //virtual
  184. void LLAudioEngine::setInternetStreamGain(F32 vol)
  185. {
  186. if (mStreamingAudioImpl)
  187. {
  188. mStreamingAudioImpl->setGain(vol);
  189. }
  190. }
  191. //virtual
  192. std::string LLAudioEngine::getInternetStreamURL()
  193. {
  194. return mStreamingAudioImpl ? mStreamingAudioImpl->getURL() : std::string();
  195. }
  196. //virtual
  197. void LLAudioEngine::updateChannels()
  198. {
  199. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  200. {
  201. if (mChannels[i])
  202. {
  203. mChannels[i]->updateBuffer();
  204. mChannels[i]->update3DPosition();
  205. mChannels[i]->updateLoop();
  206. }
  207. }
  208. }
  209. //virtual
  210. void LLAudioEngine::idle()
  211. {
  212. // "Update" all of our audio sources, clean up dead ones. Primarily does
  213. // position updating, cleanup of unused audio sources. Also does
  214. // regeneration of the current priority of each audio source.
  215. for (S32 i = 0; i < MAX_AUDIO_BUFFERS; ++i)
  216. {
  217. if (mBuffers[i])
  218. {
  219. mBuffers[i]->mInUse = false;
  220. }
  221. }
  222. // Maximum priority source without a channel
  223. LLAudioSource* max_sourcep = NULL;
  224. F32 max_priority = -1.f;
  225. for (source_map_t::iterator iter = mAllSources.begin(),
  226. end = mAllSources.end();
  227. iter != end; )
  228. {
  229. source_map_t::iterator curiter = iter++;
  230. LLAudioSource* sourcep = curiter->second;
  231. if (!sourcep) // Paranoia
  232. {
  233. llwarns << "NULL source found, removing." << llendl;
  234. mAllSources.hmap_erase(curiter);
  235. mSourcesUpdated = true;
  236. continue;
  237. }
  238. // Update this source
  239. sourcep->update();
  240. sourcep->updatePriority();
  241. if (sourcep->isDone())
  242. {
  243. // The source is done playing, clean it up.
  244. delete sourcep;
  245. mAllSources.hmap_erase(curiter);
  246. mSourcesUpdated = true;
  247. continue;
  248. }
  249. if (!sourcep->isMuted() && !sourcep->getChannel() &&
  250. sourcep->getCurrentBuffer())
  251. {
  252. // We could potentially play this sound if its priority is high
  253. // enough.
  254. if (sourcep->getPriority() > max_priority)
  255. {
  256. max_priority = sourcep->getPriority();
  257. max_sourcep = sourcep;
  258. }
  259. }
  260. }
  261. // Now, do priority-based organization of audio sources. All channels used,
  262. // check priorities. Find channel with lowest priority.
  263. if (max_sourcep)
  264. {
  265. LLAudioChannel* channelp = getFreeChannel(max_priority);
  266. if (channelp)
  267. {
  268. max_sourcep->setChannel(channelp);
  269. channelp->setSource(max_sourcep);
  270. if (max_sourcep->isSyncSlave())
  271. {
  272. // A sync slave, it does not start playing until it is synced
  273. // up with the master. Flag this channel as waiting for sync
  274. // and return true.
  275. channelp->setWaiting(true);
  276. }
  277. else
  278. {
  279. channelp->setWaiting(false);
  280. if (channelp->mCurrentBufferp)
  281. {
  282. channelp->play();
  283. }
  284. }
  285. }
  286. }
  287. // Do this BEFORE we update the channels. Update the channels to sync up
  288. // with any changes that the source made, such as changing what sound was
  289. // playing.
  290. updateChannels();
  291. // Update queued sounds (switch to next queued data if the current has
  292. // finished playing)
  293. for (source_map_t::iterator iter = mAllSources.begin(),
  294. end = mAllSources.end();
  295. iter != end; ++iter)
  296. {
  297. // This is lame, instead of this I could actually iterate through all
  298. // the sources attached to each channel, since only those with active
  299. // channels can have anything interesting happen with their queue ?
  300. // (Maybe not true)
  301. LLAudioSource* sourcep = iter->second;
  302. if (!sourcep || !sourcep->mQueuedDatap || sourcep->isMuted())
  303. {
  304. // Muted, or nothing queued, so we do not care.
  305. continue;
  306. }
  307. LLAudioChannel* channelp = sourcep->getChannel();
  308. if (!channelp)
  309. {
  310. // This sound is not playing, so we just process move the queue
  311. sourcep->mCurrentDatap = sourcep->mQueuedDatap;
  312. sourcep->mQueuedDatap = NULL;
  313. // Reset the timer so the source does not die.
  314. sourcep->mAgeTimer.reset();
  315. // Make sure we have the buffer set up if we just decoded the data
  316. if (sourcep->mCurrentDatap)
  317. {
  318. updateBufferForData(sourcep->mCurrentDatap);
  319. }
  320. // Actually play the associated data.
  321. sourcep->setupChannel();
  322. channelp = sourcep->getChannel();
  323. if (channelp)
  324. {
  325. channelp->updateBuffer();
  326. if (channelp->mCurrentBufferp)
  327. {
  328. channelp->play();
  329. }
  330. }
  331. continue;
  332. }
  333. else
  334. {
  335. // Check to see if the current sound is done playing, or looped.
  336. if (!channelp->isPlaying())
  337. {
  338. sourcep->mCurrentDatap = sourcep->mQueuedDatap;
  339. sourcep->mQueuedDatap = NULL;
  340. // Reset the timer so the source does not die.
  341. sourcep->mAgeTimer.reset();
  342. // Make sure we have the buffer set up if we just decoded the data
  343. if (sourcep->mCurrentDatap)
  344. {
  345. updateBufferForData(sourcep->mCurrentDatap);
  346. }
  347. // Actually play the associated data.
  348. sourcep->setupChannel();
  349. channelp->updateBuffer();
  350. if (channelp->mCurrentBufferp)
  351. {
  352. channelp->play();
  353. }
  354. }
  355. else if (sourcep->isLoop())
  356. {
  357. // If it is a loop, we need to check and see if we are done
  358. // with it.
  359. if (channelp->mLoopedThisFrame)
  360. {
  361. sourcep->mCurrentDatap = sourcep->mQueuedDatap;
  362. sourcep->mQueuedDatap = NULL;
  363. // Actually, should do a time sync so if we are a loop
  364. // master/slave we do not drift away.
  365. sourcep->setupChannel();
  366. channelp = sourcep->getChannel();
  367. if (channelp && channelp->mCurrentBufferp)
  368. {
  369. channelp->play();
  370. }
  371. }
  372. }
  373. }
  374. }
  375. // Lame, update the channels AGAIN.
  376. // Update the channels to sync up with any changes that the source made,
  377. // such as changing what sound was playing.
  378. updateChannels();
  379. // *HACK: for now, just use a global sync master;
  380. LLAudioSource* sync_masterp = NULL;
  381. LLAudioChannel* master_channelp = NULL;
  382. F32 max_sm_priority = -1.f;
  383. for (source_map_t::iterator iter = mAllSources.begin(),
  384. end = mAllSources.end();
  385. iter != end; ++iter)
  386. {
  387. LLAudioSource* sourcep = iter->second;
  388. if (sourcep->isMuted())
  389. {
  390. continue;
  391. }
  392. if (sourcep->isSyncMaster())
  393. {
  394. if (sourcep->getPriority() > max_sm_priority)
  395. {
  396. sync_masterp = sourcep;
  397. master_channelp = sync_masterp->getChannel();
  398. max_sm_priority = sourcep->getPriority();
  399. }
  400. }
  401. }
  402. if (master_channelp && master_channelp->mLoopedThisFrame)
  403. {
  404. // Synchronize loop slaves with their masters. Update queued sounds
  405. // (switch to next queued data if the current has finished playing).
  406. for (source_map_t::iterator iter = mAllSources.begin(),
  407. end = mAllSources.end();
  408. iter != end; ++iter)
  409. {
  410. LLAudioSource* sourcep = iter->second;
  411. if (!sourcep->isSyncSlave())
  412. {
  413. // Not a loop slave, we do not need to do anything
  414. continue;
  415. }
  416. LLAudioChannel* channelp = sourcep->getChannel();
  417. if (!channelp)
  418. {
  419. // Not playing, do not need to bother.
  420. continue;
  421. }
  422. if (!channelp->isPlaying())
  423. {
  424. // Now we need to check if our loop master has just looped, and
  425. // start playback if that is the case.
  426. if (sync_masterp->getChannel())
  427. {
  428. channelp->playSynced(master_channelp);
  429. channelp->setWaiting(false);
  430. }
  431. }
  432. }
  433. }
  434. // Sync up everything that the audio engine needs done.
  435. commitDeferredChanges();
  436. // Flush unused buffers that are stale enough
  437. for (S32 i = 0; i < MAX_AUDIO_BUFFERS; ++i)
  438. {
  439. LLAudioBuffer* buffer = mBuffers[i];
  440. if (buffer && !buffer->mInUse &&
  441. buffer->mLastUseTimer.getElapsedTimeF32() > 30.f)
  442. {
  443. LL_DEBUGS("Audio") << "Flushing unused buffer #" << i << LL_ENDL;
  444. if (buffer->mAudioDatap) // Paranoia
  445. {
  446. buffer->mAudioDatap->mBufferp = NULL;
  447. }
  448. delete buffer;
  449. mBuffers[i] = NULL;
  450. }
  451. }
  452. // Clear all of the looped flags for the channels
  453. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  454. {
  455. LLAudioChannel* channel = mChannels[i];
  456. if (channel)
  457. {
  458. channel->mLoopedThisFrame = false;
  459. }
  460. }
  461. // Decode audio files
  462. if (gAudioDecodeMgrp)
  463. {
  464. gAudioDecodeMgrp->processQueue();
  465. }
  466. // Call this every frame, just in case we somehow missed picking it up in
  467. // all the places that can add or request new data.
  468. startNextTransfer();
  469. updateInternetStream();
  470. }
  471. bool LLAudioEngine::updateBufferForData(LLAudioData* adp,
  472. const LLUUID& audio_id)
  473. {
  474. if (!adp)
  475. {
  476. LL_DEBUGS("Audio") << "No audio data; cannot process " << audio_id
  477. << LL_ENDL;
  478. return false;
  479. }
  480. // Update the audio buffer first: load a sound if we have it. Note that
  481. // this could potentially cause us to waste time updating buffers for
  482. // sounds that actually are not playing, although this should be mitigated
  483. // by the fact that we limit the number of buffers, and we flush buffers
  484. // based on priority.
  485. if (adp->getBuffer())
  486. {
  487. LL_DEBUGS("Audio") << "A buffer already exists for " << audio_id
  488. << LL_ENDL;
  489. return true;
  490. }
  491. if (adp->hasDecodedData())
  492. {
  493. LL_DEBUGS("Audio") << "Loading audio data for " << audio_id
  494. << LL_ENDL;
  495. return adp->load();
  496. }
  497. return adp->hasLocalData() && audio_id.notNull() && gAudioDecodeMgrp &&
  498. gAudioDecodeMgrp->addDecodeRequest(audio_id);
  499. }
  500. //virtual
  501. void LLAudioEngine::enableWind(bool enable)
  502. {
  503. if (enable && !mEnableWind)
  504. {
  505. mEnableWind = initWind();
  506. if (mEnableWind)
  507. {
  508. llinfos << "Wind audio enabled." << llendl;
  509. }
  510. }
  511. else if (mEnableWind && !enable)
  512. {
  513. mEnableWind = false;
  514. cleanupWind();
  515. llinfos << "Wind audio disabled." << llendl;
  516. }
  517. }
  518. LLAudioBuffer* LLAudioEngine::getFreeBuffer()
  519. {
  520. for (S32 i = 0; i < MAX_AUDIO_BUFFERS; ++i)
  521. {
  522. if (!mBuffers[i])
  523. {
  524. mBuffers[i] = createBuffer();
  525. return mBuffers[i];
  526. }
  527. }
  528. // Grab the oldest unused buffer
  529. F32 max_age = -1.f;
  530. S32 buffer_id = -1;
  531. for (S32 i = 0; i < MAX_AUDIO_BUFFERS; ++i)
  532. {
  533. LLAudioBuffer* buffer = mBuffers[i];
  534. if (buffer && !buffer->mInUse &&
  535. buffer->mLastUseTimer.getElapsedTimeF32() > max_age)
  536. {
  537. max_age = buffer->mLastUseTimer.getElapsedTimeF32();
  538. buffer_id = i;
  539. }
  540. }
  541. if (buffer_id >= 0)
  542. {
  543. // Do not spam us with such messages...
  544. llinfos_once << "Taking over unused buffer " << buffer_id << llendl;
  545. LLAudioBuffer* buffer = mBuffers[buffer_id];
  546. if (buffer->mAudioDatap) // Paranoia
  547. {
  548. buffer->mAudioDatap->mBufferp = NULL;
  549. }
  550. delete buffer;
  551. buffer = createBuffer();
  552. mBuffers[buffer_id] = buffer;
  553. return buffer;
  554. }
  555. return NULL;
  556. }
  557. LLAudioChannel* LLAudioEngine::getFreeChannel(F32 priority)
  558. {
  559. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  560. {
  561. if (mChannels[i])
  562. {
  563. // Channel is allocated but not playing right now, use it.
  564. if (!mChannels[i]->isPlaying() && !mChannels[i]->isWaiting())
  565. {
  566. mChannels[i]->cleanup();
  567. if (mChannels[i]->getSource())
  568. {
  569. mChannels[i]->getSource()->setChannel(NULL);
  570. }
  571. return mChannels[i];
  572. }
  573. }
  574. else
  575. {
  576. // No channel allocated here, use it.
  577. mChannels[i] = createChannel();
  578. return mChannels[i];
  579. }
  580. }
  581. // All channels used, check priorities.
  582. // Find channel with lowest priority and see if we want to replace it.
  583. F32 min_priority = 10000.f;
  584. LLAudioChannel* min_channelp = NULL;
  585. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  586. {
  587. LLAudioChannel* channelp = mChannels[i];
  588. LLAudioSource* sourcep = channelp->getSource();
  589. if (sourcep && sourcep->getPriority() < min_priority)
  590. {
  591. min_channelp = channelp;
  592. min_priority = sourcep->getPriority();
  593. }
  594. }
  595. if (min_priority > priority || !min_channelp)
  596. {
  597. // All playing channels have higher priority, return.
  598. return NULL;
  599. }
  600. // Flush the minimum priority channel, and return it.
  601. min_channelp->cleanup();
  602. min_channelp->getSource()->setChannel(NULL);
  603. return min_channelp;
  604. }
  605. void LLAudioEngine::cleanupBuffer(LLAudioBuffer* bufferp)
  606. {
  607. for (S32 i = 0; i < MAX_AUDIO_BUFFERS; ++i)
  608. {
  609. if (mBuffers[i] == bufferp)
  610. {
  611. delete mBuffers[i];
  612. mBuffers[i] = NULL;
  613. }
  614. }
  615. }
  616. bool LLAudioEngine::preloadSound(const LLUUID& id)
  617. {
  618. // We do not care about the return value, this is just to make sure that we
  619. // have an entry, which will mean that the audio engine knows about this:
  620. getAudioData(id);
  621. if (gAudioDecodeMgrp && gAudioDecodeMgrp->addDecodeRequest(id))
  622. {
  623. // This means that we do have a local copy, and we are working on
  624. // decoding it.
  625. return true;
  626. }
  627. LL_DEBUGS("Audio") << "Used internal preload for non-local sound"
  628. << LL_ENDL;
  629. return false;
  630. }
  631. //virtual
  632. bool LLAudioEngine::isWindEnabled()
  633. {
  634. return mEnableWind;
  635. }
  636. void LLAudioEngine::setMuted(bool muted)
  637. {
  638. if (muted != mMuted)
  639. {
  640. mMuted = muted;
  641. setMasterGain(mMasterGain);
  642. }
  643. }
  644. void LLAudioEngine::setMasterGain(F32 gain)
  645. {
  646. mMasterGain = gain;
  647. F32 internal_gain = getMuted() ? 0.f : gain;
  648. if (internal_gain != mInternalGain)
  649. {
  650. mInternalGain = internal_gain;
  651. setInternalGain(mInternalGain);
  652. }
  653. }
  654. F32 LLAudioEngine::getMasterGain()
  655. {
  656. return mMasterGain;
  657. }
  658. void LLAudioEngine::setSecondaryGain(S32 type, F32 gain)
  659. {
  660. llassert(type < LLAudioEngine::AUDIO_TYPE_COUNT);
  661. mSecondaryGain[type] = gain;
  662. }
  663. F32 LLAudioEngine::getSecondaryGain(S32 type)
  664. {
  665. return mSecondaryGain[type];
  666. }
  667. F32 LLAudioEngine::getInternetStreamGain()
  668. {
  669. return mStreamingAudioImpl ? mStreamingAudioImpl->getGain() : 1.f;
  670. }
  671. //virtual
  672. void LLAudioEngine::setMaxWindGain(F32 gain)
  673. {
  674. mMaxWindGain = gain;
  675. }
  676. F32 LLAudioEngine::mapWindVecToGain(LLVector3 wind_vec)
  677. {
  678. F32 gain = wind_vec.length();
  679. if (gain)
  680. {
  681. if (gain > 20.f)
  682. {
  683. gain = 20.f;
  684. }
  685. gain *= 0.05f; // (1/20)
  686. }
  687. return gain;
  688. }
  689. F32 LLAudioEngine::mapWindVecToPitch(LLVector3 wind_vec)
  690. {
  691. // Wind frame is in listener-relative coordinates
  692. LLVector3 norm_wind = wind_vec;
  693. norm_wind.normalize();
  694. LLVector3 listen_right(1.f, 0.f, 0.f);
  695. // Measure angle between wind vec and listener right axis (on 0,PI)
  696. F32 theta = acosf(norm_wind * listen_right);
  697. // Put it on 0, 1
  698. theta *= 1.f / F_PI;
  699. // Put it on [0, 0.5, 0]
  700. if (theta > 0.5f)
  701. {
  702. theta = 1.f - theta;
  703. }
  704. else if (theta < 0.f)
  705. {
  706. theta = 0.f;
  707. }
  708. return theta;
  709. }
  710. F32 LLAudioEngine::mapWindVecToPan(LLVector3 wind_vec)
  711. {
  712. // Wind frame is in listener-relative coordinates
  713. LLVector3 norm_wind = wind_vec;
  714. norm_wind.normalize();
  715. LLVector3 listen_right(1.f, 0.f, 0.f);
  716. // Measure angle between wind vec and listener right axis (on [0, PI])
  717. F32 theta = acosf(norm_wind * listen_right);
  718. // Put it on 0, 1
  719. theta *= 1.f / F_PI;
  720. return theta;
  721. }
  722. void LLAudioEngine::triggerSound(const LLUUID& audio_id,
  723. const LLUUID& owner_id,
  724. F32 gain, S32 type,
  725. const LLVector3d& pos_global)
  726. {
  727. if (type == AUDIO_TYPE_UI)
  728. {
  729. mUISounds.emplace(audio_id);
  730. }
  731. if (mMuted || gain < 0.0001f)
  732. {
  733. return;
  734. }
  735. // Create a new source (since this cannot be associated with an existing
  736. // source.
  737. LLUUID source_id;
  738. source_id.generate();
  739. LLAudioSource* asp = new LLAudioSource(source_id, owner_id, gain, type);
  740. addAudioSource(asp);
  741. if (pos_global.isExactlyZero())
  742. {
  743. asp->setAmbient(true);
  744. }
  745. else
  746. {
  747. asp->setPositionGlobal(pos_global);
  748. }
  749. asp->updatePriority();
  750. asp->play(audio_id);
  751. }
  752. //virtual
  753. void LLAudioEngine::setListenerPos(LLVector3 aVec)
  754. {
  755. if (mListenerp)
  756. {
  757. mListenerp->setPosition(aVec);
  758. }
  759. }
  760. //virtual
  761. LLVector3 LLAudioEngine::getListenerPos()
  762. {
  763. if (mListenerp)
  764. {
  765. return mListenerp->getPosition();
  766. }
  767. else
  768. {
  769. return LLVector3::zero;
  770. }
  771. }
  772. //virtual
  773. void LLAudioEngine::setListenerVelocity(LLVector3 aVec)
  774. {
  775. if (mListenerp)
  776. {
  777. mListenerp->setVelocity(aVec);
  778. }
  779. }
  780. //virtual
  781. void LLAudioEngine::translateListener(LLVector3 aVec)
  782. {
  783. if (mListenerp)
  784. {
  785. mListenerp->translate(aVec);
  786. }
  787. }
  788. //virtual
  789. void LLAudioEngine::orientListener(LLVector3 up, LLVector3 at)
  790. {
  791. if (mListenerp)
  792. {
  793. mListenerp->orient(up, at);
  794. }
  795. }
  796. //virtual
  797. void LLAudioEngine::setListener(LLVector3 pos, LLVector3 vel, LLVector3 up,
  798. LLVector3 at)
  799. {
  800. if (mListenerp)
  801. {
  802. mListenerp->set(pos, vel, up, at);
  803. }
  804. }
  805. //virtual
  806. void LLAudioEngine::setDopplerFactor(F32 factor)
  807. {
  808. if (mListenerp)
  809. {
  810. mListenerp->setDopplerFactor(factor);
  811. }
  812. }
  813. //virtual
  814. F32 LLAudioEngine::getDopplerFactor()
  815. {
  816. return mListenerp ? mListenerp->getDopplerFactor() : 0.f;
  817. }
  818. //virtual
  819. void LLAudioEngine::setRolloffFactor(F32 factor)
  820. {
  821. if (mListenerp)
  822. {
  823. mListenerp->setRolloffFactor(factor);
  824. }
  825. }
  826. //virtual
  827. F32 LLAudioEngine::getRolloffFactor()
  828. {
  829. return mListenerp ? mListenerp->getRolloffFactor() : 0.f;
  830. }
  831. void LLAudioEngine::commitDeferredChanges()
  832. {
  833. if (mListenerp)
  834. {
  835. mListenerp->commitDeferredChanges();
  836. }
  837. }
  838. LLAudioSource* LLAudioEngine::findAudioSource(const LLUUID& source_id)
  839. {
  840. source_map_t::iterator iter = mAllSources.find(source_id);
  841. if (iter == mAllSources.end())
  842. {
  843. return NULL;
  844. }
  845. return iter->second;
  846. }
  847. LLAudioData* LLAudioEngine::getAudioData(const LLUUID& audio_id)
  848. {
  849. data_map_t::iterator iter= mAllData.find(audio_id);
  850. if (iter == mAllData.end())
  851. {
  852. // Create the new audio data
  853. LLAudioData* adp = new LLAudioData(audio_id);
  854. mAllData.emplace(audio_id, adp);
  855. return adp;
  856. }
  857. return iter->second;
  858. }
  859. void LLAudioEngine::addAudioSource(LLAudioSource* asp)
  860. {
  861. mSourcesUpdated = true;
  862. mAllSources[asp->getID()] = asp;
  863. }
  864. void LLAudioEngine::cleanupAudioSource(LLAudioSource* asp)
  865. {
  866. source_map_t::iterator iter = mAllSources.find(asp->getID());
  867. if (iter == mAllSources.end())
  868. {
  869. llwarns << "Cleaning up unknown audio source !" << llendl;
  870. return;
  871. }
  872. delete asp;
  873. mSourcesUpdated = true;
  874. mAllSources.hmap_erase(iter);
  875. }
  876. //static
  877. #if LL_SEARCH_UI_SOUNDS_IN_SKINS
  878. bool LLAudioEngine::getUISoundFile(const LLUUID& id, std::string& sound_file,
  879. bool* in_user_settings)
  880. #else
  881. bool LLAudioEngine::getUISoundFile(const LLUUID& id, std::string& sound_file)
  882. #endif
  883. {
  884. std::string filename = id.asString() + ".dsf";
  885. // Search first in the user's account LL_PATH_USER_SETTINGS/ui_sounds/
  886. // directory.
  887. sound_file = gDirUtil.getFullPath(LL_PATH_USER_SETTINGS, "ui_sounds",
  888. filename);
  889. if (LLFile::isfile(sound_file))
  890. {
  891. #if LL_SEARCH_UI_SOUNDS_IN_SKINS
  892. if (in_user_settings)
  893. {
  894. *in_user_settings = true;
  895. }
  896. #endif
  897. return true;
  898. }
  899. #if LL_SEARCH_UI_SOUNDS_IN_SKINS
  900. // No such sound in user settings...
  901. if (in_user_settings)
  902. {
  903. *in_user_settings = false;
  904. }
  905. #endif
  906. // Then search in the viewer installation LL_PATH_SKINS/default/sounds/
  907. // sub-directory.
  908. sound_file = gDirUtil.getFullPath(LL_PATH_SKINS, "default", "sounds",
  909. filename);
  910. if (LLFile::isfile(sound_file))
  911. {
  912. return true;
  913. }
  914. // No pre-decoded sound file available
  915. sound_file.clear();
  916. return false;
  917. }
  918. bool LLAudioEngine::hasDecodedFile(const LLUUID& id)
  919. {
  920. std::string sound_file;
  921. // If it is an UI sound, search among pre-decoded UI sound files
  922. if (isUISound(id) && getUISoundFile(id, sound_file))
  923. {
  924. return true;
  925. }
  926. // Search among cached sound files
  927. sound_file = gDirUtil.getFullPath(LL_PATH_CACHE, id.asString()) + ".dsf";
  928. return LLFile::isfile(sound_file);
  929. }
  930. bool LLAudioEngine::hasLocalFile(const LLUUID& id)
  931. {
  932. // See if it is in the cache.
  933. return LLFileSystem::getExists(id);
  934. }
  935. void LLAudioEngine::startNextTransfer()
  936. {
  937. if (mCurrentTransfer.notNull() || getMuted())
  938. {
  939. return;
  940. }
  941. // Get the ID for the next asset that we want to transfer. Pick one in the
  942. // following order:
  943. LLUUID asset_id;
  944. LLAudioSource* asp = NULL;
  945. LLAudioData* adp = NULL;
  946. // Check all channels for currently playing sounds.
  947. F32 max_pri = -1.f;
  948. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  949. {
  950. if (!mChannels[i])
  951. {
  952. continue;
  953. }
  954. asp = mChannels[i]->getSource();
  955. if (!asp)
  956. {
  957. continue;
  958. }
  959. if (asp->getPriority() <= max_pri)
  960. {
  961. continue;
  962. }
  963. if (asp->getPriority() <= max_pri)
  964. {
  965. continue;
  966. }
  967. adp = asp->getCurrentData();
  968. if (!adp)
  969. {
  970. continue;
  971. }
  972. if (!adp->hasLocalData() && !adp->hasDecodeFailed())
  973. {
  974. asset_id = adp->getID();
  975. max_pri = asp->getPriority();
  976. }
  977. }
  978. // Check all channels for currently queued sounds.
  979. if (asset_id.isNull())
  980. {
  981. max_pri = -1.f;
  982. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  983. {
  984. if (!mChannels[i])
  985. {
  986. continue;
  987. }
  988. asp = mChannels[i]->getSource();
  989. if (!asp)
  990. {
  991. continue;
  992. }
  993. if (asp->getPriority() <= max_pri)
  994. {
  995. continue;
  996. }
  997. adp = asp->getQueuedData();
  998. if (!adp)
  999. {
  1000. continue;
  1001. }
  1002. if (!adp->hasLocalData() && !adp->hasDecodeFailed())
  1003. {
  1004. asset_id = adp->getID();
  1005. max_pri = asp->getPriority();
  1006. }
  1007. }
  1008. }
  1009. // Check all live channels for other sounds (preloads).
  1010. if (asset_id.isNull())
  1011. {
  1012. max_pri = -1.f;
  1013. for (S32 i = 0; i < MAX_AUDIO_CHANNELS; ++i)
  1014. {
  1015. if (!mChannels[i])
  1016. {
  1017. continue;
  1018. }
  1019. asp = mChannels[i]->getSource();
  1020. if (!asp)
  1021. {
  1022. continue;
  1023. }
  1024. if (asp->getPriority() <= max_pri)
  1025. {
  1026. continue;
  1027. }
  1028. for (data_map_t::iterator it = asp->mPreloadMap.begin(),
  1029. end = asp->mPreloadMap.end();
  1030. it != end; ++it)
  1031. {
  1032. adp = it->second;
  1033. if (adp && !adp->hasLocalData() && !adp->hasDecodeFailed())
  1034. {
  1035. asset_id = adp->getID();
  1036. max_pri = asp->getPriority();
  1037. }
  1038. }
  1039. }
  1040. }
  1041. // Check all sources
  1042. if (asset_id.isNull())
  1043. {
  1044. max_pri = -1.f;
  1045. for (source_map_t::iterator sit = mAllSources.begin(),
  1046. send = mAllSources.end();
  1047. sit != send; ++sit)
  1048. {
  1049. asp = sit->second;
  1050. if (!asp || asp->getPriority() <= max_pri)
  1051. {
  1052. continue;
  1053. }
  1054. adp = asp->getCurrentData();
  1055. if (adp && !adp->hasLocalData() && !adp->hasDecodeFailed())
  1056. {
  1057. asset_id = adp->getID();
  1058. max_pri = asp->getPriority();
  1059. continue;
  1060. }
  1061. adp = asp->getQueuedData();
  1062. if (adp && !adp->hasLocalData() && !adp->hasDecodeFailed())
  1063. {
  1064. asset_id = adp->getID();
  1065. max_pri = asp->getPriority();
  1066. continue;
  1067. }
  1068. for (data_map_t::iterator dit = asp->mPreloadMap.begin(),
  1069. dend = asp->mPreloadMap.end();
  1070. dit != dend; ++dit)
  1071. {
  1072. adp = dit->second;
  1073. if (adp && !adp->hasLocalData() && !adp->hasDecodeFailed())
  1074. {
  1075. asset_id = adp->getID();
  1076. max_pri = asp->getPriority();
  1077. break;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. if (asset_id.notNull())
  1083. {
  1084. LL_DEBUGS("Audio") << "Getting asset data for: " << asset_id
  1085. << LL_ENDL;
  1086. if (!gAssetStoragep)
  1087. {
  1088. llwarns << "No asset storage system. Transfer for " << asset_id
  1089. << " aborted." << llendl;
  1090. return;
  1091. }
  1092. mCurrentTransfer = asset_id;
  1093. mCurrentTransferTimer.reset();
  1094. gAssetStoragep->getAssetData(asset_id, LLAssetType::AT_SOUND,
  1095. assetCallback, NULL);
  1096. }
  1097. }
  1098. //static
  1099. void LLAudioEngine::assetCallback(const LLUUID& id, LLAssetType::EType,
  1100. void*, S32 result_code, LLExtStat)
  1101. {
  1102. if (!gAudiop)
  1103. {
  1104. llwarns_once << "Audio engine instance does not exist" << llendl;
  1105. return;
  1106. }
  1107. if (!gAudioDecodeMgrp)
  1108. {
  1109. llwarns_once << "Audio decode manager instance does not exist"
  1110. << llendl;
  1111. return;
  1112. }
  1113. if (result_code)
  1114. {
  1115. llwarns << "Error in audio file transfer: "
  1116. << LLAssetStorage::getErrorString(result_code) << " ("
  1117. << result_code << ")" << llendl;
  1118. // Need to mark data as bad to avoid constant re-requests.
  1119. LLAudioData* adp = gAudiop->getAudioData(id);
  1120. if (adp)
  1121. {
  1122. adp->setHasDecodeFailed(true);
  1123. adp->setHasLocalData(false);
  1124. adp->setHasDecodedData(false);
  1125. adp->setHasCompletedDecode(true);
  1126. }
  1127. }
  1128. else
  1129. {
  1130. LLAudioData* adp = gAudiop->getAudioData(id);
  1131. if (!adp)
  1132. {
  1133. // This should never happen
  1134. llwarns << "Got asset callback without audio data for " << id
  1135. << llendl;
  1136. // Make sure that corrupted sound will never be requested again
  1137. LLAudioData::blockSound(id);
  1138. }
  1139. else
  1140. {
  1141. adp->setHasDecodeFailed(false);
  1142. adp->setHasLocalData(true);
  1143. gAudioDecodeMgrp->addDecodeRequest(id);
  1144. }
  1145. }
  1146. gAudiop->mCurrentTransfer.setNull();
  1147. gAudiop->startNextTransfer();
  1148. }
  1149. //
  1150. // LLAudioSource implementation
  1151. //
  1152. LLAudioSource::LLAudioSource(const LLUUID& id, const LLUUID& owner_id,
  1153. F32 gain, S32 type)
  1154. : mID(id),
  1155. mOwnerID(owner_id),
  1156. mPriority(0.f),
  1157. mGain(gain),
  1158. mSourceMuted(false),
  1159. mAmbient(false),
  1160. mLoop(false),
  1161. mSyncMaster(false),
  1162. mSyncSlave(false),
  1163. mQueueSounds(false),
  1164. mPlayedOnce(false),
  1165. mCorrupted(false),
  1166. mType(type),
  1167. mChannelp(NULL),
  1168. mCurrentDatap(NULL),
  1169. mQueuedDatap(NULL)
  1170. {
  1171. }
  1172. LLAudioSource::~LLAudioSource()
  1173. {
  1174. if (mChannelp)
  1175. {
  1176. // Stop playback of this sound
  1177. mChannelp->setSource(NULL);
  1178. mChannelp = NULL;
  1179. }
  1180. }
  1181. void LLAudioSource::setChannel(LLAudioChannel* channelp)
  1182. {
  1183. if (channelp == mChannelp)
  1184. {
  1185. return;
  1186. }
  1187. mChannelp = channelp;
  1188. }
  1189. void LLAudioSource::update()
  1190. {
  1191. if (!mCorrupted && !getCurrentBuffer())
  1192. {
  1193. // *HACK: try and load the sound. Will do this as a callback on
  1194. // decode later.
  1195. LLAudioData* adp = getCurrentData();
  1196. if (adp)
  1197. {
  1198. if (adp->getBuffer())
  1199. {
  1200. LL_DEBUGS("Audio") << "Buffer exists for " << adp->getID()
  1201. << " - Playing it." << LL_ENDL;
  1202. play(adp->getID());
  1203. }
  1204. else if (adp->hasDecodedData() && !adp->hasWAVLoadFailed())
  1205. {
  1206. LL_DEBUGS("Audio") << "Attempting to load " << adp->getID()
  1207. << LL_ENDL;
  1208. if (adp->load())
  1209. {
  1210. LL_DEBUGS("Audio") << "Playing " << adp->getID()
  1211. << LL_ENDL;
  1212. play(adp->getID());
  1213. }
  1214. else
  1215. {
  1216. LL_DEBUGS("Audio") << "Load failed for " << adp->getID()
  1217. << LL_ENDL;
  1218. }
  1219. }
  1220. // Only mark corrupted after decode is done
  1221. else if (adp->hasCompletedDecode() && adp->hasDecodeFailed())
  1222. {
  1223. llwarns << "Marking corrupted sound: " << adp->getID()
  1224. << llendl;
  1225. mCorrupted = true;
  1226. }
  1227. }
  1228. }
  1229. }
  1230. void LLAudioSource::updatePriority()
  1231. {
  1232. if (isAmbient())
  1233. {
  1234. mPriority = 1.f;
  1235. }
  1236. else if (isMuted())
  1237. {
  1238. mPriority = 0.f;
  1239. }
  1240. else
  1241. {
  1242. // Priority is based on distance
  1243. LLVector3 dist_vec(getPositionGlobal());
  1244. if (gAudiop)
  1245. {
  1246. dist_vec -= gAudiop->getListenerPos();
  1247. }
  1248. F32 dist_squared = llmax(1.f, dist_vec.lengthSquared());
  1249. mPriority = mGain / dist_squared;
  1250. }
  1251. }
  1252. bool LLAudioSource::setupChannel()
  1253. {
  1254. if (!gAudiop)
  1255. {
  1256. llwarns_once << "Audio engine instance does not exist" << llendl;
  1257. return false;
  1258. }
  1259. LLAudioData* adp = getCurrentData();
  1260. if (!adp || !adp->getBuffer())
  1261. {
  1262. // We are not ready to play back the sound yet, so do not try and
  1263. // allocate a channel for it.
  1264. LL_DEBUGS("Audio") << "Aborting, no buffer" << LL_ENDL;
  1265. return false;
  1266. }
  1267. if (!mChannelp)
  1268. {
  1269. // Update the priority, in case we need to push out another channel.
  1270. updatePriority();
  1271. setChannel(gAudiop->getFreeChannel(getPriority()));
  1272. }
  1273. if (!mChannelp)
  1274. {
  1275. // We do not have any free channels; we should reprioritize.
  1276. // For now, just do not play the sound.
  1277. LL_DEBUGS("Audio") << "Aborting, no free channels" << LL_ENDL;
  1278. return false;
  1279. }
  1280. mChannelp->setSource(this);
  1281. return true;
  1282. }
  1283. bool LLAudioSource::play(const LLUUID& audio_id)
  1284. {
  1285. // Special abuse of play(): do not play a sound, but kill it.
  1286. if (audio_id.isNull())
  1287. {
  1288. if (getChannel())
  1289. {
  1290. LL_DEBUGS("Audio") << "Killing current sound." << LL_ENDL;
  1291. getChannel()->setSource(NULL);
  1292. setChannel(NULL);
  1293. if (!isMuted())
  1294. {
  1295. mCurrentDatap = NULL;
  1296. }
  1297. }
  1298. return false;
  1299. }
  1300. LL_DEBUGS("Audio") << "Request to play " << audio_id << LL_ENDL;
  1301. // Reset our age timeout if someone attempts to play the source.
  1302. mAgeTimer.reset();
  1303. if (!gAudiop)
  1304. {
  1305. llwarns_once << "Audio engine instance does not exist" << llendl;
  1306. return false;
  1307. }
  1308. LLAudioData* adp = gAudiop->getAudioData(audio_id);
  1309. addAudioData(adp);
  1310. if (isMuted())
  1311. {
  1312. LL_DEBUGS("Audio") << "Denied playing muted sound " << audio_id
  1313. << LL_ENDL;
  1314. return false;
  1315. }
  1316. bool has_buffer = gAudiop->updateBufferForData(adp, audio_id);
  1317. if (!has_buffer)
  1318. {
  1319. LL_DEBUGS("Audio") << "No buffer available to play sound " << audio_id
  1320. << LL_ENDL;
  1321. // Do not bother trying to set up a channel or anything, we do not have
  1322. // an audio buffer.
  1323. return false;
  1324. }
  1325. if (!setupChannel())
  1326. {
  1327. LL_DEBUGS("Audio") << "Failed to setup channel to play sound "
  1328. << audio_id << LL_ENDL;
  1329. return false;
  1330. }
  1331. if (isSyncSlave())
  1332. {
  1333. LL_DEBUGS("Audio") << "Waiting for sync to play sound " << audio_id
  1334. << LL_ENDL;
  1335. // A sync slave, it does not start playing until it is synced up with
  1336. // the master. Flag this channel as waiting for sync, and return true.
  1337. getChannel()->setWaiting(true);
  1338. return true;
  1339. }
  1340. LLAudioChannel* channelp = getChannel();
  1341. if (channelp && channelp->mCurrentBufferp)
  1342. {
  1343. LL_DEBUGS("Audio") << "Playing sound " << audio_id << LL_ENDL;
  1344. channelp->play();
  1345. return true;
  1346. }
  1347. llwarns << "Cannot get the channel for " << audio_id << llendl;
  1348. return false;
  1349. }
  1350. void LLAudioSource::stop()
  1351. {
  1352. play(LLUUID::null);
  1353. // Always reset data if something wants us to stop
  1354. mCurrentDatap = NULL;
  1355. }
  1356. bool LLAudioSource::isDone() const
  1357. {
  1358. constexpr F32 MAX_AGE = 60.f;
  1359. constexpr F32 MAX_UNPLAYED_AGE = 15.f;
  1360. constexpr F32 MAX_MUTED_AGE = 11.f;
  1361. if (isLoop())
  1362. {
  1363. // Looped sources never die on their own.
  1364. return false;
  1365. }
  1366. if (hasPendingPreloads())
  1367. {
  1368. return false;
  1369. }
  1370. if (mQueuedDatap)
  1371. {
  1372. // Do not kill this sound if we have got something queued up to play.
  1373. return false;
  1374. }
  1375. F32 elapsed = mAgeTimer.getElapsedTimeF32();
  1376. // This is a single-play source
  1377. if (!mChannelp)
  1378. {
  1379. if (mPlayedOnce ||
  1380. elapsed > (isMuted() ? MAX_MUTED_AGE : MAX_UNPLAYED_AGE))
  1381. {
  1382. // We do not have a channel assigned, and it has been over 15
  1383. // seconds since we tried to play it. Do not bother.
  1384. LL_DEBUGS("Audio") << "No channel assigned, source is done"
  1385. << LL_ENDL;
  1386. return true;
  1387. }
  1388. else
  1389. {
  1390. return false;
  1391. }
  1392. }
  1393. if (mChannelp->isPlaying())
  1394. {
  1395. if (elapsed > MAX_AGE)
  1396. {
  1397. // Arbitarily cut off non-looped sounds when they are old.
  1398. return true;
  1399. }
  1400. else
  1401. {
  1402. // Sound is still playing and we have not timed out, do not kill it
  1403. return false;
  1404. }
  1405. }
  1406. if (mPlayedOnce || elapsed > MAX_UNPLAYED_AGE)
  1407. {
  1408. // The sound is not playing back after 15 seconds or we are already
  1409. // done playing it, kill it.
  1410. return true;
  1411. }
  1412. return false;
  1413. }
  1414. void LLAudioSource::addAudioData(LLAudioData* adp, bool set_current)
  1415. {
  1416. // Only handle a single piece of audio data associated with a source right
  1417. // now, until I implement prefetch.
  1418. if (!gAudiop)
  1419. {
  1420. llwarns_once << "Audio engine instance does not exist" << llendl;
  1421. return;
  1422. }
  1423. if (set_current)
  1424. {
  1425. mPlayedSounds.emplace(adp->getID());
  1426. gAudiop->setSourcesUpdated();
  1427. if (!mCurrentDatap)
  1428. {
  1429. mCurrentDatap = adp;
  1430. if (mChannelp)
  1431. {
  1432. mChannelp->updateBuffer();
  1433. if (mChannelp->mCurrentBufferp)
  1434. {
  1435. mChannelp->play();
  1436. }
  1437. }
  1438. // Make sure the audio engine knows that we want to request this
  1439. // sound.
  1440. gAudiop->startNextTransfer();
  1441. return;
  1442. }
  1443. else if (mQueueSounds)
  1444. {
  1445. // If we have current data, and we are queuing, put the object
  1446. // onto the queue.
  1447. if (mQueuedDatap)
  1448. {
  1449. // We only queue one sound at a time, and it is a FIFO.
  1450. // Do not put it onto the queue.
  1451. return;
  1452. }
  1453. if (adp == mCurrentDatap && isLoop())
  1454. {
  1455. // No point in queueing the same sound if we are looping.
  1456. return;
  1457. }
  1458. mQueuedDatap = adp;
  1459. // Make sure the audio engine knows that we want to request this
  1460. // sound.
  1461. gAudiop->startNextTransfer();
  1462. }
  1463. else if (mCurrentDatap != adp)
  1464. {
  1465. // Right now, if we are currently playing this sound in a channel,
  1466. // we update the buffer associated with the channel and play it.
  1467. // This may not be the correct behavior.
  1468. mCurrentDatap = adp;
  1469. if (mChannelp)
  1470. {
  1471. mChannelp->updateBuffer();
  1472. if (mChannelp->mCurrentBufferp)
  1473. {
  1474. mChannelp->play();
  1475. }
  1476. }
  1477. // Make sure the audio engine knows that we want to request
  1478. // this sound.
  1479. gAudiop->startNextTransfer();
  1480. }
  1481. }
  1482. else
  1483. {
  1484. // Add it to the preload list.
  1485. mPreloadMap[adp->getID()] = adp;
  1486. gAudiop->startNextTransfer();
  1487. }
  1488. }
  1489. bool LLAudioSource::hasPendingPreloads() const
  1490. {
  1491. // Check to see if we have got any preloads on deck for this source
  1492. for (data_map_t::const_iterator iter = mPreloadMap.begin(),
  1493. end = mPreloadMap.end();
  1494. iter != end; ++iter)
  1495. {
  1496. LLAudioData* adp = iter->second;
  1497. // Note: a bad UUID will forever be !hasDecodedData() but also
  1498. // hasDecodeFailed(), hence the check for it
  1499. if (adp && !adp->hasDecodedData() && !adp->hasDecodeFailed())
  1500. {
  1501. // This source is still waiting for a preload
  1502. return true;
  1503. }
  1504. }
  1505. return false;
  1506. }
  1507. LLAudioBuffer* LLAudioSource::getCurrentBuffer()
  1508. {
  1509. return mCurrentDatap ? mCurrentDatap->getBuffer() : (LLAudioBuffer*)NULL;
  1510. }
  1511. //
  1512. // LLAudioChannel implementation
  1513. //
  1514. LLAudioChannel::LLAudioChannel()
  1515. : mCurrentSourcep(NULL),
  1516. mCurrentBufferp(NULL),
  1517. mLoopedThisFrame(false),
  1518. mWaiting(false),
  1519. mSecondaryGain(1.f)
  1520. {
  1521. }
  1522. LLAudioChannel::~LLAudioChannel()
  1523. {
  1524. // Need to disconnect any sources which are using this channel.
  1525. if (mCurrentSourcep)
  1526. {
  1527. mCurrentSourcep->setChannel(NULL);
  1528. }
  1529. mCurrentBufferp = NULL;
  1530. }
  1531. void LLAudioChannel::setSource(LLAudioSource* sourcep)
  1532. {
  1533. if (!sourcep)
  1534. {
  1535. // Clearing the source for this channel, do not need to do anything.
  1536. cleanup();
  1537. mCurrentSourcep = NULL;
  1538. mWaiting = false;
  1539. return;
  1540. }
  1541. mCurrentSourcep = sourcep;
  1542. updateBuffer();
  1543. update3DPosition();
  1544. }
  1545. bool LLAudioChannel::updateBuffer()
  1546. {
  1547. if (!gAudiop)
  1548. {
  1549. llwarns_once << "Audio engine instance does not exist" << llendl;
  1550. return false;
  1551. }
  1552. if (!mCurrentSourcep)
  1553. {
  1554. // This channel is not associated with any source, nothing
  1555. // to be updated
  1556. return false;
  1557. }
  1558. // Initialize the channel's gain setting for this sound.
  1559. if (gAudiop)
  1560. {
  1561. setSecondaryGain(gAudiop->getSecondaryGain(mCurrentSourcep->getType()));
  1562. }
  1563. LLAudioBuffer* bufferp = mCurrentSourcep->getCurrentBuffer();
  1564. if (bufferp == mCurrentBufferp)
  1565. {
  1566. if (bufferp)
  1567. {
  1568. // The source has not changed what buffer it is playing
  1569. bufferp->mLastUseTimer.reset();
  1570. bufferp->mInUse = true;
  1571. }
  1572. return false;
  1573. }
  1574. // The source changed what buffer it is playing. We need to clean up the
  1575. // existing channel
  1576. cleanup();
  1577. mCurrentBufferp = bufferp;
  1578. if (bufferp)
  1579. {
  1580. bufferp->mLastUseTimer.reset();
  1581. bufferp->mInUse = true;
  1582. }
  1583. if (!mCurrentBufferp)
  1584. {
  1585. // There's no new buffer to be played, so we just abort.
  1586. return false;
  1587. }
  1588. return true;
  1589. }
  1590. //
  1591. // LLAudioData implementation
  1592. //
  1593. LLAudioData::LLAudioData(const LLUUID& id)
  1594. : mID(id),
  1595. mBufferp(NULL),
  1596. mHasLocalData(false),
  1597. mHasDecodedData(false),
  1598. mHasCompletedDecode(false),
  1599. mHasDecodeFailed(false),
  1600. mHasWAVLoadFailed(true)
  1601. {
  1602. if (id.isNull())
  1603. {
  1604. // This is a null sound.
  1605. return;
  1606. }
  1607. if (!gAudiop)
  1608. {
  1609. llwarns_once << "Audio engine instance does not exist" << llendl;
  1610. return;
  1611. }
  1612. if (gAudiop->hasDecodedFile(id))
  1613. {
  1614. // Already have a decoded version, do not need to decode it.
  1615. mHasLocalData = true;
  1616. mHasDecodedData = true;
  1617. mHasCompletedDecode = true;
  1618. }
  1619. else if (gAssetStoragep &&
  1620. gAssetStoragep->hasLocalAsset(id, LLAssetType::AT_SOUND))
  1621. {
  1622. mHasLocalData = true;
  1623. }
  1624. }
  1625. bool LLAudioData::load()
  1626. {
  1627. // For now, just assume we are going to use one buffer per audiodata.
  1628. if (mBufferp)
  1629. {
  1630. // We already have this sound in a buffer, do not do anything.
  1631. llinfos << "Already have a buffer for this sound, do not bothering to load."
  1632. << llendl;
  1633. mHasWAVLoadFailed = false;
  1634. return true;
  1635. }
  1636. if (!gAudiop)
  1637. {
  1638. llwarns_once << "Audio engine instance does not exist" << llendl;
  1639. mHasWAVLoadFailed = true;
  1640. return false;
  1641. }
  1642. mBufferp = gAudiop->getFreeBuffer();
  1643. if (!mBufferp)
  1644. {
  1645. // No free buffers, abort.
  1646. llinfos_sparse << "Not able to allocate a new audio buffer, aborting."
  1647. << llendl;
  1648. mHasWAVLoadFailed = true;
  1649. return false;
  1650. }
  1651. std::string sound_file;
  1652. if (!gAudiop->isUISound(mID) ||
  1653. !LLAudioEngine::getUISoundFile(mID, sound_file))
  1654. {
  1655. // Not a pre-decoded UI sound file, then it will go to the cache.
  1656. sound_file = gDirUtil.getFullPath(LL_PATH_CACHE, mID.asString()) +
  1657. ".dsf";
  1658. }
  1659. mHasWAVLoadFailed = !mBufferp->loadWAV(sound_file);
  1660. if (mHasWAVLoadFailed)
  1661. {
  1662. // Hrm. Right now, let's unset the buffer, since it is empty.
  1663. gAudiop->cleanupBuffer(mBufferp);
  1664. mBufferp = NULL;
  1665. if (!LLFile::isfile(sound_file))
  1666. {
  1667. // And preload it again.
  1668. gAudiop->preloadSound(mID);
  1669. }
  1670. return false;
  1671. }
  1672. mBufferp->mAudioDatap = this;
  1673. return true;
  1674. }
  1675. //static
  1676. void LLAudioData::blockSound(const LLUUID& id, bool block)
  1677. {
  1678. bool blocked = sBlockedSounds.count(id) != 0;
  1679. if (block && !blocked)
  1680. {
  1681. sBlockedSounds.emplace(id);
  1682. }
  1683. else if (blocked && !block)
  1684. {
  1685. sBlockedSounds.erase(id);
  1686. }
  1687. }