PageRenderTime 33ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llaudio/llaudioengine.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 459 lines | 267 code | 114 blank | 78 comment | 0 complexity | 9d287c0c8f696f1456f134f9c39ff2b0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file audioengine.h
  3. * @brief Definition of LLAudioEngine base class abstracting the audio support
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_AUDIOENGINE_H
  27. #define LL_AUDIOENGINE_H
  28. #include <list>
  29. #include <map>
  30. #include "v3math.h"
  31. #include "v3dmath.h"
  32. #include "lltimer.h"
  33. #include "lluuid.h"
  34. #include "llframetimer.h"
  35. #include "llassettype.h"
  36. #include "lllistener.h"
  37. const F32 LL_WIND_UPDATE_INTERVAL = 0.1f;
  38. const F32 LL_ROLLOFF_MULTIPLIER_UNDER_WATER = 5.f; // How much sounds are weaker under water
  39. const F32 LL_WIND_UNDERWATER_CENTER_FREQ = 20.f;
  40. const F32 ATTACHED_OBJECT_TIMEOUT = 5.0f;
  41. const F32 DEFAULT_MIN_DISTANCE = 2.0f;
  42. #define MAX_CHANNELS 30
  43. #define MAX_BUFFERS 40 // Some extra for preloading, maybe?
  44. // This define is intended to allow us to switch from os based wav
  45. // file loading to vfs based wav file loading. The problem is that I
  46. // am unconvinced that the LLWaveFile works for loading sounds from
  47. // memory. So, until that is fixed up, changed, whatever, this remains
  48. // undefined.
  49. //#define USE_WAV_VFILE
  50. class LLVFS;
  51. class LLAudioSource;
  52. class LLAudioData;
  53. class LLAudioChannel;
  54. class LLAudioChannelOpenAL;
  55. class LLAudioBuffer;
  56. class LLStreamingAudioInterface;
  57. //
  58. // LLAudioEngine definition
  59. //
  60. class LLAudioEngine
  61. {
  62. friend class LLAudioChannelOpenAL; // bleh. channel needs some listener methods.
  63. public:
  64. enum LLAudioType
  65. {
  66. AUDIO_TYPE_NONE = 0,
  67. AUDIO_TYPE_SFX = 1,
  68. AUDIO_TYPE_UI = 2,
  69. AUDIO_TYPE_AMBIENT = 3,
  70. AUDIO_TYPE_COUNT = 4 // last
  71. };
  72. enum LLAudioPlayState
  73. {
  74. // isInternetStreamPlaying() returns an *int*, with
  75. // 0 = stopped, 1 = playing, 2 = paused.
  76. AUDIO_STOPPED = 0,
  77. AUDIO_PLAYING = 1,
  78. AUDIO_PAUSED = 2
  79. };
  80. LLAudioEngine();
  81. virtual ~LLAudioEngine();
  82. // initialization/startup/shutdown
  83. virtual bool init(const S32 num_channels, void *userdata);
  84. virtual std::string getDriverName(bool verbose) = 0;
  85. virtual void shutdown();
  86. // Used by the mechanics of the engine
  87. //virtual void processQueue(const LLUUID &sound_guid);
  88. virtual void setListener(LLVector3 pos,LLVector3 vel,LLVector3 up,LLVector3 at);
  89. virtual void updateWind(LLVector3 direction, F32 camera_height_above_water) = 0;
  90. virtual void idle(F32 max_decode_time = 0.f);
  91. virtual void updateChannels();
  92. //
  93. // "End user" functionality
  94. //
  95. virtual bool isWindEnabled();
  96. virtual void enableWind(bool state_b);
  97. // Use these for temporarily muting the audio system.
  98. // Does not change buffers, initialization, etc. but
  99. // stops playing new sounds.
  100. void setMuted(bool muted);
  101. bool getMuted() const { return mMuted; }
  102. #ifdef USE_PLUGIN_MEDIA
  103. LLPluginClassMedia* initializeMedia(const std::string& media_type);
  104. #endif
  105. F32 getMasterGain();
  106. void setMasterGain(F32 gain);
  107. F32 getSecondaryGain(S32 type);
  108. void setSecondaryGain(S32 type, F32 gain);
  109. F32 getInternetStreamGain();
  110. virtual void setDopplerFactor(F32 factor);
  111. virtual F32 getDopplerFactor();
  112. virtual void setRolloffFactor(F32 factor);
  113. virtual F32 getRolloffFactor();
  114. virtual void setMaxWindGain(F32 gain);
  115. // Methods actually related to setting up and removing sounds
  116. // Owner ID is the owner of the object making the request
  117. void triggerSound(const LLUUID &sound_id, const LLUUID& owner_id, const F32 gain,
  118. const S32 type = LLAudioEngine::AUDIO_TYPE_NONE,
  119. const LLVector3d &pos_global = LLVector3d::zero);
  120. bool preloadSound(const LLUUID &id);
  121. void addAudioSource(LLAudioSource *asp);
  122. void cleanupAudioSource(LLAudioSource *asp);
  123. LLAudioSource *findAudioSource(const LLUUID &source_id);
  124. LLAudioData *getAudioData(const LLUUID &audio_uuid);
  125. // Internet stream implementation manipulation
  126. LLStreamingAudioInterface *getStreamingAudioImpl();
  127. void setStreamingAudioImpl(LLStreamingAudioInterface *impl);
  128. // Internet stream methods - these will call down into the *mStreamingAudioImpl if it exists
  129. void startInternetStream(const std::string& url);
  130. void stopInternetStream();
  131. void pauseInternetStream(int pause);
  132. void updateInternetStream(); // expected to be called often
  133. LLAudioPlayState isInternetStreamPlaying();
  134. // use a value from 0.0 to 1.0, inclusive
  135. void setInternetStreamGain(F32 vol);
  136. std::string getInternetStreamURL();
  137. // For debugging usage
  138. virtual LLVector3 getListenerPos();
  139. LLAudioBuffer *getFreeBuffer(); // Get a free buffer, or flush an existing one if you have to.
  140. LLAudioChannel *getFreeChannel(const F32 priority); // Get a free channel or flush an existing one if your priority is higher
  141. void cleanupBuffer(LLAudioBuffer *bufferp);
  142. bool hasDecodedFile(const LLUUID &uuid);
  143. bool hasLocalFile(const LLUUID &uuid);
  144. bool updateBufferForData(LLAudioData *adp, const LLUUID &audio_uuid = LLUUID::null);
  145. // Asset callback when we're retrieved a sound from the asset server.
  146. void startNextTransfer();
  147. static void assetCallback(LLVFS *vfs, const LLUUID &uuid, LLAssetType::EType type, void *user_data, S32 result_code, LLExtStat ext_status);
  148. friend class LLPipeline; // For debugging
  149. public:
  150. F32 mMaxWindGain; // Hack. Public to set before fade in?
  151. protected:
  152. virtual LLAudioBuffer *createBuffer() = 0;
  153. virtual LLAudioChannel *createChannel() = 0;
  154. virtual bool initWind() = 0;
  155. virtual void cleanupWind() = 0;
  156. virtual void setInternalGain(F32 gain) = 0;
  157. void commitDeferredChanges();
  158. virtual void allocateListener() = 0;
  159. // listener methods
  160. virtual void setListenerPos(LLVector3 vec);
  161. virtual void setListenerVelocity(LLVector3 vec);
  162. virtual void orientListener(LLVector3 up, LLVector3 at);
  163. virtual void translateListener(LLVector3 vec);
  164. F64 mapWindVecToGain(LLVector3 wind_vec);
  165. F64 mapWindVecToPitch(LLVector3 wind_vec);
  166. F64 mapWindVecToPan(LLVector3 wind_vec);
  167. protected:
  168. LLListener *mListenerp;
  169. bool mMuted;
  170. void* mUserData;
  171. S32 mLastStatus;
  172. S32 mNumChannels;
  173. bool mEnableWind;
  174. LLUUID mCurrentTransfer; // Audio file currently being transferred by the system
  175. LLFrameTimer mCurrentTransferTimer;
  176. // A list of all audio sources that are known to the viewer at this time.
  177. // This is most likely a superset of the ones that we actually have audio
  178. // data for, or are playing back.
  179. typedef std::map<LLUUID, LLAudioSource *> source_map;
  180. typedef std::map<LLUUID, LLAudioData *> data_map;
  181. source_map mAllSources;
  182. data_map mAllData;
  183. LLAudioChannel *mChannels[MAX_CHANNELS];
  184. // Buffers needs to change into a different data structure, as the number of buffers
  185. // that we have active should be limited by RAM usage, not count.
  186. LLAudioBuffer *mBuffers[MAX_BUFFERS];
  187. F32 mMasterGain;
  188. F32 mInternalGain; // Actual gain set; either mMasterGain or 0 when mMuted is true.
  189. F32 mSecondaryGain[AUDIO_TYPE_COUNT];
  190. F32 mNextWindUpdate;
  191. LLFrameTimer mWindUpdateTimer;
  192. private:
  193. void setDefaults();
  194. LLStreamingAudioInterface *mStreamingAudioImpl;
  195. };
  196. //
  197. // Standard audio source. Can be derived from for special sources, such as those attached to objects.
  198. //
  199. class LLAudioSource
  200. {
  201. public:
  202. // owner_id is the id of the agent responsible for making this sound
  203. // play, for example, the owner of the object currently playing it
  204. LLAudioSource(const LLUUID &id, const LLUUID& owner_id, const F32 gain, const S32 type = LLAudioEngine::AUDIO_TYPE_NONE);
  205. virtual ~LLAudioSource();
  206. virtual void update(); // Update this audio source
  207. void updatePriority();
  208. void preload(const LLUUID &audio_id); // Only used for preloading UI sounds, now.
  209. void addAudioData(LLAudioData *adp, bool set_current = TRUE);
  210. void setAmbient(const bool ambient) { mAmbient = ambient; }
  211. bool isAmbient() const { return mAmbient; }
  212. void setLoop(const bool loop) { mLoop = loop; }
  213. bool isLoop() const { return mLoop; }
  214. void setSyncMaster(const bool master) { mSyncMaster = master; }
  215. bool isSyncMaster() const { return mSyncMaster; }
  216. void setSyncSlave(const bool slave) { mSyncSlave = slave; }
  217. bool isSyncSlave() const { return mSyncSlave; }
  218. void setQueueSounds(const bool queue) { mQueueSounds = queue; }
  219. bool isQueueSounds() const { return mQueueSounds; }
  220. void setPlayedOnce(const bool played_once) { mPlayedOnce = played_once; }
  221. void setType(S32 type) { mType = type; }
  222. S32 getType() { return mType; }
  223. void setPositionGlobal(const LLVector3d &position_global) { mPositionGlobal = position_global; }
  224. LLVector3d getPositionGlobal() const { return mPositionGlobal; }
  225. LLVector3 getVelocity() const { return mVelocity; }
  226. F32 getPriority() const { return mPriority; }
  227. // Gain should always be clamped between 0 and 1.
  228. F32 getGain() const { return mGain; }
  229. virtual void setGain(const F32 gain) { mGain = llclamp(gain, 0.f, 1.f); }
  230. const LLUUID &getID() const { return mID; }
  231. bool isDone() const;
  232. bool isMuted() const { return mSourceMuted; }
  233. LLAudioData *getCurrentData();
  234. LLAudioData *getQueuedData();
  235. LLAudioBuffer *getCurrentBuffer();
  236. bool setupChannel();
  237. bool play(const LLUUID &audio_id); // Start the audio source playing
  238. bool hasPendingPreloads() const; // Has preloads that haven't been done yet
  239. friend class LLAudioEngine;
  240. friend class LLAudioChannel;
  241. protected:
  242. void setChannel(LLAudioChannel *channelp);
  243. LLAudioChannel *getChannel() const { return mChannelp; }
  244. protected:
  245. LLUUID mID; // The ID of the source is that of the object if it's attached to an object.
  246. LLUUID mOwnerID; // owner of the object playing the sound
  247. F32 mPriority;
  248. F32 mGain;
  249. bool mSourceMuted;
  250. bool mAmbient;
  251. bool mLoop;
  252. bool mSyncMaster;
  253. bool mSyncSlave;
  254. bool mQueueSounds;
  255. bool mPlayedOnce;
  256. bool mCorrupted;
  257. S32 mType;
  258. LLVector3d mPositionGlobal;
  259. LLVector3 mVelocity;
  260. //LLAudioSource *mSyncMasterp; // If we're a slave, the source that we're synced to.
  261. LLAudioChannel *mChannelp; // If we're currently playing back, this is the channel that we're assigned to.
  262. LLAudioData *mCurrentDatap;
  263. LLAudioData *mQueuedDatap;
  264. typedef std::map<LLUUID, LLAudioData *> data_map;
  265. data_map mPreloadMap;
  266. LLFrameTimer mAgeTimer;
  267. };
  268. //
  269. // Generic metadata about a particular piece of audio data.
  270. // The actual data is handled by the derived LLAudioBuffer classes which are
  271. // derived for each audio engine.
  272. //
  273. class LLAudioData
  274. {
  275. public:
  276. LLAudioData(const LLUUID &uuid);
  277. bool load();
  278. LLUUID getID() const { return mID; }
  279. LLAudioBuffer *getBuffer() const { return mBufferp; }
  280. bool hasLocalData() const { return mHasLocalData; }
  281. bool hasDecodedData() const { return mHasDecodedData; }
  282. bool hasValidData() const { return mHasValidData; }
  283. void setHasLocalData(const bool hld) { mHasLocalData = hld; }
  284. void setHasDecodedData(const bool hdd) { mHasDecodedData = hdd; }
  285. void setHasValidData(const bool hvd) { mHasValidData = hvd; }
  286. friend class LLAudioEngine; // Severe laziness, bad.
  287. protected:
  288. LLUUID mID;
  289. LLAudioBuffer *mBufferp; // If this data is being used by the audio system, a pointer to the buffer will be set here.
  290. bool mHasLocalData;
  291. bool mHasDecodedData;
  292. bool mHasValidData;
  293. };
  294. //
  295. // Base class for an audio channel, i.e. a channel which is capable of playing back a sound.
  296. // Management of channels is done generically, methods for actually manipulating the channel
  297. // are derived for each audio engine.
  298. //
  299. class LLAudioChannel
  300. {
  301. public:
  302. LLAudioChannel();
  303. virtual ~LLAudioChannel();
  304. virtual void setSource(LLAudioSource *sourcep);
  305. LLAudioSource *getSource() const { return mCurrentSourcep; }
  306. void setSecondaryGain(F32 gain) { mSecondaryGain = gain; }
  307. F32 getSecondaryGain() { return mSecondaryGain; }
  308. friend class LLAudioEngine;
  309. friend class LLAudioSource;
  310. protected:
  311. virtual void play() = 0;
  312. virtual void playSynced(LLAudioChannel *channelp) = 0;
  313. virtual void cleanup() = 0;
  314. virtual bool isPlaying() = 0;
  315. void setWaiting(const bool waiting) { mWaiting = waiting; }
  316. bool isWaiting() const { return mWaiting; }
  317. virtual bool updateBuffer(); // Check to see if the buffer associated with the source changed, and update if necessary.
  318. virtual void update3DPosition() = 0;
  319. virtual void updateLoop() = 0; // Update your loop/completion status, for use by queueing/syncing.
  320. protected:
  321. LLAudioSource *mCurrentSourcep;
  322. LLAudioBuffer *mCurrentBufferp;
  323. bool mLoopedThisFrame;
  324. bool mWaiting; // Waiting for sync.
  325. F32 mSecondaryGain;
  326. };
  327. // Basically an interface class to the engine-specific implementation
  328. // of audio data that's ready for playback.
  329. // Will likely get more complex as we decide to do stuff like real streaming audio.
  330. class LLAudioBuffer
  331. {
  332. public:
  333. virtual ~LLAudioBuffer() {};
  334. virtual bool loadWAV(const std::string& filename) = 0;
  335. virtual U32 getLength() = 0;
  336. friend class LLAudioEngine;
  337. friend class LLAudioChannel;
  338. friend class LLAudioData;
  339. protected:
  340. bool mInUse;
  341. LLAudioData *mAudioDatap;
  342. LLFrameTimer mLastUseTimer;
  343. };
  344. extern LLAudioEngine* gAudiop;
  345. #endif