SoundSource.cpp 34 KB

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