sfxSound.h
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 /// Create a new voice for this source. 86 bool _allocVoice( SFXDevice* device ); 87 88 /// Release the voice if the source has one. 89 bool _releaseVoice(); 90 91 /// 92 void _setBuffer( SFXBuffer* buffer ); 93 94 /// Reload the sound buffer. Temporarily goes to virtualized playback when necessary. 95 void _reloadBuffer(); 96 97 /// 98 void _onProfileChanged( SFXProfile* profile ) 99 { 100 if( profile == mTrack ) 101 _reloadBuffer(); 102 } 103 104 // SFXSource. 105 virtual void _play(); 106 virtual void _pause(); 107 virtual void _stop(); 108 virtual void _updateStatus(); 109 virtual void _onParameterEvent( SFXParameter* parameter, SFXParameterEvent event ); 110 virtual void _updateVolume( const MatrixF& listener ); 111 virtual void _updatePitch(); 112 virtual void _updatePriority(); 113 virtual void _setMinMaxDistance( F32 min, F32 max ); 114 virtual void _setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume ); 115 116 public: 117 118 DECLARE_CONOBJECT( SFXSound ); 119 120 /// The default constructor is *only* here to satisfy the 121 /// construction needs of IMPLEMENT_CONOBJECT. It does not 122 /// create a valid source! 123 explicit SFXSound(); 124 125 /// This is normally called from the system to 126 /// detect if this source has been assigned a 127 /// voice for playback. 128 bool hasVoice() const { return mVoice != NULL; } 129 130 /// Return the current playback position in milliseconds. 131 /// @note For looping sources, this returns the position in the current cycle. 132 U32 getPosition() const; 133 134 /// Set the current playback position in milliseconds. 135 void setPosition( U32 ms ); 136 137 /// Returns the source's total playback time in milliseconds. 138 U32 getDuration() const { return mDuration; } 139 140 /// Return true if the sound stream tied to the source is currently in a buffer underrun situation. 141 bool isBlocked() const { return ( mVoice && mVoice->getStatus() == SFXStatusBlocked ); } 142 143 /// Returns true if this is a continuously streaming source. 144 bool isStreaming() const { return mDescription->mIsStreaming; } 145 146 /// Returns true if the source's associated data is ready for playback. 147 bool isReady() const; 148 149 /// Return the SFXProfile datablock attached to this sound. 150 SFXProfile* getProfile() const; 151 152 /// Used to sort sources by attenuated volume and channel priority. 153 static S32 QSORT_CALLBACK qsortCompare( const void* item1, const void* item2 ); 154 155 // SFXSource. 156 virtual void setTransform( const MatrixF& transform ); 157 virtual void setVelocity( const VectorF& velocity ); 158 virtual bool isVirtualized() const; 159 virtual F32 getElapsedPlayTimeCurrentCycle() const; 160 virtual F32 getTotalPlayTime() const; 161 162 // SimObject. 163 virtual void onRemove(); 164 virtual void onDeleteNotify( SimObject* object ); 165}; 166 167#endif // !_SFXSOUND_H_ 168
