source_dsound.h
Go to the documentation of this file.00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef NL_SOURCE_DSOUND_H
00025 #define NL_SOURCE_DSOUND_H
00026
00027 #include "../source.h"
00028 #include "../sound_driver.h"
00029 #include "../buffer.h"
00030
00031 #include <dsound.h>
00032
00033 namespace NLSOUND {
00034
00035 class CBufferDSound;
00036
00042
00043
00045
00047
00049
00050
00051
00052
00054
00055
00056
00058
00060
00062
00063
00064
00065
00070
00071
00072
00073
00074
00075
00076
00077
00078
00089 class CSourceDSound : public ISource
00090 {
00091 friend class CSoundDriverDSound;
00092
00093 public:
00095 CSourceDSound(uint sourcename = 0);
00097 virtual ~CSourceDSound();
00098
00100 void init(LPDIRECTSOUND directSound, bool useEax);
00101
00103
00104
00105 virtual void setStreaming(bool streaming);
00110 virtual void setStaticBuffer(IBuffer *buffer);
00112 virtual IBuffer *getStaticBuffer();
00115 virtual void submitStreamingBuffer(IBuffer *buffer);
00117 virtual uint countStreamingBuffers() const;
00119
00121
00122
00123 virtual void setLooping(bool l);
00125 virtual bool getLooping() const;
00126
00130 virtual bool play();
00132 virtual void stop();
00134 virtual void pause();
00136 virtual bool isPlaying() const;
00138 virtual bool isStopped() const;
00140 virtual bool isPaused() const;
00142 virtual uint32 getTime();
00144
00146
00147
00151 virtual void setPos(const NLMISC::CVector& pos, bool deffered = true);
00155 virtual const NLMISC::CVector &getPos() const;
00157 virtual void setVelocity(const NLMISC::CVector& vel, bool deferred = true);
00159 virtual void getVelocity(NLMISC::CVector& vel) const;
00161 virtual void setDirection(const NLMISC::CVector& dir);
00163 virtual void getDirection(NLMISC::CVector& dir) const;
00170 virtual void setGain(float gain);
00172 virtual float getGain() const;
00176 virtual void setPitch(float pitch);
00178 virtual float getPitch() const;
00180 virtual void setSourceRelativeMode(bool mode);
00182 virtual bool getSourceRelativeMode() const;
00184 virtual void setMinMaxDistances(float mindist, float maxdist, bool deferred = true);
00186 virtual void getMinMaxDistances(float& mindist, float& maxdist) const;
00188 virtual void setCone(float innerAngle, float outerAngle, float outerGain);
00190 virtual void getCone(float& innerAngle, float& outerAngle, float& outerGain) const;
00204 virtual void setAlpha(double a);
00206
00208
00209
00210 virtual void setDirect(bool enable = true);
00212 virtual bool getDirect() const;
00214 virtual void setDirectGain(float gain);
00216 virtual float getDirectGain() const;
00217
00219 virtual void enableDirectFilter(bool enable = true);
00221 virtual bool isDirectFilterEnabled() const;
00223 virtual void setDirectFilter(TFilter filter, float lowFrequency, float highFrequency, float passGain);
00225 virtual void getDirectFilter(TFilter &filterType, float &lowFrequency, float &highFrequency, float &passGain) const;
00227 virtual void setDirectFilterPassGain(float passGain);
00229 virtual float getDirectFilterPassGain() const;
00231
00233
00234
00235 virtual void setEffect(IReverbEffect *reverbEffect);
00237 virtual IEffect *getEffect() const;
00239 virtual void setEffectGain(float gain);
00241 virtual float getEffectGain() const;
00242
00244 virtual void enableEffectFilter(bool enable = true);
00246 virtual bool isEffectFilterEnabled() const;
00248 virtual void setEffectFilter(TFilter filter, float lowFrequency, float highFrequency, float passGain);
00250 virtual void getEffectFilter(TFilter &filterType, float &lowFrequency, float &highFrequency, float &passGain) const;
00252 virtual void setEffectFilterPassGain(float passGain);
00254 virtual float getEffectFilterPassGain() const;
00256
00258 uint sourceName() { return _SourceName; }
00259
00261 IBuffer *getBuffer();
00262
00264 void reset();
00265
00267 bool update();
00268
00271 void updateVolume(const NLMISC::CVector& listener);
00272
00273 private:
00274
00275 void copySampleTo16BitsTrack(void *dst, void *src, uint nbSample, TSampleFormat sourceFormat);
00276
00277 enum TSourceState
00278 {
00279 source_stoped,
00280 source_playing,
00281 source_silencing,
00282 source_swap_pending
00283 };
00284
00285
00287 void release();
00288
00289
00290 static const uint32 _UpdateCopySize;
00291
00292 static const uint32 _SwapCopySize;
00293
00294 static const uint _DefaultChannels;
00295
00296 static const uint _DefaultSampleRate;
00297
00298 static const uint _DefaultSampleSize;
00299
00300 static const uint32 _XFadeSize;
00301
00303 struct TCursors
00304 {
00305 uint32 PlayCursor;
00306 uint32 WriteCursor;
00307 uint32 WriteSize;
00308 };
00309
00311 struct TLockedBufferInfo
00312 {
00313
00314 sint16 *Ptr1;
00315 uint32 Size1;
00316
00317
00318 sint16 *Ptr2;
00319 uint32 Size2;
00320 };
00321
00322
00323
00324 bool lock(uint32 writePos, uint32 size, TLockedBufferInfo &lockedInfo);
00325
00326
00327
00328 bool unlock(const TLockedBufferInfo &lockedInfo);
00329
00330 void getCursors(TCursors &cursors);
00331 uint32 checkFillCursor();
00332
00333
00334 void fillData(const TLockedBufferInfo &lbi, int nbSample);
00335 void fillData(sint16 *dst, uint nbSample);
00336 void fillSilence(const TLockedBufferInfo &lbi, int nbSample);
00337
00338 void xfade(const TLockedBufferInfo &lbi, sint16 *src);
00339 void fadeOut(const TLockedBufferInfo &lbi);
00340 void fadeIn(const TLockedBufferInfo &lbi);
00341
00342 void advanceFill(TLockedBufferInfo &lbi, uint nbSample);
00343
00344
00345
00346
00347 void swap();
00348
00349
00350
00351
00352
00353
00354
00355
00356 bool fill();
00357
00358 bool silence();
00359
00360
00361 void crossFade();
00362
00363 void fadeOut();
00364
00365 void fadeIn();
00367 bool needsUpdate();
00368
00369
00370
00371 uint _SourceName;
00372
00373
00374 TSourceState _State;
00375
00376
00377
00378
00379
00380 IBuffer *_Sample;
00381
00382 uint _SampleSize;
00383
00384 uint _SampleOffset;
00385
00386 uint32 _PlayOffset;
00387 TSampleFormat _Format;
00388 uint _SampleFreq;
00389
00390
00391
00392 float _Freq;
00393
00394 uint32 _SampleRate;
00395
00396
00397 IBuffer *_NextSample;
00398
00399 uint32 _LastPlayPos;
00400 uint32 _FillOffset;
00401 uint32 _SilenceWriten;
00402
00403
00404
00405
00406
00407 bool _Loop;
00408
00409
00410
00411
00412
00413 LPDIRECTSOUNDBUFFER _SecondaryBuffer;
00414
00415 static const uint32 _SecondaryBufferSize;
00416
00417 static const uint32 _SizeMask;
00418
00419 LPDIRECTSOUND3DBUFFER _3DBuffer;
00420
00421 CRITICAL_SECTION _CriticalSection;
00422
00423
00424 IBuffer::TADPCMState _ADPCMState;
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 sint32 _Volume;
00463 float _Gain;
00464 double _Alpha;
00465 NLMISC::CVector _Pos;
00466 bool _PosRelative;
00467
00468 #if EAX_AVAILABLE == 1
00469 LPKSPROPERTYSET _EAXSource;
00470 #endif
00471
00472
00473 #if NLSOUND_PROFILE
00474
00475 public:
00476
00477 static double _LastSwapTime;
00478 static double _TotalSwapTime;
00479 static double _MaxSwapTime;
00480 static double _MinSwapTime;
00481 static uint32 _SwapCount;
00482
00483 static double _TotalUpdateTime;
00484 static double _MaxUpdateTime;
00485 static double _MinUpdateTime;
00486 static uint32 _UpdateCount;
00487 static uint32 _TotalUpdateSize;
00488
00489 static double _PosTime;
00490 static double _LockTime;
00491 static double _CopyTime;
00492 static double _UnlockTime;
00493 static uint32 _CopyCount;
00494
00495 public:
00496
00497 static double getTestLast() { return 1000.0f * _LastSwapTime; };
00498 static double getTestMax() { return 1000.0f * _MaxSwapTime; };
00499 static double getTestMin() { return 1000.0f * _MinSwapTime; };
00500 static double getTestAverage() { return (_SwapCount > 0) ? 1000.0f * _TotalSwapTime / _SwapCount : 0.0; };
00501
00502 static double getAveragePosTime() { return (_CopyCount > 0) ? 1000.0f * _PosTime / _CopyCount : 0.0; };
00503 static double getAverageLockTime() { return (_CopyCount > 0) ? 1000.0f * _LockTime / _CopyCount : 0.0; };
00504 static double getAverageCopyTime() { return (_CopyCount > 0) ? 1000.0f * _CopyTime / _CopyCount : 0.0; };
00505 static double getAverageUnlockTime() { return (_CopyCount > 0) ? 1000.0f * _UnlockTime / _CopyCount : 0.0; };
00506 static double getAverageCumulTime() { return (_CopyCount > 0) ? 1000.0f * (_PosTime + _LockTime + _CopyTime + _UnlockTime) / _CopyCount : 0.0; };
00507 static uint getAverageUpdateSize() { return (_CopyCount > 0) ? (uint) (_TotalUpdateSize / _CopyCount) : 0; };
00508
00509 static double getMaxUpdateTime() { return 1000.0f * _MaxUpdateTime; };
00510 static double getMinUpdateTime() { return 1000.0f * _MinUpdateTime; };
00511 static double getAverageUpdateTime() { return (_UpdateCount > 0) ? 1000.0f * _TotalUpdateTime / _UpdateCount : 0.0; };
00512
00513 static double getTotalUpdateTime() { return 1000.0f * _TotalUpdateTime; };
00514 static double getUpdateBytesPerMsec() { return (_UpdateCount > 0) ? _TotalUpdateSize / _TotalUpdateTime / 1000.0 : 0.0; }
00515
00516 #endif
00517
00518 };
00519
00520
00521 }
00522
00523
00524 #endif // NL_SOURCE_DSOUND_H
00525
00526