GameSpeech.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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. //----------------------------------------------------------------------------
  19. //
  20. // Westwood Studios Pacific.
  21. //
  22. // Confidential Information
  23. // Copyright(C) 2001 - All Rights Reserved
  24. //
  25. //----------------------------------------------------------------------------
  26. //
  27. // Project: RTS3
  28. //
  29. // File name: GameSpeech.cpp
  30. //
  31. // Created: 10/30/01
  32. //
  33. //----------------------------------------------------------------------------
  34. //----------------------------------------------------------------------------
  35. // Includes
  36. //----------------------------------------------------------------------------
  37. #include "wpaudio/attributes.h"
  38. #include "wsys/File.h"
  39. #include "wsys/List.h"
  40. #include "wpaudio/Streamer.h"
  41. #include "wpaudio/Time.h"
  42. #include "wpaudio/Device.h"
  43. #include "wpaudio/Streamer.h"
  44. #define DEFINE_DLG_EVENT_PRIORITY_NAMES
  45. #include "Common/GameAudio.h"
  46. #include "Common/GameSpeech.h"
  47. #include "Common/INI.h"
  48. #include "Common/STLTypedefs.h"
  49. //----------------------------------------------------------------------------
  50. // Externals
  51. //----------------------------------------------------------------------------
  52. //----------------------------------------------------------------------------
  53. // Defines
  54. //----------------------------------------------------------------------------
  55. #define BASE_DLG_ID ((ID) 5000)
  56. #define BASE_DLG_DIR "Data\\Audio\\Sounds"
  57. #define BASE_DLG_EXT "wav"
  58. #define NUM_DLG_PRIORITIES 5
  59. //----------------------------------------------------------------------------
  60. // Private Types
  61. //----------------------------------------------------------------------------
  62. //===============================
  63. // Speech
  64. //===============================
  65. /**
  66. * The Speech struct holds all information about a line of dialog. While Speech itself is not
  67. * accessible from outside SpeechInterface, the embedded struct SpeechInfo is accessible to the game code.
  68. * Place data in SpeechInfo that is useful to the game code in determining what speech to play.
  69. */
  70. //===============================
  71. struct Speech
  72. {
  73. Speech();
  74. ~Speech();
  75. ID id; ///< Unquie GDI id
  76. Int index; ///< Speech index
  77. AsciiString name; ///< Logical name for line of speech
  78. Real volume; ///< Mixing level for this line
  79. SpeechInfo info; ///< Game info about this line of dialog (public)
  80. Bool valid; ///< Is this entry have valid data
  81. Int priority; ///< speech priority
  82. Int timeout; ///< speech time out in milliseconds
  83. Int interrupt; ///< speech interrupts current playing speech
  84. };
  85. static void speechFromInfo(SpeechInfo* pInSpeechInfo, Speech* pOutSpeech);
  86. static AsciiString getNextFilenameFromSpeech(Speech* pSpeechToGetFilenameFrom);
  87. //===============================
  88. // SpeechRequest
  89. //===============================
  90. /**
  91. * This is an internal structure used by the SpeechManager. It holds information
  92. * about the playback of a line of dialog.
  93. */
  94. //===============================
  95. struct SpeechItem
  96. {
  97. enum Flags
  98. {
  99. PAUSED = 0x00000001
  100. };
  101. LListNode nd;
  102. Flags flags;
  103. Speech *speech; ///< What to say
  104. Int priority; ///< Priority of this line
  105. TimeStamp timeout; ///< Time when this event is stale in milliseconds
  106. };
  107. //===============================
  108. // Speaker
  109. //===============================
  110. /**
  111. * Actual implementation of SpeakerInterface
  112. */
  113. //===============================
  114. class SpeechManager;
  115. class Speaker : public SpeakerInterface
  116. {
  117. AsciiString m_name; ///< name of this speaker
  118. AudioStreamer *m_stream; ///< Audio streamer for this speaker
  119. File *m_file; ///< file we are streaming
  120. Int m_priority; ///< Speaker's priority
  121. Int m_paused; ///< paused level
  122. TimeStamp m_delay; ///< current delay interval
  123. TimeStamp m_delayTime; ///< delay time between speeches
  124. SpeechManager *m_manager; ///< Manager that created use
  125. // info about current playing dialog
  126. SpeechItem *m_currentSpeech; ///< currently playing speech
  127. // list of pending speech items
  128. LList m_pending; ///< list of pending SpeechItems
  129. public:
  130. Speaker();
  131. ~Speaker();
  132. virtual void destroy( void ); ///< Delete and free speaker
  133. /// Submits speech to play
  134. virtual void say ( Speech *speech, // speech to say
  135. Int priority, // priority
  136. Int timeout, // time in which to say this line, 0 = infinite
  137. Int interrupt); // whether to interrupt current line
  138. virtual void say ( Char *speechName, // name ofspeech to say
  139. Int priority, // priority
  140. Int timeout, // time in which to say this line, 0 = infinite
  141. Int interrupt ); // whether to interrupt current line
  142. virtual void setPriority ( Int priority ); ///< Set speaker's priority level
  143. virtual void setDelay ( Int delay ); ///< Set speaker's delay between speeches( in milliseconds)
  144. virtual void setBuffering ( Int buffer_time ); ///< Set speaker's amount of buffer (in milliseconds)
  145. virtual Int getPriority ( void ); ///< Get speaker's priority level
  146. virtual Int getDelay ( void ); ///< Get speaker's delay between speeches( in milliseconds)
  147. virtual Int getBuffering ( void ); ///< Get speaker's amount of buffer (in milliseconds)
  148. virtual void pause ( void ); ///< pause speaker
  149. virtual void resume ( void ); ///< resume speaking
  150. virtual void stop ( void ); ///< stop speaking (flushes queue)
  151. virtual void cancel ( Speech *speech ); ///< cancel pending line of dialog
  152. virtual Bool hasSaid ( Speech *speech ); ///< is line of dialog no longer pending
  153. virtual Bool isGoingToSay ( Speech *speech ); ///< check in line of dialog is pending
  154. virtual Bool isTalking ( void ); ///< is speaker talking
  155. virtual Speech* saying ( void ); ///< returns currently spoken speech
  156. virtual void update( void ); ///< Service speaker
  157. void init ( char *name, Int priority, Int btime, Int delay ); ///< initialized speaker for use
  158. void deinit( void );
  159. void setManager( SpeechManager *manager ) { m_manager = manager;};
  160. SpeechItem* firstItem( TimeStamp now = 0 );
  161. SpeechItem* nextItem( SpeechItem *item, TimeStamp now = 0 );
  162. SpeechItem* findItem( Speech *speech );
  163. void flush( void );
  164. };
  165. typedef std::vector<Speech> VecSpeech;
  166. typedef std::hash_map<const char*, Speech, std::hash<const char*>, rts::equal_to<const char*> > HashSpeech;
  167. //===============================
  168. // SpeechManager:
  169. //===============================
  170. /**
  171. * Actual implementation od SpeechInterface
  172. */
  173. //===============================
  174. class SpeechManager: public SpeechInterface
  175. {
  176. protected:
  177. struct AudioAttribs *m_masterAttribs; ///< Attribs for all speech
  178. struct AudioAttribs *m_masterFadeAttribs; ///< Fade attribs for all speech
  179. Real m_volume; ///< Current speech volume
  180. Bool m_on; ///< Is speech turned on?
  181. Int m_count; ///< Number of speeches in the table
  182. struct AudioDeviceTag *m_device; ///< The audio device we were initilaized with
  183. LList m_speakers; ///< list of speakers created by this manager
  184. HashSpeech m_speech; ///< This is a hash table of all speeches we currently have loaded.
  185. VecAsciiString m_speechNames; ///< The purpose of this is to return to someone asking for the name of some element.
  186. VecSpeech m_temporarySpeeches; ///< This is a vector of speeches that are _like_ something in the hash, but differ by priority.
  187. AsciiString getFilenameForPlay( Speech *speech ); ///< Return the filename corresponding to this speech
  188. public:
  189. SpeechManager();
  190. virtual ~SpeechManager();
  191. virtual Bool init( void ); ///< Initlaizes the speech system
  192. virtual void deinit( void ); ///< De-initlaizes the speech system
  193. virtual void update( void ); ///< Services speech tasks. Called by AudioInterface
  194. virtual void reset( void ); ///< Resets the speech system
  195. virtual void loseFocus( void ); ///< Called when application loses focus
  196. virtual void regainFocus( void ); ///< Called when application regains focus
  197. // speech info access
  198. virtual Int numSpeeches( void ); ///< Returns the number of speechs defined
  199. virtual SpeechInfo* getSpeechInfo( Speech *speech ); ///< Returns speech info by speech
  200. virtual Int getSpeechIndex( Speech *speech ); ///< Returns speech index
  201. virtual ID getSpeechID( Speech *speech ); ///< Returns speech ID
  202. virtual Speech* getSpeech( Int index ); ///< Converts index to speech
  203. virtual Speech* getSpeech( ID id ); ///< Converts is to speech
  204. virtual Speech* getSpeech( const Char *speech_name );///< Converts speech name to speech
  205. virtual const Char* getSpeechName( Speech *speech ); ///< Converts speech to speech name
  206. // volume control
  207. virtual void fadeIn( void ); ///< Fade all speech in
  208. virtual void fadeOut( void ); ///< Fade all speech out
  209. virtual void fade( Real fade_value ); ///< Fade all speech by a specified amout (1.0 .. 0.0 )
  210. virtual Bool isFading( void ); ///< Returns whether or not any speech is in the process of a fade
  211. virtual void waitForFade( void ); ///< Returns when all fading has finished
  212. virtual void setVolume( Real new_volume ); ///< Set new volume level for all speech 1.0 (loudest) 0.0 (silent)
  213. virtual Real getVolume( void ); ///< Return current volume level for all speech
  214. /// Speaker factory
  215. virtual SpeakerInterface* createSpeaker ( Char *name,
  216. Int priority,
  217. Int buffer_time,
  218. Int delay);
  219. virtual Speech* addNewSpeech( SpeechInfo* pSpeechToAdd); ///< Create a speech specified by pSpeechToAdd
  220. virtual Speech* addTemporaryDialog( Speech& temporarySpeech ); ///< Speech to be taken over by
  221. // playback control
  222. virtual void stop( void ); ///< Stops all speech that's currently playing.
  223. virtual void pause( void ); ///< Pauses all speech
  224. virtual void resume( void ); ///< Resumes all paused speech
  225. virtual void turnOff( void ); ///< Turns off all speech. No more speech will play
  226. virtual void turnOn( void ); ///< Turns speech on.
  227. virtual Bool waitToStop( Int milliseconds ); ///< Returns when all speech has completely stopped playing
  228. virtual Bool say( Speech* pSpeechToSay); ///< Attempts to add a speech to the speach queue.
  229. virtual Bool say( AsciiString speechName); ///< Attempts to find and add a speech to the speech queue
  230. // info
  231. virtual Bool isOn( void ); ///< Returns whether or not speech is turned on at present
  232. virtual struct AudioAttribs* getMasterAttribs( void ); ///< Returns the master attribute control for all speech
  233. virtual struct AudioAttribs* getMasterFadeAttribs( void );///< Returns the master fade control for all speech
  234. virtual void addDialogEvent(const AudioEventRTS *eventRTS, Speech* speech, AudioEventRTS *returnEvent); ///< Add an event. speech can be NULL
  235. virtual void removeDialogEvent(const AudioEventRTS *eventRTS, AudioEventRTS *eventToUse); ///< remove an event, eventToUse shouldn't be NULL.
  236. void removeSpeaker ( Speaker *speaker );
  237. void addSpeaker ( Speaker *speaker );
  238. // empty string means that this sound wasn't found or some error occurred. CHECK FOR EMPTY STRING.
  239. virtual AsciiString getFilenameForPlayFromAudioEvent( const AudioEventRTS *eventToGetFrom );
  240. };
  241. //----------------------------------------------------------------------------
  242. // Private Data
  243. //----------------------------------------------------------------------------
  244. //----------------------------------------------------------------------------
  245. // Public Data
  246. //----------------------------------------------------------------------------
  247. //----------------------------------------------------------------------------
  248. // Private Prototypes
  249. //----------------------------------------------------------------------------
  250. //----------------------------------------------------------------------------
  251. // Private Functions
  252. //----------------------------------------------------------------------------
  253. //----------------------------------------------------------------------------
  254. // Public Functions
  255. //----------------------------------------------------------------------------
  256. //============================================================================
  257. // createSpeechInterface
  258. //============================================================================
  259. SpeechInterface* CreateSpeechInterface( void )
  260. {
  261. return NEW SpeechManager;
  262. }
  263. //============================================================================
  264. // Speech::Speech
  265. //============================================================================
  266. Speech::Speech()
  267. : id(INVALID_ID),
  268. valid(FALSE),
  269. volume(TheAudio->VOLUME_MIN)
  270. {
  271. }
  272. //============================================================================
  273. // Speech::~Speech
  274. //============================================================================
  275. Speech::~Speech()
  276. {
  277. }
  278. //============================================================================
  279. // SpeechManager::SpeechManager
  280. //============================================================================
  281. SpeechManager::SpeechManager()
  282. : m_on(FALSE),
  283. m_device(NULL),
  284. m_masterAttribs(NULL),
  285. m_masterFadeAttribs(NULL),
  286. m_speech(NULL),
  287. m_count(0)
  288. {
  289. if( (m_masterFadeAttribs = NEW AudioAttribs) != 0 ) // poolify
  290. {
  291. AudioAttribsInit( m_masterFadeAttribs );
  292. }
  293. if( (m_masterAttribs = NEW AudioAttribs) != 0 )
  294. {
  295. AudioAttribsInit( m_masterAttribs );
  296. }
  297. }
  298. //============================================================================
  299. // SpeechManager::~SpeechManager
  300. //============================================================================
  301. SpeechManager::~SpeechManager()
  302. {
  303. deinit();
  304. delete m_masterAttribs;
  305. delete m_masterFadeAttribs;
  306. }
  307. //============================================================================
  308. // SpeechManager::init
  309. //============================================================================
  310. Bool SpeechManager::init( void )
  311. {
  312. deinit();
  313. if ( m_device )
  314. {
  315. return TRUE;
  316. }
  317. m_device = TheAudio->device();
  318. if( m_device )
  319. {
  320. AudioDeviceAttribsAdd( m_device, m_masterAttribs );
  321. AudioDeviceAttribsAdd( m_device, m_masterFadeAttribs );
  322. }
  323. for (int i = 0; i < NUM_DLG_PRIORITIES; ++i) {
  324. createSpeaker(dlgPriorityNames[i], (i + 1), 3000, 300);
  325. }
  326. m_on = TRUE;
  327. return TRUE;
  328. }
  329. //============================================================================
  330. // SpeechManager::deinit
  331. //============================================================================
  332. void SpeechManager::deinit( void )
  333. {
  334. m_count = 0;
  335. m_speech.clear();
  336. m_speechNames.clear();
  337. for (int i = 0; i < NUM_DLG_PRIORITIES && m_speakers.firstNode(); ++i) {
  338. ((Speaker*) m_speakers.firstNode()->item())->destroy();
  339. }
  340. stop();
  341. if( m_device )
  342. {
  343. AudioDeviceAttribsRemove( m_device, m_masterAttribs );
  344. AudioDeviceAttribsRemove( m_device, m_masterFadeAttribs );
  345. m_device = NULL;
  346. }
  347. }
  348. //============================================================================
  349. // SpeechManager::update
  350. //============================================================================
  351. void SpeechManager::update( void )
  352. {
  353. LListNode *node = m_speakers.firstNode();
  354. while ( node )
  355. {
  356. Speaker *speaker = (Speaker *) node->item();
  357. if ( speaker )
  358. {
  359. speaker->update();
  360. }
  361. node = node->next();
  362. }
  363. }
  364. //============================================================================
  365. // SpeechManager::reset
  366. //============================================================================
  367. void SpeechManager::reset( void )
  368. {
  369. }
  370. //============================================================================
  371. // SpeechManager::loseFocus
  372. //============================================================================
  373. void SpeechManager::loseFocus( void )
  374. {
  375. }
  376. //============================================================================
  377. // SpeechManager::regainFocus
  378. //============================================================================
  379. void SpeechManager::regainFocus( void )
  380. {
  381. }
  382. //============================================================================
  383. // SpeechManager::numSpeeches
  384. //============================================================================
  385. Int SpeechManager::numSpeeches( void )
  386. {
  387. return m_count ;
  388. }
  389. //============================================================================
  390. // SpeechManager::getSpeechInfo
  391. //============================================================================
  392. SpeechInfo* SpeechManager::getSpeechInfo( Speech *speech )
  393. {
  394. if( speech )
  395. {
  396. return &speech->info ;
  397. }
  398. return NULL;
  399. }
  400. //============================================================================
  401. // SpeechManager::getSpeechIndex
  402. //============================================================================
  403. Int SpeechManager::getSpeechIndex( Speech *speech )
  404. {
  405. if( speech )
  406. {
  407. return speech->index ;
  408. }
  409. return SpeechInterface::INVALID_SPEECH;
  410. }
  411. //============================================================================
  412. // SpeechManager::getSpeechID
  413. //============================================================================
  414. ID SpeechManager::getSpeechID( Speech *speech )
  415. {
  416. if( speech )
  417. {
  418. return speech->id ;
  419. }
  420. return INVALID_ID;
  421. }
  422. //============================================================================
  423. // SpeechManager::getSpeech
  424. //============================================================================
  425. Speech* SpeechManager::getSpeech( const Char *name )
  426. {
  427. HashSpeech::iterator it = m_speech.find(name);
  428. if (it != m_speech.end()) {
  429. return &(*it).second;
  430. }
  431. return NULL;
  432. }
  433. //============================================================================
  434. // SpeechManager::getSpeech
  435. //============================================================================
  436. Speech* SpeechManager::getSpeech( ID id )
  437. {
  438. if ( m_count == 0 )
  439. {
  440. return NULL;
  441. }
  442. for (int i = 0; i < m_speechNames.size(); ++i) {
  443. Speech* pSpeech = getSpeech(m_speechNames[i].str());
  444. if (pSpeech && pSpeech->id == id) {
  445. return pSpeech;
  446. }
  447. }
  448. return NULL;
  449. }
  450. //============================================================================
  451. // SpeechManager::getSpeech
  452. //============================================================================
  453. Speech* SpeechManager::getSpeech( int index )
  454. {
  455. if (index < 0 || index > m_count) {
  456. return NULL;
  457. }
  458. return getSpeech(m_speechNames[index].str());
  459. }
  460. //============================================================================
  461. // SpeechManager::getSpeechName
  462. //============================================================================
  463. const Char* SpeechManager::getSpeechName( Speech *speech )
  464. {
  465. if( speech )
  466. {
  467. return speech->name.str();
  468. }
  469. return "(null)";
  470. }
  471. //============================================================================
  472. // SpeechManager::fadeIn
  473. //============================================================================
  474. void SpeechManager::fadeIn( void )
  475. {
  476. if(m_masterFadeAttribs )
  477. {
  478. AudioAttribsAdjustVolume( m_masterFadeAttribs, AUDIO_VOLUME_MAX );
  479. }
  480. }
  481. //============================================================================
  482. // SpeechManager::fadeOut
  483. //============================================================================
  484. void SpeechManager::fadeOut( void )
  485. {
  486. if(m_masterFadeAttribs )
  487. {
  488. AudioAttribsAdjustVolume( m_masterFadeAttribs, AUDIO_VOLUME_MIN );
  489. }
  490. }
  491. //============================================================================
  492. // SpeechManager::fade
  493. //============================================================================
  494. void SpeechManager::fade( Real new_volume )
  495. {
  496. if(m_masterFadeAttribs )
  497. {
  498. AudioAttribsAdjustVolume( m_masterFadeAttribs, TheAudio->convertRealVolume( new_volume) );
  499. }
  500. }
  501. //============================================================================
  502. // SpeechManager::isFading
  503. //============================================================================
  504. Bool SpeechManager::isFading( void )
  505. {
  506. if( m_masterFadeAttribs )
  507. {
  508. return !AudioAttribsVolumeAdjusted( m_masterFadeAttribs );
  509. }
  510. return FALSE;
  511. }
  512. //============================================================================
  513. // SpeechManager::waitForFade
  514. //============================================================================
  515. void SpeechManager::waitForFade( void )
  516. {
  517. }
  518. //============================================================================
  519. // SpeechManager::setVolume
  520. //============================================================================
  521. void SpeechManager::setVolume( Real new_volume )
  522. {
  523. m_volume = TheAudio->validateVolume(new_volume);
  524. if( m_masterAttribs )
  525. {
  526. AudioAttribsSetVolume( m_masterAttribs, TheAudio->convertRealVolume( new_volume ));
  527. }
  528. }
  529. //============================================================================
  530. // SpeechManager::getVolume
  531. //============================================================================
  532. Real SpeechManager::getVolume( void )
  533. {
  534. return m_volume;
  535. }
  536. void SpeechManager::stop( void )
  537. {
  538. LListNode *node = m_speakers.firstNode();
  539. while ( node )
  540. {
  541. Speaker *speaker = (Speaker *) node->item();
  542. if ( speaker )
  543. {
  544. speaker->stop();
  545. }
  546. node = node->next();
  547. }
  548. }
  549. //============================================================================
  550. // SpeechManager::pause
  551. //============================================================================
  552. void SpeechManager::pause( void )
  553. {
  554. LListNode *node = m_speakers.firstNode();
  555. while ( node )
  556. {
  557. Speaker *speaker = (Speaker *) node->item();
  558. if ( speaker )
  559. {
  560. speaker->pause();
  561. }
  562. node = node->next();
  563. }
  564. }
  565. //============================================================================
  566. // SpeechManager::resume
  567. //============================================================================
  568. void SpeechManager::resume( void )
  569. {
  570. LListNode *node = m_speakers.firstNode();
  571. while ( node )
  572. {
  573. Speaker *speaker = (Speaker *) node->item();
  574. if ( speaker )
  575. {
  576. speaker->resume();
  577. }
  578. node = node->next();
  579. }
  580. }
  581. //============================================================================
  582. // SpeechManager::turnOff
  583. //============================================================================
  584. void SpeechManager::turnOff( void )
  585. {
  586. stop();
  587. m_on = FALSE;
  588. }
  589. //============================================================================
  590. // SpeechManager::turnOn
  591. //============================================================================
  592. void SpeechManager::turnOn( void )
  593. {
  594. m_on = TRUE;
  595. }
  596. //============================================================================
  597. // SpeechManager::waitToStop
  598. //============================================================================
  599. Bool SpeechManager::waitToStop( Int milliseconds )
  600. {
  601. return TRUE;
  602. }
  603. //============================================================================
  604. // SpeechManager::say
  605. //============================================================================
  606. Bool SpeechManager::say( Speech* pSpeechToSay)
  607. {
  608. // TBD: Find the other conditions
  609. if (m_speakers.isEmpty() || !pSpeechToSay) {
  610. return false;
  611. }
  612. LListNode *node = m_speakers.firstNode();
  613. for (int i = 0; i < pSpeechToSay->priority && node; ++i) {
  614. node = node->next();
  615. }
  616. if (!node) {
  617. return false;
  618. }
  619. Speaker* currentSpeaker = (Speaker*) node->item();
  620. currentSpeaker->say(pSpeechToSay, pSpeechToSay->priority, pSpeechToSay->timeout, pSpeechToSay->interrupt);
  621. return true;
  622. }
  623. //============================================================================
  624. // SpeechManager::say
  625. //============================================================================
  626. Bool SpeechManager::say( AsciiString speechName)
  627. {
  628. Speech* pSpeech = getSpeech(speechName.str());
  629. if (pSpeech) {
  630. return false;
  631. }
  632. return say(pSpeech);
  633. }
  634. //============================================================================
  635. // SpeechManager::isOn
  636. //============================================================================
  637. Bool SpeechManager::isOn( void )
  638. {
  639. return m_on;
  640. }
  641. //============================================================================
  642. // SpeechManager::removeSpeaker
  643. //============================================================================
  644. void SpeechManager::removeSpeaker ( Speaker *speaker )
  645. {
  646. LListNode *node = m_speakers.findItem( speaker );
  647. if ( node )
  648. {
  649. node->remove();
  650. node->destroy();
  651. }
  652. }
  653. //============================================================================
  654. // SpeechManager::addSpeaker
  655. //============================================================================
  656. void SpeechManager::addSpeaker ( Speaker *speaker )
  657. {
  658. // remove it if it is already in the list
  659. removeSpeaker( speaker );
  660. m_speakers.addItem( speaker->getPriority(), speaker );
  661. }
  662. //============================================================================
  663. // SpeechManager::CreateSpeaker
  664. //============================================================================
  665. SpeakerInterface* SpeechManager::createSpeaker ( Char *name, Int priority, Int btime, Int delay )
  666. {
  667. Speaker *speaker = NEW Speaker; // poolify
  668. if ( speaker )
  669. {
  670. speaker->setManager( this );
  671. speaker->init( name, priority, btime, delay );
  672. m_speakers.addItem( priority, speaker );
  673. }
  674. return speaker;
  675. }
  676. //============================================================================
  677. // SpeechManager::addNewSpeech
  678. //============================================================================
  679. Speech* SpeechManager::addNewSpeech( SpeechInfo* pSpeechToAdd )
  680. {
  681. if (!pSpeechToAdd) {
  682. return NULL;
  683. }
  684. const char* entryKey = pSpeechToAdd->m_dialogEvent.str();
  685. if (m_speech.find(entryKey) == m_speech.end()) {
  686. ++m_count;
  687. m_speechNames.push_back(pSpeechToAdd->m_dialogEvent);
  688. }
  689. Speech newSpeech;
  690. speechFromInfo(pSpeechToAdd, &newSpeech);
  691. newSpeech.id = (ID) (m_count + BASE_DLG_ID);
  692. m_speech[entryKey] = newSpeech;
  693. return &m_speech[entryKey];
  694. }
  695. //============================================================================
  696. // SpeechManager::addTemporaryDialog
  697. //============================================================================
  698. Speech* SpeechManager::addTemporaryDialog( Speech& temporarySpeech )
  699. {
  700. m_temporarySpeeches.push_back(temporarySpeech);
  701. return &(m_temporarySpeeches.back());
  702. }
  703. //============================================================================
  704. // SpeechManager::getMasterAttribs
  705. //============================================================================
  706. struct AudioAttribs* SpeechManager::getMasterAttribs( void )
  707. {
  708. return m_masterAttribs;
  709. };
  710. //============================================================================
  711. // SpeechManager::getMasterFadeAttribs
  712. //============================================================================
  713. struct AudioAttribs* SpeechManager::getMasterFadeAttribs( void )
  714. {
  715. return m_masterFadeAttribs;
  716. };
  717. //============================================================================
  718. // SpeechManager::getMasterFadeAttribs
  719. //============================================================================
  720. void SpeechManager::addDialogEvent(const AudioEventRTS *eventRTS, Speech* speech, AudioEventRTS *returnEvent)
  721. {
  722. if (!eventRTS) {
  723. return;
  724. }
  725. Speech *useSpeech = speech;
  726. if (!useSpeech) {
  727. useSpeech = getSpeech(eventRTS->m_eventName.str());
  728. }
  729. if (!useSpeech) {
  730. return;
  731. }
  732. if (1 || (eventRTS->m_priorityBoost == 0 &&
  733. eventRTS->m_overrideLoopingBehavior == 0 &&
  734. eventRTS->m_timeOfDay == useSpeech->info.m_timeOfDay)) {
  735. // We can use the default sound, so no need to create and add a new one.
  736. say(useSpeech);
  737. if (returnEvent) {
  738. (*returnEvent) = (*eventRTS);
  739. (*returnEvent).m_speechToPlay = useSpeech;
  740. (*returnEvent).m_isCurrentlyPlaying = true;
  741. }
  742. return;
  743. }
  744. Speech tmpSpeech = (*useSpeech);
  745. tmpSpeech.info.m_priority += eventRTS->m_priorityBoost;
  746. // Can't go less than 0 priority or > NUM_DLG_PRIORITIES
  747. if (tmpSpeech.info.m_priority < 0) {
  748. tmpSpeech.info.m_priority = 0;
  749. } else if (tmpSpeech.info.m_priority > NUM_DLG_PRIORITIES) {
  750. tmpSpeech.info.m_priority = NUM_DLG_PRIORITIES;
  751. }
  752. tmpSpeech.info.m_timeOfDay = eventRTS->m_timeOfDay;
  753. #if 0
  754. // CRASHING BUG IN HERE. NEED TO FIX.
  755. Speech* playSpeech = addTemporaryDialog(tmpSpeech);
  756. if (returnEvent) {
  757. (*returnEvent) = (*eventRTS);
  758. returnEvent->m_speechToPlay = NULL;
  759. }
  760. say(playSpeech);
  761. #endif
  762. }
  763. //============================================================================
  764. // SpeechManager::removeDialogEvent
  765. //============================================================================
  766. void SpeechManager::removeDialogEvent(const AudioEventRTS *eventRTS, AudioEventRTS* eventToUse)
  767. {
  768. if (!eventRTS) {
  769. return;
  770. }
  771. Speech *useSpeech = eventRTS->m_speechToPlay;
  772. if (!useSpeech) {
  773. useSpeech = getSpeech(eventRTS->m_eventName.str());
  774. }
  775. if (!useSpeech) {
  776. return;
  777. }
  778. if (eventToUse) {
  779. (*eventToUse) = (*eventRTS);
  780. eventToUse->m_speechToPlay = useSpeech;
  781. }
  782. Bool isPlaying = true;
  783. LListNode *node = m_speakers.firstNode();
  784. while (node && isPlaying) {
  785. Speaker *pCurrSpeaker = (Speaker*) node->item();
  786. if (pCurrSpeaker->saying() == useSpeech || pCurrSpeaker->isGoingToSay(useSpeech)) {
  787. pCurrSpeaker->cancel(useSpeech);
  788. isPlaying = false;
  789. }
  790. }
  791. if (eventToUse) {
  792. (*eventToUse).m_isCurrentlyPlaying = isPlaying;
  793. }
  794. }
  795. //============================================================================
  796. // SpeechManager::getFilenameForPlayFromAudioEvent
  797. //============================================================================
  798. AsciiString SpeechManager::getFilenameForPlayFromAudioEvent( const AudioEventRTS *eventToGetFrom )
  799. {
  800. if (!eventToGetFrom) {
  801. return AsciiString::TheEmptyString;
  802. }
  803. if (eventToGetFrom->m_eventName.isEmpty()) {
  804. return AsciiString::TheEmptyString;
  805. }
  806. Speech* speech = getSpeech(eventToGetFrom->m_eventName.str());
  807. return getFilenameForPlay(speech);
  808. }
  809. //============================================================================
  810. // SpeechManager::getFilenameForPlay
  811. //============================================================================
  812. AsciiString SpeechManager::getFilenameForPlay( Speech *speech )
  813. {
  814. if (!speech) {
  815. return AsciiString::TheEmptyString;
  816. }
  817. SpeechInfo* speechInfo = getSpeechInfo(speech);
  818. if (!speechInfo) {
  819. return AsciiString::TheEmptyString;
  820. }
  821. Int regularSamples = speechInfo->m_dialogFiles.size();
  822. Int eveningSamples = speechInfo->m_dialogFilesEvening.size();
  823. Int morningSamples = speechInfo->m_dialogFilesMorning.size();
  824. Int nightSamples = speechInfo->m_dialogFilesNight.size();
  825. Int numSamples = regularSamples + eveningSamples + morningSamples + nightSamples;
  826. // using a random number generator, select a random sample from all the samples
  827. // that correspond to this sound name
  828. Int soundToPlay = GameClientRandomValue(0, numSamples - 1);
  829. char name[_MAX_PATH];
  830. if (soundToPlay < regularSamples) {
  831. strcpy(name, speechInfo->m_dialogFiles[soundToPlay].str());
  832. } else if (soundToPlay < (regularSamples + eveningSamples)) {
  833. strcpy(name, speechInfo->m_dialogFilesEvening[soundToPlay - regularSamples].str());
  834. } else if (soundToPlay < (regularSamples + eveningSamples + morningSamples)) {
  835. strcpy(name, speechInfo->m_dialogFilesMorning[soundToPlay - regularSamples - eveningSamples].str());
  836. } else if (soundToPlay < numSamples) {
  837. strcpy(name, speechInfo->m_dialogFilesNight[soundToPlay - regularSamples - eveningSamples - morningSamples].str());
  838. } else {
  839. return AsciiString::TheEmptyString;
  840. }
  841. // @todo this should all go into a function "generateLocalizedSoundName"
  842. Bool localized = FALSE;
  843. if ( name[0] == '$' )
  844. {
  845. localized = TRUE;
  846. char nameTemp[_MAX_PATH];
  847. strcpy(nameTemp, name + 1);
  848. strcpy(name, nameTemp);
  849. }
  850. if ( name[0] == 0 )
  851. {
  852. return NULL;
  853. }
  854. AsciiString path;
  855. char *localDir = "";
  856. if ( localized )
  857. {
  858. localDir = "english\\";
  859. }
  860. path.format( "data\\audio\\sounds\\%s%s.wav", localDir, name);
  861. return path;
  862. }
  863. //============================================================================
  864. //============================================================================
  865. //============================================================================
  866. // Speaker Code
  867. //============================================================================
  868. //============================================================================
  869. //============================================================================
  870. //============================================================================
  871. // Speaker::Speaker
  872. //============================================================================
  873. Speaker::Speaker()
  874. : m_delayTime(300),
  875. m_priority(0),
  876. m_currentSpeech(NULL),
  877. m_file(NULL),
  878. m_stream(NULL)
  879. {
  880. m_name.set("Unnamed");
  881. }
  882. //============================================================================
  883. // Speaker::~Speaker
  884. //============================================================================
  885. Speaker::~Speaker()
  886. {
  887. deinit();
  888. }
  889. //============================================================================
  890. // Speaker::init
  891. //============================================================================
  892. void Speaker::init( char *name, Int priority, Int btime, Int delay )
  893. {
  894. deinit();
  895. m_name.set( name );
  896. m_priority = priority;
  897. m_delayTime = delay;
  898. m_stream = AudioStreamerCreate( TheAudio->device(), btime );
  899. if ( m_stream )
  900. {
  901. AudioStreamerSetName ( m_stream, name );
  902. AudioStreamerSetAttribs ( m_stream, m_manager->getMasterAttribs());
  903. AudioStreamerSetFadeAttribs ( m_stream, m_manager->getMasterFadeAttribs() );
  904. AudioStreamerSetPriority ( m_stream, m_priority );
  905. }
  906. }
  907. //============================================================================
  908. // Speaker::deinit
  909. //============================================================================
  910. void Speaker::deinit( void )
  911. {
  912. stop();
  913. if ( m_stream != NULL )
  914. {
  915. AudioStreamerDestroy ( m_stream );
  916. m_stream = NULL;
  917. }
  918. }
  919. //============================================================================
  920. // Speaker::firstItem
  921. //============================================================================
  922. SpeechItem* Speaker::firstItem( TimeStamp now )
  923. {
  924. LListNode *node = m_pending.firstNode();
  925. SpeechItem *item = NULL;
  926. while ( node )
  927. {
  928. item = (SpeechItem*) node->item();
  929. if ( !now || !item->timeout || item->timeout >= now )
  930. {
  931. break;
  932. }
  933. // this dialog item has timed out so remove it
  934. node = item->nd.next();
  935. item->nd.remove();
  936. delete item;
  937. item = NULL;
  938. }
  939. return item;
  940. }
  941. //============================================================================
  942. // Speaker::nextItem
  943. //============================================================================
  944. SpeechItem* Speaker::nextItem ( SpeechItem *item, TimeStamp now )
  945. {
  946. LListNode *node = NULL;
  947. SpeechItem *next = NULL;
  948. if ( item )
  949. {
  950. node = item->nd.next();
  951. }
  952. while ( node )
  953. {
  954. next = (SpeechItem*) node->item();
  955. if ( !now || !next->timeout || next->timeout >= now )
  956. {
  957. break;
  958. }
  959. // this dialog item has timed out so remove it
  960. node = next->nd.next();
  961. next->nd.remove();
  962. delete next;
  963. next = NULL;
  964. }
  965. return next;
  966. }
  967. //============================================================================
  968. // Speaker::findItem
  969. //============================================================================
  970. SpeechItem* Speaker::findItem( Speech *speech )
  971. {
  972. TimeStamp now = AudioGetTime();
  973. SpeechItem *item = firstItem ( now );
  974. while ( item )
  975. {
  976. if ( item->speech == speech )
  977. {
  978. break;
  979. }
  980. item = nextItem( item, now );
  981. }
  982. return item;
  983. }
  984. //============================================================================
  985. // Speaker::flush
  986. //============================================================================
  987. void Speaker::flush( void )
  988. {
  989. SpeechItem *item;
  990. while ( ( item = firstItem()) != 0 )
  991. {
  992. item->nd.remove();
  993. delete item;
  994. }
  995. }
  996. //============================================================================
  997. // Speaker::destroy
  998. //============================================================================
  999. void Speaker::destroy( void )
  1000. {
  1001. if ( m_manager )
  1002. {
  1003. m_manager->removeSpeaker( this );
  1004. }
  1005. delete this;
  1006. }
  1007. //============================================================================
  1008. // Speaker::say
  1009. //============================================================================
  1010. void Speaker::say ( char *speechName, Int priority, Int timeout, Int interrupt)
  1011. {
  1012. Speech *speech = TheAudio->Speeches->getSpeech( speechName );
  1013. say( speech, priority, timeout, interrupt );
  1014. }
  1015. //============================================================================
  1016. // Speaker::say
  1017. //============================================================================
  1018. void Speaker::say ( Speech *speech, Int priority, Int timeout, Int interrupt)
  1019. {
  1020. if ( speech == NULL || !m_stream || !speech->valid || !m_manager->isOn())
  1021. {
  1022. return;
  1023. }
  1024. if ( saying() == speech || isGoingToSay( speech ))
  1025. {
  1026. return ;
  1027. }
  1028. SpeechItem *item = NEW SpeechItem; // poolify
  1029. if ( item == NULL )
  1030. {
  1031. return;
  1032. }
  1033. if ( timeout == 0 )
  1034. {
  1035. timeout = speech->timeout;
  1036. }
  1037. if ( priority == 0 )
  1038. {
  1039. priority = speech->priority;
  1040. }
  1041. if ( interrupt == 0 )
  1042. {
  1043. interrupt = speech->interrupt;
  1044. }
  1045. item->speech = speech;
  1046. item->priority = priority;
  1047. if ( m_paused )
  1048. {
  1049. item->flags = SpeechItem::Flags((Int)item->flags | (Int)SpeechItem::PAUSED);
  1050. }
  1051. if ( timeout )
  1052. {
  1053. item->timeout = m_paused ? MSECONDS(timeout) : AudioGetTime() + MSECONDS(timeout);
  1054. }
  1055. item->nd.setItem( item ) ;
  1056. item->nd.setPriority( item->priority);
  1057. m_pending.add( &item->nd );
  1058. if ( interrupt && m_currentSpeech )
  1059. {
  1060. if ( m_currentSpeech->priority < item->priority )
  1061. {
  1062. AudioStreamerStop ( m_stream );
  1063. m_delay = 0;
  1064. }
  1065. }
  1066. }
  1067. //============================================================================
  1068. // Speaker::update
  1069. //============================================================================
  1070. void Speaker::update( void )
  1071. {
  1072. if ( m_stream == NULL )
  1073. {
  1074. stop();
  1075. return;
  1076. }
  1077. if ( !AudioStreamerActive ( m_stream ))
  1078. {
  1079. if ( m_currentSpeech )
  1080. {
  1081. delete m_currentSpeech;
  1082. m_currentSpeech = NULL;
  1083. }
  1084. }
  1085. TimeStamp now = AudioGetTime();
  1086. if ( !m_paused && m_currentSpeech == NULL && (AudioStreamerEndTimeStamp ( m_stream ) + m_delay) <= now )
  1087. {
  1088. SpeechItem *next = firstItem ( now );
  1089. if ( next )
  1090. {
  1091. // ok we can start the next one
  1092. next->nd.remove();
  1093. m_currentSpeech = next;
  1094. AudioStreamerSetMaxVolume ( m_stream, TheAudio->convertRealVolume(next->speech->volume) );
  1095. AudioStreamerSetVolume ( m_stream, TheAudio->convertRealVolume(next->speech->volume) );
  1096. AudioStreamerOpen( m_stream, getNextFilenameFromSpeech(next->speech).str());
  1097. if (AudioStreamerStart( m_stream ) ) {
  1098. m_delay = m_delayTime;
  1099. }
  1100. ///@todo write an INI compatible dialog player
  1101. /*
  1102. // REMOVE_GDF --> this must be converted to INI or something
  1103. GDI_Asset *asset = TheGameData->openAsset( next->speech->handle );
  1104. if( asset )
  1105. {
  1106. if ( (m_file = asset->open()))
  1107. {
  1108. if ( AudioStreamerOpen( m_stream, m_file ))
  1109. {
  1110. if ( AudioStreamerStart ( m_stream ) )
  1111. {
  1112. m_delay = m_delayTime;
  1113. }
  1114. }
  1115. }
  1116. asset->close();
  1117. }
  1118. */
  1119. }
  1120. }
  1121. }
  1122. //============================================================================
  1123. // Speaker::setPriority
  1124. //============================================================================
  1125. void Speaker::setPriority ( Int priority )
  1126. {
  1127. m_priority = priority;
  1128. if ( m_manager )
  1129. {
  1130. m_manager->removeSpeaker( this );
  1131. m_manager->addSpeaker( this );
  1132. }
  1133. if ( m_stream )
  1134. {
  1135. AudioStreamerSetPriority( m_stream, priority );
  1136. }
  1137. }
  1138. //============================================================================
  1139. // Speaker::getPriority
  1140. //============================================================================
  1141. Int Speaker::getPriority ( void )
  1142. {
  1143. return m_priority;
  1144. }
  1145. //============================================================================
  1146. // Speaker::setDelay
  1147. //============================================================================
  1148. void Speaker::setDelay ( Int delay )
  1149. {
  1150. m_delay = MSECONDS(delay);
  1151. }
  1152. //============================================================================
  1153. // Speaker::getDelay
  1154. //============================================================================
  1155. Int Speaker::getDelay ( void )
  1156. {
  1157. return IN_MSECONDS(m_delay);
  1158. }
  1159. //============================================================================
  1160. // Speaker::setBuffering
  1161. //============================================================================
  1162. void Speaker::setBuffering ( Int buffer_time )
  1163. {
  1164. DEBUG_ASSERTCRASH( FALSE, ("setBuffering not implemented"));
  1165. }
  1166. //===============================
  1167. // Speaker::getBuffering
  1168. //===============================
  1169. Int Speaker::getBuffering ( void )
  1170. {
  1171. DEBUG_ASSERTCRASH( FALSE, ("setBuffering not implemented"));
  1172. return 0;
  1173. }
  1174. //============================================================================
  1175. // Speaker::pause
  1176. //============================================================================
  1177. void Speaker::pause ( void )
  1178. {
  1179. if ( !m_paused )
  1180. {
  1181. if ( m_stream )
  1182. {
  1183. AudioStreamerPause ( m_stream );
  1184. }
  1185. TimeStamp now = AudioGetTime();
  1186. SpeechItem *item = firstItem( now );
  1187. while ( item )
  1188. {
  1189. if ( !( item->flags & SpeechItem::PAUSED ))
  1190. {
  1191. item->flags = SpeechItem::Flags( (Int) item->flags | (Int) SpeechItem::PAUSED);
  1192. if ( item->timeout )
  1193. {
  1194. item->timeout = item->timeout - now;
  1195. }
  1196. }
  1197. item = nextItem ( item, now );
  1198. }
  1199. }
  1200. m_paused++;
  1201. }
  1202. //============================================================================
  1203. // Speaker::resume
  1204. //============================================================================
  1205. void Speaker::resume ( void )
  1206. {
  1207. if ( m_paused == 1 )
  1208. {
  1209. TimeStamp now = AudioGetTime();
  1210. SpeechItem *item = firstItem ( now );
  1211. while ( item )
  1212. {
  1213. if ( ( item->flags & SpeechItem::PAUSED ))
  1214. {
  1215. item->flags = SpeechItem::Flags( (Int) item->flags & ~((Int) SpeechItem::PAUSED));
  1216. if ( item->timeout )
  1217. {
  1218. item->timeout = now + item->timeout;
  1219. }
  1220. }
  1221. item = nextItem ( item, now );
  1222. }
  1223. if ( m_stream )
  1224. {
  1225. AudioStreamerResume ( m_stream );
  1226. }
  1227. }
  1228. m_paused--;
  1229. if ( m_paused < 0 )
  1230. {
  1231. m_paused = 0;
  1232. }
  1233. }
  1234. //============================================================================
  1235. // Speaker::stop
  1236. //============================================================================
  1237. void Speaker::stop ( void )
  1238. {
  1239. if ( m_stream )
  1240. {
  1241. AudioStreamerClose( m_stream );
  1242. }
  1243. if ( m_file )
  1244. {
  1245. m_file->close();
  1246. m_file = NULL;
  1247. }
  1248. flush();
  1249. if ( m_currentSpeech )
  1250. {
  1251. delete m_currentSpeech;
  1252. m_currentSpeech = NULL;
  1253. }
  1254. }
  1255. //============================================================================
  1256. // Speaker::cancel
  1257. //============================================================================
  1258. void Speaker::cancel ( Speech *speech )
  1259. {
  1260. SpeechItem *item;
  1261. while ( ( item = findItem ( speech )) != 0 )
  1262. {
  1263. item->nd.remove();
  1264. delete item;
  1265. }
  1266. }
  1267. //============================================================================
  1268. // Speaker::hasSaid
  1269. //============================================================================
  1270. Bool Speaker::hasSaid ( Speech *speech )
  1271. {
  1272. return findItem ( speech ) == NULL && ( m_currentSpeech == NULL || m_currentSpeech->speech != speech);
  1273. }
  1274. //============================================================================
  1275. // Speaker::isGoingToSay
  1276. //============================================================================
  1277. Bool Speaker::isGoingToSay ( Speech *speech )
  1278. {
  1279. return findItem ( speech ) != NULL;
  1280. }
  1281. //============================================================================
  1282. // Speaker::isTalking
  1283. //============================================================================
  1284. Bool Speaker::isTalking ( void )
  1285. {
  1286. return m_currentSpeech != NULL;
  1287. }
  1288. //============================================================================
  1289. // Speaker::saying
  1290. //============================================================================
  1291. Speech* Speaker::saying ( void )
  1292. {
  1293. if ( m_currentSpeech )
  1294. {
  1295. return m_currentSpeech->speech;
  1296. }
  1297. return NULL;
  1298. }
  1299. static void speechFromInfo(SpeechInfo *pInSpeechInfo, Speech *pOutSpeech)
  1300. {
  1301. if (!pInSpeechInfo || !pOutSpeech) {
  1302. return;
  1303. }
  1304. // TBD: Do we need these fields:
  1305. // index? info?
  1306. pOutSpeech->name = pInSpeechInfo->m_dialogEvent;
  1307. pOutSpeech->id = (ID) 1;
  1308. pOutSpeech->volume = pInSpeechInfo->m_volume;
  1309. pOutSpeech->timeout = 65000;
  1310. pOutSpeech->interrupt = pInSpeechInfo->m_interruptable;
  1311. pOutSpeech->valid = true;
  1312. pOutSpeech->priority = pInSpeechInfo->m_priority;
  1313. // Some housekeeping for pInSpeechInfo
  1314. pInSpeechInfo->m_internalPlayCount = 0;
  1315. pOutSpeech->info = *pInSpeechInfo;
  1316. }
  1317. static AsciiString getNextFilenameFromSpeech(Speech *pSpeechToPlay)
  1318. {
  1319. AsciiString returnString;
  1320. if (!pSpeechToPlay) {
  1321. return returnString;
  1322. }
  1323. ++pSpeechToPlay->info.m_internalPlayCount;
  1324. Int indexToPlay = 0;
  1325. if (pSpeechToPlay->info.m_sequentialStartIndex < 0) {
  1326. indexToPlay = GameClientRandomValue(pSpeechToPlay->info.m_randomStartIndex,
  1327. pSpeechToPlay->info.m_dialogFiles.size() - 1);
  1328. }
  1329. if (pSpeechToPlay->info.m_dialogFiles.size() > 0) {
  1330. returnString = AsciiString(BASE_DLG_DIR);
  1331. returnString.concat('\\');
  1332. returnString.concat(pSpeechToPlay->info.m_dialogFiles[indexToPlay]);
  1333. returnString.concat('.');
  1334. returnString.concat(BASE_DLG_EXT);
  1335. }
  1336. return returnString;
  1337. }