Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 77d9843

Browse files
committed
Changed volume setting back to account for 3DLevel < 1 making automatic attenuation not work. Also made it mute when set to 0 so it won't screw things up
Changed soundcontainer playing in audioman.cpp to do panning and positioning properly, fixed audioman 3d effects handling to deal with volume setting when needed Cleaned up some stuff in audioman
1 parent 862eb55 commit 77d9843

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

Entities/SoundContainer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ namespace RTE {
278278
}
279279
}
280280
}
281-
return nullptr;
281+
return NULL;
282282
}
283283

284284
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Entities/SoundContainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ namespace RTE {
323323
/// Sets the volume sounds in this SoundContainer should be played at. Note that this does not factor volume changes due to the SoundContainer's position. Does not affect currently playing sounds.
324324
/// </summary>
325325
/// <param name="newVolume">The new volume sounds in this SoundContainer should be played at. Limited between 0 and 10.</param>
326-
void SetVolume(float newVolume) { m_Volume = std::clamp(newVolume, 0.0F, 10.0F); if (IsBeingPlayed()) { g_AudioMan.ChangeSoundContainerPlayingChannelsVolume(this); } }
326+
void SetVolume(float newVolume) { newVolume = std::clamp(newVolume, 0.0F, 10.0F); if (IsBeingPlayed()) { g_AudioMan.ChangeSoundContainerPlayingChannelsVolume(this, newVolume); m_Volume = newVolume; } }
327327

328328
/// <summary>
329329
/// Gets the pitch the sounds in this SoundContainer are played at. Note that this does not factor in global pitch.

Managers/AudioMan.cpp

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,17 @@ namespace RTE {
508508

509509
result = (result == FMOD_OK) ? channel->setUserData(soundContainer) : result;
510510
result = (result == FMOD_OK) ? channel->setCallback(SoundChannelEndedCallback) : result;
511-
result = (result == FMOD_OK) ? channel->set3DAttributes(&GetAsFMODVector(soundContainer->GetPosition() + soundData.Offset), nullptr) : result;
512-
result = (result == FMOD_OK) ? channel->set3DLevel(g_SettingsMan.SoundPanningEffectStrength()) : result;
511+
if (!soundContainer->IsImmobile()) {
512+
m_SoundChannelMinimumAudibleDistances.insert({channelIndex, soundData.MinimumAudibleDistance});
513+
UpdatePositionalEffectsForSoundChannel(channel, &GetAsFMODVector(soundContainer->GetPosition() + soundData.Offset));
514+
result = (result == FMOD_OK) ? channel->set3DLevel(g_SettingsMan.SoundPanningEffectStrength()) : result;
515+
} else {
516+
result = (result == FMOD_OK) ? channel->set3DLevel(0.0F) : result;
517+
}
513518
result = (result == FMOD_OK) ? channel->setPriority(soundContainer->GetPriority()) : result;
514519
result = (result == FMOD_OK) ? channel->setVolume(soundContainer->GetVolume()) : result;
515520
result = (result == FMOD_OK) ? channel->setPitch(soundContainer->GetPitch()) : result;
516521

517-
m_SoundChannelMinimumAudibleDistances.insert({channelIndex, soundData.MinimumAudibleDistance});
518-
if (!soundContainer->IsImmobile()) { UpdatePositionalEffectsForSoundChannel(channel); }
519522

520523
if (result != FMOD_OK) {
521524
g_ConsoleMan.PrintString("ERROR: Could not play sounds from SoundContainer " + soundContainer->GetPresetName() + ": " + std::string(FMOD_ErrorString(result)));
@@ -566,19 +569,29 @@ namespace RTE {
566569

567570
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
568571

569-
bool AudioMan::ChangeSoundContainerPlayingChannelsVolume(SoundContainer *soundContainer) {
572+
bool AudioMan::ChangeSoundContainerPlayingChannelsVolume(SoundContainer *soundContainer, float newVolume) {
570573
if (!m_AudioEnabled || !soundContainer || !soundContainer->IsBeingPlayed()) {
571574
return false;
572575
}
573576
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_VOLUME, soundContainer->GetPlayingChannels(), &soundContainer->GetSelectedSoundHashes(), Vector(), 0, soundContainer->GetVolume()); }
574577

575578
FMOD_RESULT result = FMOD_OK;
576579
FMOD::Channel *soundChannel;
580+
float soundContainerOldVolume = soundContainer->GetVolume() == 0 ? 1.0F : soundContainer->GetVolume();
581+
float soundChannelCurrentVolume;
577582

578583
const std::unordered_set<int> *playingChannels = soundContainer->GetPlayingChannels();
579584
for (int channelIndex : *playingChannels) {
580585
result = m_AudioSystem->getChannel(channelIndex, &soundChannel);
581-
result = result == FMOD_OK ? soundChannel->setVolume(soundContainer->GetVolume()) : result;
586+
result = result == FMOD_OK ? soundChannel->getVolume(&soundChannelCurrentVolume) : result;
587+
588+
if (newVolume == 0.0F) {
589+
result = result == FMOD_OK ? soundChannel->setMute(true) : result;
590+
result = result == FMOD_OK ? soundChannel->setVolume(soundChannelCurrentVolume / soundContainerOldVolume) : result;
591+
} else {
592+
result = result == FMOD_OK ? soundChannel->setMute(false) : result;
593+
result = result == FMOD_OK ? soundChannel->setVolume(newVolume / soundContainerOldVolume * soundChannelCurrentVolume) : result;
594+
}
582595
if (result != FMOD_OK) {
583596
g_ConsoleMan.PrintString("ERROR: Could not update sound volume for the sound being played on channel " + std::to_string(channelIndex) + " for SoundContainer " + soundContainer->GetPresetName() + ": " + std::string(FMOD_ErrorString(result)));
584597
}
@@ -669,24 +682,48 @@ namespace RTE {
669682
wrappedChannelPositions = {channelPosition};
670683
} else {
671684
wrappedChannelPositions = (channelPosition.x <= halfSceneWidth) ?
672-
wrappedChannelPositions = {channelPosition, GetAsFMODVector({channelPosition.x + g_SceneMan.GetSceneWidth(), channelPosition.y})} :
673-
wrappedChannelPositions = {GetAsFMODVector({channelPosition.x - g_SceneMan.GetSceneWidth(), channelPosition.y}), channelPosition};
685+
wrappedChannelPositions = {channelPosition, {channelPosition.x + g_SceneMan.GetSceneWidth(), channelPosition.y}} :
686+
wrappedChannelPositions = {FMOD_VECTOR({channelPosition.x - g_SceneMan.GetSceneWidth(), channelPosition.y}), channelPosition};
674687
}
675688

676-
float shortestDistance = -1;
689+
float shortestDistance = c_SoundMaxAudibleDistance;
690+
float longestDistance = 0;
677691
for (const Vector *humanPlayerPosition : m_CurrentActivityHumanPlayerPositions) {
678692
for (const FMOD_VECTOR &wrappedChannelPosition : wrappedChannelPositions) {
679693
float distanceToChannelPosition = (*humanPlayerPosition - GetAsVector(wrappedChannelPosition)).GetMagnitude();
680-
if (shortestDistance == -1 || distanceToChannelPosition < shortestDistance) {
694+
if (distanceToChannelPosition < shortestDistance) {
681695
shortestDistance = distanceToChannelPosition;
682696
channelPosition = wrappedChannelPosition;
683697
}
698+
if (distanceToChannelPosition > longestDistance) { longestDistance = distanceToChannelPosition; }
684699
}
685700
}
686701

687702
int soundChannelIndex;
688703
result = result == FMOD_OK ? soundChannel->getIndex(&soundChannelIndex) : result;
689-
result = result == FMOD_OK ? soundChannel->setMute(shortestDistance < m_SoundChannelMinimumAudibleDistances.at(soundChannelIndex)) : result;
704+
705+
float attenuationStartDistance;
706+
float soundMaxDistance;
707+
result = result == FMOD_OK ? soundChannel->get3DMinMaxDistance(&attenuationStartDistance, &soundMaxDistance) : result;
708+
709+
float attenuatedVolume = shortestDistance <= attenuationStartDistance ? 1.0F : attenuationStartDistance / shortestDistance;
710+
if (shortestDistance > soundMaxDistance) {
711+
attenuatedVolume = 0.0F;
712+
} else if (m_SoundChannelMinimumAudibleDistances.empty() || m_SoundChannelMinimumAudibleDistances.find(soundChannelIndex) == m_SoundChannelMinimumAudibleDistances.end()) {
713+
g_ConsoleMan.PrintString("ERROR: An error occurred when checking to see if the sound at channel " + std::to_string(soundChannelIndex) + " was less than its minimum audible distance away from the farthest listener.");
714+
} else if (longestDistance < m_SoundChannelMinimumAudibleDistances.at(soundChannelIndex)) {
715+
attenuatedVolume = 0.0F;
716+
}
717+
718+
void *userData;
719+
result = result == FMOD_OK ? soundChannel->getUserData(&userData) : result;
720+
float panLevel;
721+
result = result == FMOD_OK ? soundChannel->get3DLevel(&panLevel) : result;
722+
if (result == FMOD_OK && panLevel < 1.0F) {
723+
SoundContainer *channelSoundContainer = static_cast<SoundContainer *>(userData);
724+
result = soundChannel->setVolume(attenuatedVolume * channelSoundContainer->GetVolume());
725+
}
726+
690727
result = (result == FMOD_OK && (sceneWraps || positionOverride)) ? soundChannel->set3DAttributes(&channelPosition, nullptr) : result;
691728

692729
return result;

Managers/AudioMan.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,23 +433,21 @@ namespace RTE {
433433
/// Sets/updates the position of a SoundContainer's playing sounds.
434434
/// </summary>
435435
/// <param name="soundContainer">A pointer to a SoundContainer object. Ownership IS NOT transferred!</param>
436-
/// <param name="position">The position at which to play the SoundContainer's sounds.</param>
437436
/// <returns>Whether the position was successfully set.</returns>
438437
bool ChangeSoundContainerPlayingChannelsPosition(SoundContainer *soundContainer);
439438

440439
/// <summary>
441440
/// Changes the volume of a SoundContainer's playing sounds.
442441
/// </summary>
443442
/// <param name="soundContainer">A pointer to a SoundContainer object. Ownership IS NOT transferred!</param>
444-
/// <param name="volume">The new volume to play sounds at, between 0 and 1.</param>
443+
/// <param name="newVolume">The new volume to play sounds at, between 0 and 1.</param>
445444
/// <returns>Whether the volume was successfully updated.</returns>
446-
bool ChangeSoundContainerPlayingChannelsVolume(SoundContainer *soundContainer);
445+
bool ChangeSoundContainerPlayingChannelsVolume(SoundContainer *soundContainer, float newVolume);
447446

448447
/// <summary>
449448
/// Changes the frequency/pitch of a SoundContainer's playing sounds.
450449
/// </summary>
451450
/// <param name="soundContainer">A pointer to a SoundContainer object. Ownership IS NOT transferred!</param>
452-
/// <param name="pitch">New pitch to play sounds at, limited to 8 octaves up or down (i.e. 0.125 - 8).</param>
453451
/// <returns>Whether the pitch was successfully updated.</returns>
454452
bool ChangeSoundContainerPlayingChannelsPitch(SoundContainer *soundContainer);
455453
#pragma endregion

0 commit comments

Comments
 (0)