SoundSource.cpp 34 KB

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