SoundSource.cpp 34 KB

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