PageRenderTime 112ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llaudio/llaudioengine.cpp

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