My Project
AudioEngine2.h
1 #pragma once
2 #include "IParaAudioEngine.h"
3 #include "util/intrusive_ptr.h"
4 using namespace std;
5 
6 namespace ParaEngine
7 {
10  {
11  public:
12  CAudioSource2(const char* sName, IParaAudioSource* pSource=NULL):m_name(sName), m_pSource(pSource), m_nLoopCount(0), m_status(AUDIO_FLAG_Uninitialized), m_bReleaseOnStop(false), m_bIsAsyncLoadingWhileLoopPlaying(false){}
13  ~CAudioSource2(){};
22  HRESULT Stop(ParaAudioFlagsEnum dwFlags);
23 
25  void SetSource(IParaAudioSource* pSource)
26  {
27  m_pSource = pSource;
28  };
29  IParaAudioSource* GetSource()
30  {
31  return m_pSource;
32  };
33 
35  void SetName(const char* sName) {m_name = sName;}
36 
38  const std::string& GetName() const {return m_name;}
39 
41  void EnableCallback(bool bEnable = true)
42  {
43  if(m_pSource!=0)
44  {
45  if(bEnable)
46  m_pSource->registerEventHandler(this);
47  else
48  m_pSource->unRegisterAllEventHandlers();
49  }
50  }
51  public:
54  bool play();
55 
62  bool play2d(const bool& toLoop = false, bool bIgnoreIfPlaying = true);
63 
69  bool play3d(const PARAVECTOR3& position, const float& soundstr = 1.0 , const bool& toLoop = false);
70 
72  void pause();
73 
75  void stop();
76 
78 
79  void loop(const bool& toLoop);
80 
82  bool IsAsyncLoadingWhileLoopPlaying();
83 
85  bool IsWaveFileLoopPlaying();
86 
88  bool IsPlaying();
89  public:
91  //
92  // audio event callback, invoked on the update thread (so need lock to feedback to main thread)
93  //
95 
97  virtual void onUpdate();
98 
100  virtual void onRelease();
101 
103  virtual void onPlay();
104 
106  virtual void onStop();
107 
109  virtual void onPause();
110 
111  public:
112 
113  IParaAudioSource* m_pSource;
114  int m_nLoopCount;
115  bool m_bReleaseOnStop;
116  std::string m_name;
117  ParaAudioFlagsEnum m_status;
120  };
121  typedef ParaIntrusivePtr<CAudioSource2> CAudioSource2_ptr;
122 
129  {
130  public:
131  typedef std::map<std::string, CAudioSource2_ptr> AudioFileMap_type;
132 
133  CAudioEngine2();
134  ~CAudioEngine2();
135 
137  static CAudioEngine2* GetInstance();
138 
140  IParaAudioEngine* GetInterface();
141 
143  HRESULT InitAudioEngine(IParaAudioEngine* pInteface = NULL);
144 
146  void CleanupAudioEngine();
147 
149  bool IsValid();
150 
152  inline bool IsValidAndEnabled(){return IsValid() && IsAudioEngineEnabled();};
153 
155  bool IsAudioEngineEnabled() {return m_bEnableAudioEngine;};
156 
158  void EnableAudioEngine(bool bEnable);
159 
161  float GetGlobalVolume(){return m_fGlobalVolume;};
162 
165  void SetGlobalVolume(const float& volume);
166 
168  void OnSwitch(bool bOn);
169 
176  CAudioSource2_ptr Create(const char* sName, const char* sWavePath=NULL, bool bStream=false);
177 
179  CAudioSource2_ptr Get(const char* sName);
180 
187  CAudioSource2_ptr CreateGet(const char* sName, const char* sWavePath=NULL, bool bStream=false);
188 
190  void release(CAudioSource2_ptr& audio_src);
191 
194  void Update();
195 
197  void SetAutoMoveListener(bool bAuto) { m_bAutoMoveListener = bAuto; };
198 
200  bool GetAutoMoveListener() {return m_bAutoMoveListener; };
201  public:
203  // handy functions for playback
205 
215  HRESULT PlayWaveFile(const char* sWavePath, bool bLoop=false, bool bStream=false, int dwPlayOffset=0);
216 
222  HRESULT PrepareWaveFile(CAudioSource2_ptr& pWave, const char* sWavePath, bool bStream = false);
223 
232  HRESULT StopWaveFile(const char* sWavePath, ParaAudioFlagsEnum dwFlags);
233 
239  HRESULT ReleaseWaveFile(const std::string& szWavePath);
240 
244  bool IsWaveFileInQueue(const std::string& filename);
247  bool IsWaveFileLoopPlaying(const std::string& filename);
248 
264  void SetDistanceModel(ParaAudioDistanceModelEnum eDistModel);
265 
267  //
268  // the audio listener
269  //
271 
276  void setPosition(const PARAVECTOR3& pos);
277 
281  void setDirection(const PARAVECTOR3& dir);
282 
286  void setUpVector(const PARAVECTOR3& up);
287 
290  void setVelocity(const PARAVECTOR3& vel);
291 
295  void move(const PARAVECTOR3& pos);
296 
298  PARAVECTOR3 getPosition() const;
299 
301  PARAVECTOR3 getDirection() const;
302 
304  PARAVECTOR3 getUpVector() const;
305 
307  PARAVECTOR3 getVelocity() const;
308 
309  void PauseAll();
310  void ResumeAll();
311  private:
312  IParaAudioEngine* m_pAudioEngine;
313  bool m_bEnableAudioEngine;
314  AudioFileMap_type m_audio_file_map;
315  std::vector<CAudioSource2_ptr> m_paused_audios;
316  float m_fGlobalVolume;
317  float m_fGlobalVolumeBeforeSwitch;
318  bool m_bAutoMoveListener;
319  };
320 }
bool IsValidAndEnabled()
return true if the audio is both valid and enabled.
Definition: AudioEngine2.h:152
void SetSource(IParaAudioSource *pSource)
reset the source.
Definition: AudioEngine2.h:25
different physics engine has different winding order.
Definition: EventBinding.h:32
bool m_bIsAsyncLoadingWhileLoopPlaying
this is true, if an audio resource is being loop played but without being downloaded yet...
Definition: AudioEngine2.h:119
CAudioEngine2 is based on the cAudioEngine plugin dll.
Definition: AudioEngine2.h:128
float GetGlobalVolume()
Returns the global volume modifier for all sources.
Definition: AudioEngine2.h:161
it represent an audio engine source
Definition: AudioEngine2.h:9
bool IsAudioEngineEnabled()
get is audio engine enabled
Definition: AudioEngine2.h:155
ParaAudioFlagsEnum
Audio flags.
Definition: IParaAudioEngine.h:16
an audio source
Definition: IParaAudioEngine.h:55
const std::string & GetName() const
get the name
Definition: AudioEngine2.h:38
void EnableCallback(bool bEnable=true)
this function must be called before the music is played.
Definition: AudioEngine2.h:41
ParaAudioEngine core interface.
Definition: IParaAudioEngine.h:278
Definition: PEtypes.h:298
single-threaded reference counted base class for boost::intrusive_ptr all boost::intrusive_ptr<T>, should derive from this class.
Definition: intrusive_ptr.h:75
void SetName(const char *sName)
set the name
Definition: AudioEngine2.h:35
Interface for event handlers on Audio Sources.
Definition: IParaAudioEngine.h:35
void SetAutoMoveListener(bool bAuto)
whether to automatically move the listener according to current camera position.
Definition: AudioEngine2.h:197
bool GetAutoMoveListener()
whether to automatically move the listener according to current camera position.
Definition: AudioEngine2.h:200
ParaAudioDistanceModelEnum
Audio flags.
Definition: IParaAudioEngine.h:23