SoundSource.cpp 34 KB

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