MilesAudioManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // FILE: MilesAudioManager.h //////////////////////////////////////////////////////////////////////////
  19. // MilesAudioManager implementation
  20. // Author: John K. McDonald, July 2002
  21. #include "Common/AsciiString.h"
  22. #include "Common/GameAudio.h"
  23. #include "MSS/MSS.h"
  24. class AudioEventRTS;
  25. enum { MAXPROVIDERS = 64 };
  26. enum PlayingAudioType
  27. {
  28. PAT_Sample,
  29. PAT_3DSample,
  30. PAT_Stream,
  31. PAT_INVALID
  32. };
  33. enum PlayingStatus
  34. {
  35. PS_Playing,
  36. PS_Stopped,
  37. PS_Paused
  38. };
  39. enum PlayingWhich
  40. {
  41. PW_Attack,
  42. PW_Sound,
  43. PW_Decay,
  44. PW_INVALID
  45. };
  46. struct PlayingAudio
  47. {
  48. union
  49. {
  50. HSAMPLE m_sample;
  51. H3DSAMPLE m_3DSample;
  52. HSTREAM m_stream;
  53. };
  54. PlayingAudioType m_type;
  55. volatile PlayingStatus m_status; // This member is adjusted by another running thread.
  56. AudioEventRTS *m_audioEventRTS;
  57. void *m_file; // The file that was opened to play this
  58. Bool m_requestStop;
  59. Bool m_cleanupAudioEventRTS;
  60. Int m_framesFaded;
  61. PlayingAudio() :
  62. m_type(PAT_INVALID),
  63. m_audioEventRTS(NULL),
  64. m_requestStop(false),
  65. m_cleanupAudioEventRTS(true),
  66. m_framesFaded(0)
  67. { }
  68. };
  69. struct ProviderInfo
  70. {
  71. AsciiString name;
  72. HPROVIDER id;
  73. Bool m_isValid;
  74. };
  75. struct OpenAudioFile
  76. {
  77. AILSOUNDINFO m_soundInfo;
  78. void *m_file;
  79. UnsignedInt m_openCount;
  80. UnsignedInt m_fileSize;
  81. Bool m_compressed; // if the file was compressed, then we need to free it with a miles function.
  82. // Note: OpenAudioFile does not own this m_eventInfo, and should not delete it.
  83. const AudioEventInfo *m_eventInfo; // Not mutable, unlike the one on AudioEventRTS.
  84. };
  85. typedef std::hash_map< AsciiString, OpenAudioFile, rts::hash<AsciiString>, rts::equal_to<AsciiString> > OpenFilesHash;
  86. typedef OpenFilesHash::iterator OpenFilesHashIt;
  87. class AudioFileCache
  88. {
  89. public:
  90. AudioFileCache();
  91. // Protected by mutex
  92. virtual ~AudioFileCache();
  93. void *openFile( AudioEventRTS *eventToOpenFrom );
  94. void closeFile( void *fileToClose );
  95. void setMaxSize( UnsignedInt size );
  96. // End Protected by mutex
  97. // Note: These functions should be used for informational purposes only. For speed reasons,
  98. // they are not protected by the mutex, so they are not guarenteed to be valid if called from
  99. // outside the audio cache. They should be used as a rough estimate only.
  100. UnsignedInt getCurrentlyUsedSize() const { return m_currentlyUsedSize; }
  101. UnsignedInt getMaxSize() const { return m_maxSize; }
  102. protected:
  103. void releaseOpenAudioFile( OpenAudioFile *fileToRelease );
  104. // This function will return TRUE if it was able to free enough space, and FALSE otherwise.
  105. Bool freeEnoughSpaceForSample(const OpenAudioFile& sampleThatNeedsSpace);
  106. OpenFilesHash m_openFiles;
  107. UnsignedInt m_currentlyUsedSize;
  108. UnsignedInt m_maxSize;
  109. HANDLE m_mutex;
  110. const char *m_mutexName;
  111. };
  112. class MilesAudioManager : public AudioManager
  113. {
  114. public:
  115. #if defined(_DEBUG) || defined(_INTERNAL)
  116. virtual void audioDebugDisplay(DebugDisplayInterface *dd, void *, FILE *fp = NULL );
  117. virtual AudioHandle addAudioEvent( const AudioEventRTS *eventToAdd ); ///< Add an audio event (event must be declared in an INI file)
  118. #endif
  119. // from AudioDevice
  120. virtual void init();
  121. virtual void postProcessLoad();
  122. virtual void reset();
  123. virtual void update();
  124. MilesAudioManager();
  125. virtual ~MilesAudioManager();
  126. virtual void nextMusicTrack( void );
  127. virtual void prevMusicTrack( void );
  128. virtual Bool isMusicPlaying( void ) const;
  129. virtual Bool hasMusicTrackCompleted( const AsciiString& trackName, Int numberOfTimes ) const;
  130. virtual AsciiString getMusicTrackName( void ) const;
  131. virtual void openDevice( void );
  132. virtual void closeDevice( void );
  133. virtual void *getDevice( void ) { return m_digitalHandle; }
  134. virtual void stopAudio( AudioAffect which );
  135. virtual void pauseAudio( AudioAffect which );
  136. virtual void resumeAudio( AudioAffect which );
  137. virtual void pauseAmbient( Bool shouldPause );
  138. virtual void killAudioEventImmediately( AudioHandle audioEvent );
  139. virtual void stopAllAmbientsBy( Object *objID );
  140. virtual void stopAllAmbientsBy( Drawable *drawID );
  141. ///< Return whether the current audio is playing or not.
  142. ///< NOTE NOTE NOTE !!DO NOT USE THIS IN FOR GAMELOGIC PURPOSES!! NOTE NOTE NOTE
  143. virtual Bool isCurrentlyPlaying( AudioHandle handle );
  144. virtual void notifyOfAudioCompletion( UnsignedInt audioCompleted, UnsignedInt flags );
  145. virtual PlayingAudio *findPlayingAudioFrom( UnsignedInt audioCompleted, UnsignedInt flags );
  146. virtual UnsignedInt getProviderCount( void ) const;
  147. virtual AsciiString getProviderName( UnsignedInt providerNum ) const;
  148. virtual UnsignedInt getProviderIndex( AsciiString providerName ) const;
  149. virtual void selectProvider( UnsignedInt providerNdx );
  150. virtual void unselectProvider( void );
  151. virtual UnsignedInt getSelectedProvider( void ) const;
  152. virtual void setSpeakerType( UnsignedInt speakerType );
  153. virtual UnsignedInt getSpeakerType( void );
  154. virtual void *getHandleForBink( void );
  155. virtual void releaseHandleForBink( void );
  156. virtual void friend_forcePlayAudioEventRTS(const AudioEventRTS* eventToPlay);
  157. virtual UnsignedInt getNum2DSamples( void ) const;
  158. virtual UnsignedInt getNum3DSamples( void ) const;
  159. virtual UnsignedInt getNumStreams( void ) const;
  160. virtual Bool doesViolateLimit( AudioEventRTS *event ) const;
  161. virtual Bool isPlayingLowerPriority( AudioEventRTS *event ) const;
  162. virtual Bool isPlayingAlready( AudioEventRTS *event ) const;
  163. virtual Bool isObjectPlayingVoice( UnsignedInt objID ) const;
  164. Bool killLowestPrioritySoundImmediately( AudioEventRTS *event );
  165. AudioEventRTS* findLowestPrioritySound( AudioEventRTS *event );
  166. virtual void adjustVolumeOfPlayingAudio(AsciiString eventName, Real newVolume);
  167. virtual void removePlayingAudio( AsciiString eventName );
  168. virtual void removeAllDisabledAudio();
  169. virtual void processRequestList( void );
  170. virtual void processPlayingList( void );
  171. virtual void processFadingList( void );
  172. virtual void processStoppedList( void );
  173. Bool shouldProcessRequestThisFrame( AudioRequest *req ) const;
  174. void adjustRequest( AudioRequest *req );
  175. Bool checkForSample( AudioRequest *req );
  176. virtual void setHardwareAccelerated(Bool accel);
  177. virtual void setSpeakerSurround(Bool surround);
  178. virtual void setPreferredProvider(AsciiString provider) { m_pref3DProvider = provider; }
  179. virtual void setPreferredSpeaker(AsciiString speakerType) { m_prefSpeaker = speakerType; }
  180. virtual Real getFileLengthMS( AsciiString strToLoad ) const;
  181. virtual void closeAnySamplesUsingFile( const void *fileToClose );
  182. protected:
  183. // 3-D functions
  184. virtual void setDeviceListenerPosition( void );
  185. const Coord3D *getCurrentPositionFromEvent( AudioEventRTS *event );
  186. Bool isOnScreen( const Coord3D *pos ) const;
  187. Real getEffectiveVolume(AudioEventRTS *event) const;
  188. // Looping functions
  189. Bool startNextLoop( PlayingAudio *looping );
  190. void playStream( AudioEventRTS *event, HSTREAM stream );
  191. // Returns the file handle for attachment to the PlayingAudio structure
  192. void *playSample( AudioEventRTS *event, HSAMPLE sample );
  193. void *playSample3D( AudioEventRTS *event, H3DSAMPLE sample3D );
  194. protected:
  195. void buildProviderList( void );
  196. void createListener( void );
  197. void initDelayFilter( void );
  198. Bool isValidProvider( void );
  199. void initSamplePools( void );
  200. void processRequest( AudioRequest *req );
  201. void playAudioEvent( AudioEventRTS *event );
  202. void stopAudioEvent( AudioHandle handle );
  203. void pauseAudioEvent( AudioHandle handle );
  204. void *loadFileForRead( AudioEventRTS *eventToLoadFrom );
  205. void closeFile( void *fileRead );
  206. PlayingAudio *allocatePlayingAudio( void );
  207. void releaseMilesHandles( PlayingAudio *release );
  208. void releasePlayingAudio( PlayingAudio *release );
  209. void stopAllAudioImmediately( void );
  210. void freeAllMilesHandles( void );
  211. HSAMPLE getFirst2DSample( AudioEventRTS *event );
  212. H3DSAMPLE getFirst3DSample( AudioEventRTS *event );
  213. void adjustPlayingVolume( PlayingAudio *audio );
  214. void stopAllSpeech( void );
  215. protected:
  216. void initFilters( HSAMPLE sample, const AudioEventRTS *eventInfo );
  217. void initFilters3D( H3DSAMPLE sample, const AudioEventRTS *eventInfo, const Coord3D *pos );
  218. protected:
  219. ProviderInfo m_provider3D[MAXPROVIDERS];
  220. UnsignedInt m_providerCount;
  221. UnsignedInt m_selectedProvider;
  222. UnsignedInt m_lastProvider;
  223. UnsignedInt m_selectedSpeakerType;
  224. AsciiString m_pref3DProvider;
  225. AsciiString m_prefSpeaker;
  226. HDIGDRIVER m_digitalHandle;
  227. H3DPOBJECT m_listener;
  228. HPROVIDER m_delayFilter;
  229. // This is a list of all handles that are forcibly played. They always play as UI sounds.
  230. std::list<HAUDIO> m_audioForcePlayed;
  231. // Available handles for play. Note that there aren't handles open in advance for
  232. // streaming things, only 2-D and 3-D sounds.
  233. std::list<HSAMPLE> m_availableSamples;
  234. std::list<H3DSAMPLE> m_available3DSamples;
  235. // Currently Playing stuff. Useful if we have to preempt it.
  236. // This should rarely if ever happen, as we mirror this in Sounds, and attempt to
  237. // keep preemption from taking place here.
  238. std::list<PlayingAudio *> m_playingSounds;
  239. std::list<PlayingAudio *> m_playing3DSounds;
  240. std::list<PlayingAudio *> m_playingStreams;
  241. // Currently fading stuff. At this point, we just want to let it finish fading, when it is
  242. // done it should be added to the completed list, then "freed" and the counts should be updated
  243. // on the next update
  244. std::list<PlayingAudio *> m_fadingAudio;
  245. // Stuff that is done playing (either because it has finished or because it was killed)
  246. // This stuff should be cleaned up during the next update cycle. This includes updating counts
  247. // in the sound engine
  248. std::list<PlayingAudio *> m_stoppedAudio;
  249. AudioFileCache *m_audioCache;
  250. PlayingAudio *m_binkHandle;
  251. UnsignedInt m_num2DSamples;
  252. UnsignedInt m_num3DSamples;
  253. UnsignedInt m_numStreams;
  254. #if defined(_DEBUG) || defined(_INTERNAL)
  255. typedef std::set<AsciiString> SetAsciiString;
  256. typedef SetAsciiString::iterator SetAsciiStringIt;
  257. SetAsciiString m_allEventsLoaded;
  258. void dumpAllAssetsUsed();
  259. #endif
  260. };