SoundSource.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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 "SoundStream.h"
  29. #include <cstring>
  30. #include "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 char* typeNames[] =
  83. {
  84. "Effect",
  85. "Ambient",
  86. "Voice",
  87. "Music",
  88. 0
  89. };
  90. static const float AUTOREMOVE_DELAY = 0.25f;
  91. static const int STREAM_SAFETY_SAMPLES = 4;
  92. extern const char* AUDIO_CATEGORY;
  93. SoundSource::SoundSource(Context* context) :
  94. Component(context),
  95. soundType_(SOUND_EFFECT),
  96. frequency_(0.0f),
  97. gain_(1.0f),
  98. attenuation_(1.0f),
  99. panning_(0.0f),
  100. autoRemoveTimer_(0.0f),
  101. autoRemove_(false),
  102. position_(0),
  103. fractPosition_(0),
  104. timePosition_(0.0f),
  105. unusedStreamSize_(0)
  106. {
  107. audio_ = GetSubsystem<Audio>();
  108. if (audio_)
  109. audio_->AddSoundSource(this);
  110. }
  111. SoundSource::~SoundSource()
  112. {
  113. if (audio_)
  114. audio_->RemoveSoundSource(this);
  115. }
  116. void SoundSource::RegisterObject(Context* context)
  117. {
  118. context->RegisterFactory<SoundSource>(AUDIO_CATEGORY);
  119. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  120. MIXED_ACCESSOR_ATTRIBUTE("Sound", GetSoundAttr, SetSoundAttr, ResourceRef, ResourceRef(Sound::GetTypeStatic()), AM_DEFAULT);
  121. ENUM_ATTRIBUTE("Sound Type", soundType_, typeNames, SOUND_EFFECT, AM_DEFAULT);
  122. ATTRIBUTE(VAR_FLOAT, "Frequency", frequency_, 0.0f, AM_DEFAULT);
  123. ATTRIBUTE(VAR_FLOAT, "Gain", gain_, 1.0f, AM_DEFAULT);
  124. ATTRIBUTE(VAR_FLOAT, "Attenuation", attenuation_, 1.0f, AM_DEFAULT);
  125. ATTRIBUTE(VAR_FLOAT, "Panning", panning_, 0.0f, AM_DEFAULT);
  126. ACCESSOR_ATTRIBUTE("Is Playing", IsPlaying, SetPlayingAttr, bool, false, AM_DEFAULT);
  127. ATTRIBUTE(VAR_BOOL, "Autoremove on Stop", autoRemove_, false, AM_FILE);
  128. ACCESSOR_ATTRIBUTE("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::Play(SoundStream* stream)
  166. {
  167. if (!audio_)
  168. return;
  169. // If no frequency set yet, set from the stream's default
  170. if (frequency_ == 0.0f && stream)
  171. SetFrequency(stream->GetFrequency());
  172. SharedPtr<SoundStream> streamPtr(stream);
  173. // If sound source is currently playing, have to lock the audio mutex. When stream playback is explicitly
  174. // requested, clear the existing sound if any
  175. if (position_)
  176. {
  177. MutexLock lock(audio_->GetMutex());
  178. sound_.Reset();
  179. PlayLockless(streamPtr);
  180. }
  181. else
  182. {
  183. sound_.Reset();
  184. PlayLockless(streamPtr);
  185. }
  186. // Stream playback is not supported for network replication, no need to mark network dirty
  187. }
  188. void SoundSource::Stop()
  189. {
  190. if (!audio_)
  191. return;
  192. // If sound source is currently playing, have to lock the audio mutex
  193. if (position_)
  194. {
  195. MutexLock lock(audio_->GetMutex());
  196. StopLockless();
  197. }
  198. else
  199. StopLockless();
  200. MarkNetworkUpdate();
  201. }
  202. void SoundSource::SetSoundType(SoundType type)
  203. {
  204. if (type == SOUND_MASTER || type >= MAX_SOUND_TYPES)
  205. return;
  206. soundType_ = type;
  207. MarkNetworkUpdate();
  208. }
  209. void SoundSource::SetFrequency(float frequency)
  210. {
  211. frequency_ = Clamp(frequency, 0.0f, 535232.0f);
  212. MarkNetworkUpdate();
  213. }
  214. void SoundSource::SetGain(float gain)
  215. {
  216. gain_ = Max(gain, 0.0f);
  217. MarkNetworkUpdate();
  218. }
  219. void SoundSource::SetAttenuation(float attenuation)
  220. {
  221. attenuation_ = Clamp(attenuation, 0.0f, 1.0f);
  222. MarkNetworkUpdate();
  223. }
  224. void SoundSource::SetPanning(float panning)
  225. {
  226. panning_ = Clamp(panning, -1.0f, 1.0f);
  227. MarkNetworkUpdate();
  228. }
  229. void SoundSource::SetAutoRemove(bool enable)
  230. {
  231. autoRemove_ = enable;
  232. }
  233. bool SoundSource::IsPlaying() const
  234. {
  235. return (sound_ || soundStream_) && position_ != 0;
  236. }
  237. void SoundSource::SetPlayPosition(signed char* pos)
  238. {
  239. // Setting play position on a stream is not supported
  240. if (!audio_ || !sound_ || soundStream_)
  241. return;
  242. MutexLock lock(audio_->GetMutex());
  243. SetPlayPositionLockless(pos);
  244. }
  245. void SoundSource::Update(float timeStep)
  246. {
  247. if (!audio_ || !IsEnabledEffective())
  248. return;
  249. // If there is no actual audio output, perform fake mixing into a nonexistent buffer to check stopping/looping
  250. if (!audio_->IsInitialized())
  251. MixNull(timeStep);
  252. // Free the stream if playback has stopped
  253. if (soundStream_ && !position_)
  254. StopLockless();
  255. // Check for autoremove
  256. if (autoRemove_)
  257. {
  258. if (!IsPlaying())
  259. {
  260. autoRemoveTimer_ += timeStep;
  261. if (autoRemoveTimer_ > AUTOREMOVE_DELAY)
  262. {
  263. Remove();
  264. // Note: this object is now deleted, so only returning immediately is safe
  265. return;
  266. }
  267. }
  268. else
  269. autoRemoveTimer_ = 0.0f;
  270. }
  271. }
  272. void SoundSource::Mix(int* dest, unsigned samples, int mixRate, bool stereo, bool interpolation)
  273. {
  274. if (!position_ || (!sound_ && !soundStream_) || !IsEnabledEffective())
  275. return;
  276. int streamFilledSize, outBytes;
  277. if (soundStream_ && streamBuffer_)
  278. {
  279. int streamBufferSize = streamBuffer_->GetDataSize();
  280. // Calculate how many bytes of stream sound data is needed
  281. int neededSize = (int)((float)samples * frequency_ / (float)mixRate);
  282. // Add a little safety buffer. Subtract previous unused data
  283. neededSize += STREAM_SAFETY_SAMPLES;
  284. neededSize *= soundStream_->GetSampleSize();
  285. neededSize -= unusedStreamSize_;
  286. neededSize = Clamp(neededSize, 0, streamBufferSize - unusedStreamSize_);
  287. // Always start play position at the beginning of the stream buffer
  288. position_ = streamBuffer_->GetStart();
  289. // Request new data from the stream
  290. signed char* dest = streamBuffer_->GetStart() + unusedStreamSize_;
  291. outBytes = neededSize ? soundStream_->GetData(dest, neededSize) : 0;
  292. dest += outBytes;
  293. // Zero-fill rest if stream did not produce enough data
  294. if (outBytes < neededSize)
  295. memset(dest, 0, neededSize - outBytes);
  296. // Calculate amount of total bytes of data in stream buffer now, to know how much went unused after mixing
  297. streamFilledSize = neededSize + unusedStreamSize_;
  298. }
  299. // If streaming, play the stream buffer. Otherwise play the original sound
  300. Sound* sound = soundStream_ ? streamBuffer_ : sound_;
  301. if (!sound)
  302. return;
  303. // Choose the correct mixing routine
  304. if (!sound->IsStereo())
  305. {
  306. if (interpolation)
  307. {
  308. if (stereo)
  309. MixMonoToStereoIP(sound, dest, samples, mixRate);
  310. else
  311. MixMonoToMonoIP(sound, dest, samples, mixRate);
  312. }
  313. else
  314. {
  315. if (stereo)
  316. MixMonoToStereo(sound, dest, samples, mixRate);
  317. else
  318. MixMonoToMono(sound, dest, samples, mixRate);
  319. }
  320. }
  321. else
  322. {
  323. if (interpolation)
  324. {
  325. if (stereo)
  326. MixStereoToStereoIP(sound, dest, samples, mixRate);
  327. else
  328. MixStereoToMonoIP(sound, dest, samples, mixRate);
  329. }
  330. else
  331. {
  332. if (stereo)
  333. MixStereoToStereo(sound, dest, samples, mixRate);
  334. else
  335. MixStereoToMono(sound, dest, samples, mixRate);
  336. }
  337. }
  338. // Update the time position. In stream mode, copy unused data back to the beginning of the stream buffer
  339. if (soundStream_)
  340. {
  341. timePosition_ += ((float)samples / (float)mixRate) * frequency_ / soundStream_->GetFrequency();
  342. unusedStreamSize_ = Max(streamFilledSize - (int)(size_t)(position_ - streamBuffer_->GetStart()), 0);
  343. if (unusedStreamSize_)
  344. memcpy(streamBuffer_->GetStart(), (const void*)position_, unusedStreamSize_);
  345. // If stream did not produce any data, stop if applicable
  346. if (!outBytes && soundStream_->GetStopAtEnd())
  347. {
  348. position_ = 0;
  349. return;
  350. }
  351. }
  352. else if (sound_)
  353. timePosition_ = ((float)(int)(size_t)(position_ - sound_->GetStart())) / (sound_->GetSampleSize() * sound_->GetFrequency());
  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 = audio_->GetSoundSourceMasterGain(soundType_) * 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 = audio_->GetSoundSourceMasterGain(soundType_) * 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 = audio_->GetSoundSourceMasterGain(soundType_) * 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 = audio_->GetSoundSourceMasterGain(soundType_) * 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 = audio_->GetSoundSourceMasterGain(soundType_) * 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 = audio_->GetSoundSourceMasterGain(soundType_) * 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 = audio_->GetSoundSourceMasterGain(soundType_) * 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 = audio_->GetSoundSourceMasterGain(soundType_) * 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. }