MilesAudioManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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_sample(0),
  67. m_3DSample(0),
  68. m_stream(0),
  69. m_framesFaded(0)
  70. { }
  71. };
  72. struct ProviderInfo
  73. {
  74. AsciiString name;
  75. HPROVIDER id;
  76. Bool m_isValid;
  77. };
  78. struct OpenAudioFile
  79. {
  80. AILSOUNDINFO m_soundInfo;
  81. void *m_file;
  82. UnsignedInt m_openCount;
  83. UnsignedInt m_fileSize;
  84. Bool m_compressed; // if the file was compressed, then we need to free it with a miles function.
  85. // Note: OpenAudioFile does not own this m_eventInfo, and should not delete it.
  86. const AudioEventInfo *m_eventInfo; // Not mutable, unlike the one on AudioEventRTS.
  87. };
  88. typedef std::hash_map< AsciiString, OpenAudioFile, rts::hash<AsciiString>, rts::equal_to<AsciiString> > OpenFilesHash;
  89. typedef OpenFilesHash::iterator OpenFilesHashIt;
  90. class AudioFileCache
  91. {
  92. public:
  93. AudioFileCache();
  94. // Protected by mutex
  95. virtual ~AudioFileCache();
  96. void *openFile( AudioEventRTS *eventToOpenFrom );
  97. void closeFile( void *fileToClose );
  98. void setMaxSize( UnsignedInt size );
  99. // End Protected by mutex
  100. // Note: These functions should be used for informational purposes only. For speed reasons,
  101. // they are not protected by the mutex, so they are not guarenteed to be valid if called from
  102. // outside the audio cache. They should be used as a rough estimate only.
  103. UnsignedInt getCurrentlyUsedSize() const { return m_currentlyUsedSize; }
  104. UnsignedInt getMaxSize() const { return m_maxSize; }
  105. protected:
  106. void releaseOpenAudioFile( OpenAudioFile *fileToRelease );
  107. // This function will return TRUE if it was able to free enough space, and FALSE otherwise.
  108. Bool freeEnoughSpaceForSample(const OpenAudioFile& sampleThatNeedsSpace);
  109. OpenFilesHash m_openFiles;
  110. UnsignedInt m_currentlyUsedSize;
  111. UnsignedInt m_maxSize;
  112. HANDLE m_mutex;
  113. const char *m_mutexName;
  114. };
  115. class MilesAudioManager : public AudioManager
  116. {
  117. public:
  118. #if defined(_DEBUG) || defined(_INTERNAL)
  119. virtual void audioDebugDisplay(DebugDisplayInterface *dd, void *, FILE *fp = NULL );
  120. virtual AudioHandle addAudioEvent( const AudioEventRTS *eventToAdd ); ///< Add an audio event (event must be declared in an INI file)
  121. #endif
  122. // from AudioDevice
  123. virtual void init();
  124. virtual void postProcessLoad();
  125. virtual void reset();
  126. virtual void update();
  127. MilesAudioManager();
  128. virtual ~MilesAudioManager();
  129. virtual void nextMusicTrack( void );
  130. virtual void prevMusicTrack( void );
  131. virtual Bool isMusicPlaying( void ) const;
  132. virtual Bool hasMusicTrackCompleted( const AsciiString& trackName, Int numberOfTimes ) const;
  133. virtual AsciiString getMusicTrackName( void ) const;
  134. virtual void openDevice( void );
  135. virtual void closeDevice( void );
  136. virtual void *getDevice( void ) { return m_digitalHandle; }
  137. virtual void stopAudio( AudioAffect which );
  138. virtual void pauseAudio( AudioAffect which );
  139. virtual void resumeAudio( AudioAffect which );
  140. virtual void pauseAmbient( Bool shouldPause );
  141. virtual void killAudioEventImmediately( AudioHandle audioEvent );
  142. ///< Return whether the current audio is playing or not.
  143. ///< NOTE NOTE NOTE !!DO NOT USE THIS IN FOR GAMELOGIC PURPOSES!! NOTE NOTE NOTE
  144. virtual Bool isCurrentlyPlaying( AudioHandle handle );
  145. virtual void notifyOfAudioCompletion( UnsignedInt audioCompleted, UnsignedInt flags );
  146. virtual PlayingAudio *findPlayingAudioFrom( UnsignedInt audioCompleted, UnsignedInt flags );
  147. virtual UnsignedInt getProviderCount( void ) const;
  148. virtual AsciiString getProviderName( UnsignedInt providerNum ) const;
  149. virtual UnsignedInt getProviderIndex( AsciiString providerName ) const;
  150. virtual void selectProvider( UnsignedInt providerNdx );
  151. virtual void unselectProvider( void );
  152. virtual UnsignedInt getSelectedProvider( void ) const;
  153. virtual void setSpeakerType( UnsignedInt speakerType );
  154. virtual UnsignedInt getSpeakerType( void );
  155. virtual void *getHandleForBink( void );
  156. virtual void releaseHandleForBink( void );
  157. virtual void friend_forcePlayAudioEventRTS(const AudioEventRTS* eventToPlay);
  158. virtual UnsignedInt getNum2DSamples( void ) const;
  159. virtual UnsignedInt getNum3DSamples( void ) const;
  160. virtual UnsignedInt getNumStreams( void ) const;
  161. virtual Bool doesViolateLimit( AudioEventRTS *event ) const;
  162. virtual Bool isPlayingLowerPriority( AudioEventRTS *event ) const;
  163. virtual Bool isPlayingAlready( AudioEventRTS *event ) const;
  164. virtual Bool isObjectPlayingVoice( UnsignedInt objID ) const;
  165. Bool killLowestPrioritySoundImmediately( AudioEventRTS *event );
  166. AudioEventRTS* findLowestPrioritySound( AudioEventRTS *event );
  167. virtual void adjustVolumeOfPlayingAudio(AsciiString eventName, Real newVolume);
  168. virtual void removePlayingAudio( AsciiString eventName );
  169. virtual void removeAllDisabledAudio();
  170. virtual void processRequestList( void );
  171. virtual void processPlayingList( void );
  172. virtual void processFadingList( void );
  173. virtual void processStoppedList( void );
  174. Bool shouldProcessRequestThisFrame( AudioRequest *req ) const;
  175. void adjustRequest( AudioRequest *req );
  176. Bool checkForSample( AudioRequest *req );
  177. virtual void setHardwareAccelerated(Bool accel);
  178. virtual void setSpeakerSurround(Bool surround);
  179. virtual void setPreferredProvider(AsciiString provider) { m_pref3DProvider = provider; }
  180. virtual void setPreferredSpeaker(AsciiString speakerType) { m_prefSpeaker = speakerType; }
  181. virtual Real getFileLengthMS( AsciiString strToLoad ) const;
  182. virtual void closeAnySamplesUsingFile( const void *fileToClose );
  183. virtual Bool has3DSensitiveStreamsPlaying( void ) const;
  184. protected:
  185. // 3-D functions
  186. virtual void setDeviceListenerPosition( void );
  187. const Coord3D *getCurrentPositionFromEvent( AudioEventRTS *event );
  188. Bool isOnScreen( const Coord3D *pos ) const;
  189. Real getEffectiveVolume(AudioEventRTS *event) const;
  190. // Looping functions
  191. Bool startNextLoop( PlayingAudio *looping );
  192. void playStream( AudioEventRTS *event, HSTREAM stream );
  193. // Returns the file handle for attachment to the PlayingAudio structure
  194. void *playSample( AudioEventRTS *event, HSAMPLE sample );
  195. void *playSample3D( AudioEventRTS *event, H3DSAMPLE sample3D );
  196. protected:
  197. void buildProviderList( void );
  198. void createListener( void );
  199. void initDelayFilter( void );
  200. Bool isValidProvider( void );
  201. void initSamplePools( void );
  202. void processRequest( AudioRequest *req );
  203. void playAudioEvent( AudioEventRTS *event );
  204. void stopAudioEvent( AudioHandle handle );
  205. void pauseAudioEvent( AudioHandle handle );
  206. void *loadFileForRead( AudioEventRTS *eventToLoadFrom );
  207. void closeFile( void *fileRead );
  208. PlayingAudio *allocatePlayingAudio( void );
  209. void releaseMilesHandles( PlayingAudio *release );
  210. void releasePlayingAudio( PlayingAudio *release );
  211. void stopAllAudioImmediately( void );
  212. void freeAllMilesHandles( void );
  213. HSAMPLE getFirst2DSample( AudioEventRTS *event );
  214. H3DSAMPLE getFirst3DSample( AudioEventRTS *event );
  215. void adjustPlayingVolume( PlayingAudio *audio );
  216. void stopAllSpeech( void );
  217. protected:
  218. void initFilters( HSAMPLE sample, const AudioEventRTS *eventInfo );
  219. void initFilters3D( H3DSAMPLE sample, const AudioEventRTS *eventInfo, const Coord3D *pos );
  220. protected:
  221. ProviderInfo m_provider3D[MAXPROVIDERS];
  222. UnsignedInt m_providerCount;
  223. UnsignedInt m_selectedProvider;
  224. UnsignedInt m_lastProvider;
  225. UnsignedInt m_selectedSpeakerType;
  226. AsciiString m_pref3DProvider;
  227. AsciiString m_prefSpeaker;
  228. HDIGDRIVER m_digitalHandle;
  229. H3DPOBJECT m_listener;
  230. HPROVIDER m_delayFilter;
  231. // This is a list of all handles that are forcibly played. They always play as UI sounds.
  232. std::list<HAUDIO> m_audioForcePlayed;
  233. // Available handles for play. Note that there aren't handles open in advance for
  234. // streaming things, only 2-D and 3-D sounds.
  235. std::list<HSAMPLE> m_availableSamples;
  236. std::list<H3DSAMPLE> m_available3DSamples;
  237. // Currently Playing stuff. Useful if we have to preempt it.
  238. // This should rarely if ever happen, as we mirror this in Sounds, and attempt to
  239. // keep preemption from taking place here.
  240. std::list<PlayingAudio *> m_playingSounds;
  241. std::list<PlayingAudio *> m_playing3DSounds;
  242. std::list<PlayingAudio *> m_playingStreams;
  243. // Currently fading stuff. At this point, we just want to let it finish fading, when it is
  244. // done it should be added to the completed list, then "freed" and the counts should be updated
  245. // on the next update
  246. std::list<PlayingAudio *> m_fadingAudio;
  247. // Stuff that is done playing (either because it has finished or because it was killed)
  248. // This stuff should be cleaned up during the next update cycle. This includes updating counts
  249. // in the sound engine
  250. std::list<PlayingAudio *> m_stoppedAudio;
  251. AudioFileCache *m_audioCache;
  252. PlayingAudio *m_binkHandle;
  253. UnsignedInt m_num2DSamples;
  254. UnsignedInt m_num3DSamples;
  255. UnsignedInt m_numStreams;
  256. #if defined(_DEBUG) || defined(_INTERNAL)
  257. typedef std::set<AsciiString> SetAsciiString;
  258. typedef SetAsciiString::iterator SetAsciiStringIt;
  259. SetAsciiString m_allEventsLoaded;
  260. void dumpAllAssetsUsed();
  261. #endif
  262. };