My Project
AudioEngine.h
1 //---------------------------------------
2 // File: AudioEngine.h
3 // Author: WangTian
4 //
5 // Desc: ParaEngine Audio Engine
6 // Email: mitnick_wang@hotmail.com
7 // wangtian@cad.zju.edu.cn
8 // Date: 2006.8
9 //---------------------------------------
10 
11 #pragma once
12 #include <xact.h>
13 #include <xact3d.h>
14 #include <map>
15 #include <vector>
16 #include <list>
17 
18 #include "util/mutex.h"
19 
20 using namespace std;
21 
22 namespace ParaEngine
23 {
29  {
31  bool bActive;
33  IXACTCue* pCue;
39  string strKeyName;
40  };
41 
50  {
51  public:
52  typedef map<string, ParaAudio_3DCUE> POOL_3DCUE_TYPE;
53 
54  CAudioEngine(void);
55  ~CAudioEngine(void);
57  static CAudioEngine * GetInstance();
58 
73  HRESULT InitAudioEngine();
79  void CleanupAudioEngine();
80 
87  //
88  // Only for documentation
89  // void WINAPI AudioEngineNotificationCallback(const XACT_NOTIFICATION* pNotification)
90 
92  bool IsValid();
93 
95  inline bool IsValidAndEnabled(){return IsValid() && IsAudioEngineEnabled();};
96 
100  void DoWork();
101 
103  //
104  // recording functions
105  //
107  public:
112  const char* GetRecordingDeviceEnum();
113 
118  const char* GetRecordingFormatEnum();
119 
126  bool SetRecordingOutput(const char* sOutputFile, int nDeviceIndex, int nWaveFormatIndex);
127 
129  bool BeginRecording();
130 
132  bool IsRecording();
133 
135  bool IsRecordingPaused();
136 
140  const char* GetLastRecordedData(int nTimeLength);
141 
145  bool PauseOrResumeRecording(int nState);
146 
148  bool StopRecording();
149 
153  void SetVolume(float fVolume);
154 
158  float GetVolume();
159  private:
161  HRESULT InitSoundCapture();
162 
164  HRESULT ReleaseSoundCapture();
165 
169  HRESULT ScanAvailableInputFormats();
170 
174  void GetWaveFormatFromIndex( INT nIndex, WAVEFORMATEX* pwfx );
175 
179  void ConvertWaveFormatToString( WAVEFORMATEX* pwfx, TCHAR* strFormatName, int cchFormatName );
180 
182  HRESULT InitNotifications();
183 
185  HRESULT DestoryNotifications();
186 
187  const static int NUM_REC_NOTIFICATIONS = 10;
188 
189  LPDIRECTSOUNDCAPTURE m_pDSCapture;
190  LPDIRECTSOUNDCAPTUREBUFFER m_pDSBCapture;
191  LPDIRECTSOUNDNOTIFY m_pDSNotify;
192  GUID m_guidCaptureDevice;
193  BOOL m_bRecording;
194  WAVEFORMATEX m_wfxInput;
195  DSBPOSITIONNOTIFY m_aPosNotify[ NUM_REC_NOTIFICATIONS + 1 ];
196  BOOL m_abInputFormatSupported[20];
197  DWORD m_dwCaptureBufferSize;
198  DWORD m_dwNextCaptureOffset;
199  DWORD m_dwNotifySize;
200  CWaveFile* m_pWaveFile;
201  bool m_bCaptureDeviceAvailable;
203  int m_nCaptureDeviceIndex;
205  int m_nWaveFormatIndex;
207  string m_sCapureFileName;
208 
210  string m_strCaptureFormats;
211 
213  HRESULT RecordCapturedData();
214 
216  static unsigned int __stdcall RecordSoundLoop(void* parameter);
217 
219  HANDLE m_hRecordingThread;
221  //
222  // playing functions
223  //
225  public:
226  // ---------------- Audio Engine Functions ----------------
228  bool IsAudioEngineEnabled();
230  HRESULT EnableAudioEngine( );
232  HRESULT DisableAudioEngine( );
235  HRESULT PauseCategory(const char * strCategoryName);
238  HRESULT ResumeCategory(const char * strCategoryName);
241  HRESULT StopCategory(const char * strCategoryName);
244  HRESULT EnableAudioBank(const char * strBankName);
247  HRESULT DisableAudioBank(const char * strBankName);
250  string GetCurrentAudioBankName();
252  HRESULT SetAudioStereo();
254  HRESULT SetAudio5Point1();
257  int GetAudioFormat();
258 
259  // ---------------- Background Functions ----------------
262  HRESULT PlayBGMusic( const char * strBGMusicName );
265  HRESULT PauseBGMusic( );
268  HRESULT ResumeBGMusic( );
271  HRESULT StopBGMusic( );
273  bool IsBGMusicEnabled();
275  HRESULT EnableBGMusic( );
277  HRESULT DisableBGMusic( );
280  float GetBGMusicVolume( );
283  HRESULT SetBGMusicVolume( const float fVolume );
284 
285  // ---------------- Dialog Functions ----------------
288  HRESULT PlayDialog( const char * strDialogName , const char * strScript);
290  HRESULT SkipDialog( );
293  HRESULT PauseDialog( );
296  HRESULT ResumeDialog( );
299  HRESULT StopDialog( );
301  bool IsDialogEnabled();
303  HRESULT EnableDialog( );
305  HRESULT DisableDialog( );
308  float GetDialogVolume( );
311  HRESULT SetDialogVolume( const float fVolume );
312 
313  // ---------------- Ambient Sound Functions ----------------
316  HRESULT PlayAmbientSound( const char * strAmbientSoundName );
319  HRESULT PauseAmbientSound( const char * strAmbientSoundName );
322  HRESULT ResumeAmbientSound( const char * strAmbientSoundName );
325  HRESULT StopAmbientSound( const char * strAmbientSoundName );
327  bool IsAmbientSoundEnabled();
329  HRESULT EnableAmbientSound( );
331  HRESULT DisableAmbientSound( );
334  float GetAmbientSoundVolume( );
337  HRESULT SetAmbientSoundVolume( const float fVolume );
338 
339  // ---------------- UserInterface Sound Functions ----------------
342  HRESULT PlayUISound( const char * strUISoundName );
345  float GetUISoundVolume( );
348  HRESULT SetUISoundVolume( const float fVolume );
349 
350  // ---------------- 3D Sound Functions ----------------
351 
356  HRESULT PlayStatic3DSound( const char * str3DSoundName, const char * assetName, float x, float y, float z);
359  HRESULT PauseStatic3DSound( const char * assetName );
362  HRESULT ResumeStatic3DSound( const char * assetName );
365  HRESULT StopStatic3DSound( const char * assetName );
369  // this is the name of the base class BaseObject
370  HRESULT PlayDynamic3DSound( const char * str3DSoundName, const char * objectName );
373  float Get3DSoundVolume( );
376  HRESULT Set3DSoundVolume( const float fVolume );
377 
378  // ---------------- Interactive Sound Functions ----------------
382  HRESULT PlayInteractiveSound( const char * strInteractiveSoundName );
385  float GetInteractiveSoundVolume( );
388  HRESULT SetInteractiveSoundVolume( const float fVolume );
389 
390 
391  // ---------------- Loading Functions ----------------
397  HRESULT LoadSoundBank(const char* filename);
399  HRESULT UnLoadSoundBank(const char* filename=NULL);
400 
406  HRESULT LoadInMemoryWaveBank(const char* filename);
408  HRESULT UnLoadInMemoryWaveBank(const char* filename=NULL);
409 
415  HRESULT LoadStreamWaveBank(const char* filename);
417  HRESULT UnLoadStreamWaveBank(const char* filename=NULL);
418 
419  private:
420  HRESULT FindMediaFileCch(CHAR* strDestPath, int cchDest, LPCSTR strFilename);
421  HRESULT PlayAmbientWater();
422 
426  void UpdateAudio();
427  private:
428 
430  IXACTEngine* m_pEngine;
431 
433  HANDLE m_hStreamingWaveBankFile;
434  VOID* m_pbWaveBankInMemory; // Handle to in memory wave bank data. Its memory mapped so call UnmapViewOfFile() upon cleanup to release file
435  VOID* m_pbSoundBank; // Pointer to sound bank data. Call delete on it when the sound bank is destroyed
436 
438  bool m_bEnableAudioEngine;
439  bool m_bEnableBGMusic;
440  bool m_bEnableDialogMusic;
441  bool m_bEnableAmbientSound;
442  bool m_bEnableUISound;
443  bool m_bEnable3DSound;
444  bool m_bEnableInteractiveSound;
445 
447  float m_fBGMusicVolume;
448  float m_fDialogMusicVolume;
449  float m_fAmbientSoundVolume;
450  float m_fUISoundVolume;
451  float m_f3DSoundVolume;
452  float m_fInteractiveSoundVolume;
453 
456  string m_strCurrentAudioBankName;
458  bool m_bInitializeBanks;
459 
461  typedef pair <XACTINDEX, IXACTCue*> CuePointerPair;
464  map<XACTINDEX, IXACTCue*> m_mapAmbientCuePointer;
465 
468  X3DAUDIO_HANDLE m_x3DInstance;
470  int m_nFrameToApply3DAudio;
472  X3DAUDIO_DSP_SETTINGS m_dspSettings;
474  X3DAUDIO_LISTENER m_listener;
476  X3DAUDIO_EMITTER m_emitter;
478  FLOAT32 m_DelayTimes[2];
480  FLOAT32 m_MatrixCoefficients[2 * 2];
481 
483  POOL_3DCUE_TYPE m_pool_3D_Static_Cues;
485  POOL_3DCUE_TYPE m_pool_3D_Dynamic_Cues;
486 
489  ParaEngine::mutex m_mutex;
490 
491  bool m_bHandleStreamingWaveBankPrepared;
492  bool m_bHandleDialogCuePrepared;
493  bool m_bHandleDialogCueStop;
494  bool m_bHandleBGCuePrepared;
495  bool m_bHandleBGCueStop;
496 
498  IXACTSoundBank* m_pSoundBank;
499  IXACTWaveBank* m_pWaveBankInMemory;
500  IXACTWaveBank* m_pWaveBankStream;
501 
503  bool m_bStreamingWaveBankPrepared;
504 
507  XACTINDEX m_iCurrentBGMusic;
508  IXACTCue* m_pCurrentBGMusic;
509 
512  XACTINDEX m_iCurrentDialog;
513  IXACTCue* m_pCurrentDialog;
514 
516  map<string, IXACTWave*> m_codeDrivenFiles;
517 
518  public:
519  IXACTWaveBank* GetWaveBankStream();
520  IXACTCue* GetCurrentDialog();
521  IXACTCue* GetCurrentBGMusic();
522  void SetHandleStreamingWaveBankPrepared( bool bHandle );
523  void SetHandleDialogCuePrepared( bool bHandle );
524  void SetHandleDialogCueStop( bool bHandle );
525  void SetHandleBGCuePrepared( bool bHandle );
526  void SetHandleBGCueStop( bool bHandle );
527 
543  HRESULT PlayWaveFile(PCSTR szWavePath,XACTLOOPCOUNT nLoopCount, WORD wStreamingPacketSize=0,DWORD dwPlayOffset=0, DWORD dwAlignment=2048);
552  HRESULT StopWaveFile(PCSTR szWavePath, DWORD dwFlags);
553 
559  HRESULT ReleaseWaveFile(PCSTR szWavePath);
560 
561 
565  bool IsWaveFileInQueue(const string& filename);
566  };
567 }
568 
569 
570 // Old Audio Engine Interface
571 
574 //HRESULT PlayBGMusic(const char * name);
576 //HRESULT EnableBGMusic( );
578 //HRESULT DisableBGMusic( );
579 
581 //HRESULT PlayUISound(const char * name);
582 
583 // --------- Audio Engine ---------
584 // void EnableAudioEngine(bool enable);
585 // bool IsAudioEngineEnabled();
586 
587 //HRESULT PlayAudioCue( XACTINDEX iCueIndex );
588 //HRESULT StopAudioCue( XACTINDEX iCueIndex );
589 
590 //XACTINDEX GetCueIndexByName(const char * strName);
Definition: dsutil.h:134
Vector3 vEmitterPosition
position of the emitter
Definition: AudioEngine.h:35
IXACTCue * pCue
pointer to the cue instance
Definition: AudioEngine.h:33
different physics engine has different winding order.
Definition: EventBinding.h:32
string strKeyName
identity of a 3D cue instance this may be the name of the object in ParaEngine or the script defined ...
Definition: AudioEngine.h:39
Class: CAudioEngine.
Definition: AudioEngine.h:49
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
Struct: ParaAudio_3DCUE.
Definition: AudioEngine.h:28
bool IsValidAndEnabled()
return true if the audio is both valid and enabled.
Definition: AudioEngine.h:95
cross platform mutex
Definition: mutex.h:95
bool bActive
true if playing, false if stopped for garbage collection
Definition: AudioEngine.h:31