sfxALVoice.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "platform/platform.h"
  23. #include "sfx/openal/sfxALVoice.h"
  24. #include "sfx/openal/sfxALBuffer.h"
  25. #include "sfx/openal/sfxALDevice.h"
  26. #ifdef TORQUE_DEBUG
  27. # define AL_SANITY_CHECK() \
  28. AssertFatal( mOpenAL.alIsSource( mSourceName ), "AL Source Sanity Check Failed!" );
  29. #else
  30. # define AL_SANITY_CHECK()
  31. #endif
  32. //#define DEBUG_SPEW
  33. SFXALVoice* SFXALVoice::create( SFXALDevice* device, SFXALBuffer *buffer )
  34. {
  35. AssertFatal( buffer, "SFXALVoice::create() - Got null buffer!" );
  36. ALuint sourceName;
  37. device->mOpenAL.alGenSources( 1, &sourceName );
  38. AssertFatal( device->mOpenAL.alIsSource( sourceName ), "AL Source Sanity Check Failed!" );
  39. // Is this 3d?
  40. // Okay, this looks odd, but bear with me for a moment. AL_SOURCE_RELATIVE does NOT indicate
  41. // whether or not the volume of the sound should change depending on the position of the listener.
  42. // OpenAL assumes that the volume will ALWAYS depend on the position of the listener. What AL_SOURCE_RELATIVE
  43. // does do is dictate if the position of THIS SOURCE is relative to the listener. If AL_SOURCE_RELATIVE is AL_TRUE
  44. // and the source's position is 0, 0, 0, then the source is directly on top of the listener at all times, which is what
  45. // we want for non-3d sounds.
  46. device->mOpenAL.alSourcei( sourceName, AL_SOURCE_RELATIVE, ( buffer->mIs3d ? AL_FALSE : AL_TRUE ) );
  47. if( buffer->mIs3d )
  48. device->mOpenAL.alSourcef( sourceName, AL_ROLLOFF_FACTOR, device->mRolloffFactor );
  49. SFXALVoice *voice = new SFXALVoice( device->mOpenAL,
  50. buffer,
  51. sourceName );
  52. return voice;
  53. }
  54. SFXALVoice::SFXALVoice( const OPENALFNTABLE &oalft,
  55. SFXALBuffer *buffer,
  56. ALuint sourceName )
  57. : Parent( buffer ),
  58. mSourceName( sourceName ),
  59. mResumeAtSampleOffset( -1.0f ),
  60. mSampleOffset( 0 ),
  61. mOpenAL( oalft )
  62. {
  63. AL_SANITY_CHECK();
  64. }
  65. SFXALVoice::~SFXALVoice()
  66. {
  67. mOpenAL.alDeleteSources( 1, &mSourceName );
  68. }
  69. void SFXALVoice::_lateBindStaticBufferIfNecessary()
  70. {
  71. if( !mBuffer->isStreaming() )
  72. {
  73. ALint bufferId;
  74. mOpenAL.alGetSourcei( mSourceName, AL_BUFFER, &bufferId );
  75. if( !bufferId )
  76. mOpenAL.alSourcei( mSourceName, AL_BUFFER, _getBuffer()->mALBuffer );
  77. }
  78. }
  79. SFXStatus SFXALVoice::_status() const
  80. {
  81. AL_SANITY_CHECK();
  82. ALint state;
  83. mOpenAL.alGetSourcei( mSourceName, AL_SOURCE_STATE, &state );
  84. switch( state )
  85. {
  86. case AL_PLAYING: return SFXStatusPlaying;
  87. case AL_PAUSED: return SFXStatusPaused;
  88. default: return SFXStatusStopped;
  89. }
  90. }
  91. void SFXALVoice::_play()
  92. {
  93. AL_SANITY_CHECK();
  94. _lateBindStaticBufferIfNecessary();
  95. #ifdef DEBUG_SPEW
  96. Platform::outputDebugString( "[SFXALVoice] Starting playback" );
  97. #endif
  98. //send every voice that plays to the alauxiliary slot that has the reverb
  99. mOpenAL.alSource3i(mSourceName, AL_AUXILIARY_SEND_FILTER, 1, 0, AL_FILTER_NULL);
  100. mOpenAL.alSourcePlay( mSourceName );
  101. //WORKAROUND: Adjust play cursor for buggy OAL when resuming playback. Do this after alSourcePlay
  102. // as it is the play function that will cause the cursor to jump.
  103. if( mResumeAtSampleOffset != -1.0f )
  104. {
  105. mOpenAL.alSourcef( mSourceName, AL_SAMPLE_OFFSET, mResumeAtSampleOffset );
  106. mResumeAtSampleOffset = -1.0f;
  107. }
  108. }
  109. void SFXALVoice::_pause()
  110. {
  111. AL_SANITY_CHECK();
  112. #ifdef DEBUG_SPEW
  113. Platform::outputDebugString( "[SFXALVoice] Pausing playback" );
  114. #endif
  115. mOpenAL.alSourcePause( mSourceName );
  116. //WORKAROUND: Another workaround for buggy OAL. Resuming playback of a paused source will cause the
  117. // play cursor to jump. Save the cursor so we can manually move it into position in _play(). Sigh.
  118. mOpenAL.alGetSourcef( mSourceName, AL_SAMPLE_OFFSET, &mResumeAtSampleOffset );
  119. }
  120. void SFXALVoice::_stop()
  121. {
  122. AL_SANITY_CHECK();
  123. #ifdef DEBUG_SPEW
  124. Platform::outputDebugString( "[SFXALVoice] Stopping playback" );
  125. #endif
  126. mOpenAL.alSourceStop( mSourceName );
  127. mSampleOffset = 0;
  128. mResumeAtSampleOffset = -1.0f;
  129. }
  130. void SFXALVoice::_seek( U32 sample )
  131. {
  132. AL_SANITY_CHECK();
  133. _lateBindStaticBufferIfNecessary();
  134. mOpenAL.alSourcei( mSourceName, AL_SAMPLE_OFFSET, sample );
  135. mResumeAtSampleOffset = -1.0f;
  136. }
  137. U32 SFXALVoice::_tell() const
  138. {
  139. // Flush processed buffers as AL_SAMPLE_OFFSET will snap back to zero as soon
  140. // as the queue is processed in whole.
  141. if( mBuffer->isStreaming() )
  142. mBuffer->write( NULL, 0 );
  143. ALint pos;
  144. mOpenAL.alGetSourcei( mSourceName, AL_SAMPLE_OFFSET, &pos );
  145. return ( pos + mSampleOffset );
  146. }
  147. void SFXALVoice::setMinMaxDistance( F32 min, F32 max )
  148. {
  149. AL_SANITY_CHECK();
  150. mOpenAL.alSourcef( mSourceName, AL_REFERENCE_DISTANCE, min );
  151. mOpenAL.alSourcef( mSourceName, AL_MAX_DISTANCE, max );
  152. }
  153. void SFXALVoice::play( bool looping )
  154. {
  155. AL_SANITY_CHECK();
  156. mOpenAL.alSourceStop( mSourceName );
  157. if( !mBuffer->isStreaming() )
  158. mOpenAL.alSourcei( mSourceName, AL_LOOPING, ( looping ? AL_TRUE : AL_FALSE ) );
  159. Parent::play( looping );
  160. }
  161. void SFXALVoice::setVelocity( const VectorF& velocity )
  162. {
  163. AL_SANITY_CHECK();
  164. // Torque and OpenAL are both right handed
  165. // systems, so no coordinate flipping is needed.
  166. mOpenAL.alSourcefv( mSourceName, AL_VELOCITY, velocity );
  167. }
  168. void SFXALVoice::setTransform( const MatrixF& transform )
  169. {
  170. AL_SANITY_CHECK();
  171. // Torque and OpenAL are both right handed
  172. // systems, so no coordinate flipping is needed.
  173. Point3F pos, dir;
  174. transform.getColumn( 3, &pos );
  175. transform.getColumn( 1, &dir );
  176. mOpenAL.alSourcefv( mSourceName, AL_POSITION, pos );
  177. mOpenAL.alSourcefv( mSourceName, AL_DIRECTION, dir );
  178. }
  179. void SFXALVoice::setVolume( F32 volume )
  180. {
  181. AL_SANITY_CHECK();
  182. mOpenAL.alSourcef( mSourceName, AL_GAIN, volume );
  183. }
  184. void SFXALVoice::setPitch( F32 pitch )
  185. {
  186. AL_SANITY_CHECK();
  187. mOpenAL.alSourcef( mSourceName, AL_PITCH, pitch );
  188. }
  189. void SFXALVoice::setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume )
  190. {
  191. AL_SANITY_CHECK();
  192. mOpenAL.alSourcef( mSourceName, AL_CONE_INNER_ANGLE, innerAngle );
  193. mOpenAL.alSourcef( mSourceName, AL_CONE_OUTER_ANGLE, outerAngle );
  194. mOpenAL.alSourcef( mSourceName, AL_CONE_OUTER_GAIN, outerVolume );
  195. }
  196. void SFXALVoice::setRolloffFactor( F32 factor )
  197. {
  198. mOpenAL.alSourcef( mSourceName, AL_ROLLOFF_FACTOR, factor );
  199. }