snd_emitter.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../framework/Session.h"
  23. #include "../renderer/RenderWorld.h"
  24. #include "snd_local.h"
  25. /*
  26. ===================
  27. idSoundFade::Clear
  28. ===================
  29. */
  30. void idSoundFade::Clear() {
  31. fadeStart44kHz = 0;
  32. fadeEnd44kHz = 0;
  33. fadeStartVolume = 0;
  34. fadeEndVolume = 0;
  35. }
  36. /*
  37. ===================
  38. idSoundFade::FadeDbAt44kHz
  39. ===================
  40. */
  41. float idSoundFade::FadeDbAt44kHz( int current44kHz ) {
  42. float fadeDb;
  43. if ( current44kHz >= fadeEnd44kHz ) {
  44. fadeDb = fadeEndVolume;
  45. } else if ( current44kHz > fadeStart44kHz ) {
  46. float fraction = ( fadeEnd44kHz - fadeStart44kHz );
  47. float over = ( current44kHz - fadeStart44kHz );
  48. fadeDb = fadeStartVolume + ( fadeEndVolume - fadeStartVolume ) * over / fraction;
  49. } else {
  50. fadeDb = fadeStartVolume;
  51. }
  52. return fadeDb;
  53. }
  54. //========================================================================
  55. /*
  56. =======================
  57. GeneratePermutedList
  58. Fills in elements[0] .. elements[numElements-1] with a permutation of
  59. 0 .. numElements-1 based on the permute parameter
  60. numElements == 3
  61. maxPermute = 6
  62. permute 0 = 012
  63. permute 1 = 021
  64. permute 2 = 102
  65. permute 3 = 120
  66. permute 4 = 201
  67. permute 5 = 210
  68. =======================
  69. */
  70. void PermuteList_r( int *list, int listLength, int permute, int maxPermute ) {
  71. if ( listLength < 2 ) {
  72. return;
  73. }
  74. permute %= maxPermute;
  75. int swap = permute * listLength / maxPermute;
  76. int old = list[swap];
  77. list[swap] = list[0];
  78. list[0] = old;
  79. maxPermute /= listLength;
  80. PermuteList_r( list + 1, listLength - 1, permute, maxPermute );
  81. }
  82. int Factorial( int val ) {
  83. int fact = val;
  84. while ( val > 1 ) {
  85. val--;
  86. fact *= val;
  87. }
  88. return fact;
  89. }
  90. void GeneratePermutedList( int *list, int listLength, int permute ) {
  91. for ( int i = 0 ; i < listLength ; i++ ) {
  92. list[i] = i;
  93. }
  94. // we can't calculate > 12 factorial, so we can't easily build a permuted list
  95. if ( listLength > 12 ) {
  96. return;
  97. }
  98. // calculate listLength factorial
  99. int maxPermute = Factorial( listLength );
  100. // recursively permute
  101. PermuteList_r( list, listLength, permute, maxPermute );
  102. }
  103. void TestPermutations( void ) {
  104. int list[SOUND_MAX_LIST_WAVS];
  105. for ( int len = 1 ; len < 5 ; len++ ) {
  106. common->Printf( "list length: %i\n", len );
  107. int max = Factorial( len );
  108. for ( int j = 0 ; j < max * 2 ; j++ ) {
  109. GeneratePermutedList( list, len, j );
  110. common->Printf( "%4i : ", j );
  111. for ( int k = 0 ; k < len ; k++ ) {
  112. common->Printf( "%i", list[k] );
  113. }
  114. common->Printf( "\n" );
  115. }
  116. }
  117. }
  118. //=====================================================================================
  119. /*
  120. ===================
  121. idSoundChannel::idSoundChannel
  122. ===================
  123. */
  124. idSoundChannel::idSoundChannel( void ) {
  125. decoder = NULL;
  126. Clear();
  127. }
  128. /*
  129. ===================
  130. idSoundChannel::~idSoundChannel
  131. ===================
  132. */
  133. idSoundChannel::~idSoundChannel( void ) {
  134. Clear();
  135. }
  136. /*
  137. ===================
  138. idSoundChannel::Clear
  139. ===================
  140. */
  141. void idSoundChannel::Clear( void ) {
  142. int j;
  143. Stop();
  144. soundShader = NULL;
  145. lastVolume = 0.0f;
  146. triggerChannel = SCHANNEL_ANY;
  147. channelFade.Clear();
  148. diversity = 0.0f;
  149. leadinSample = NULL;
  150. trigger44kHzTime = 0;
  151. for( j = 0; j < 6; j++ ) {
  152. lastV[j] = 0.0f;
  153. }
  154. memset( &parms, 0, sizeof(parms) );
  155. triggered = false;
  156. openalSource = 0;
  157. openalStreamingOffset = 0;
  158. openalStreamingBuffer[0] = openalStreamingBuffer[1] = openalStreamingBuffer[2] = 0;
  159. lastopenalStreamingBuffer[0] = lastopenalStreamingBuffer[1] = lastopenalStreamingBuffer[2] = 0;
  160. }
  161. /*
  162. ===================
  163. idSoundChannel::Start
  164. ===================
  165. */
  166. void idSoundChannel::Start( void ) {
  167. triggerState = true;
  168. if ( decoder == NULL ) {
  169. decoder = idSampleDecoder::Alloc();
  170. }
  171. }
  172. /*
  173. ===================
  174. idSoundChannel::Stop
  175. ===================
  176. */
  177. void idSoundChannel::Stop( void ) {
  178. triggerState = false;
  179. if ( decoder != NULL ) {
  180. idSampleDecoder::Free( decoder );
  181. decoder = NULL;
  182. }
  183. }
  184. /*
  185. ===================
  186. idSoundChannel::ALStop
  187. ===================
  188. */
  189. void idSoundChannel::ALStop( void ) {
  190. if ( alIsSource( openalSource ) ) {
  191. alSourceStop( openalSource );
  192. alSourcei( openalSource, AL_BUFFER, 0 );
  193. soundSystemLocal.FreeOpenALSource( openalSource );
  194. }
  195. if ( openalStreamingBuffer[0] && openalStreamingBuffer[1] && openalStreamingBuffer[2] ) {
  196. alGetError();
  197. alDeleteBuffers( 3, &openalStreamingBuffer[0] );
  198. if ( alGetError() == AL_NO_ERROR ) {
  199. openalStreamingBuffer[0] = openalStreamingBuffer[1] = openalStreamingBuffer[2] = 0;
  200. }
  201. }
  202. if ( lastopenalStreamingBuffer[0] && lastopenalStreamingBuffer[1] && lastopenalStreamingBuffer[2] ) {
  203. alGetError();
  204. alDeleteBuffers( 3, &lastopenalStreamingBuffer[0] );
  205. if ( alGetError() == AL_NO_ERROR ) {
  206. lastopenalStreamingBuffer[0] = lastopenalStreamingBuffer[1] = lastopenalStreamingBuffer[2] = 0;
  207. }
  208. }
  209. }
  210. /*
  211. ===================
  212. idSoundChannel::GatherChannelSamples
  213. Will always return 44kHz samples for the given range, even if it deeply looped or
  214. out of the range of the unlooped samples. Handles looping between multiple different
  215. samples and leadins
  216. ===================
  217. */
  218. void idSoundChannel::GatherChannelSamples( int sampleOffset44k, int sampleCount44k, float *dest ) const {
  219. float *dest_p = dest;
  220. int len;
  221. //Sys_DebugPrintf( "msec:%i sample:%i : %i : %i\n", Sys_Milliseconds(), soundSystemLocal.GetCurrent44kHzTime(), sampleOffset44k, sampleCount44k ); //!@#
  222. // negative offset times will just zero fill
  223. if ( sampleOffset44k < 0 ) {
  224. len = -sampleOffset44k;
  225. if ( len > sampleCount44k ) {
  226. len = sampleCount44k;
  227. }
  228. memset( dest_p, 0, len * sizeof( dest_p[0] ) );
  229. dest_p += len;
  230. sampleCount44k -= len;
  231. sampleOffset44k += len;
  232. }
  233. // grab part of the leadin sample
  234. idSoundSample *leadin = leadinSample;
  235. if ( !leadin || sampleOffset44k < 0 || sampleCount44k <= 0 ) {
  236. memset( dest_p, 0, sampleCount44k * sizeof( dest_p[0] ) );
  237. return;
  238. }
  239. if ( sampleOffset44k < leadin->LengthIn44kHzSamples() ) {
  240. len = leadin->LengthIn44kHzSamples() - sampleOffset44k;
  241. if ( len > sampleCount44k ) {
  242. len = sampleCount44k;
  243. }
  244. // decode the sample
  245. decoder->Decode( leadin, sampleOffset44k, len, dest_p );
  246. dest_p += len;
  247. sampleCount44k -= len;
  248. sampleOffset44k += len;
  249. }
  250. // if not looping, zero fill any remaining spots
  251. if ( !soundShader || !( parms.soundShaderFlags & SSF_LOOPING ) ) {
  252. memset( dest_p, 0, sampleCount44k * sizeof( dest_p[0] ) );
  253. return;
  254. }
  255. // fill the remainder with looped samples
  256. idSoundSample *loop = soundShader->entries[0];
  257. if ( !loop ) {
  258. memset( dest_p, 0, sampleCount44k * sizeof( dest_p[0] ) );
  259. return;
  260. }
  261. sampleOffset44k -= leadin->LengthIn44kHzSamples();
  262. while( sampleCount44k > 0 ) {
  263. int totalLen = loop->LengthIn44kHzSamples();
  264. sampleOffset44k %= totalLen;
  265. len = totalLen - sampleOffset44k;
  266. if ( len > sampleCount44k ) {
  267. len = sampleCount44k;
  268. }
  269. // decode the sample
  270. decoder->Decode( loop, sampleOffset44k, len, dest_p );
  271. dest_p += len;
  272. sampleCount44k -= len;
  273. sampleOffset44k += len;
  274. }
  275. }
  276. //=====================================================================================
  277. /*
  278. ===============
  279. idSoundEmitterLocal::idSoundEmitterLocal
  280. ===============
  281. */
  282. idSoundEmitterLocal::idSoundEmitterLocal( void ) {
  283. soundWorld = NULL;
  284. Clear();
  285. }
  286. /*
  287. ===============
  288. idSoundEmitterLocal::~idSoundEmitterLocal
  289. ===============
  290. */
  291. idSoundEmitterLocal::~idSoundEmitterLocal( void ) {
  292. Clear();
  293. }
  294. /*
  295. ===============
  296. idSoundEmitterLocal::Clear
  297. ===============
  298. */
  299. void idSoundEmitterLocal::Clear( void ) {
  300. int i;
  301. for( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  302. channels[i].ALStop();
  303. channels[i].Clear();
  304. }
  305. removeStatus = REMOVE_STATUS_SAMPLEFINISHED;
  306. distance = 0.0f;
  307. lastValidPortalArea = -1;
  308. playing = false;
  309. hasShakes = false;
  310. ampTime = 0; // last time someone queried
  311. amplitude = 0;
  312. maxDistance = 10.0f; // meters
  313. spatializedOrigin.Zero();
  314. memset( &parms, 0, sizeof( parms ) );
  315. }
  316. /*
  317. ==================
  318. idSoundEmitterLocal::OverrideParms
  319. ==================
  320. */
  321. void idSoundEmitterLocal::OverrideParms( const soundShaderParms_t *base,
  322. const soundShaderParms_t *over, soundShaderParms_t *out ) {
  323. if ( !over ) {
  324. *out = *base;
  325. return;
  326. }
  327. if ( over->minDistance ) {
  328. out->minDistance = over->minDistance;
  329. } else {
  330. out->minDistance = base->minDistance;
  331. }
  332. if ( over->maxDistance ) {
  333. out->maxDistance = over->maxDistance;
  334. } else {
  335. out->maxDistance = base->maxDistance;
  336. }
  337. if ( over->shakes ) {
  338. out->shakes = over->shakes;
  339. } else {
  340. out->shakes = base->shakes;
  341. }
  342. if ( over->volume ) {
  343. out->volume = over->volume;
  344. } else {
  345. out->volume = base->volume;
  346. }
  347. if ( over->soundClass ) {
  348. out->soundClass = over->soundClass;
  349. } else {
  350. out->soundClass = base->soundClass;
  351. }
  352. out->soundShaderFlags = base->soundShaderFlags | over->soundShaderFlags;
  353. }
  354. /*
  355. ==================
  356. idSoundEmitterLocal::CheckForCompletion
  357. Checks to see if all the channels have completed, clearing the playing flag if necessary.
  358. Sets the playing and shakes bools.
  359. ==================
  360. */
  361. void idSoundEmitterLocal::CheckForCompletion( int current44kHzTime ) {
  362. bool hasActive;
  363. int i;
  364. hasActive = false;
  365. hasShakes = false;
  366. if ( playing ) {
  367. for ( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  368. idSoundChannel *chan = &channels[i];
  369. if ( !chan->triggerState ) {
  370. continue;
  371. }
  372. const idSoundShader *shader = chan->soundShader;
  373. if ( !shader ) {
  374. continue;
  375. }
  376. // see if this channel has completed
  377. if ( !( chan->parms.soundShaderFlags & SSF_LOOPING ) ) {
  378. ALint state = AL_PLAYING;
  379. if ( alIsSource( chan->openalSource ) ) {
  380. alGetSourcei( chan->openalSource, AL_SOURCE_STATE, &state );
  381. }
  382. idSlowChannel slow = GetSlowChannel( chan );
  383. if ( soundWorld->slowmoActive && slow.IsActive() ) {
  384. if ( slow.GetCurrentPosition().time >= chan->leadinSample->LengthIn44kHzSamples() / 2 ) {
  385. chan->Stop();
  386. // if this was an onDemand sound, purge the sample now
  387. if ( chan->leadinSample->onDemand ) {
  388. chan->leadinSample->PurgeSoundSample();
  389. }
  390. continue;
  391. }
  392. } else if ( ( chan->trigger44kHzTime + chan->leadinSample->LengthIn44kHzSamples() < current44kHzTime ) || ( state == AL_STOPPED ) ) {
  393. chan->Stop();
  394. // free hardware resources
  395. chan->ALStop();
  396. // if this was an onDemand sound, purge the sample now
  397. if ( chan->leadinSample->onDemand ) {
  398. chan->leadinSample->PurgeSoundSample();
  399. }
  400. continue;
  401. }
  402. }
  403. // free decoder memory if no sound was decoded for a while
  404. if ( chan->decoder != NULL && chan->decoder->GetLastDecodeTime() < current44kHzTime - SOUND_DECODER_FREE_DELAY ) {
  405. chan->decoder->ClearDecoder();
  406. }
  407. hasActive = true;
  408. if ( chan->parms.shakes > 0.0f ) {
  409. hasShakes = true;
  410. }
  411. }
  412. }
  413. // mark the entire sound emitter as non-playing if there aren't any active channels
  414. if ( !hasActive ) {
  415. playing = false;
  416. if ( removeStatus == REMOVE_STATUS_WAITSAMPLEFINISHED ) {
  417. // this can now be reused by the next request for a new soundEmitter
  418. removeStatus = REMOVE_STATUS_SAMPLEFINISHED;
  419. }
  420. }
  421. }
  422. /*
  423. ===================
  424. idSoundEmitterLocal::Spatialize
  425. Called once each sound frame by the main thread from idSoundWorldLocal::PlaceOrigin
  426. ===================
  427. */
  428. void idSoundEmitterLocal::Spatialize( idVec3 listenerPos, int listenerArea, idRenderWorld *rw ) {
  429. int i;
  430. //
  431. // work out the maximum distance of all the playing channels
  432. //
  433. maxDistance = 0;
  434. for ( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  435. idSoundChannel *chan = &channels[i];
  436. if ( !chan->triggerState ) {
  437. continue;
  438. }
  439. if ( chan->parms.maxDistance > maxDistance ) {
  440. maxDistance = chan->parms.maxDistance;
  441. }
  442. }
  443. //
  444. // work out where the sound comes from
  445. //
  446. idVec3 realOrigin = origin * DOOM_TO_METERS;
  447. idVec3 len = listenerPos - realOrigin;
  448. realDistance = len.LengthFast();
  449. if ( realDistance >= maxDistance ) {
  450. // no way to possibly hear it
  451. distance = realDistance;
  452. return;
  453. }
  454. //
  455. // work out virtual origin and distance, which may be from a portal instead of the actual origin
  456. //
  457. distance = maxDistance * METERS_TO_DOOM;
  458. if ( listenerArea == -1 ) { // listener is outside the world
  459. return;
  460. }
  461. if ( rw ) {
  462. // we have a valid renderWorld
  463. int soundInArea = rw->PointInArea( origin );
  464. if ( soundInArea == -1 ) {
  465. if ( lastValidPortalArea == -1 ) { // sound is outside the world
  466. distance = realDistance;
  467. spatializedOrigin = origin; // sound is in our area
  468. return;
  469. }
  470. soundInArea = lastValidPortalArea;
  471. }
  472. lastValidPortalArea = soundInArea;
  473. if ( soundInArea == listenerArea ) {
  474. distance = realDistance;
  475. spatializedOrigin = origin; // sound is in our area
  476. return;
  477. }
  478. soundWorld->ResolveOrigin( 0, NULL, soundInArea, 0.0f, origin, this );
  479. distance /= METERS_TO_DOOM;
  480. } else {
  481. // no portals available
  482. distance = realDistance;
  483. spatializedOrigin = origin; // sound is in our area
  484. }
  485. }
  486. /*
  487. ===========================================================================================
  488. PUBLIC FUNCTIONS
  489. ===========================================================================================
  490. */
  491. /*
  492. =====================
  493. idSoundEmitterLocal::UpdateEmitter
  494. =====================
  495. */
  496. void idSoundEmitterLocal::UpdateEmitter( const idVec3 &origin, int listenerId, const soundShaderParms_t *parms ) {
  497. if ( !parms ) {
  498. common->Error( "idSoundEmitterLocal::UpdateEmitter: NULL parms" );
  499. }
  500. if ( soundWorld && soundWorld->writeDemo ) {
  501. soundWorld->writeDemo->WriteInt( DS_SOUND );
  502. soundWorld->writeDemo->WriteInt( SCMD_UPDATE );
  503. soundWorld->writeDemo->WriteInt( index );
  504. soundWorld->writeDemo->WriteVec3( origin );
  505. soundWorld->writeDemo->WriteInt( listenerId );
  506. soundWorld->writeDemo->WriteFloat( parms->minDistance );
  507. soundWorld->writeDemo->WriteFloat( parms->maxDistance );
  508. soundWorld->writeDemo->WriteFloat( parms->volume );
  509. soundWorld->writeDemo->WriteFloat( parms->shakes );
  510. soundWorld->writeDemo->WriteInt( parms->soundShaderFlags );
  511. soundWorld->writeDemo->WriteInt( parms->soundClass );
  512. }
  513. this->origin = origin;
  514. this->listenerId = listenerId;
  515. this->parms = *parms;
  516. // FIXME: change values on all channels?
  517. }
  518. /*
  519. =====================
  520. idSoundEmitterLocal::Free
  521. They are never truly freed, just marked so they can be reused by the soundWorld
  522. =====================
  523. */
  524. void idSoundEmitterLocal::Free( bool immediate ) {
  525. if ( removeStatus != REMOVE_STATUS_ALIVE ) {
  526. return;
  527. }
  528. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  529. common->Printf( "FreeSound (%i,%i)\n", index, (int)immediate );
  530. }
  531. if ( soundWorld && soundWorld->writeDemo ) {
  532. soundWorld->writeDemo->WriteInt( DS_SOUND );
  533. soundWorld->writeDemo->WriteInt( SCMD_FREE );
  534. soundWorld->writeDemo->WriteInt( index );
  535. soundWorld->writeDemo->WriteInt( immediate );
  536. }
  537. if ( !immediate ) {
  538. removeStatus = REMOVE_STATUS_WAITSAMPLEFINISHED;
  539. } else {
  540. Clear();
  541. }
  542. }
  543. /*
  544. =====================
  545. idSoundEmitterLocal::StartSound
  546. returns the length of the started sound in msec
  547. =====================
  548. */
  549. int idSoundEmitterLocal::StartSound( const idSoundShader *shader, const s_channelType channel, float diversity, int soundShaderFlags, bool allowSlow ) {
  550. int i;
  551. if ( !shader ) {
  552. return 0;
  553. }
  554. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  555. common->Printf( "StartSound %ims (%i,%i,%s) = ", soundWorld->gameMsec, index, (int)channel, shader->GetName() );
  556. }
  557. if ( soundWorld && soundWorld->writeDemo ) {
  558. soundWorld->writeDemo->WriteInt( DS_SOUND );
  559. soundWorld->writeDemo->WriteInt( SCMD_START );
  560. soundWorld->writeDemo->WriteInt( index );
  561. soundWorld->writeDemo->WriteHashString( shader->GetName() );
  562. soundWorld->writeDemo->WriteInt( channel );
  563. soundWorld->writeDemo->WriteFloat( diversity );
  564. soundWorld->writeDemo->WriteInt( soundShaderFlags );
  565. }
  566. // build the channel parameters by taking the shader parms and optionally overriding
  567. soundShaderParms_t chanParms;
  568. chanParms = shader->parms;
  569. OverrideParms( &chanParms, &this->parms, &chanParms );
  570. chanParms.soundShaderFlags |= soundShaderFlags;
  571. if ( chanParms.shakes > 0.0f ) {
  572. shader->CheckShakesAndOgg();
  573. }
  574. // this is the sample time it will be first mixed
  575. int start44kHz;
  576. if ( soundWorld->fpa[0] ) {
  577. // if we are recording an AVI demo, don't use hardware time
  578. start44kHz = soundWorld->lastAVI44kHz + MIXBUFFER_SAMPLES;
  579. } else {
  580. start44kHz = soundSystemLocal.GetCurrent44kHzTime() + MIXBUFFER_SAMPLES;
  581. }
  582. //
  583. // pick which sound to play from the shader
  584. //
  585. if ( !shader->numEntries ) {
  586. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  587. common->Printf( "no samples in sound shader\n" );
  588. }
  589. return 0; // no sounds
  590. }
  591. int choice;
  592. // pick a sound from the list based on the passed diversity
  593. choice = (int)(diversity * shader->numEntries);
  594. if ( choice < 0 || choice >= shader->numEntries ) {
  595. choice = 0;
  596. }
  597. // bump the choice if the exact sound was just played and we are NO_DUPS
  598. if ( chanParms.soundShaderFlags & SSF_NO_DUPS ) {
  599. idSoundSample *sample;
  600. if ( shader->leadins[ choice ] ) {
  601. sample = shader->leadins[ choice ];
  602. } else {
  603. sample = shader->entries[ choice ];
  604. }
  605. for( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  606. idSoundChannel *chan = &channels[i];
  607. if ( chan->leadinSample == sample ) {
  608. choice = ( choice + 1 ) % shader->numEntries;
  609. break;
  610. }
  611. }
  612. }
  613. // PLAY_ONCE sounds will never be restarted while they are running
  614. if ( chanParms.soundShaderFlags & SSF_PLAY_ONCE ) {
  615. for( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  616. idSoundChannel *chan = &channels[i];
  617. if ( chan->triggerState && chan->soundShader == shader ) {
  618. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  619. common->Printf( "PLAY_ONCE not restarting\n" );
  620. }
  621. return 0;
  622. }
  623. }
  624. }
  625. // never play the same sound twice with the same starting time, even
  626. // if they are on different channels
  627. for( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  628. idSoundChannel *chan = &channels[i];
  629. if ( chan->triggerState && chan->soundShader == shader && chan->trigger44kHzTime == start44kHz ) {
  630. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  631. common->Printf( "already started this frame\n" );
  632. }
  633. return 0;
  634. }
  635. }
  636. Sys_EnterCriticalSection();
  637. // kill any sound that is currently playing on this channel
  638. if ( channel != SCHANNEL_ANY ) {
  639. for( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  640. idSoundChannel *chan = &channels[i];
  641. if ( chan->triggerState && chan->soundShader && chan->triggerChannel == channel ) {
  642. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  643. common->Printf( "(override %s)", chan->soundShader->base->GetName() );
  644. }
  645. chan->Stop();
  646. // if this was an onDemand sound, purge the sample now
  647. if ( chan->leadinSample->onDemand ) {
  648. chan->ALStop();
  649. chan->leadinSample->PurgeSoundSample();
  650. }
  651. break;
  652. }
  653. }
  654. }
  655. // find a free channel to play the sound on
  656. idSoundChannel *chan;
  657. for( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  658. chan = &channels[i];
  659. if ( !chan->triggerState ) {
  660. break;
  661. }
  662. }
  663. if ( i == SOUND_MAX_CHANNELS ) {
  664. // we couldn't find a channel for it
  665. Sys_LeaveCriticalSection();
  666. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  667. common->Printf( "no channels available\n" );
  668. }
  669. return 0;
  670. }
  671. chan = &channels[i];
  672. if ( shader->leadins[ choice ] ) {
  673. chan->leadinSample = shader->leadins[ choice ];
  674. } else {
  675. chan->leadinSample = shader->entries[ choice ];
  676. }
  677. // if the sample is onDemand (voice mails, etc), load it now
  678. if ( chan->leadinSample->purged ) {
  679. int start = Sys_Milliseconds();
  680. chan->leadinSample->Load();
  681. int end = Sys_Milliseconds();
  682. session->TimeHitch( end - start );
  683. // recalculate start44kHz, because loading may have taken a fair amount of time
  684. if ( !soundWorld->fpa[0] ) {
  685. start44kHz = soundSystemLocal.GetCurrent44kHzTime() + MIXBUFFER_SAMPLES;
  686. }
  687. }
  688. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  689. common->Printf( "'%s'\n", chan->leadinSample->name.c_str() );
  690. }
  691. if ( idSoundSystemLocal::s_skipHelltimeFX.GetBool() ) {
  692. chan->disallowSlow = true;
  693. } else {
  694. chan->disallowSlow = !allowSlow;
  695. }
  696. ResetSlowChannel( chan );
  697. // the sound will start mixing in the next async mix block
  698. chan->triggered = true;
  699. chan->openalStreamingOffset = 0;
  700. chan->trigger44kHzTime = start44kHz;
  701. chan->parms = chanParms;
  702. chan->triggerGame44kHzTime = soundWorld->game44kHz;
  703. chan->soundShader = shader;
  704. chan->triggerChannel = channel;
  705. chan->Start();
  706. // we need to start updating the def and mixing it in
  707. playing = true;
  708. // spatialize it immediately, so it will start the next mix block
  709. // even if that happens before the next PlaceOrigin()
  710. Spatialize( soundWorld->listenerPos, soundWorld->listenerArea, soundWorld->rw );
  711. // return length of sound in milliseconds
  712. int length = chan->leadinSample->LengthIn44kHzSamples();
  713. if ( chan->leadinSample->objectInfo.nChannels == 2 ) {
  714. length /= 2; // stereo samples
  715. }
  716. // adjust the start time based on diversity for looping sounds, so they don't all start
  717. // at the same point
  718. if ( chan->parms.soundShaderFlags & SSF_LOOPING && !chan->leadinSample->LengthIn44kHzSamples() ) {
  719. chan->trigger44kHzTime -= diversity * length;
  720. chan->trigger44kHzTime &= ~7; // so we don't have to worry about the 22kHz and 11kHz expansions
  721. // starting in fractional samples
  722. chan->triggerGame44kHzTime -= diversity * length;
  723. chan->triggerGame44kHzTime &= ~7;
  724. }
  725. length *= 1000 / (float)PRIMARYFREQ;
  726. Sys_LeaveCriticalSection();
  727. return length;
  728. }
  729. /*
  730. ===================
  731. idSoundEmitterLocal::ModifySound
  732. ===================
  733. */
  734. void idSoundEmitterLocal::ModifySound( const s_channelType channel, const soundShaderParms_t *parms ) {
  735. if ( !parms ) {
  736. common->Error( "idSoundEmitterLocal::ModifySound: NULL parms" );
  737. }
  738. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  739. common->Printf( "ModifySound(%i,%i)\n", index, channel );
  740. }
  741. if ( soundWorld && soundWorld->writeDemo ) {
  742. soundWorld->writeDemo->WriteInt( DS_SOUND );
  743. soundWorld->writeDemo->WriteInt( SCMD_MODIFY );
  744. soundWorld->writeDemo->WriteInt( index );
  745. soundWorld->writeDemo->WriteInt( channel );
  746. soundWorld->writeDemo->WriteFloat( parms->minDistance );
  747. soundWorld->writeDemo->WriteFloat( parms->maxDistance );
  748. soundWorld->writeDemo->WriteFloat( parms->volume );
  749. soundWorld->writeDemo->WriteFloat( parms->shakes );
  750. soundWorld->writeDemo->WriteInt( parms->soundShaderFlags );
  751. soundWorld->writeDemo->WriteInt( parms->soundClass );
  752. }
  753. for ( int i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  754. idSoundChannel *chan = &channels[i];
  755. if ( !chan->triggerState ) {
  756. continue;
  757. }
  758. if ( channel != SCHANNEL_ANY && chan->triggerChannel != channel ) {
  759. continue;
  760. }
  761. OverrideParms( &chan->parms, parms, &chan->parms );
  762. if ( chan->parms.shakes > 0.0f && chan->soundShader != NULL ) {
  763. chan->soundShader->CheckShakesAndOgg();
  764. }
  765. }
  766. }
  767. /*
  768. ===================
  769. idSoundEmitterLocal::StopSound
  770. can pass SCHANNEL_ANY
  771. ===================
  772. */
  773. void idSoundEmitterLocal::StopSound( const s_channelType channel ) {
  774. int i;
  775. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  776. common->Printf( "StopSound(%i,%i)\n", index, channel );
  777. }
  778. if ( soundWorld && soundWorld->writeDemo ) {
  779. soundWorld->writeDemo->WriteInt( DS_SOUND );
  780. soundWorld->writeDemo->WriteInt( SCMD_STOP );
  781. soundWorld->writeDemo->WriteInt( index );
  782. soundWorld->writeDemo->WriteInt( channel );
  783. }
  784. Sys_EnterCriticalSection();
  785. for( i = 0; i < SOUND_MAX_CHANNELS; i++ ) {
  786. idSoundChannel *chan = &channels[i];
  787. if ( !chan->triggerState ) {
  788. continue;
  789. }
  790. if ( channel != SCHANNEL_ANY && chan->triggerChannel != channel ) {
  791. continue;
  792. }
  793. // stop it
  794. chan->Stop();
  795. // free hardware resources
  796. chan->ALStop();
  797. // if this was an onDemand sound, purge the sample now
  798. if ( chan->leadinSample->onDemand ) {
  799. chan->leadinSample->PurgeSoundSample();
  800. }
  801. chan->leadinSample = NULL;
  802. chan->soundShader = NULL;
  803. }
  804. Sys_LeaveCriticalSection();
  805. }
  806. /*
  807. ===================
  808. idSoundEmitterLocal::FadeSound
  809. to is in Db (sigh), over is in seconds
  810. ===================
  811. */
  812. void idSoundEmitterLocal::FadeSound( const s_channelType channel, float to, float over ) {
  813. if ( idSoundSystemLocal::s_showStartSound.GetInteger() ) {
  814. common->Printf( "FadeSound(%i,%i,%f,%f )\n", index, channel, to, over );
  815. }
  816. if ( !soundWorld ) {
  817. return;
  818. }
  819. if ( soundWorld->writeDemo ) {
  820. soundWorld->writeDemo->WriteInt( DS_SOUND );
  821. soundWorld->writeDemo->WriteInt( SCMD_FADE );
  822. soundWorld->writeDemo->WriteInt( index );
  823. soundWorld->writeDemo->WriteInt( channel );
  824. soundWorld->writeDemo->WriteFloat( to );
  825. soundWorld->writeDemo->WriteFloat( over );
  826. }
  827. int start44kHz;
  828. if ( soundWorld->fpa[0] ) {
  829. // if we are recording an AVI demo, don't use hardware time
  830. start44kHz = soundWorld->lastAVI44kHz + MIXBUFFER_SAMPLES;
  831. } else {
  832. start44kHz = soundSystemLocal.GetCurrent44kHzTime() + MIXBUFFER_SAMPLES;
  833. }
  834. int length44kHz = soundSystemLocal.MillisecondsToSamples( over * 1000 );
  835. for( int i = 0; i < SOUND_MAX_CHANNELS ; i++ ) {
  836. idSoundChannel *chan = &channels[i];
  837. if ( !chan->triggerState ) {
  838. continue;
  839. }
  840. if ( channel != SCHANNEL_ANY && chan->triggerChannel != channel ) {
  841. continue;
  842. }
  843. // if it is already fading to this volume at this rate, don't change it
  844. if ( chan->channelFade.fadeEndVolume == to &&
  845. chan->channelFade.fadeEnd44kHz - chan->channelFade.fadeStart44kHz == length44kHz ) {
  846. continue;
  847. }
  848. // fade it
  849. chan->channelFade.fadeStartVolume = chan->channelFade.FadeDbAt44kHz( start44kHz );
  850. chan->channelFade.fadeStart44kHz = start44kHz;
  851. chan->channelFade.fadeEnd44kHz = start44kHz + length44kHz;
  852. chan->channelFade.fadeEndVolume = to;
  853. }
  854. }
  855. /*
  856. ===================
  857. idSoundEmitterLocal::CurrentlyPlaying
  858. ===================
  859. */
  860. bool idSoundEmitterLocal::CurrentlyPlaying( void ) const {
  861. return playing;
  862. }
  863. /*
  864. ===================
  865. idSoundEmitterLocal::Index
  866. ===================
  867. */
  868. int idSoundEmitterLocal::Index( void ) const {
  869. return index;
  870. }
  871. /*
  872. ===================
  873. idSoundEmitterLocal::CurrentAmplitude
  874. this is called from the main thread by the material shader system
  875. to allow lights and surface flares to vary with the sound amplitude
  876. ===================
  877. */
  878. float idSoundEmitterLocal::CurrentAmplitude( void ) {
  879. if ( idSoundSystemLocal::s_constantAmplitude.GetFloat() >= 0.0f ) {
  880. return idSoundSystemLocal::s_constantAmplitude.GetFloat();
  881. }
  882. if ( removeStatus > REMOVE_STATUS_WAITSAMPLEFINISHED ) {
  883. return 0.0;
  884. }
  885. int localTime = soundSystemLocal.GetCurrent44kHzTime();
  886. // see if we can use our cached value
  887. if ( ampTime == localTime ) {
  888. return amplitude;
  889. }
  890. // calculate a new value
  891. ampTime = localTime;
  892. amplitude = soundWorld->FindAmplitude( this, localTime, NULL, SCHANNEL_ANY, false );
  893. return amplitude;
  894. }
  895. /*
  896. ===================
  897. idSoundEmitterLocal::GetSlowChannel
  898. ===================
  899. */
  900. idSlowChannel idSoundEmitterLocal::GetSlowChannel( const idSoundChannel *chan ) {
  901. return slowChannels[chan - channels];
  902. }
  903. /*
  904. ===================
  905. idSoundEmitterLocal::SetSlowChannel
  906. ===================
  907. */
  908. void idSoundEmitterLocal::SetSlowChannel( const idSoundChannel *chan, idSlowChannel slow ) {
  909. slowChannels[chan - channels] = slow;
  910. }
  911. /*
  912. ===================
  913. idSoundEmitterLocal::ResetSlowChannel
  914. ===================
  915. */
  916. void idSoundEmitterLocal::ResetSlowChannel( const idSoundChannel *chan ) {
  917. int index = chan - channels;
  918. slowChannels[index].Reset();
  919. }
  920. /*
  921. ===================
  922. idSlowChannel::Reset
  923. ===================
  924. */
  925. void idSlowChannel::Reset() {
  926. memset( this, 0, sizeof( *this ) );
  927. this->chan = chan;
  928. curPosition.Set( 0 );
  929. newPosition.Set( 0 );
  930. curSampleOffset = -10000;
  931. newSampleOffset = -10000;
  932. triggerOffset = 0;
  933. }
  934. /*
  935. ===================
  936. idSlowChannel::AttachSoundChannel
  937. ===================
  938. */
  939. void idSlowChannel::AttachSoundChannel( const idSoundChannel *chan ) {
  940. this->chan = chan;
  941. }
  942. /*
  943. ===================
  944. idSlowChannel::GetSlowmoSpeed
  945. ===================
  946. */
  947. float idSlowChannel::GetSlowmoSpeed() {
  948. idSoundWorldLocal *sw = static_cast<idSoundWorldLocal*>( soundSystemLocal.GetPlayingSoundWorld() );
  949. if ( sw ) {
  950. return sw->slowmoSpeed;
  951. } else {
  952. return 0;
  953. }
  954. }
  955. /*
  956. ===================
  957. idSlowChannel::GenerateSlowChannel
  958. ===================
  959. */
  960. void idSlowChannel::GenerateSlowChannel( FracTime& playPos, int sampleCount44k, float* finalBuffer ) {
  961. idSoundWorldLocal *sw = static_cast<idSoundWorldLocal*>( soundSystemLocal.GetPlayingSoundWorld() );
  962. float in[MIXBUFFER_SAMPLES+3], out[MIXBUFFER_SAMPLES+3], *src, *spline, slowmoSpeed;
  963. int i, neededSamples, zeroedPos, count = 0;
  964. src = in + 2;
  965. spline = out + 2;
  966. if ( sw ) {
  967. slowmoSpeed = sw->slowmoSpeed;
  968. }
  969. else {
  970. slowmoSpeed = 1;
  971. }
  972. neededSamples = sampleCount44k * slowmoSpeed + 4;
  973. // get the channel's samples
  974. chan->GatherChannelSamples( playPos.time * 2, neededSamples, src );
  975. for ( i = 0; i < neededSamples >> 1; i++ ) {
  976. spline[i] = src[i*2];
  977. }
  978. // interpolate channel
  979. zeroedPos = playPos.time;
  980. playPos.time = 0;
  981. for ( i = 0; i < sampleCount44k >> 1; i++, count += 2 ) {
  982. float val;
  983. val = spline[playPos.time];
  984. src[i] = val;
  985. playPos.Increment( slowmoSpeed );
  986. }
  987. // lowpass filter
  988. float *in_p = in + 2, *out_p = out + 2;
  989. int numSamples = sampleCount44k >> 1;
  990. lowpass.GetContinuitySamples( in_p[-1], in_p[-2], out_p[-1], out_p[-2] );
  991. lowpass.SetParms( slowmoSpeed * 15000, 1.2f );
  992. for ( int i = 0, count = 0; i < numSamples; i++, count += 2 ) {
  993. lowpass.ProcessSample( in_p + i, out_p + i );
  994. finalBuffer[count] = finalBuffer[count+1] = out[i];
  995. }
  996. lowpass.SetContinuitySamples( in_p[numSamples-2], in_p[numSamples-3], out_p[numSamples-2], out_p[numSamples-3] );
  997. playPos.time += zeroedPos;
  998. }
  999. /*
  1000. ===================
  1001. idSlowChannel::GatherChannelSamples
  1002. ===================
  1003. */
  1004. void idSlowChannel::GatherChannelSamples( int sampleOffset44k, int sampleCount44k, float *dest ) {
  1005. int state = 0;
  1006. // setup chan
  1007. active = true;
  1008. newSampleOffset = sampleOffset44k >> 1;
  1009. // set state
  1010. if ( newSampleOffset < curSampleOffset ) {
  1011. state = PLAYBACK_RESET;
  1012. } else if ( newSampleOffset > curSampleOffset ) {
  1013. state = PLAYBACK_ADVANCING;
  1014. }
  1015. if ( state == PLAYBACK_RESET ) {
  1016. curPosition.Set( newSampleOffset );
  1017. }
  1018. // set current vars
  1019. curSampleOffset = newSampleOffset;
  1020. newPosition = curPosition;
  1021. // do the slow processing
  1022. GenerateSlowChannel( newPosition, sampleCount44k, dest );
  1023. // finish off
  1024. if ( state == PLAYBACK_ADVANCING )
  1025. curPosition = newPosition;
  1026. }