sfxDSVoice.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "sfx/dsound/sfxDSVoice.h"
  23. #include "sfx/dsound/sfxDSDevice.h"
  24. #include "core/util/safeRelease.h"
  25. SFXDSVoice* SFXDSVoice::create( SFXDSDevice *device, SFXDSBuffer *buffer )
  26. {
  27. AssertFatal( buffer, "SFXDSVoice::create() - Got null buffer!" );
  28. IDirectSoundBuffer8 *dsBuffer8 = NULL;
  29. if ( !buffer->createVoice( &dsBuffer8 ) || !dsBuffer8 )
  30. return NULL;
  31. // Now try to grab a 3D interface... if we don't
  32. // get one its probably because its not a 3d sound.
  33. IDirectSound3DBuffer8* dsBuffer3d8 = NULL;
  34. dsBuffer8->QueryInterface( IID_IDirectSound3DBuffer8, (LPVOID*)&dsBuffer3d8 );
  35. // Create the voice and return!
  36. SFXDSVoice* voice = new SFXDSVoice( device,
  37. buffer,
  38. dsBuffer8,
  39. dsBuffer3d8 );
  40. // Now set the voice to a default state.
  41. // The buffer from which we have duplicated may have been assigned different
  42. // properties and we don't want to inherit these.
  43. voice->setVolume( 1.0 );
  44. voice->setPitch( 1.0 );
  45. return voice;
  46. }
  47. SFXDSVoice::SFXDSVoice( SFXDSDevice *device,
  48. SFXDSBuffer *buffer,
  49. IDirectSoundBuffer8 *dsBuffer,
  50. IDirectSound3DBuffer8 *dsBuffer3d )
  51. : Parent( buffer ),
  52. mDevice( device ),
  53. mDSBuffer( dsBuffer ),
  54. mDSBuffer3D( dsBuffer3d ),
  55. mIsLooping( false )
  56. {
  57. AssertFatal( mDevice, "SFXDSVoice::SFXDSVoice() - SFXDSDevice is null!" );
  58. AssertFatal( mBuffer, "SFXDSVoice::SFXDSVoice() - SFXDSBuffer is null!" );
  59. AssertFatal( mDSBuffer, "SFXDSVoice::SFXDSVoice() - Dsound buffer is null!" );
  60. }
  61. SFXDSVoice::~SFXDSVoice()
  62. {
  63. SAFE_RELEASE( mDSBuffer3D );
  64. SFXDSBuffer* dsBuffer = _getBuffer();
  65. if( dsBuffer )
  66. dsBuffer->releaseVoice( &mDSBuffer );
  67. mBuffer = NULL;
  68. }
  69. SFXStatus SFXDSVoice::_status() const
  70. {
  71. DWORD status = 0;
  72. mDSBuffer->GetStatus( &status );
  73. if ( status & DSBSTATUS_PLAYING )
  74. return SFXStatusPlaying;
  75. else
  76. return SFXStatusStopped;
  77. }
  78. void SFXDSVoice::_play()
  79. {
  80. DSAssert( mDSBuffer->Play( 0, 0, mIsLooping ? DSBPLAY_LOOPING : 0 ),
  81. "SFXDSVoice::_play() - Playback failed!" );
  82. }
  83. void SFXDSVoice::_stop()
  84. {
  85. DSAssert( mDSBuffer->Stop(), "SFXDSVoice::pause - stop failed!" );
  86. mDSBuffer->SetCurrentPosition( 0 );
  87. }
  88. void SFXDSVoice::_pause()
  89. {
  90. DSAssert( mDSBuffer->Stop(), "SFXDSVoice::pause - stop failed!" );
  91. }
  92. void SFXDSVoice::_seek( U32 sample )
  93. {
  94. U32 pos = mBuffer->getFormat().getBytesPerSample() * sample;
  95. mDSBuffer->SetCurrentPosition( pos );
  96. }
  97. U32 SFXDSVoice::_tell() const
  98. {
  99. DWORD position = 0;
  100. mDSBuffer->GetCurrentPosition( &position, NULL );
  101. U32 samplePos = _getBuffer()->getSamplePos( position );
  102. return samplePos;
  103. }
  104. void SFXDSVoice::setMinMaxDistance( F32 min, F32 max )
  105. {
  106. if ( !mDSBuffer3D )
  107. return;
  108. mDSBuffer3D->SetMinDistance( min, DS3D_DEFERRED );
  109. mDSBuffer3D->SetMaxDistance( max, DS3D_DEFERRED );
  110. }
  111. void SFXDSVoice::play( bool looping )
  112. {
  113. // If this is a 3d sound then we need
  114. // to commit any deferred settings before
  115. // we start playback else we can get some
  116. // glitches.
  117. if ( mDSBuffer3D )
  118. mDevice->_commitDeferred();
  119. // If this is a streaming buffer,
  120. // force looping.
  121. const bool isStreaming = mBuffer->isStreaming();
  122. if( isStreaming )
  123. looping = true;
  124. mIsLooping = looping;
  125. Parent::play( looping );
  126. }
  127. void SFXDSVoice::setVelocity( const VectorF& velocity )
  128. {
  129. if ( !mDSBuffer3D )
  130. return;
  131. DSAssert( mDSBuffer3D->SetVelocity( velocity.x, velocity.z, velocity.y, DS3D_DEFERRED ),
  132. "SFXDSVoice::setVelocity - couldn't update buffer!" );
  133. }
  134. void SFXDSVoice::setTransform( const MatrixF& transform )
  135. {
  136. if ( !mDSBuffer3D )
  137. return;
  138. Point3F pos, dir;
  139. transform.getColumn( 3, &pos );
  140. transform.getColumn( 1, &dir );
  141. DSAssert( mDSBuffer3D->SetPosition( pos.x, pos.z, pos.y, DS3D_DEFERRED ),
  142. "SFXDSVoice::setTransform - couldn't set position of the buffer." );
  143. DSAssert( mDSBuffer3D->SetConeOrientation( dir.x, dir.z, dir.y, DS3D_DEFERRED ),
  144. "SFXDSVoice::setTransform - couldn't set cone orientation of the buffer." );
  145. }
  146. /// Helper for converting floating point linear volume
  147. /// to a logrithmic integer volume for dsound.
  148. LONG SFXDSVoice::_linearToLogVolume( F32 linVolume )
  149. {
  150. LONG logVolume;
  151. if ( linVolume <= 0.0f )
  152. logVolume = DSBVOLUME_MIN;
  153. else
  154. {
  155. logVolume = -2000.0 * mLog( 1.0f / linVolume );
  156. logVolume = mClamp( logVolume, DSBVOLUME_MIN, DSBVOLUME_MAX );
  157. }
  158. return logVolume;
  159. }
  160. void SFXDSVoice::setVolume( F32 volume )
  161. {
  162. LONG logVolume = _linearToLogVolume( volume );
  163. HRESULT hr = mDSBuffer->SetVolume( logVolume );
  164. DSAssert( hr, "SFXDSVoice::setVolume - couldn't set volume!" );
  165. }
  166. void SFXDSVoice::setPitch( F32 pitch )
  167. {
  168. F32 sampleRate = _getBuffer()->getFormat().getSamplesPerSecond();
  169. F32 frequency = mFloor( mClampF( sampleRate * pitch, DSBFREQUENCY_MIN, DSBFREQUENCY_MAX ) );
  170. DSAssert( mDSBuffer->SetFrequency( ( U32 )frequency ),
  171. "SFXDSVoice::setPitch - couldn't set playback frequency.");
  172. }
  173. void SFXDSVoice::setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume )
  174. {
  175. if ( !mDSBuffer3D )
  176. return;
  177. DSAssert( mDSBuffer3D->SetConeAngles( innerAngle,
  178. outerAngle,
  179. DS3D_DEFERRED ),
  180. "SFXDSVoice::setCone - couldn't set cone angles!" );
  181. LONG logVolume = _linearToLogVolume( outerVolume );
  182. DSAssert( mDSBuffer3D->SetConeOutsideVolume( logVolume,
  183. DS3D_DEFERRED ),
  184. "SFXDSVoice::setCone - couldn't set cone outside volume!" );
  185. }