llaudioengine.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /**
  2. * @file llaudioengine.h
  3. * @brief Definition of LLAudioEngine base class abstracting the audio support
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_AUDIOENGINE_H
  33. #define LL_AUDIOENGINE_H
  34. #include <list>
  35. #include "llassettype.h"
  36. #include "llextendedstatus.h"
  37. #include "hbfastmap.h"
  38. #include "llframetimer.h"
  39. #include "lllistener.h"
  40. #include "lltimer.h"
  41. #include "lluuid.h"
  42. #include "llvector3.h"
  43. #include "llvector3d.h"
  44. constexpr F32 LL_WIND_UPDATE_INTERVAL = 0.1f;
  45. // How much sounds are weaker under water:
  46. constexpr F32 LL_WIND_UNDERWATER_CENTER_FREQ = 20.f;
  47. constexpr F32 ATTACHED_OBJECT_TIMEOUT = 5.f;
  48. constexpr F32 DEFAULT_MIN_DISTANCE = 2.f;
  49. // Define to 1 to allow searching for pre-decode UI sounds in the
  50. // LL_PATH_SKINS/default/sounds/ sub-directory of the viewer installation
  51. #define LL_SEARCH_UI_SOUNDS_IN_SKINS 0
  52. #define MAX_AUDIO_CHANNELS 30
  53. // Number of maximum rezzed objects with sounds + sounds without an object + UI
  54. // sounds.
  55. #define MAX_AUDIO_BUFFERS 100
  56. class LLAudioBuffer;
  57. class LLAudioChannel;
  58. class LLAudioChannelOpenAL;
  59. class LLAudioData;
  60. class LLAudioSource;
  61. class LLStreamingAudioInterface;
  62. //
  63. // LLAudioEngine definition
  64. //
  65. class LLAudioEngine
  66. {
  67. friend class LLAudioChannelOpenAL; // Channel needs some listener methods.
  68. friend class HBFloaterSoundsList; // For sounds list
  69. friend class LLPipeline; // For sound beacons
  70. protected:
  71. LOG_CLASS(LLAudioEngine);
  72. public:
  73. enum LLAudioType
  74. {
  75. AUDIO_TYPE_NONE = 0,
  76. AUDIO_TYPE_SFX = 1,
  77. AUDIO_TYPE_UI = 2,
  78. AUDIO_TYPE_AMBIENT = 3,
  79. AUDIO_TYPE_COUNT = 4 // last
  80. };
  81. LLAudioEngine();
  82. virtual ~LLAudioEngine() = default;
  83. // Initialization/startup/shutdown
  84. virtual bool init(void* userdata);
  85. virtual std::string getDriverName(bool verbose) = 0;
  86. virtual void shutdown();
  87. #if 0
  88. // Used by the mechanics of the engine
  89. virtual void processQueue(const LLUUID& sound_guid);
  90. #endif
  91. virtual void setListener(LLVector3 pos,LLVector3 vel,
  92. LLVector3 up,LLVector3 at);
  93. virtual void updateWind(LLVector3 direction,
  94. F32 camera_height_above_water) = 0;
  95. virtual void idle();
  96. virtual void updateChannels();
  97. //
  98. // "End user" functionality
  99. //
  100. virtual bool isWindEnabled();
  101. virtual void enableWind(bool state_b);
  102. // Use these for temporarily muting the audio system. Does not change
  103. // buffers, initialization, etc, but stops playing new sounds.
  104. void setMuted(bool muted);
  105. LL_INLINE bool getMuted() const { return mMuted; }
  106. F32 getMasterGain();
  107. void setMasterGain(F32 gain);
  108. F32 getSecondaryGain(S32 type);
  109. void setSecondaryGain(S32 type, F32 gain);
  110. F32 getInternetStreamGain();
  111. virtual void setDopplerFactor(F32 factor);
  112. virtual F32 getDopplerFactor();
  113. virtual void setRolloffFactor(F32 factor);
  114. virtual F32 getRolloffFactor();
  115. virtual void setMaxWindGain(F32 gain);
  116. // Methods actually related to setting up and removing sounds. Owner ID is
  117. // the owner of the object making the request.
  118. void triggerSound(const LLUUID& sound_id, const LLUUID& owner_id,
  119. F32 gain, S32 type = LLAudioEngine::AUDIO_TYPE_NONE,
  120. const LLVector3d& pos_global = LLVector3d::zero);
  121. bool preloadSound(const LLUUID& id);
  122. void addAudioSource(LLAudioSource* asp);
  123. void cleanupAudioSource(LLAudioSource* asp);
  124. LLAudioSource* findAudioSource(const LLUUID& source_id);
  125. LLAudioData* getAudioData(const LLUUID& audio_id);
  126. // Internet stream implementation manipulation
  127. LLStreamingAudioInterface* getStreamingAudioImpl();
  128. void setStreamingAudioImpl(LLStreamingAudioInterface* impl);
  129. // Internet stream methods - these will call down into the
  130. // *mStreamingAudioImpl if it exists:
  131. void startInternetStream(const std::string& url);
  132. void stopInternetStream();
  133. void pauseInternetStream(S32 pause);
  134. void updateInternetStream(); // Expected to be called often
  135. S32 isInternetStreamPlaying();
  136. // Use a value from 0.0 to 1.0, inclusive:
  137. void setInternetStreamGain(F32 vol);
  138. std::string getInternetStreamURL();
  139. // For debugging usage
  140. virtual LLVector3 getListenerPos();
  141. // Get a free buffer, or flush an existing one if you have to:
  142. LLAudioBuffer* getFreeBuffer();
  143. // Get a free channel or flush an existing one if your priority is higher:
  144. LLAudioChannel* getFreeChannel(F32 priority);
  145. void cleanupBuffer(LLAudioBuffer* bufferp);
  146. LL_INLINE bool isUISound(const LLUUID& id) { return mUISounds.count(id) != 0; }
  147. LL_INLINE void setSourcesUpdated() { mSourcesUpdated = true; }
  148. bool hasDecodedFile(const LLUUID& id);
  149. bool hasLocalFile(const LLUUID& id);
  150. bool updateBufferForData(LLAudioData* adp,
  151. const LLUUID& audio_id = LLUUID::null);
  152. #if LL_SEARCH_UI_SOUNDS_IN_SKINS
  153. // Helper function to find pre-decoded UI sound files in either the user's
  154. // account LL_PATH_USER_SETTINGS/ui_sounds/ directory or in the viewer
  155. // installation LL_PATH_SKINS/default/sounds/ sub-directory.
  156. // If an in_user_settings bool pointer is passed, then the corresponding
  157. // boolean variable is set to true whenever the sound is found in the user
  158. // settings directory (which got the highest priority and is searched
  159. // first), and set to false otherwise.
  160. // NOTE: no check is done in this method whether the UUID corresposnds to
  161. // a registered UI sound or not: this is on purpose so that the existence
  162. // of pre-cached sounds can be checked before their UUID got registered as
  163. // pertaining to an UI sound.
  164. static bool getUISoundFile(const LLUUID& id, std::string& sound_file,
  165. bool* in_user_settings = NULL);
  166. #else
  167. // Helper function to find pre-decoded UI sound files in the viewer
  168. // installation LL_PATH_SKINS/default/sounds/ sub-directory.
  169. // NOTE: no check is done in this method whether the UUID corresposnds to
  170. // a registered UI sound or not: this is on purpose so that the existence
  171. // of pre-cached sounds can be checked before their UUID got registered as
  172. // pertaining to an UI sound.
  173. static bool getUISoundFile(const LLUUID& id, std::string& sound_file);
  174. #endif
  175. // Asset callback when we have retrieved a sound from the asset server.
  176. void startNextTransfer();
  177. static void assetCallback(const LLUUID& id, LLAssetType::EType, void*,
  178. S32 result_code, LLExtStat);
  179. protected:
  180. virtual LLAudioBuffer* createBuffer() = 0;
  181. virtual LLAudioChannel* createChannel() = 0;
  182. virtual bool initWind() = 0;
  183. virtual void cleanupWind() = 0;
  184. virtual void setInternalGain(F32 gain) = 0;
  185. void commitDeferredChanges();
  186. virtual void allocateListener() = 0;
  187. // Listener methods
  188. virtual void setListenerPos(LLVector3 vec);
  189. virtual void setListenerVelocity(LLVector3 vec);
  190. virtual void orientListener(LLVector3 up, LLVector3 at);
  191. virtual void translateListener(LLVector3 vec);
  192. F32 mapWindVecToGain(LLVector3 wind_vec);
  193. F32 mapWindVecToPitch(LLVector3 wind_vec);
  194. F32 mapWindVecToPan(LLVector3 wind_vec);
  195. private:
  196. void setDefaults();
  197. public:
  198. // *HACK: public to set before fade in ?
  199. F32 mMaxWindGain;
  200. protected:
  201. LLListener* mListenerp;
  202. bool mMuted;
  203. void* mUserData;
  204. S32 mLastStatus;
  205. bool mEnableWind;
  206. // Audio file currently being transferred by the system:
  207. LLUUID mCurrentTransfer;
  208. LLFrameTimer mCurrentTransferTimer;
  209. // A list of all audio sources that are known to the viewer at this time.
  210. // This is most likely a superset of the ones that we actually have audio
  211. // data for, or are playing back.
  212. typedef fast_hmap<LLUUID, LLAudioSource*> source_map_t;
  213. typedef fast_hmap<LLUUID, LLAudioData*> data_map_t;
  214. source_map_t mAllSources;
  215. data_map_t mAllData;
  216. uuid_list_t mUISounds;
  217. LLAudioChannel* mChannels[MAX_AUDIO_CHANNELS];
  218. // Buffers needs to change into a different data structure, as the number
  219. // of buffers that we have active should be limited by RAM usage, not
  220. // count.
  221. LLAudioBuffer* mBuffers[MAX_AUDIO_BUFFERS];
  222. F32 mMasterGain;
  223. // Actual gain set, i.e. either mMasterGain or 0 when mMuted is true:
  224. F32 mInternalGain;
  225. F32 mSecondaryGain[AUDIO_TYPE_COUNT];
  226. F32 mNextWindUpdate;
  227. LLFrameTimer mWindUpdateTimer;
  228. std::string mAudioDevice;
  229. private:
  230. LLStreamingAudioInterface* mStreamingAudioImpl;
  231. // *HACK: checked/reset by HBFloaterSoundsList *only* (keep it that way !)
  232. bool mSourcesUpdated;
  233. };
  234. //
  235. // Generic metadata about a particular piece of audio data.
  236. // The actual data is handled by the derived LLAudioBuffer classes which are
  237. // derived for each audio engine.
  238. //
  239. class LLAudioData
  240. {
  241. friend class LLAudioEngine; // Severe laziness, bad.
  242. protected:
  243. LOG_CLASS(LLAudioData);
  244. public:
  245. LLAudioData(const LLUUID& id);
  246. bool load();
  247. LL_INLINE LLUUID getID() const { return mID; }
  248. LL_INLINE LLAudioBuffer* getBuffer() const { return mBufferp; }
  249. LL_INLINE bool hasLocalData() const { return mHasLocalData; }
  250. LL_INLINE bool hasDecodedData() const { return mHasDecodedData; }
  251. LL_INLINE bool hasCompletedDecode() const { return mHasCompletedDecode; }
  252. LL_INLINE bool hasDecodeFailed() const { return mHasDecodeFailed; }
  253. LL_INLINE bool hasWAVLoadFailed() const { return mHasWAVLoadFailed; }
  254. LL_INLINE void setHasLocalData(bool b) { mHasLocalData = b; }
  255. LL_INLINE void setHasDecodedData(bool b) { mHasDecodedData = b; }
  256. LL_INLINE void setHasCompletedDecode(bool b) { mHasCompletedDecode = b; }
  257. LL_INLINE void setHasDecodeFailed(bool b) { mHasDecodeFailed = b; }
  258. LL_INLINE void setHasWAVLoadFailed(bool b) { mHasWAVLoadFailed = b; }
  259. LL_INLINE bool isBlocked() { return sBlockedSounds.count(mID) != 0; }
  260. static void blockSound(const LLUUID& id, bool block = true);
  261. LL_INLINE static bool isBlockedSound(const LLUUID& id)
  262. {
  263. return sBlockedSounds.count(id) != 0;
  264. }
  265. LL_INLINE static const uuid_list_t& getBlockedSounds()
  266. {
  267. return sBlockedSounds;
  268. }
  269. LL_INLINE static void setBlockedSounds(const uuid_list_t& sounds)
  270. {
  271. sBlockedSounds = sounds;
  272. }
  273. protected:
  274. LLUUID mID;
  275. // If this data is being used by the audio system, a pointer to the buffer
  276. // will be set here:
  277. LLAudioBuffer* mBufferp;
  278. bool mHasLocalData;
  279. bool mHasDecodedData;
  280. bool mHasCompletedDecode;
  281. bool mHasDecodeFailed;
  282. bool mHasWAVLoadFailed;
  283. static uuid_list_t sBlockedSounds;
  284. };
  285. //
  286. // Standard audio source. Can be derived from for special sources, such as
  287. // those attached to objects.
  288. //
  289. class LLAudioSource
  290. {
  291. friend class LLAudioEngine;
  292. friend class LLAudioChannel;
  293. protected:
  294. LOG_CLASS(LLAudioSource);
  295. public:
  296. // owner_id is the id of the agent responsible for making this sound
  297. // play, for example, the owner of the object currently playing it
  298. LLAudioSource(const LLUUID& id, const LLUUID& owner_id, F32 gain,
  299. S32 type = LLAudioEngine::AUDIO_TYPE_NONE);
  300. virtual ~LLAudioSource();
  301. virtual void update(); // Update this audio source
  302. void updatePriority();
  303. // Only used for preloading UI sounds, now.
  304. void preload(const LLUUID& audio_id);
  305. void addAudioData(LLAudioData* adp, bool set_current = TRUE);
  306. LL_INLINE void setAmbient(bool ambient) { mAmbient = ambient; }
  307. LL_INLINE bool isAmbient() const { return mAmbient; }
  308. LL_INLINE void setLoop(bool loop) { mLoop = loop; }
  309. LL_INLINE bool isLoop() const { return mLoop; }
  310. LL_INLINE void setSyncMaster(bool master) { mSyncMaster = master; }
  311. LL_INLINE bool isSyncMaster() const { return mSyncMaster; }
  312. LL_INLINE void setSyncSlave(bool slave) { mSyncSlave = slave; }
  313. LL_INLINE bool isSyncSlave() const { return mSyncSlave; }
  314. LL_INLINE void setQueueSounds(bool queue) { mQueueSounds = queue; }
  315. LL_INLINE bool isQueueSounds() const { return mQueueSounds; }
  316. LL_INLINE void setPlayedOnce(bool played) { mPlayedOnce = played; }
  317. LL_INLINE void setType(S32 type) { mType = type; }
  318. LL_INLINE S32 getType() { return mType; }
  319. LL_INLINE void setPositionGlobal(const LLVector3d& pos)
  320. {
  321. mPositionGlobal = pos;
  322. }
  323. LL_INLINE LLVector3d getPositionGlobal() const { return mPositionGlobal; }
  324. LL_INLINE LLVector3 getVelocity() const { return mVelocity; }
  325. LL_INLINE F32 getPriority() const { return mPriority; }
  326. // Gain should always be clamped between 0 and 1.
  327. LL_INLINE F32 getGain() const { return mGain; }
  328. virtual void setGain(F32 gain) { mGain = llclamp(gain, 0.f, 1.f); }
  329. LL_INLINE const LLUUID& getID() const { return mID; }
  330. LL_INLINE const LLUUID& getOwnerID() const { return mOwnerID; }
  331. bool isDone() const;
  332. LL_INLINE bool isMuted() const
  333. {
  334. return mSourceMuted || (mCurrentDatap && mCurrentDatap->isBlocked());
  335. }
  336. const uuid_list_t& getPlayedSoundsUUIDs() const { return mPlayedSounds; }
  337. LL_INLINE LLAudioData* getCurrentData() { return mCurrentDatap; }
  338. LL_INLINE LLAudioData* getQueuedData() { return mQueuedDatap; }
  339. LLAudioBuffer* getCurrentBuffer();
  340. bool setupChannel();
  341. // Start the audio source playing, taking mute into account to preserve the
  342. // previous audio_id if nessesary.
  343. bool play(const LLUUID& audio_id);
  344. // Stops the audio source and resets audio_id, even if muted.
  345. void stop();
  346. LL_INLINE bool isPlaying() { return mChannelp != NULL; }
  347. // Returns true when we have preloads that have not been done yet
  348. bool hasPendingPreloads() const;
  349. protected:
  350. void setChannel(LLAudioChannel* channelp);
  351. LL_INLINE LLAudioChannel* getChannel() const { return mChannelp; }
  352. protected:
  353. // If we are currently playing back, this is the channel that we have
  354. // assigned to:
  355. LLAudioChannel* mChannelp;
  356. LLAudioData* mCurrentDatap;
  357. LLAudioData* mQueuedDatap;
  358. // The ID of the source is that of the object if it is attached to an
  359. // object. For sounds not attached to an object, this UUID is a randomly
  360. // generated one.
  361. LLUUID mID;
  362. // Owner of the sound source
  363. LLUUID mOwnerID;
  364. LLVector3d mPositionGlobal;
  365. LLVector3 mVelocity;
  366. S32 mType;
  367. F32 mPriority;
  368. F32 mGain;
  369. bool mSourceMuted;
  370. bool mAmbient;
  371. bool mLoop;
  372. bool mSyncMaster;
  373. bool mSyncSlave;
  374. bool mQueueSounds;
  375. bool mPlayedOnce;
  376. bool mCorrupted;
  377. typedef fast_hmap<LLUUID, LLAudioData*> data_map_t;
  378. data_map_t mPreloadMap;
  379. uuid_list_t mPlayedSounds;
  380. LLFrameTimer mAgeTimer;
  381. };
  382. //
  383. // Base class for an audio channel, i.e. a channel which is capable of playing
  384. // back a sound. Management of channels is done generically, methods for
  385. // actually manipulating the channel are derived for each audio engine.
  386. //
  387. class LLAudioChannel
  388. {
  389. friend class LLAudioEngine;
  390. friend class LLAudioSource;
  391. protected:
  392. LOG_CLASS(LLAudioChannel);
  393. public:
  394. LLAudioChannel();
  395. virtual ~LLAudioChannel();
  396. virtual void setSource(LLAudioSource* sourcep);
  397. LL_INLINE LLAudioSource* getSource() const { return mCurrentSourcep; }
  398. LL_INLINE void setSecondaryGain(F32 gain) { mSecondaryGain = gain; }
  399. LL_INLINE F32 getSecondaryGain() { return mSecondaryGain; }
  400. protected:
  401. virtual void play() = 0;
  402. virtual void playSynced(LLAudioChannel* channelp) = 0;
  403. virtual void cleanup() = 0;
  404. virtual bool isPlaying() = 0;
  405. LL_INLINE void setWaiting(bool waiting) { mWaiting = waiting; }
  406. LL_INLINE bool isWaiting() const { return mWaiting; }
  407. // Check to see if the buffer associated with the source changed and update
  408. // if necessary:
  409. virtual bool updateBuffer();
  410. virtual void update3DPosition() = 0;
  411. // Update your loop/completion status, for use by queueing/syncing:
  412. virtual void updateLoop() = 0;
  413. protected:
  414. LLAudioSource* mCurrentSourcep;
  415. LLAudioBuffer* mCurrentBufferp;
  416. bool mLoopedThisFrame;
  417. bool mWaiting; // Waiting for sync.
  418. F32 mSecondaryGain;
  419. };
  420. // Basically an interface class to the engine-specific implementation of audio
  421. // data that is ready for playback. Will likely get more complex as we decide
  422. // to do stuff like real streaming audio.
  423. class LLAudioBuffer
  424. {
  425. friend class LLAudioEngine;
  426. friend class LLAudioChannel;
  427. friend class LLAudioData;
  428. protected:
  429. LOG_CLASS(LLAudioBuffer);
  430. public:
  431. virtual ~LLAudioBuffer() = default;
  432. virtual bool loadWAV(const std::string& filename) = 0;
  433. virtual U32 getLength() = 0;
  434. protected:
  435. LLAudioData* mAudioDatap;
  436. LLFrameTimer mLastUseTimer;
  437. bool mInUse;
  438. };
  439. extern LLAudioEngine* gAudiop;
  440. #endif