SoundSource.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // 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 FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Audio.h"
  25. #include "Context.h"
  26. #include "ResourceCache.h"
  27. #include "Sound.h"
  28. #include "SoundSource.h"
  29. #include <cstring>
  30. #include "DebugNew.h"
  31. #define INC_POS_LOOPED() \
  32. pos += intAdd; \
  33. fractPos += fractAdd; \
  34. if (fractPos > 65535) \
  35. { \
  36. fractPos &= 65535; \
  37. ++pos; \
  38. } \
  39. while (pos >= end) \
  40. pos -= (end - repeat); \
  41. #define INC_POS_ONESHOT() \
  42. pos += intAdd; \
  43. fractPos += fractAdd; \
  44. if (fractPos > 65535) \
  45. { \
  46. fractPos &= 65535; \
  47. ++pos; \
  48. } \
  49. if (pos >= end) \
  50. { \
  51. pos = 0; \
  52. break; \
  53. } \
  54. #define INC_POS_STEREO_LOOPED() \
  55. pos += (intAdd << 1); \
  56. fractPos += fractAdd; \
  57. if (fractPos > 65535) \
  58. { \
  59. fractPos &= 65535; \
  60. pos += 2; \
  61. } \
  62. while (pos >= end) \
  63. pos -= (end - repeat); \
  64. #define INC_POS_STEREO_ONESHOT() \
  65. pos += (intAdd << 1); \
  66. fractPos += fractAdd; \
  67. if (fractPos > 65535) \
  68. { \
  69. fractPos &= 65535; \
  70. pos += 2; \
  71. } \
  72. if (pos >= end) \
  73. { \
  74. pos = 0; \
  75. break; \
  76. } \
  77. #define GET_IP_SAMPLE() (((((int)pos[1] - (int)pos[0]) * fractPos) / 65536) + (int)pos[0])
  78. #define GET_IP_SAMPLE_LEFT() (((((int)pos[2] - (int)pos[0]) * fractPos) / 65536) + (int)pos[0])
  79. #define GET_IP_SAMPLE_RIGHT() (((((int)pos[3] - (int)pos[1]) * fractPos) / 65536) + (int)pos[1])
  80. static const String typeNames[] =
  81. {
  82. "Effect",
  83. "Ambient",
  84. "Voice",
  85. "Music",
  86. ""
  87. };
  88. static const float AUTOREMOVE_DELAY = 0.25f;
  89. OBJECTTYPESTATIC(SoundSource);
  90. SoundSource::SoundSource(Context* context) :
  91. Component(context),
  92. position_(0),
  93. fractPosition_(0),
  94. timePosition_(0.0f),
  95. soundType_(SOUND_EFFECT),
  96. frequency_(0.0f),
  97. gain_(1.0f),
  98. attenuation_(1.0f),
  99. panning_(0.0f),
  100. autoRemoveTimer_(0.0f),
  101. autoRemove_(false),
  102. decoder_(0),
  103. decodePosition_(0)
  104. {
  105. audio_ = GetSubsystem<Audio>();
  106. if (audio_)
  107. audio_->AddSoundSource(this);
  108. }
  109. SoundSource::~SoundSource()
  110. {
  111. if (audio_)
  112. audio_->RemoveSoundSource(this);
  113. FreeDecoder();
  114. }
  115. void SoundSource::RegisterObject(Context* context)
  116. {
  117. context->RegisterFactory<SoundSource>();
  118. ENUM_ATTRIBUTE(SoundSource, "Sound Type", soundType_, typeNames, SOUND_EFFECT, AM_DEFAULT);
  119. ATTRIBUTE(SoundSource, VAR_FLOAT, "Frequency", frequency_, 0.0f, AM_DEFAULT);
  120. ATTRIBUTE(SoundSource, VAR_FLOAT, "Gain", gain_, 1.0f, AM_DEFAULT);
  121. ATTRIBUTE(SoundSource, VAR_FLOAT, "Attenuation", attenuation_, 1.0f, AM_DEFAULT);
  122. ATTRIBUTE(SoundSource, VAR_FLOAT, "Panning", panning_, 0.0f, AM_DEFAULT);
  123. ATTRIBUTE(SoundSource, VAR_BOOL, "Autoremove on Stop", autoRemove_, false, AM_FILE);
  124. ACCESSOR_ATTRIBUTE(SoundSource, VAR_RESOURCEREF, "Sound", GetSoundAttr, SetSoundAttr, ResourceRef, ResourceRef(Sound::GetTypeStatic()), AM_DEFAULT);
  125. ACCESSOR_ATTRIBUTE(SoundSource, VAR_INT, "Play Position", GetPositionAttr, SetPositionAttr, int, 0, AM_FILE);
  126. }
  127. void SoundSource::Play(Sound* sound)
  128. {
  129. if (!audio_)
  130. return;
  131. // If no frequency set yet, set from the sound's default
  132. if (frequency_ == 0.0f && sound)
  133. SetFrequency(sound->GetFrequency());
  134. // If sound source is currently playing, have to lock the audio mutex
  135. if (position_)
  136. {
  137. MutexLock lock(audio_->GetMutex());
  138. PlayLockless(sound);
  139. }
  140. else
  141. PlayLockless(sound);
  142. MarkNetworkUpdate();
  143. }
  144. void SoundSource::Play(Sound* sound, float frequency)
  145. {
  146. SetFrequency(frequency);
  147. Play(sound);
  148. }
  149. void SoundSource::Play(Sound* sound, float frequency, float gain)
  150. {
  151. SetFrequency(frequency);
  152. SetGain(gain);
  153. Play(sound);
  154. }
  155. void SoundSource::Play(Sound* sound, float frequency, float gain, float panning)
  156. {
  157. SetFrequency(frequency);
  158. SetGain(gain);
  159. SetPanning(panning);
  160. Play(sound);
  161. }
  162. void SoundSource::Stop()
  163. {
  164. if (!audio_)
  165. return;
  166. // If sound source is currently playing, have to lock the audio mutex
  167. if (position_)
  168. {
  169. MutexLock lock(audio_->GetMutex());
  170. StopLockless();
  171. }
  172. // Free the compressed sound decoder now if any
  173. FreeDecoder();
  174. sound_.Reset();
  175. MarkNetworkUpdate();
  176. }
  177. void SoundSource::SetSoundType(SoundType type)
  178. {
  179. if (type == SOUND_MASTER || type >= MAX_SOUND_TYPES)
  180. return;
  181. soundType_ = type;
  182. MarkNetworkUpdate();
  183. }
  184. void SoundSource::SetFrequency(float frequency)
  185. {
  186. frequency_ = Clamp(frequency, 0.0f, 535232.0f);
  187. MarkNetworkUpdate();
  188. }
  189. void SoundSource::SetGain(float gain)
  190. {
  191. gain_ = Max(gain, 0.0f);
  192. MarkNetworkUpdate();
  193. }
  194. void SoundSource::SetAttenuation(float attenuation)
  195. {
  196. attenuation_ = Clamp(attenuation, 0.0f, 1.0f);
  197. MarkNetworkUpdate();
  198. }
  199. void SoundSource::SetPanning(float panning)
  200. {
  201. panning_ = Clamp(panning, -1.0f, 1.0f);
  202. MarkNetworkUpdate();
  203. }
  204. void SoundSource::SetAutoRemove(bool enable)
  205. {
  206. autoRemove_ = enable;
  207. }
  208. bool SoundSource::IsPlaying() const
  209. {
  210. return sound_.Get() != 0;
  211. }
  212. void SoundSource::SetPlayPosition(signed char* pos)
  213. {
  214. if (!audio_ || !sound_)
  215. return;
  216. MutexLock lock(audio_->GetMutex());
  217. SetPlayPositionLockless(pos);
  218. }
  219. void SoundSource::PlayLockless(Sound* sound)
  220. {
  221. // Reset the time position in any case
  222. timePosition_ = 0.0f;
  223. if (sound)
  224. {
  225. if (!sound->IsCompressed())
  226. {
  227. // Uncompressed sound start
  228. signed char* start = sound->GetStart();
  229. if (start)
  230. {
  231. // Free Decoder in case previous sound was compressed
  232. FreeDecoder();
  233. sound_ = sound;
  234. position_ = start;
  235. fractPosition_ = 0;
  236. return;
  237. }
  238. }
  239. else
  240. {
  241. // Compressed sound start
  242. if (sound == sound_)
  243. {
  244. // If same compressed sound is already playing, rewind the Decoder
  245. sound_->RewindDecoder(decoder_);
  246. return;
  247. }
  248. else
  249. {
  250. // Else just set the new sound with a dummy start position. The mixing routine will allocate the new Decoder
  251. FreeDecoder();
  252. sound_ = sound;
  253. position_ = sound->GetStart();
  254. return;
  255. }
  256. }
  257. }
  258. // If sound pointer is null or if sound has no data, stop playback
  259. FreeDecoder();
  260. sound_.Reset();
  261. position_ = 0;
  262. }
  263. void SoundSource::StopLockless()
  264. {
  265. position_ = 0;
  266. timePosition_ = 0.0f;
  267. }
  268. void SoundSource::SetPlayPositionLockless(signed char* pos)
  269. {
  270. // Setting position on a compressed sound is not supported
  271. if (!sound_ || sound_->IsCompressed())
  272. return;
  273. signed char* start = sound_->GetStart();
  274. signed char* end = sound_->GetEnd();
  275. if (pos < start)
  276. pos = start;
  277. if (sound_->IsSixteenBit() && (pos - start) & 1)
  278. ++pos;
  279. if (pos > end)
  280. pos = end;
  281. position_ = pos;
  282. timePosition_ = (float)((int)pos - (int)sound_->GetStart()) / (sound_->GetSampleSize() * sound_->GetFrequency());
  283. }
  284. void SoundSource::Update(float timeStep)
  285. {
  286. if (!audio_)
  287. return;
  288. // If there is no actual audio output, perform fake mixing into a nonexistent buffer to check stopping/looping
  289. if (!audio_->IsInitialized())
  290. MixNull(timeStep);
  291. // Free the sound if playback has stopped
  292. if (sound_ && !position_)
  293. {
  294. FreeDecoder();
  295. sound_.Reset();
  296. }
  297. // Check for autoremove
  298. if (autoRemove_)
  299. {
  300. if (!IsPlaying())
  301. {
  302. autoRemoveTimer_ += timeStep;
  303. if (autoRemoveTimer_ > AUTOREMOVE_DELAY)
  304. {
  305. Remove();
  306. // Note: this object is now deleted, so only returning immediately is safe
  307. return;
  308. }
  309. }
  310. else
  311. autoRemoveTimer_ = 0.0f;
  312. }
  313. }
  314. void SoundSource::Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolation)
  315. {
  316. if (!position_ || !sound_)
  317. return;
  318. if (sound_->IsCompressed())
  319. {
  320. if (decoder_)
  321. {
  322. // If Decoder already exists, Decode new compressed audio
  323. bool eof = false;
  324. unsigned currentPos = position_ - decodeBuffer_->GetStart();
  325. if (currentPos != decodePosition_)
  326. {
  327. // If buffer has wrapped, Decode first to the end
  328. if (currentPos < decodePosition_)
  329. {
  330. unsigned bytes = decodeBuffer_->GetDataSize() - decodePosition_;
  331. unsigned outBytes = sound_->Decode(decoder_, decodeBuffer_->GetStart() + decodePosition_, bytes);
  332. // If produced less output, end of sound encountered. Fill rest with zero
  333. if (outBytes < bytes)
  334. {
  335. memset(decodeBuffer_->GetStart() + decodePosition_ + outBytes, 0, bytes - outBytes);
  336. eof = true;
  337. }
  338. decodePosition_ = 0;
  339. }
  340. if (currentPos > decodePosition_)
  341. {
  342. unsigned bytes = currentPos - decodePosition_;
  343. unsigned outBytes = sound_->Decode(decoder_, decodeBuffer_->GetStart() + decodePosition_, bytes);
  344. // If produced less output, end of sound encountered. Fill rest with zero
  345. if (outBytes < bytes)
  346. {
  347. memset(decodeBuffer_->GetStart() + decodePosition_ + outBytes, 0, bytes - outBytes);
  348. if (sound_->IsLooped())
  349. eof = true;
  350. }
  351. // If wrote to buffer start, correct interpolation wraparound
  352. if (!decodePosition_)
  353. decodeBuffer_->FixInterpolation();
  354. }
  355. }
  356. // If end of stream encountered, check whether we should rewind or stop
  357. if (eof)
  358. {
  359. if (sound_->IsLooped())
  360. {
  361. sound_->RewindDecoder(decoder_);
  362. timePosition_ = 0.0f;
  363. }
  364. else
  365. decodeBuffer_->SetLooped(false); // Stop after the current decode buffer has been played
  366. }
  367. decodePosition_ = currentPos;
  368. }
  369. else
  370. {
  371. // Setup the decoder and decode buffer
  372. decoder_ = sound_->AllocateDecoder();
  373. unsigned sampleSize = sound_->GetSampleSize();
  374. unsigned DecodeBufferSize = sampleSize * sound_->GetIntFrequency() * DECODE_BUFFER_LENGTH / 1000;
  375. decodeBuffer_ = new Sound(context_);
  376. decodeBuffer_->SetSize(DecodeBufferSize);
  377. decodeBuffer_->SetFormat(sound_->GetIntFrequency(), true, sound_->IsStereo());
  378. // Clear the decode buffer, then fill with initial audio data and set it to loop
  379. memset(decodeBuffer_->GetStart(), 0, DecodeBufferSize);
  380. sound_->Decode(decoder_, decodeBuffer_->GetStart(), DecodeBufferSize);
  381. decodeBuffer_->SetLooped(true);
  382. decodePosition_ = 0;
  383. // Start playing the decode buffer
  384. position_ = decodeBuffer_->GetStart();
  385. fractPosition_ = 0;
  386. }
  387. }
  388. // If compressed, play the decode buffer. Otherwise play the original sound
  389. Sound* sound = sound_->IsCompressed() ? decodeBuffer_ : sound_;
  390. if (!sound)
  391. return;
  392. // Choose the correct mixing routine
  393. if (!sound->IsStereo())
  394. {
  395. if (interpolation)
  396. {
  397. if (stereo)
  398. MixMonoToStereoIP(sound, dest, samples, mixRate);
  399. else
  400. MixMonoToMonoIP(sound, dest, samples, mixRate);
  401. }
  402. else
  403. {
  404. if (stereo)
  405. MixMonoToStereo(sound, dest, samples, mixRate);
  406. else
  407. MixMonoToMono(sound, dest, samples, mixRate);
  408. }
  409. }
  410. else
  411. {
  412. if (interpolation)
  413. {
  414. if (stereo)
  415. MixStereoToStereoIP(sound, dest, samples, mixRate);
  416. else
  417. MixStereoToMonoIP(sound, dest, samples, mixRate);
  418. }
  419. else
  420. {
  421. if (stereo)
  422. MixStereoToStereo(sound, dest, samples, mixRate);
  423. else
  424. MixStereoToMono(sound, dest, samples, mixRate);
  425. }
  426. }
  427. // Update the time position
  428. if (!sound_->IsCompressed())
  429. timePosition_ = (float)((int)position_ - (int)sound_->GetStart()) / (sound_->GetSampleSize() * sound_->GetFrequency());
  430. else
  431. timePosition_ += ((float)samples / (float)mixRate) * frequency_ / sound_->GetFrequency();
  432. }
  433. void SoundSource::SetSoundAttr(ResourceRef value)
  434. {
  435. ResourceCache* cache = GetSubsystem<ResourceCache>();
  436. Play(cache->GetResource<Sound>(value.id_));
  437. }
  438. void SoundSource::SetPositionAttr(int value)
  439. {
  440. if (sound_)
  441. SetPlayPosition(sound_->GetStart() + value);
  442. }
  443. ResourceRef SoundSource::GetSoundAttr() const
  444. {
  445. return GetResourceRef(sound_, Sound::GetTypeStatic());
  446. }
  447. int SoundSource::GetPositionAttr() const
  448. {
  449. if (sound_)
  450. return (int)(GetPlayPosition() - sound_->GetStart());
  451. else
  452. return 0;
  453. }
  454. void SoundSource::MixMonoToMono(Sound* sound, int* dest, unsigned samples, int mixRate)
  455. {
  456. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  457. int vol = (int)(256.0f * totalGain + 0.5f);
  458. if (!vol)
  459. {
  460. MixZeroVolume(sound, samples, mixRate);
  461. return;
  462. }
  463. float add = frequency_ / (float)mixRate;
  464. int intAdd = (int)add;
  465. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  466. int fractPos = fractPosition_;
  467. if (sound->IsSixteenBit())
  468. {
  469. short* pos = (short*)position_;
  470. short* end = (short*)sound->GetEnd();
  471. short* repeat = (short*)sound->GetRepeat();
  472. if (sound->IsLooped())
  473. {
  474. while (--samples)
  475. {
  476. *dest = *dest + (*pos * vol) / 256;
  477. ++dest;
  478. INC_POS_LOOPED();
  479. }
  480. position_ = (signed char*)pos;
  481. }
  482. else
  483. {
  484. while (--samples)
  485. {
  486. *dest = *dest + (*pos * vol) / 256;
  487. ++dest;
  488. INC_POS_ONESHOT();
  489. }
  490. position_ = (signed char*)pos;
  491. }
  492. }
  493. else
  494. {
  495. signed char* pos = (signed char*)position_;
  496. signed char* end = sound->GetEnd();
  497. signed char* repeat = sound->GetRepeat();
  498. if (sound->IsLooped())
  499. {
  500. while (--samples)
  501. {
  502. *dest = *dest + *pos * vol;
  503. ++dest;
  504. INC_POS_LOOPED();
  505. }
  506. position_ = pos;
  507. }
  508. else
  509. {
  510. while (--samples)
  511. {
  512. *dest = *dest + *pos * vol;
  513. ++dest;
  514. INC_POS_ONESHOT();
  515. }
  516. position_ = pos;
  517. }
  518. }
  519. fractPosition_ = fractPos;
  520. }
  521. void SoundSource::MixMonoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate)
  522. {
  523. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  524. int leftVol = (int)((-panning_ + 1.0f) * (256.0f * totalGain + 0.5f));
  525. int rightVol = (int)((panning_ + 1.0f) * (256.0f * totalGain + 0.5f));
  526. if (!leftVol && !rightVol)
  527. {
  528. MixZeroVolume(sound, samples, mixRate);
  529. return;
  530. }
  531. float add = frequency_ / (float)mixRate;
  532. int intAdd = (int)add;
  533. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  534. int fractPos = fractPosition_;
  535. if (sound->IsSixteenBit())
  536. {
  537. short* pos = (short*)position_;
  538. short* end = (short*)sound->GetEnd();
  539. short* repeat = (short*)sound->GetRepeat();
  540. if (sound->IsLooped())
  541. {
  542. while (--samples)
  543. {
  544. *dest = *dest + (*pos * leftVol) / 256;
  545. ++dest;
  546. *dest = *dest + (*pos * rightVol) / 256;
  547. ++dest;
  548. INC_POS_LOOPED();
  549. }
  550. position_ = (signed char*)pos;
  551. }
  552. else
  553. {
  554. while (--samples)
  555. {
  556. *dest = *dest + (*pos * leftVol) / 256;
  557. ++dest;
  558. *dest = *dest + (*pos * rightVol) / 256;
  559. ++dest;
  560. INC_POS_ONESHOT();
  561. }
  562. position_ = (signed char*)pos;
  563. }
  564. }
  565. else
  566. {
  567. signed char* pos = (signed char*)position_;
  568. signed char* end = sound->GetEnd();
  569. signed char* repeat = sound->GetRepeat();
  570. if (sound->IsLooped())
  571. {
  572. while (--samples)
  573. {
  574. *dest = *dest + *pos * leftVol;
  575. ++dest;
  576. *dest = *dest + *pos * rightVol;
  577. ++dest;
  578. INC_POS_LOOPED();
  579. }
  580. position_ = pos;
  581. }
  582. else
  583. {
  584. while (--samples)
  585. {
  586. *dest = *dest + *pos * leftVol;
  587. ++dest;
  588. *dest = *dest + *pos * rightVol;
  589. ++dest;
  590. INC_POS_ONESHOT();
  591. }
  592. position_ = pos;
  593. }
  594. }
  595. fractPosition_ = fractPos;
  596. }
  597. void SoundSource::MixMonoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate)
  598. {
  599. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  600. int vol = (int)(256.0f * totalGain + 0.5f);
  601. if (!vol)
  602. {
  603. MixZeroVolume(sound, samples, mixRate);
  604. return;
  605. }
  606. float add = frequency_ / (float)mixRate;
  607. int intAdd = (int)add;
  608. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  609. int fractPos = fractPosition_;
  610. if (sound->IsSixteenBit())
  611. {
  612. short* pos = (short*)position_;
  613. short* end = (short*)sound->GetEnd();
  614. short* repeat = (short*)sound->GetRepeat();
  615. if (sound->IsLooped())
  616. {
  617. while (--samples)
  618. {
  619. *dest = *dest + (GET_IP_SAMPLE() * vol) / 256;
  620. ++dest;
  621. INC_POS_LOOPED();
  622. }
  623. position_ = (signed char*)pos;
  624. }
  625. else
  626. {
  627. while (--samples)
  628. {
  629. *dest = *dest + (GET_IP_SAMPLE() * vol) / 256;
  630. ++dest;
  631. INC_POS_ONESHOT();
  632. }
  633. position_ = (signed char*)pos;
  634. }
  635. }
  636. else
  637. {
  638. signed char* pos = (signed char*)position_;
  639. signed char* end = sound->GetEnd();
  640. signed char* repeat = sound->GetRepeat();
  641. if (sound->IsLooped())
  642. {
  643. while (--samples)
  644. {
  645. *dest = *dest + GET_IP_SAMPLE() * vol;
  646. ++dest;
  647. INC_POS_LOOPED();
  648. }
  649. position_ = pos;
  650. }
  651. else
  652. {
  653. while (--samples)
  654. {
  655. *dest = *dest + GET_IP_SAMPLE() * vol;
  656. ++dest;
  657. INC_POS_ONESHOT();
  658. }
  659. position_ = pos;
  660. }
  661. }
  662. fractPosition_ = fractPos;
  663. }
  664. void SoundSource::MixMonoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate)
  665. {
  666. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  667. int leftVol = (int)((-panning_ + 1.0f) * (256.0f * totalGain + 0.5f));
  668. int rightVol = (int)((panning_ + 1.0f) * (256.0f * totalGain + 0.5f));
  669. if (!leftVol && !rightVol)
  670. {
  671. MixZeroVolume(sound, samples, mixRate);
  672. return;
  673. }
  674. float add = frequency_ / (float)mixRate;
  675. int intAdd = (int)add;
  676. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  677. int fractPos = fractPosition_;
  678. if (sound->IsSixteenBit())
  679. {
  680. short* pos = (short*)position_;
  681. short* end = (short*)sound->GetEnd();
  682. short* repeat = (short*)sound->GetRepeat();
  683. if (sound->IsLooped())
  684. {
  685. while (--samples)
  686. {
  687. int s = GET_IP_SAMPLE();
  688. *dest = *dest + (s * leftVol) / 256;
  689. ++dest;
  690. *dest = *dest + (s * rightVol) / 256;
  691. ++dest;
  692. INC_POS_LOOPED();
  693. }
  694. position_ = (signed char*)pos;
  695. }
  696. else
  697. {
  698. while (--samples)
  699. {
  700. int s = GET_IP_SAMPLE();
  701. *dest = *dest + (s * leftVol) / 256;
  702. ++dest;
  703. *dest = *dest + (s * rightVol) / 256;
  704. ++dest;
  705. INC_POS_ONESHOT();
  706. }
  707. position_ = (signed char*)pos;
  708. }
  709. }
  710. else
  711. {
  712. signed char* pos = (signed char*)position_;
  713. signed char* end = sound->GetEnd();
  714. signed char* repeat = sound->GetRepeat();
  715. if (sound->IsLooped())
  716. {
  717. while (--samples)
  718. {
  719. int s = GET_IP_SAMPLE();
  720. *dest = *dest + s * leftVol;
  721. ++dest;
  722. *dest = *dest + s * rightVol;
  723. ++dest;
  724. INC_POS_LOOPED();
  725. }
  726. position_ = pos;
  727. }
  728. else
  729. {
  730. while (--samples)
  731. {
  732. int s = GET_IP_SAMPLE();
  733. *dest = *dest + s * leftVol;
  734. ++dest;
  735. *dest = *dest + s * rightVol;
  736. ++dest;
  737. INC_POS_ONESHOT();
  738. }
  739. position_ = pos;
  740. }
  741. }
  742. fractPosition_ = fractPos;
  743. }
  744. void SoundSource::MixStereoToMono(Sound* sound, int* dest, unsigned samples, int mixRate)
  745. {
  746. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  747. int vol = (int)(256.0f * totalGain + 0.5f);
  748. if (!vol)
  749. {
  750. MixZeroVolume(sound, samples, mixRate);
  751. return;
  752. }
  753. float add = frequency_ / (float)mixRate;
  754. int intAdd = (int)add;
  755. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  756. int fractPos = fractPosition_;
  757. if (sound->IsSixteenBit())
  758. {
  759. short* pos = (short*)position_;
  760. short* end = (short*)sound->GetEnd();
  761. short* repeat = (short*)sound->GetRepeat();
  762. if (sound->IsLooped())
  763. {
  764. while (--samples)
  765. {
  766. int s = ((int)pos[0] + (int)pos[1]) / 2;
  767. *dest = *dest + (s * vol) / 256;
  768. ++dest;
  769. INC_POS_STEREO_LOOPED();
  770. }
  771. position_ = (signed char*)pos;
  772. }
  773. else
  774. {
  775. while (--samples)
  776. {
  777. int s = ((int)pos[0] + (int)pos[1]) / 2;
  778. *dest = *dest + (s * vol) / 256;
  779. ++dest;
  780. INC_POS_STEREO_ONESHOT();
  781. }
  782. position_ = (signed char*)pos;
  783. }
  784. }
  785. else
  786. {
  787. signed char* pos = (signed char*)position_;
  788. signed char* end = sound->GetEnd();
  789. signed char* repeat = sound->GetRepeat();
  790. if (sound->IsLooped())
  791. {
  792. while (--samples)
  793. {
  794. int s = ((int)pos[0] + (int)pos[1]) / 2;
  795. *dest = *dest + s * vol;
  796. ++dest;
  797. INC_POS_STEREO_LOOPED();
  798. }
  799. position_ = pos;
  800. }
  801. else
  802. {
  803. while (--samples)
  804. {
  805. int s = ((int)pos[0] + (int)pos[1]) / 2;
  806. *dest = *dest + s * vol;
  807. ++dest;
  808. INC_POS_STEREO_ONESHOT();
  809. }
  810. position_ = pos;
  811. }
  812. }
  813. fractPosition_ = fractPos;
  814. }
  815. void SoundSource::MixStereoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate)
  816. {
  817. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  818. int vol = (int)(256.0f * totalGain + 0.5f);
  819. if (!vol)
  820. {
  821. MixZeroVolume(sound, samples, mixRate);
  822. return;
  823. }
  824. float add = frequency_ / (float)mixRate;
  825. int intAdd = (int)add;
  826. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  827. int fractPos = fractPosition_;
  828. if (sound->IsSixteenBit())
  829. {
  830. short* pos = (short*)position_;
  831. short* end = (short*)sound->GetEnd();
  832. short* repeat = (short*)sound->GetRepeat();
  833. if (sound->IsLooped())
  834. {
  835. while (--samples)
  836. {
  837. *dest = *dest + (pos[0] * vol) / 256;
  838. ++dest;
  839. *dest = *dest + (pos[1] * vol) / 256;
  840. ++dest;
  841. INC_POS_STEREO_LOOPED();
  842. }
  843. position_ = (signed char*)pos;
  844. }
  845. else
  846. {
  847. while (--samples)
  848. {
  849. *dest = *dest + (pos[0] * vol) / 256;
  850. ++dest;
  851. *dest = *dest + (pos[1] * vol) / 256;
  852. ++dest;
  853. INC_POS_STEREO_ONESHOT();
  854. }
  855. position_ = (signed char*)pos;
  856. }
  857. }
  858. else
  859. {
  860. signed char* pos = (signed char*)position_;
  861. signed char* end = sound->GetEnd();
  862. signed char* repeat = sound->GetRepeat();
  863. if (sound->IsLooped())
  864. {
  865. while (--samples)
  866. {
  867. *dest = *dest + pos[0] * vol;
  868. ++dest;
  869. *dest = *dest + pos[1] * vol;
  870. ++dest;
  871. INC_POS_STEREO_LOOPED();
  872. }
  873. position_ = pos;
  874. }
  875. else
  876. {
  877. while (--samples)
  878. {
  879. *dest = *dest + pos[0] * vol;
  880. ++dest;
  881. *dest = *dest + pos[1] * vol;
  882. ++dest;
  883. INC_POS_STEREO_ONESHOT();
  884. }
  885. position_ = pos;
  886. }
  887. }
  888. fractPosition_ = fractPos;
  889. }
  890. void SoundSource::MixStereoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate)
  891. {
  892. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  893. int vol = (int)(256.0f * totalGain + 0.5f);
  894. if (!vol)
  895. {
  896. MixZeroVolume(sound, samples, mixRate);
  897. return;
  898. }
  899. float add = frequency_ / (float)mixRate;
  900. int intAdd = (int)add;
  901. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  902. int fractPos = fractPosition_;
  903. if (sound->IsSixteenBit())
  904. {
  905. short* pos = (short*)position_;
  906. short* end = (short*)sound->GetEnd();
  907. short* repeat = (short*)sound->GetRepeat();
  908. if (sound->IsLooped())
  909. {
  910. while (--samples)
  911. {
  912. int s = (GET_IP_SAMPLE_LEFT() + GET_IP_SAMPLE_RIGHT()) / 2;
  913. *dest = *dest + (s * vol) / 256;
  914. ++dest;
  915. INC_POS_STEREO_LOOPED();
  916. }
  917. position_ = (signed char*)pos;
  918. }
  919. else
  920. {
  921. while (--samples)
  922. {
  923. int s = (GET_IP_SAMPLE_LEFT() + GET_IP_SAMPLE_RIGHT()) / 2;
  924. *dest = *dest + (s * vol) / 256;
  925. ++dest;
  926. INC_POS_STEREO_ONESHOT();
  927. }
  928. position_ = (signed char*)pos;
  929. }
  930. }
  931. else
  932. {
  933. signed char* pos = (signed char*)position_;
  934. signed char* end = sound->GetEnd();
  935. signed char* repeat = sound->GetRepeat();
  936. if (sound->IsLooped())
  937. {
  938. while (--samples)
  939. {
  940. int s = (GET_IP_SAMPLE_LEFT() + GET_IP_SAMPLE_RIGHT()) / 2;
  941. *dest = *dest + s * vol;
  942. ++dest;
  943. INC_POS_STEREO_LOOPED();
  944. }
  945. position_ = pos;
  946. }
  947. else
  948. {
  949. while (--samples)
  950. {
  951. int s = (GET_IP_SAMPLE_LEFT() + GET_IP_SAMPLE_RIGHT()) / 2;
  952. *dest = *dest + s * vol;
  953. ++dest;
  954. INC_POS_STEREO_ONESHOT();
  955. }
  956. position_ = pos;
  957. }
  958. }
  959. fractPosition_ = fractPos;
  960. }
  961. void SoundSource::MixStereoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate)
  962. {
  963. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  964. int vol = (int)(256.0f * totalGain + 0.5f);
  965. if (!vol)
  966. {
  967. MixZeroVolume(sound, samples, mixRate);
  968. return;
  969. }
  970. float add = frequency_ / (float)mixRate;
  971. int intAdd = (int)add;
  972. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  973. int fractPos = fractPosition_;
  974. if (sound->IsSixteenBit())
  975. {
  976. short* pos = (short*)position_;
  977. short* end = (short*)sound->GetEnd();
  978. short* repeat = (short*)sound->GetRepeat();
  979. if (sound->IsLooped())
  980. {
  981. while (--samples)
  982. {
  983. *dest = *dest + (GET_IP_SAMPLE_LEFT() * vol) / 256;
  984. ++dest;
  985. *dest = *dest + (GET_IP_SAMPLE_RIGHT() * vol) / 256;
  986. ++dest;
  987. INC_POS_STEREO_LOOPED();
  988. }
  989. position_ = (signed char*)pos;
  990. }
  991. else
  992. {
  993. while (--samples)
  994. {
  995. *dest = *dest + (GET_IP_SAMPLE_LEFT() * vol) / 256;
  996. ++dest;
  997. *dest = *dest + (GET_IP_SAMPLE_RIGHT() * vol) / 256;
  998. ++dest;
  999. INC_POS_STEREO_ONESHOT();
  1000. }
  1001. position_ = (signed char*)pos;
  1002. }
  1003. }
  1004. else
  1005. {
  1006. signed char* pos = (signed char*)position_;
  1007. signed char* end = sound->GetEnd();
  1008. signed char* repeat = sound->GetRepeat();
  1009. if (sound->IsLooped())
  1010. {
  1011. while (--samples)
  1012. {
  1013. *dest = *dest + GET_IP_SAMPLE_LEFT() * vol;
  1014. ++dest;
  1015. *dest = *dest + GET_IP_SAMPLE_RIGHT() * vol;
  1016. ++dest;
  1017. INC_POS_STEREO_LOOPED();
  1018. }
  1019. position_ = pos;
  1020. }
  1021. else
  1022. {
  1023. while (--samples)
  1024. {
  1025. *dest = *dest + GET_IP_SAMPLE_LEFT() * vol;
  1026. ++dest;
  1027. *dest = *dest + GET_IP_SAMPLE_RIGHT() * vol;
  1028. ++dest;
  1029. INC_POS_STEREO_ONESHOT();
  1030. }
  1031. position_ = pos;
  1032. }
  1033. }
  1034. fractPosition_ = fractPos;
  1035. }
  1036. void SoundSource::MixZeroVolume(Sound* sound, unsigned samples, int mixRate)
  1037. {
  1038. float add = frequency_ * (float)samples / (float)mixRate;
  1039. int intAdd = (int)add;
  1040. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  1041. unsigned sampleSize = sound->GetSampleSize();
  1042. fractPosition_ += fractAdd;
  1043. if (fractPosition_ > 65535)
  1044. {
  1045. fractPosition_ &= 65535;
  1046. position_ += sampleSize;
  1047. }
  1048. position_ += intAdd * sampleSize;
  1049. if (position_ > sound->GetEnd())
  1050. {
  1051. if (sound->IsLooped())
  1052. {
  1053. while (position_ >= sound->GetEnd())
  1054. {
  1055. position_ -= (sound->GetEnd() - sound->GetRepeat());
  1056. }
  1057. }
  1058. else
  1059. position_ = 0;
  1060. }
  1061. }
  1062. void SoundSource::MixNull(float timeStep)
  1063. {
  1064. if (!position_ || !sound_)
  1065. return;
  1066. // Advance only the time position
  1067. timePosition_ += timeStep * frequency_ / sound_->GetFrequency();
  1068. if (sound_->IsLooped())
  1069. {
  1070. // For simulated playback, simply reset the time position to zero when the sound loops
  1071. if (timePosition_ >= sound_->GetLength())
  1072. timePosition_ -= sound_->GetLength();
  1073. }
  1074. else
  1075. {
  1076. if (timePosition_ >= sound_->GetLength())
  1077. {
  1078. position_ = 0;
  1079. timePosition_ = 0.0f;
  1080. }
  1081. }
  1082. }
  1083. void SoundSource::FreeDecoder()
  1084. {
  1085. if (sound_ && decoder_)
  1086. {
  1087. sound_->FreeDecoder(decoder_);
  1088. decoder_ = 0;
  1089. }
  1090. decodeBuffer_.Reset();
  1091. }