SoundSource.cpp 33 KB

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