SoundSource.cpp 34 KB

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