source_dsound.h

Go to the documentation of this file.
00001 
00005 /* Copyright, 2001 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
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 /*enum TSourceDSoundBufferState
00043 {
00045     NL_DSOUND_FILLING,
00047     NL_DSOUND_SILENCING,
00049     NL_DSOUND_SILENCED
00050 } ;
00051 /*
00052 
00054 /*
00055 enum TSourceDSoundUserState
00056 {
00058     NL_DSOUND_PLAYING,
00060     NL_DSOUND_PAUSED,
00062     NL_DSOUND_STOPPED
00063 };
00064 */
00065 
00070 /*
00071 enum TSourceDSoundEndState
00072 {
00073     NL_DSOUND_TAIL1,
00074     NL_DSOUND_TAIL2,
00075     NL_DSOUND_ENDED
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     // The minimum size of available space in the DS buffer for update
00290     static const uint32         _UpdateCopySize;
00291     // The size of the samples that are copied when buffers are swapped
00292     static const uint32         _SwapCopySize;
00293     // The number of channels
00294     static const uint           _DefaultChannels;
00295     // The default sample rate
00296     static const uint           _DefaultSampleRate;
00297     // The default sample size
00298     static const uint           _DefaultSampleSize;
00299     // The length of the crossfade, in samples
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         // First locked part.
00314         sint16      *Ptr1;
00315         uint32      Size1;
00316 
00317         // second locked part (or 0 if none)
00318         sint16      *Ptr2;
00319         uint32      Size2;
00320     };
00321     
00322     // Utility function that locks the DirectSound buffer and restores it if it was lost.
00323 //  bool                    lock(uint32 writePos, uint32 size, uint8* &ptr1, DWORD &bytes1, uint8* &ptr2, DWORD &bytes2);
00324     bool                    lock(uint32 writePos, uint32 size, TLockedBufferInfo &lockedInfo);
00325     
00326     // Utility function that unlocks the DirectSound buffer
00327 //  bool                    unlock(uint8* ptr1, DWORD bytes1, uint8* ptr2, DWORD bytes2);
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     // Replace the current buffer with the swap buffer
00347     void                    swap();
00348     // getFadeOutSize() calculates how many samples have been written after the
00349     // write cursor in the DirectSound buffer. This value is returned in the
00350     // writtenTooMuch variable. The xfadeSize contains the number of samples over
00351     // which to do a xfade or fade out, and in1 points to the sample in the sample
00352     // buffer where to start the fade.
00353 //  void                    getFadeOutSize(uint32 writePos, uint32& xfadeSize, sint16* &in1, uint32 &writtenTooMuch);
00354     
00355     // Fill the buffer with fresh samples. Should be called inside the critical zone.
00356     bool                    fill();
00357     // Fill the buffer with sparkling silence. Should be called inside the critical zone.
00358     bool                    silence();
00359     // Do a cross fade between the current buffer and the buffer stored in the _SwapBuffer
00360     // variable. Call inside the critical zone.
00361     void                    crossFade();
00362     // Fade out the current buffer. Call inside the critical zone.
00363     void                    fadeOut();
00364     // Fade in the current buffer. Call inside the critical zone.
00365     void                    fadeIn();
00367     bool                    needsUpdate();
00368     
00369     
00370     // Source name
00371     uint                    _SourceName;
00372     
00373     
00374     TSourceState            _State;
00375 //  uint32                  _FillCursor;
00376     
00377     // The size of the sound buffer, in bytes
00378 //    uint32                    _BufferSize;
00379     
00380     IBuffer                 *_Sample;
00381     // Size of the buffer in sample
00382     uint                    _SampleSize;
00383     // Position in the buffer in sample.
00384     uint                    _SampleOffset;
00385     // The number of sample realy played (depend on play cursor).
00386     uint32                  _PlayOffset;
00387     TSampleFormat           _Format;
00388     uint                    _SampleFreq;
00389     
00390     
00391     // The frequency of the source [0,10], i.e. slowed down or accelerated
00392     float                   _Freq;
00393     // The sample rate of the source (= _Freq * _Buffer sample rate)
00394     uint32                  _SampleRate;
00395     
00396     
00397     IBuffer                 *_NextSample;
00398     
00399     uint32                  _LastPlayPos;
00400     uint32                  _FillOffset;
00401     uint32                  _SilenceWriten;
00402     
00403     // The next sound buffer
00404 //    IBuffer                   *_SwapBuffer;
00405     
00406     // To loop or not to loop
00407     bool                    _Loop;
00408     
00409     // The state of the source (playing, paused, stopped)
00410 //  TSourceDSoundUserState  _UserState;
00411     
00412     // DirectSound secondary buffer
00413     LPDIRECTSOUNDBUFFER     _SecondaryBuffer;
00414     // The byte size of the DirectSound secondary buffers
00415     static const uint32         _SecondaryBufferSize;
00416     // The mask for the buffer size
00417     static const uint32         _SizeMask;
00418     // 3D interface of the secondary buffer.
00419     LPDIRECTSOUND3DBUFFER   _3DBuffer;
00420     // The critial section object to protect the swap and update functions
00421     CRITICAL_SECTION        _CriticalSection;
00422     
00423     // The state for ADPCM decompression.
00424     IBuffer::TADPCMState    _ADPCMState;
00425     
00426     
00427     // The state of the DirectSound buffer (filling, silencing, silenced)
00428 //  TSourceDSoundBufferState _SecondaryBufferState;
00429     
00430     
00431     
00432     // The next position in the DirectSound buffer where we should write next
00433 //    uint32                    _NextWritePos;
00434     
00435     // The total number of bytes written from the current sound buffer
00436 //    uint32                    _BytesWritten;
00437     
00438     // The amount of silence written (in bytes) after the end of the current sound buffer
00439 //    uint32                    _SilenceWritten;
00440     
00441     // The position of the last audio sample written.
00442 //    uint32                    _EndPosition;
00443     
00444     
00445     // The state of the buffer to reach the end of the audio samples. To flag the buffer as
00446     // STOPPED, we have to make sure all the samples in the buffer are played. The play
00447     // cursor in the DirectSound buffer is inspected and when it crosses the position of
00448     // the last sample written (_EndPosition) the buffer is flagged as stopped. The _EndState
00449     // field is used to keep a trace of the whereabouts of the play cursor.
00450 //  TSourceDSoundEndState   _EndState;
00451     
00452     
00453     // Has this source been handed out.
00454 /*  bool                    _IsUsed;
00455     
00456     // Set the 'used' state of the source. Managed by the driver.
00457     void                    setUsed(bool v) { _IsUsed = v; }
00458     
00459     // Return the 'used' state of the source
00460     bool                    isUsed() { return _IsUsed; }
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 } // NLSOUND
00522 
00523 
00524 #endif // NL_SOURCE_DSOUND_H
00525 
00526 /* End of source_al.h */

Generated on Thu Jan 7 08:26:37 2010 for NeL by  doxygen 1.6.1