SoundSource.cpp 33 KB

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