SoundSource.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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(SoundSource, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  120. ACCESSOR_ATTRIBUTE(SoundSource, VAR_RESOURCEREF, "Sound", GetSoundAttr, SetSoundAttr, ResourceRef, ResourceRef(Sound::GetTypeStatic()), AM_DEFAULT);
  121. ENUM_ATTRIBUTE(SoundSource, "Sound Type", soundType_, typeNames, SOUND_EFFECT, AM_DEFAULT);
  122. ATTRIBUTE(SoundSource, VAR_FLOAT, "Frequency", frequency_, 0.0f, AM_DEFAULT);
  123. ATTRIBUTE(SoundSource, VAR_FLOAT, "Gain", gain_, 1.0f, AM_DEFAULT);
  124. ATTRIBUTE(SoundSource, VAR_FLOAT, "Attenuation", attenuation_, 1.0f, AM_DEFAULT);
  125. ATTRIBUTE(SoundSource, VAR_FLOAT, "Panning", panning_, 0.0f, AM_DEFAULT);
  126. ACCESSOR_ATTRIBUTE(SoundSource, VAR_BOOL, "Is Playing", IsPlaying, SetPlayingAttr, bool, false, AM_DEFAULT);
  127. ATTRIBUTE(SoundSource, VAR_BOOL, "Autoremove on Stop", autoRemove_, false, AM_FILE);
  128. ACCESSOR_ATTRIBUTE(SoundSource, VAR_INT, "Play Position", GetPositionAttr, SetPositionAttr, int, 0, AM_FILE);
  129. }
  130. void SoundSource::Play(Sound* sound)
  131. {
  132. if (!audio_)
  133. return;
  134. // If no frequency set yet, set from the sound's default
  135. if (frequency_ == 0.0f && sound)
  136. SetFrequency(sound->GetFrequency());
  137. // If sound source is currently playing, have to lock the audio mutex
  138. if (position_)
  139. {
  140. MutexLock lock(audio_->GetMutex());
  141. PlayLockless(sound);
  142. }
  143. else
  144. PlayLockless(sound);
  145. MarkNetworkUpdate();
  146. }
  147. void SoundSource::Play(Sound* sound, float frequency)
  148. {
  149. SetFrequency(frequency);
  150. Play(sound);
  151. }
  152. void SoundSource::Play(Sound* sound, float frequency, float gain)
  153. {
  154. SetFrequency(frequency);
  155. SetGain(gain);
  156. Play(sound);
  157. }
  158. void SoundSource::Play(Sound* sound, float frequency, float gain, float panning)
  159. {
  160. SetFrequency(frequency);
  161. SetGain(gain);
  162. SetPanning(panning);
  163. Play(sound);
  164. }
  165. void SoundSource::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(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. return;
  421. }
  422. }
  423. // If sound pointer is null or if sound has no data, stop playback
  424. StopLockless();
  425. sound_.Reset();
  426. }
  427. void SoundSource::PlayLockless(SharedPtr<SoundStream> stream)
  428. {
  429. // Reset the time position in any case
  430. timePosition_ = 0.0f;
  431. if (stream)
  432. {
  433. // Setup the stream buffer
  434. unsigned sampleSize = stream->GetSampleSize();
  435. unsigned streamBufferSize = sampleSize * stream->GetIntFrequency() * STREAM_BUFFER_LENGTH / 1000;
  436. streamBuffer_ = new Sound(context_);
  437. streamBuffer_->SetSize(streamBufferSize);
  438. streamBuffer_->SetFormat(stream->GetIntFrequency(), stream->IsSixteenBit(), stream->IsStereo());
  439. streamBuffer_->SetLooped(true);
  440. soundStream_ = stream;
  441. unusedStreamSize_ = 0;
  442. position_ = streamBuffer_->GetStart();
  443. fractPosition_ = 0;
  444. return;
  445. }
  446. // If stream pointer is null, stop playback
  447. StopLockless();
  448. }
  449. void SoundSource::StopLockless()
  450. {
  451. position_ = 0;
  452. timePosition_ = 0.0f;
  453. // Free the sound stream and decode buffer if a stream was playing
  454. soundStream_.Reset();
  455. streamBuffer_.Reset();
  456. }
  457. void SoundSource::SetPlayPositionLockless(signed char* pos)
  458. {
  459. // Setting position on a stream is not supported
  460. if (!sound_ || soundStream_)
  461. return;
  462. signed char* start = sound_->GetStart();
  463. signed char* end = sound_->GetEnd();
  464. if (pos < start)
  465. pos = start;
  466. if (sound_->IsSixteenBit() && (pos - start) & 1)
  467. ++pos;
  468. if (pos > end)
  469. pos = end;
  470. position_ = pos;
  471. timePosition_ = ((float)(int)(size_t)(pos - sound_->GetStart())) / (sound_->GetSampleSize() * sound_->GetFrequency());
  472. }
  473. void SoundSource::MixMonoToMono(Sound* sound, int* dest, unsigned samples, int mixRate)
  474. {
  475. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  476. int vol = (int)(256.0f * totalGain + 0.5f);
  477. if (!vol)
  478. {
  479. MixZeroVolume(sound, samples, mixRate);
  480. return;
  481. }
  482. float add = frequency_ / (float)mixRate;
  483. int intAdd = (int)add;
  484. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  485. int fractPos = fractPosition_;
  486. if (sound->IsSixteenBit())
  487. {
  488. short* pos = (short*)position_;
  489. short* end = (short*)sound->GetEnd();
  490. short* repeat = (short*)sound->GetRepeat();
  491. if (sound->IsLooped())
  492. {
  493. while (samples--)
  494. {
  495. *dest = *dest + (*pos * vol) / 256;
  496. ++dest;
  497. INC_POS_LOOPED();
  498. }
  499. position_ = (signed char*)pos;
  500. }
  501. else
  502. {
  503. while (samples--)
  504. {
  505. *dest = *dest + (*pos * vol) / 256;
  506. ++dest;
  507. INC_POS_ONESHOT();
  508. }
  509. position_ = (signed char*)pos;
  510. }
  511. }
  512. else
  513. {
  514. signed char* pos = (signed char*)position_;
  515. signed char* end = sound->GetEnd();
  516. signed char* repeat = sound->GetRepeat();
  517. if (sound->IsLooped())
  518. {
  519. while (samples--)
  520. {
  521. *dest = *dest + *pos * vol;
  522. ++dest;
  523. INC_POS_LOOPED();
  524. }
  525. position_ = pos;
  526. }
  527. else
  528. {
  529. while (samples--)
  530. {
  531. *dest = *dest + *pos * vol;
  532. ++dest;
  533. INC_POS_ONESHOT();
  534. }
  535. position_ = pos;
  536. }
  537. }
  538. fractPosition_ = fractPos;
  539. }
  540. void SoundSource::MixMonoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate)
  541. {
  542. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  543. int leftVol = (int)((-panning_ + 1.0f) * (256.0f * totalGain + 0.5f));
  544. int rightVol = (int)((panning_ + 1.0f) * (256.0f * totalGain + 0.5f));
  545. if (!leftVol && !rightVol)
  546. {
  547. MixZeroVolume(sound, samples, mixRate);
  548. return;
  549. }
  550. float add = frequency_ / (float)mixRate;
  551. int intAdd = (int)add;
  552. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  553. int fractPos = fractPosition_;
  554. if (sound->IsSixteenBit())
  555. {
  556. short* pos = (short*)position_;
  557. short* end = (short*)sound->GetEnd();
  558. short* repeat = (short*)sound->GetRepeat();
  559. if (sound->IsLooped())
  560. {
  561. while (samples--)
  562. {
  563. *dest = *dest + (*pos * leftVol) / 256;
  564. ++dest;
  565. *dest = *dest + (*pos * rightVol) / 256;
  566. ++dest;
  567. INC_POS_LOOPED();
  568. }
  569. position_ = (signed char*)pos;
  570. }
  571. else
  572. {
  573. while (samples--)
  574. {
  575. *dest = *dest + (*pos * leftVol) / 256;
  576. ++dest;
  577. *dest = *dest + (*pos * rightVol) / 256;
  578. ++dest;
  579. INC_POS_ONESHOT();
  580. }
  581. position_ = (signed char*)pos;
  582. }
  583. }
  584. else
  585. {
  586. signed char* pos = (signed char*)position_;
  587. signed char* end = sound->GetEnd();
  588. signed char* repeat = sound->GetRepeat();
  589. if (sound->IsLooped())
  590. {
  591. while (samples--)
  592. {
  593. *dest = *dest + *pos * leftVol;
  594. ++dest;
  595. *dest = *dest + *pos * rightVol;
  596. ++dest;
  597. INC_POS_LOOPED();
  598. }
  599. position_ = pos;
  600. }
  601. else
  602. {
  603. while (samples--)
  604. {
  605. *dest = *dest + *pos * leftVol;
  606. ++dest;
  607. *dest = *dest + *pos * rightVol;
  608. ++dest;
  609. INC_POS_ONESHOT();
  610. }
  611. position_ = pos;
  612. }
  613. }
  614. fractPosition_ = fractPos;
  615. }
  616. void SoundSource::MixMonoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate)
  617. {
  618. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  619. int vol = (int)(256.0f * totalGain + 0.5f);
  620. if (!vol)
  621. {
  622. MixZeroVolume(sound, samples, mixRate);
  623. return;
  624. }
  625. float add = frequency_ / (float)mixRate;
  626. int intAdd = (int)add;
  627. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  628. int fractPos = fractPosition_;
  629. if (sound->IsSixteenBit())
  630. {
  631. short* pos = (short*)position_;
  632. short* end = (short*)sound->GetEnd();
  633. short* repeat = (short*)sound->GetRepeat();
  634. if (sound->IsLooped())
  635. {
  636. while (samples--)
  637. {
  638. *dest = *dest + (GET_IP_SAMPLE() * vol) / 256;
  639. ++dest;
  640. INC_POS_LOOPED();
  641. }
  642. position_ = (signed char*)pos;
  643. }
  644. else
  645. {
  646. while (samples--)
  647. {
  648. *dest = *dest + (GET_IP_SAMPLE() * vol) / 256;
  649. ++dest;
  650. INC_POS_ONESHOT();
  651. }
  652. position_ = (signed char*)pos;
  653. }
  654. }
  655. else
  656. {
  657. signed char* pos = (signed char*)position_;
  658. signed char* end = sound->GetEnd();
  659. signed char* repeat = sound->GetRepeat();
  660. if (sound->IsLooped())
  661. {
  662. while (samples--)
  663. {
  664. *dest = *dest + GET_IP_SAMPLE() * vol;
  665. ++dest;
  666. INC_POS_LOOPED();
  667. }
  668. position_ = pos;
  669. }
  670. else
  671. {
  672. while (samples--)
  673. {
  674. *dest = *dest + GET_IP_SAMPLE() * vol;
  675. ++dest;
  676. INC_POS_ONESHOT();
  677. }
  678. position_ = pos;
  679. }
  680. }
  681. fractPosition_ = fractPos;
  682. }
  683. void SoundSource::MixMonoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate)
  684. {
  685. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  686. int leftVol = (int)((-panning_ + 1.0f) * (256.0f * totalGain + 0.5f));
  687. int rightVol = (int)((panning_ + 1.0f) * (256.0f * totalGain + 0.5f));
  688. if (!leftVol && !rightVol)
  689. {
  690. MixZeroVolume(sound, samples, mixRate);
  691. return;
  692. }
  693. float add = frequency_ / (float)mixRate;
  694. int intAdd = (int)add;
  695. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  696. int fractPos = fractPosition_;
  697. if (sound->IsSixteenBit())
  698. {
  699. short* pos = (short*)position_;
  700. short* end = (short*)sound->GetEnd();
  701. short* repeat = (short*)sound->GetRepeat();
  702. if (sound->IsLooped())
  703. {
  704. while (samples--)
  705. {
  706. int s = GET_IP_SAMPLE();
  707. *dest = *dest + (s * leftVol) / 256;
  708. ++dest;
  709. *dest = *dest + (s * rightVol) / 256;
  710. ++dest;
  711. INC_POS_LOOPED();
  712. }
  713. position_ = (signed char*)pos;
  714. }
  715. else
  716. {
  717. while (samples--)
  718. {
  719. int s = GET_IP_SAMPLE();
  720. *dest = *dest + (s * leftVol) / 256;
  721. ++dest;
  722. *dest = *dest + (s * rightVol) / 256;
  723. ++dest;
  724. INC_POS_ONESHOT();
  725. }
  726. position_ = (signed char*)pos;
  727. }
  728. }
  729. else
  730. {
  731. signed char* pos = (signed char*)position_;
  732. signed char* end = sound->GetEnd();
  733. signed char* repeat = sound->GetRepeat();
  734. if (sound->IsLooped())
  735. {
  736. while (samples--)
  737. {
  738. int s = GET_IP_SAMPLE();
  739. *dest = *dest + s * leftVol;
  740. ++dest;
  741. *dest = *dest + s * rightVol;
  742. ++dest;
  743. INC_POS_LOOPED();
  744. }
  745. position_ = pos;
  746. }
  747. else
  748. {
  749. while (samples--)
  750. {
  751. int s = GET_IP_SAMPLE();
  752. *dest = *dest + s * leftVol;
  753. ++dest;
  754. *dest = *dest + s * rightVol;
  755. ++dest;
  756. INC_POS_ONESHOT();
  757. }
  758. position_ = pos;
  759. }
  760. }
  761. fractPosition_ = fractPos;
  762. }
  763. void SoundSource::MixStereoToMono(Sound* sound, int* dest, unsigned samples, int mixRate)
  764. {
  765. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  766. int vol = (int)(256.0f * totalGain + 0.5f);
  767. if (!vol)
  768. {
  769. MixZeroVolume(sound, samples, mixRate);
  770. return;
  771. }
  772. float add = frequency_ / (float)mixRate;
  773. int intAdd = (int)add;
  774. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  775. int fractPos = fractPosition_;
  776. if (sound->IsSixteenBit())
  777. {
  778. short* pos = (short*)position_;
  779. short* end = (short*)sound->GetEnd();
  780. short* repeat = (short*)sound->GetRepeat();
  781. if (sound->IsLooped())
  782. {
  783. while (samples--)
  784. {
  785. int s = ((int)pos[0] + (int)pos[1]) / 2;
  786. *dest = *dest + (s * vol) / 256;
  787. ++dest;
  788. INC_POS_STEREO_LOOPED();
  789. }
  790. position_ = (signed char*)pos;
  791. }
  792. else
  793. {
  794. while (samples--)
  795. {
  796. int s = ((int)pos[0] + (int)pos[1]) / 2;
  797. *dest = *dest + (s * vol) / 256;
  798. ++dest;
  799. INC_POS_STEREO_ONESHOT();
  800. }
  801. position_ = (signed char*)pos;
  802. }
  803. }
  804. else
  805. {
  806. signed char* pos = (signed char*)position_;
  807. signed char* end = sound->GetEnd();
  808. signed char* repeat = sound->GetRepeat();
  809. if (sound->IsLooped())
  810. {
  811. while (samples--)
  812. {
  813. int s = ((int)pos[0] + (int)pos[1]) / 2;
  814. *dest = *dest + s * vol;
  815. ++dest;
  816. INC_POS_STEREO_LOOPED();
  817. }
  818. position_ = pos;
  819. }
  820. else
  821. {
  822. while (samples--)
  823. {
  824. int s = ((int)pos[0] + (int)pos[1]) / 2;
  825. *dest = *dest + s * vol;
  826. ++dest;
  827. INC_POS_STEREO_ONESHOT();
  828. }
  829. position_ = pos;
  830. }
  831. }
  832. fractPosition_ = fractPos;
  833. }
  834. void SoundSource::MixStereoToStereo(Sound* sound, int* dest, unsigned samples, int mixRate)
  835. {
  836. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  837. int vol = (int)(256.0f * totalGain + 0.5f);
  838. if (!vol)
  839. {
  840. MixZeroVolume(sound, samples, mixRate);
  841. return;
  842. }
  843. float add = frequency_ / (float)mixRate;
  844. int intAdd = (int)add;
  845. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  846. int fractPos = fractPosition_;
  847. if (sound->IsSixteenBit())
  848. {
  849. short* pos = (short*)position_;
  850. short* end = (short*)sound->GetEnd();
  851. short* repeat = (short*)sound->GetRepeat();
  852. if (sound->IsLooped())
  853. {
  854. while (samples--)
  855. {
  856. *dest = *dest + (pos[0] * vol) / 256;
  857. ++dest;
  858. *dest = *dest + (pos[1] * vol) / 256;
  859. ++dest;
  860. INC_POS_STEREO_LOOPED();
  861. }
  862. position_ = (signed char*)pos;
  863. }
  864. else
  865. {
  866. while (samples--)
  867. {
  868. *dest = *dest + (pos[0] * vol) / 256;
  869. ++dest;
  870. *dest = *dest + (pos[1] * vol) / 256;
  871. ++dest;
  872. INC_POS_STEREO_ONESHOT();
  873. }
  874. position_ = (signed char*)pos;
  875. }
  876. }
  877. else
  878. {
  879. signed char* pos = (signed char*)position_;
  880. signed char* end = sound->GetEnd();
  881. signed char* repeat = sound->GetRepeat();
  882. if (sound->IsLooped())
  883. {
  884. while (samples--)
  885. {
  886. *dest = *dest + pos[0] * vol;
  887. ++dest;
  888. *dest = *dest + pos[1] * vol;
  889. ++dest;
  890. INC_POS_STEREO_LOOPED();
  891. }
  892. position_ = pos;
  893. }
  894. else
  895. {
  896. while (samples--)
  897. {
  898. *dest = *dest + pos[0] * vol;
  899. ++dest;
  900. *dest = *dest + pos[1] * vol;
  901. ++dest;
  902. INC_POS_STEREO_ONESHOT();
  903. }
  904. position_ = pos;
  905. }
  906. }
  907. fractPosition_ = fractPos;
  908. }
  909. void SoundSource::MixStereoToMonoIP(Sound* sound, int* dest, unsigned samples, int mixRate)
  910. {
  911. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  912. int vol = (int)(256.0f * totalGain + 0.5f);
  913. if (!vol)
  914. {
  915. MixZeroVolume(sound, samples, mixRate);
  916. return;
  917. }
  918. float add = frequency_ / (float)mixRate;
  919. int intAdd = (int)add;
  920. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  921. int fractPos = fractPosition_;
  922. if (sound->IsSixteenBit())
  923. {
  924. short* pos = (short*)position_;
  925. short* end = (short*)sound->GetEnd();
  926. short* repeat = (short*)sound->GetRepeat();
  927. if (sound->IsLooped())
  928. {
  929. while (samples--)
  930. {
  931. int s = (GET_IP_SAMPLE_LEFT() + GET_IP_SAMPLE_RIGHT()) / 2;
  932. *dest = *dest + (s * vol) / 256;
  933. ++dest;
  934. INC_POS_STEREO_LOOPED();
  935. }
  936. position_ = (signed char*)pos;
  937. }
  938. else
  939. {
  940. while (samples--)
  941. {
  942. int s = (GET_IP_SAMPLE_LEFT() + GET_IP_SAMPLE_RIGHT()) / 2;
  943. *dest = *dest + (s * vol) / 256;
  944. ++dest;
  945. INC_POS_STEREO_ONESHOT();
  946. }
  947. position_ = (signed char*)pos;
  948. }
  949. }
  950. else
  951. {
  952. signed char* pos = (signed char*)position_;
  953. signed char* end = sound->GetEnd();
  954. signed char* repeat = sound->GetRepeat();
  955. if (sound->IsLooped())
  956. {
  957. while (samples--)
  958. {
  959. int s = (GET_IP_SAMPLE_LEFT() + GET_IP_SAMPLE_RIGHT()) / 2;
  960. *dest = *dest + s * vol;
  961. ++dest;
  962. INC_POS_STEREO_LOOPED();
  963. }
  964. position_ = pos;
  965. }
  966. else
  967. {
  968. while (samples--)
  969. {
  970. int s = (GET_IP_SAMPLE_LEFT() + GET_IP_SAMPLE_RIGHT()) / 2;
  971. *dest = *dest + s * vol;
  972. ++dest;
  973. INC_POS_STEREO_ONESHOT();
  974. }
  975. position_ = pos;
  976. }
  977. }
  978. fractPosition_ = fractPos;
  979. }
  980. void SoundSource::MixStereoToStereoIP(Sound* sound, int* dest, unsigned samples, int mixRate)
  981. {
  982. float totalGain = audio_->GetSoundSourceMasterGain(soundType_) * attenuation_ * gain_;
  983. int vol = (int)(256.0f * totalGain + 0.5f);
  984. if (!vol)
  985. {
  986. MixZeroVolume(sound, samples, mixRate);
  987. return;
  988. }
  989. float add = frequency_ / (float)mixRate;
  990. int intAdd = (int)add;
  991. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  992. int fractPos = fractPosition_;
  993. if (sound->IsSixteenBit())
  994. {
  995. short* pos = (short*)position_;
  996. short* end = (short*)sound->GetEnd();
  997. short* repeat = (short*)sound->GetRepeat();
  998. if (sound->IsLooped())
  999. {
  1000. while (samples--)
  1001. {
  1002. *dest = *dest + (GET_IP_SAMPLE_LEFT() * vol) / 256;
  1003. ++dest;
  1004. *dest = *dest + (GET_IP_SAMPLE_RIGHT() * vol) / 256;
  1005. ++dest;
  1006. INC_POS_STEREO_LOOPED();
  1007. }
  1008. position_ = (signed char*)pos;
  1009. }
  1010. else
  1011. {
  1012. while (samples--)
  1013. {
  1014. *dest = *dest + (GET_IP_SAMPLE_LEFT() * vol) / 256;
  1015. ++dest;
  1016. *dest = *dest + (GET_IP_SAMPLE_RIGHT() * vol) / 256;
  1017. ++dest;
  1018. INC_POS_STEREO_ONESHOT();
  1019. }
  1020. position_ = (signed char*)pos;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. signed char* pos = (signed char*)position_;
  1026. signed char* end = sound->GetEnd();
  1027. signed char* repeat = sound->GetRepeat();
  1028. if (sound->IsLooped())
  1029. {
  1030. while (samples--)
  1031. {
  1032. *dest = *dest + GET_IP_SAMPLE_LEFT() * vol;
  1033. ++dest;
  1034. *dest = *dest + GET_IP_SAMPLE_RIGHT() * vol;
  1035. ++dest;
  1036. INC_POS_STEREO_LOOPED();
  1037. }
  1038. position_ = pos;
  1039. }
  1040. else
  1041. {
  1042. while (samples--)
  1043. {
  1044. *dest = *dest + GET_IP_SAMPLE_LEFT() * vol;
  1045. ++dest;
  1046. *dest = *dest + GET_IP_SAMPLE_RIGHT() * vol;
  1047. ++dest;
  1048. INC_POS_STEREO_ONESHOT();
  1049. }
  1050. position_ = pos;
  1051. }
  1052. }
  1053. fractPosition_ = fractPos;
  1054. }
  1055. void SoundSource::MixZeroVolume(Sound* sound, unsigned samples, int mixRate)
  1056. {
  1057. float add = frequency_ * (float)samples / (float)mixRate;
  1058. int intAdd = (int)add;
  1059. int fractAdd = (int)((add - floorf(add)) * 65536.0f);
  1060. unsigned sampleSize = sound->GetSampleSize();
  1061. fractPosition_ += fractAdd;
  1062. if (fractPosition_ > 65535)
  1063. {
  1064. fractPosition_ &= 65535;
  1065. position_ += sampleSize;
  1066. }
  1067. position_ += intAdd * sampleSize;
  1068. if (position_ > sound->GetEnd())
  1069. {
  1070. if (sound->IsLooped())
  1071. {
  1072. while (position_ >= sound->GetEnd())
  1073. {
  1074. position_ -= (sound->GetEnd() - sound->GetRepeat());
  1075. }
  1076. }
  1077. else
  1078. position_ = 0;
  1079. }
  1080. }
  1081. void SoundSource::MixNull(float timeStep)
  1082. {
  1083. if (!position_ || !sound_ || !IsEnabledEffective())
  1084. return;
  1085. // Advance only the time position
  1086. timePosition_ += timeStep * frequency_ / sound_->GetFrequency();
  1087. if (sound_->IsLooped())
  1088. {
  1089. // For simulated playback, simply reset the time position to zero when the sound loops
  1090. if (timePosition_ >= sound_->GetLength())
  1091. timePosition_ -= sound_->GetLength();
  1092. }
  1093. else
  1094. {
  1095. if (timePosition_ >= sound_->GetLength())
  1096. {
  1097. position_ = 0;
  1098. timePosition_ = 0.0f;
  1099. }
  1100. }
  1101. }
  1102. }