SoundSource.cpp 34 KB

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