SoundSource.cpp 34 KB

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