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

Commit a3598e2

Browse files
committed
Modified AudioMan PlaySound to so it doesn't call SetPitch and SetAttenuation, in order to lower the number of SoundEvents registered and sent back and forth during multiplayer.
Changed size limit of NetworkSoundData SoundFileHashes to c_MaxPlayingSoundsPerContainer to lower the struct's size. I was confused when I set it to c_MaxAudioChannels anyway; if a SoundContainer can only have 64 channels playing at once, it couldn't need more than that many hashes. Made the number of sound events required to trigger early sending of sound events to clients be based on the size of the NetworkSoundData struct, so it won't overflow its limits
1 parent 85ccf0f commit a3598e2

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

Managers/AudioMan.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,9 @@ namespace RTE {
427427
return false;
428428
}
429429

430+
attenuation = Limit(attenuation, 0.95, 0); //Limit attenuation so it can't be closer than 0 (full volume) or farther than 0.95 (quiet but not quite silent)
430431
priority = priority < 0 ? pSoundContainer->GetPriority() : priority;
431-
pitch = pSoundContainer->IsAffectedByGlobalPitch() ? m_GlobalPitch : pitch;
432+
pitch = Limit(pSoundContainer->IsAffectedByGlobalPitch() ? m_GlobalPitch : pitch, 8, 0.125); //Limit pitch change to 8 octaves up or down, and set it to global pitch if applicable
432433

433434
FMOD::Channel *channel;
434435
FMOD_RESULT result = FMOD_OK;
@@ -442,24 +443,24 @@ namespace RTE {
442443
result = result == FMOD_OK ? channel->getIndex(&channelIndex) : result;
443444
result = result == FMOD_OK ? channel->setUserData(pSoundContainer) : result;
444445
result = result == FMOD_OK ? channel->setCallback(SoundChannelEndedCallback) : result;
446+
result = result == FMOD_OK ? channel->setVolume(1 - attenuation) : result;
445447
result = result == FMOD_OK ? channel->setLoopCount(pSoundContainer->GetLoopSetting()) : result;
446448
result = result == FMOD_OK ? channel->setPriority(priority) : result;
449+
result = result == FMOD_OK ? channel->setPitch(pitch) : result;
447450
}
448451

449452
if (result != FMOD_OK) {
450453
g_ConsoleMan.PrintString("ERROR: Could not play sounds from SoundContainer " + pSoundContainer->GetPresetName() + ": " + std::string(FMOD_ErrorString(result)));
451454
return false;
452455
}
453456

454-
pSoundContainer->AddPlayingChannel(channelIndex);
455-
SetSoundAttenuation(pSoundContainer, attenuation);
456-
if (pitch != 1) { SetSoundPitch(pSoundContainer, pitch); }
457-
458457
result = channel->setPaused(false);
459458
if (result != FMOD_OK) {
460459
g_ConsoleMan.PrintString("ERROR: Failed to start playing sounds from SoundContainer " + pSoundContainer->GetPresetName() + " after setting it up: " + std::string(FMOD_ErrorString(result)));
461460
}
462461

462+
pSoundContainer->AddPlayingChannel(channelIndex);
463+
463464
// Now that the sound is playing we can register an event with the SoundContainer's channels, which can be used by clients to identify the sound being played.
464465
if (m_IsInMultiplayerMode) {
465466
RegisterSoundEvent(player, SOUND_PLAY, pSoundContainer->GetPlayingChannels(), &pSoundContainer->GetSelectedSoundHashes(), attenuation, pSoundContainer->GetLoopSetting(), pitch, pSoundContainer->IsAffectedByGlobalPitch());
@@ -604,7 +605,7 @@ namespace RTE {
604605
if (channels) {
605606
std::copy(channels->begin(), channels->end(), soundData.Channels);
606607
}
607-
std::fill_n(soundData.SoundFileHashes, c_MaxAudioChannels, 0);
608+
std::fill_n(soundData.SoundFileHashes, c_MaxPlayingSoundsPerContainer, 0);
608609
if (soundFileHashes) {
609610
std::copy(soundFileHashes->begin(), soundFileHashes->end(), soundData.SoundFileHashes);
610611
}

Managers/AudioMan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace RTE {
5757
struct NetworkSoundData {
5858
unsigned char State;
5959
unsigned short int Channels[c_MaxPlayingSoundsPerContainer];
60-
size_t SoundFileHashes[c_MaxAudioChannels];
60+
size_t SoundFileHashes[c_MaxPlayingSoundsPerContainer];
6161
short int Distance;
6262
short int Loops;
6363
float Pitch;

Managers/NetworkClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ namespace RTE
715715
}
716716

717717
// We always have to add the newly made sound container to the map of server sounds, regardless of whether we were able to delete existing sounds above
718-
if (sndDataPtr->State == AudioMan::SOUND_PLAY) { m_ServerSounds[serverSoundChannelIndex] = newlyMadeSoundContainer; }
718+
if (sndDataPtr->State == AudioMan::SOUND_PLAY) { m_ServerSounds.insert({serverSoundChannelIndex, soundContainerToHandle}); }
719719
}
720720
}
721721
}

Managers/NetworkServer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,8 @@ namespace RTE
12371237
msg->SoundEventsCount++;
12381238
sndDataPtr++;
12391239

1240-
if (msg->SoundEventsCount >= 50)
1240+
//If one more sound would overflow the container, send sounds now then reset to continue
1241+
if ((msg->SoundEventsCount * sizeof(AudioMan::NetworkSoundData)) >= (MAX_PIXEL_LINE_BUFFER_SIZE - sizeof(AudioMan::NetworkSoundData) - sizeof(MsgSoundEvents)))
12411242
{
12421243
//char buf[128];
12431244
//sprintf_s(buf, sizeof(buf), "%d %d", msg->FrameNumber, msg->PostEffectsCount);

0 commit comments

Comments
 (0)