sfxSound.h

Engine/source/sfx/sfxSound.h

More...

Classes:

class

A scriptable controller playing a specific single sound file.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _SFXSOUND_H_
 25#define _SFXSOUND_H_
 26
 27#ifndef _SFXSOURCE_H_
 28   #include "sfx/sfxSource.h"
 29#endif
 30#ifndef _SFXVOICE_H_
 31   #include "sfx/sfxVoice.h"
 32#endif
 33#ifndef _SIMBASE_H_
 34   #include "console/simBase.h"
 35#endif
 36#ifndef _MPOINT3_H_
 37   #include "math/mPoint3.h"
 38#endif
 39#ifndef _MMATRIX_H_
 40   #include "math/mMatrix.h"
 41#endif
 42#ifndef _TSTREAM_H_
 43   #include "core/stream/tStream.h"
 44#endif
 45#ifndef _SFXPROFILE_H_
 46   #include "sfx/sfxProfile.h"
 47#endif
 48
 49
 50class SFXBuffer;
 51class SFXDevice;
 52
 53
 54
 55/// A scriptable controller playing a specific single sound file.
 56class SFXSound : public SFXSource,
 57                 public IPositionable< U32 >
 58{
 59      friend class SFXSystem;
 60
 61      typedef SFXSource Parent;
 62   
 63   protected:
 64
 65      /// Used by SFXSystem to create sources.
 66      static SFXSound* _create( SFXDevice* device, SFXProfile* profile );
 67      static SFXSound* _create( SFXDevice* device, const ThreadSafeRef< SFXStream>& stream, SFXDescription* description );
 68
 69      /// Internal constructor used for sources.
 70      SFXSound( SFXProfile* profile, SFXDescription* description );
 71      
 72      /// The device specific voice which is used during
 73      /// playback.  By making it a SafePtr it will NULL
 74      /// automatically when the device is deleted.
 75      StrongWeakRefPtr< SFXVoice> mVoice;
 76
 77      /// The reference counted device specific buffer used by 
 78      /// the voice for playback.
 79      StrongWeakRefPtr< SFXBuffer> mBuffer;
 80
 81      /// The duration of the sound cached from the buffer in
 82      /// _initBuffer() used for managing virtual sources.
 83      U32 mDuration;
 84
 85      ///Used for setPosition (time in miliseconds)
 86      U32 mSetPositionValue;
 87
 88      /// Create a new voice for this source.
 89      bool _allocVoice( SFXDevice* device );
 90
 91      /// Release the voice if the source has one.
 92      bool _releaseVoice();
 93      
 94      ///
 95      void _setBuffer( SFXBuffer* buffer );
 96      
 97      /// Reload the sound buffer.  Temporarily goes to virtualized playback when necessary.
 98      void _reloadBuffer();
 99
100      ///
101      void _onProfileChanged( SFXProfile* profile )
102      {
103         if( profile == mTrack )
104            _reloadBuffer();
105      }
106      
107      // SFXSource.
108      virtual void _play();
109      virtual void _pause();
110      virtual void _stop();
111      virtual void _updateStatus();
112      virtual void _onParameterEvent( SFXParameter* parameter, SFXParameterEvent event );
113      virtual void _updateVolume( const MatrixF& listener );
114      virtual void _updatePitch();
115      virtual void _updatePriority();
116      virtual void _setMinMaxDistance( F32 min, F32 max );
117      virtual void _setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume );
118
119   public:
120
121      DECLARE_CONOBJECT( SFXSound );
122
123      /// The default constructor is *only* here to satisfy the
124      /// construction needs of IMPLEMENT_CONOBJECT.  It does not
125      /// create a valid source!
126      explicit SFXSound();
127
128      /// This is normally called from the system to 
129      /// detect if this source has been assigned a
130      /// voice for playback.
131      bool hasVoice() const { return mVoice != NULL; }
132
133      /// Return the current playback position in milliseconds.
134      /// @note For looping sources, this returns the position in the current cycle.
135      U32 getPosition() const;
136
137      /// Set the current playback position in milliseconds.
138      void setPosition( U32 ms );
139
140      /// Returns the source's total playback time in milliseconds.
141      U32 getDuration() const { return mDuration; }
142
143      /// Return true if the sound stream tied to the source is currently in a buffer underrun situation.
144      bool isBlocked() const { return ( mVoice && mVoice->getStatus() == SFXStatusBlocked ); }
145      
146      /// Returns true if this is a continuously streaming source.
147      bool isStreaming() const { return mDescription->mIsStreaming; }
148
149      /// Returns true if the source's associated data is ready for playback.
150      bool isReady() const;
151
152      /// Return the SFXProfile datablock attached to this sound.
153      SFXProfile* getProfile() const;
154
155      /// Used to sort sources by attenuated volume and channel priority.
156      static S32 QSORT_CALLBACK qsortCompare( const void* item1, const void* item2 );
157
158      // SFXSource.
159      virtual void setTransform( const MatrixF& transform );
160      virtual void setVelocity( const VectorF& velocity );
161      virtual bool isVirtualized() const;
162      virtual F32 getElapsedPlayTimeCurrentCycle() const;
163      virtual F32 getTotalPlayTime() const;
164
165      // SimObject.
166      virtual void onRemove();
167      virtual void onDeleteNotify( SimObject* object );
168};
169
170#endif // !_SFXSOUND_H_
171