SoundVoice.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "snd_local.h"
  23. idCVar s_subFraction( "s_subFraction", "0.5", CVAR_ARCHIVE | CVAR_FLOAT, "Amount of each sound to send to the LFE channel" );
  24. idVec2 idSoundVoice_Base::speakerPositions[idWaveFile::CHANNEL_INDEX_MAX];
  25. int idSoundVoice_Base::speakerLeft[idWaveFile::CHANNEL_INDEX_MAX] = {0 };
  26. int idSoundVoice_Base::speakerRight[idWaveFile::CHANNEL_INDEX_MAX] = {0 };
  27. int idSoundVoice_Base::dstChannels = 0;
  28. int idSoundVoice_Base::dstMask = 0;
  29. int idSoundVoice_Base::dstCenter = -1;
  30. int idSoundVoice_Base::dstLFE = -1;
  31. int idSoundVoice_Base::dstMap[MAX_CHANNELS_PER_VOICE] = { 0 };
  32. int idSoundVoice_Base::invMap[idWaveFile::CHANNEL_INDEX_MAX] = { 0 };
  33. float idSoundVoice_Base::omniLevel = 1.0f;
  34. /*
  35. ========================
  36. idSoundVoice_Base::idSoundVoice_Base
  37. ========================
  38. */
  39. idSoundVoice_Base::idSoundVoice_Base() :
  40. position( 0.0f ),
  41. gain( 1.0f ),
  42. centerChannel( 0.0f ),
  43. pitch( 1.0f ),
  44. innerRadius( 32.0f ),
  45. occlusion( 0.0f ),
  46. channelMask( 0 ),
  47. innerSampleRangeSqr( 0.0f ),
  48. outerSampleRangeSqr( 0.0f )
  49. {
  50. }
  51. /*
  52. ========================
  53. idSoundVoice_Base::InitSurround
  54. ========================
  55. */
  56. void idSoundVoice_Base::InitSurround( int outputChannels, int channelMask ) {
  57. speakerPositions[idWaveFile::CHANNEL_INDEX_FRONT_LEFT ].Set( 0.70710678118654752440084436210485f, 0.70710678118654752440084436210485f ); // 45 degrees
  58. speakerPositions[idWaveFile::CHANNEL_INDEX_FRONT_RIGHT ].Set( 0.70710678118654752440084436210485f, -0.70710678118654752440084436210485f ); // 315 degrees
  59. speakerPositions[idWaveFile::CHANNEL_INDEX_FRONT_CENTER ].Set( 0.0f, 0.0f ); // 0 degrees
  60. speakerPositions[idWaveFile::CHANNEL_INDEX_LOW_FREQUENCY ].Set( 0.0f, 0.0f ); // -
  61. speakerPositions[idWaveFile::CHANNEL_INDEX_BACK_LEFT ].Set( -0.70710678118654752440084436210485f, 0.70710678118654752440084436210485f ); // 135 degrees
  62. speakerPositions[idWaveFile::CHANNEL_INDEX_BACK_RIGHT ].Set( -0.70710678118654752440084436210485f, -0.70710678118654752440084436210485f ); // 225 degrees
  63. speakerPositions[idWaveFile::CHANNEL_INDEX_FRONT_LEFT_CENTER ].Set( 0.92387953251128675612818318939679f, 0.3826834323650897717284599840304f ); // 22.5 degrees
  64. speakerPositions[idWaveFile::CHANNEL_INDEX_FRONT_RIGHT_CENTER ].Set( 0.92387953251128675612818318939679f, -0.3826834323650897717284599840304f ); // 337.5 degrees
  65. speakerPositions[idWaveFile::CHANNEL_INDEX_BACK_CENTER ].Set( -1.0f, 0.0f ); // 180 degrees
  66. speakerPositions[idWaveFile::CHANNEL_INDEX_SIDE_LEFT ].Set( 0.0f, 1.0f ); // 90 degrees
  67. speakerPositions[idWaveFile::CHANNEL_INDEX_SIDE_RIGHT ].Set( 0.0f, -1.0f ); // 270 degrees
  68. speakerLeft[idWaveFile::CHANNEL_INDEX_FRONT_LEFT_CENTER] = idWaveFile::CHANNEL_INDEX_FRONT_LEFT;
  69. speakerLeft[idWaveFile::CHANNEL_INDEX_FRONT_LEFT] = idWaveFile::CHANNEL_INDEX_SIDE_LEFT;
  70. speakerLeft[idWaveFile::CHANNEL_INDEX_SIDE_LEFT] = idWaveFile::CHANNEL_INDEX_BACK_LEFT;
  71. speakerLeft[idWaveFile::CHANNEL_INDEX_BACK_LEFT] = idWaveFile::CHANNEL_INDEX_BACK_CENTER;
  72. speakerLeft[idWaveFile::CHANNEL_INDEX_BACK_CENTER] = idWaveFile::CHANNEL_INDEX_BACK_RIGHT;
  73. speakerLeft[idWaveFile::CHANNEL_INDEX_BACK_RIGHT] = idWaveFile::CHANNEL_INDEX_SIDE_RIGHT;
  74. speakerLeft[idWaveFile::CHANNEL_INDEX_SIDE_RIGHT] = idWaveFile::CHANNEL_INDEX_FRONT_RIGHT;
  75. speakerLeft[idWaveFile::CHANNEL_INDEX_FRONT_RIGHT] = idWaveFile::CHANNEL_INDEX_FRONT_RIGHT_CENTER;
  76. speakerLeft[idWaveFile::CHANNEL_INDEX_FRONT_RIGHT_CENTER] = idWaveFile::CHANNEL_INDEX_FRONT_LEFT_CENTER;
  77. speakerLeft[idWaveFile::CHANNEL_INDEX_FRONT_CENTER] = idWaveFile::CHANNEL_INDEX_FRONT_CENTER;
  78. speakerLeft[idWaveFile::CHANNEL_INDEX_LOW_FREQUENCY] = idWaveFile::CHANNEL_INDEX_LOW_FREQUENCY;
  79. speakerRight[idWaveFile::CHANNEL_INDEX_FRONT_RIGHT_CENTER] = idWaveFile::CHANNEL_INDEX_FRONT_RIGHT;
  80. speakerRight[idWaveFile::CHANNEL_INDEX_FRONT_RIGHT] = idWaveFile::CHANNEL_INDEX_SIDE_RIGHT;
  81. speakerRight[idWaveFile::CHANNEL_INDEX_SIDE_RIGHT] = idWaveFile::CHANNEL_INDEX_BACK_RIGHT;
  82. speakerRight[idWaveFile::CHANNEL_INDEX_BACK_RIGHT] = idWaveFile::CHANNEL_INDEX_BACK_CENTER;
  83. speakerRight[idWaveFile::CHANNEL_INDEX_BACK_CENTER] = idWaveFile::CHANNEL_INDEX_BACK_LEFT;
  84. speakerRight[idWaveFile::CHANNEL_INDEX_BACK_LEFT] = idWaveFile::CHANNEL_INDEX_SIDE_LEFT;
  85. speakerRight[idWaveFile::CHANNEL_INDEX_SIDE_LEFT] = idWaveFile::CHANNEL_INDEX_FRONT_LEFT;
  86. speakerRight[idWaveFile::CHANNEL_INDEX_FRONT_LEFT] = idWaveFile::CHANNEL_INDEX_FRONT_LEFT_CENTER;
  87. speakerRight[idWaveFile::CHANNEL_INDEX_FRONT_LEFT_CENTER] = idWaveFile::CHANNEL_INDEX_FRONT_RIGHT_CENTER;
  88. speakerRight[idWaveFile::CHANNEL_INDEX_FRONT_CENTER] = idWaveFile::CHANNEL_INDEX_FRONT_CENTER;
  89. speakerRight[idWaveFile::CHANNEL_INDEX_LOW_FREQUENCY] = idWaveFile::CHANNEL_INDEX_LOW_FREQUENCY;
  90. dstChannels = outputChannels;
  91. dstMask = channelMask;
  92. // dstMap maps a destination channel to a speaker
  93. // invMap maps a speaker to a destination channel
  94. dstLFE = -1;
  95. dstCenter = -1;
  96. memset( dstMap, 0, sizeof( dstMap ) );
  97. memset( invMap, 0, sizeof( invMap ) );
  98. for ( int i = 0, c = 0; i < idWaveFile::CHANNEL_INDEX_MAX && c < MAX_CHANNELS_PER_VOICE; i++ ) {
  99. if ( dstMask & BIT(i) ) {
  100. if ( i == idWaveFile::CHANNEL_INDEX_LOW_FREQUENCY ) {
  101. dstLFE = c;
  102. }
  103. if ( i == idWaveFile::CHANNEL_INDEX_FRONT_CENTER ) {
  104. dstCenter = c;
  105. }
  106. dstMap[c] = i;
  107. invMap[i] = c++;
  108. } else {
  109. // Remove this speaker from the chain
  110. int right = speakerRight[i];
  111. int left = speakerLeft[i];
  112. speakerRight[left] = right;
  113. speakerLeft[right] = left;
  114. }
  115. }
  116. assert( ( dstLFE == -1 ) || ( ( dstMask & idWaveFile::CHANNEL_MASK_LOW_FREQUENCY ) != 0 ) );
  117. assert( ( dstCenter == -1 ) || ( ( dstMask & idWaveFile::CHANNEL_MASK_FRONT_CENTER ) != 0 ) );
  118. float omniChannels = (float)dstChannels;
  119. if ( dstMask & idWaveFile::CHANNEL_MASK_LOW_FREQUENCY ) {
  120. omniChannels -= 1.0f;
  121. }
  122. if ( dstMask & idWaveFile::CHANNEL_MASK_FRONT_CENTER ) {
  123. omniChannels -= 1.0f;
  124. }
  125. if ( omniChannels > 0.0f ) {
  126. omniLevel = 1.0f / omniChannels;
  127. } else {
  128. // This happens in mono mode
  129. omniLevel = 1.0f;
  130. }
  131. }
  132. /*
  133. ========================
  134. idSoundVoice_Base::CalculateSurround
  135. ========================
  136. */
  137. void idSoundVoice_Base::CalculateSurround( int srcChannels, float pLevelMatrix[ MAX_CHANNELS_PER_VOICE * MAX_CHANNELS_PER_VOICE ], float scale ) {
  138. // Hack for mono
  139. if ( dstChannels == 1 ) {
  140. if ( srcChannels == 1 ) {
  141. pLevelMatrix[ 0 ] = scale;
  142. } else if ( srcChannels == 2 ) {
  143. pLevelMatrix[ 0 ] = scale * 0.7071f;
  144. pLevelMatrix[ 1 ] = scale * 0.7071f;
  145. }
  146. return;
  147. }
  148. #define MATINDEX( src, dst ) ( srcChannels * dst + src )
  149. float subFraction = s_subFraction.GetFloat();
  150. if ( srcChannels == 1 ) {
  151. idVec2 p2 = position.ToVec2();
  152. float centerFraction = centerChannel;
  153. float sqrLength = p2.LengthSqr();
  154. if ( sqrLength <= 0.01f ) {
  155. // If we are on top of the listener, simply route all channels to each speaker equally
  156. for ( int i = 0; i < dstChannels; i++ ) {
  157. pLevelMatrix[MATINDEX( 0, i )] = omniLevel;
  158. }
  159. } else {
  160. float invLength = idMath::InvSqrt( sqrLength );
  161. float distance = ( invLength * sqrLength );
  162. p2 *= invLength;
  163. float spatialize = 1.0f;
  164. if ( distance < innerRadius ) {
  165. spatialize = distance / innerRadius;
  166. }
  167. float omni = omniLevel * ( 1.0f - spatialize );
  168. if ( dstCenter != -1 ) {
  169. centerFraction *= Max( 0.0f, p2.x );
  170. spatialize *= ( 1.0f - centerFraction );
  171. omni *= ( 1.0f - centerFraction );
  172. }
  173. float channelDots[MAX_CHANNELS_PER_VOICE] = { 0 };
  174. for ( int i = 0; i < dstChannels; i++ ) {
  175. // Calculate the contribution to each destination channel
  176. channelDots[i] = speakerPositions[dstMap[i]] * p2;
  177. }
  178. // Find the speaker nearest to the sound
  179. int channelA = 0;
  180. for ( int i = 1; i < dstChannels; i++ ) {
  181. if ( channelDots[i] > channelDots[channelA] ) {
  182. channelA = i;
  183. }
  184. }
  185. int speakerA = dstMap[channelA];
  186. // Find the 2nd nearest speaker
  187. int speakerB;
  188. float speakerACross = ( speakerPositions[speakerA].x * p2.y ) - ( speakerPositions[speakerA].y * p2.x );
  189. if ( speakerACross > 0.0f ) {
  190. speakerB = speakerLeft[speakerA];
  191. } else {
  192. speakerB = speakerRight[speakerA];
  193. }
  194. int channelB = invMap[speakerB];
  195. // Divide the amplitude between the 2 closest speakers
  196. float distA = ( speakerPositions[speakerA] - p2 ).Length();
  197. float distB = ( speakerPositions[speakerB] - p2 ).Length();
  198. float distCinv = 1.0f / ( distA + distB );
  199. float volumes[MAX_CHANNELS_PER_VOICE] = { 0 };
  200. volumes[channelA] = ( distB * distCinv );
  201. volumes[channelB] = ( distA * distCinv );
  202. for ( int i = 0; i < dstChannels; i++ ) {
  203. pLevelMatrix[MATINDEX( 0, i )] = ( volumes[i] * spatialize ) + omni;
  204. }
  205. }
  206. if ( dstLFE != -1 ) {
  207. pLevelMatrix[MATINDEX( 0, dstLFE )] = subFraction;
  208. }
  209. if ( dstCenter != -1 ) {
  210. pLevelMatrix[MATINDEX( 0, dstCenter )] = centerFraction;
  211. }
  212. } else if ( srcChannels == 2 ) {
  213. pLevelMatrix[ MATINDEX( 0, 0 ) ] = 1.0f;
  214. pLevelMatrix[ MATINDEX( 1, 1 ) ] = 1.0f;
  215. if ( dstLFE != -1 ) {
  216. pLevelMatrix[ MATINDEX( 0, dstLFE ) ] = subFraction * 0.5f;
  217. pLevelMatrix[ MATINDEX( 1, dstLFE ) ] = subFraction * 0.5f;
  218. }
  219. } else {
  220. idLib::Warning( "We don't support %d channel sound files", srcChannels );
  221. }
  222. for ( int i = 0; i < srcChannels * dstChannels; i++ ) {
  223. pLevelMatrix[ i ] *= scale;
  224. }
  225. }