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

Commit 8f28ee1

Browse files
committed
Changed lambdas to use capture variables so they're nice and clean
Changed SetGlobalPitch to default to only setting it for mobile sounds, since the rest is dumb, and fiddled with the logic of its arguments a little so they're flags for including things, that default to false Changed c-style casts to static_casts for userData and a reinterpret_cast for FMOD::Channel (cause static_cast wouldn't work) Made it so multiplayer set global pitch will only affect mobile sounds, until I redo sound passing to be more robust
1 parent a269eeb commit 8f28ee1

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

Entities/SoundContainer.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,30 +114,27 @@ namespace RTE {
114114
SoundData soundData;
115115

116116
/// <summary>
117-
/// This lambda exists to have some easy, private reused code.
118-
/// It loads an audio file by path in as a ContentFile, which in turn loads it into FMOD, then returns SoundData for it in the outParam outSoundData.
117+
/// Internal lambda function to load an audio file by path in as a ContentFile, which in turn loads it into FMOD, then returns SoundData for it in the outParam outSoundData.
119118
/// </summary>
120119
/// <param name="soundPath">The path to the sound file.</param>
121-
/// <param name="parentReader">The reader being used when calling this. Used to report errors if loading the sound fails.</param>
122-
/// <param name="outSoundData">The outParam for the SoundData struct produced by reading the given sound.</param>
123-
auto readSound = [](const std::string &soundPath, Reader &parentReader, SoundData *outSoundData) {
120+
auto readSound = [&soundData, &reader](const std::string &soundPath) {
124121
ContentFile soundFile(soundPath.c_str());
125122
FMOD::Sound *soundObject = soundFile.GetAsSample();
126-
if (g_AudioMan.IsAudioEnabled() && !soundObject) { parentReader.ReportError(std::string("Failed to load the sound from the file")); }
123+
if (g_AudioMan.IsAudioEnabled() && !soundObject) { reader.ReportError(std::string("Failed to load the sound from the file")); }
127124

128-
outSoundData->SoundFile = soundFile;
129-
outSoundData->SoundObject = soundObject;
125+
soundData.SoundFile = soundFile;
126+
soundData.SoundObject = soundObject;
130127
};
131128

132129
if (propValue != "Sound" && propValue != "ContentFile") {
133-
readSound(propValue, reader, &soundData);
130+
readSound(propValue);
134131
return soundData;
135132
}
136133

137134
while (reader.NextProperty()) {
138135
std::string soundSubPropertyName = reader.ReadPropName();
139136
if (soundSubPropertyName == "FilePath" || soundSubPropertyName == "Path") {
140-
readSound(reader.ReadPropValue(), reader, &soundData);
137+
readSound(reader.ReadPropValue());
141138
} else if (soundSubPropertyName == "Offset") {
142139
reader >> soundData.Offset;
143140
} else if (soundSubPropertyName == "MinimumAudibleDistance") {
@@ -205,17 +202,20 @@ namespace RTE {
205202
m_SelectedSoundSet = (m_SelectedSoundSet + 1) % 2;
206203
break;
207204
default:
208-
auto selectRandomSound = [](int soundSetSize, size_t *outSelectedSoundSet) {
209-
size_t soundToSelect = std::floorf(static_cast<float>(soundSetSize) * PosRand());
210-
while (soundToSelect == *outSelectedSoundSet) {
211-
soundToSelect = std::floorf(static_cast<float>(soundSetSize) * PosRand());
205+
/// <summary>
206+
/// Internal lambda function to pick a random sound that's not the previously played sound. Done to avoid scoping issues inside the switch below.
207+
/// </summary>
208+
auto selectRandomSound = [&soundSetCount, this]() {
209+
size_t soundToSelect = std::floorf(static_cast<float>(soundSetCount) * PosRand());
210+
while (soundToSelect == m_SelectedSoundSet) {
211+
soundToSelect = std::floorf(static_cast<float>(soundSetCount) * PosRand());
212212
}
213-
*outSelectedSoundSet = soundToSelect;
213+
m_SelectedSoundSet = soundToSelect;
214214
};
215215

216216
switch (m_SoundSelectionCycleMode) {
217217
case MODE_RANDOM:
218-
selectRandomSound(soundSetCount, &m_SelectedSoundSet);
218+
selectRandomSound();
219219
break;
220220
case MODE_FORWARDS:
221221
m_SelectedSoundSet = (m_SelectedSoundSet + 1) % soundSetCount;

Managers/AudioMan.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace RTE {
5858
return -1;
5959
}
6060

61-
SetGlobalPitch(m_GlobalPitch);
61+
SetGlobalPitch(m_GlobalPitch, false, false);
6262
SetSoundsVolume(m_SoundsVolume);
6363
SetMusicVolume(m_MusicVolume);
6464

@@ -117,17 +117,17 @@ namespace RTE {
117117

118118
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
119119

120-
void AudioMan::SetGlobalPitch(double pitch, bool excludeImmobileSounds, bool excludeMusic) {
120+
void AudioMan::SetGlobalPitch(double pitch, bool includeImmobileSounds, bool includeMusic) {
121121
if (!m_AudioEnabled) {
122122
return;
123123
}
124-
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_GLOBAL_PITCH, NULL, NULL, Vector(), 0, pitch, excludeMusic); }
124+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_GLOBAL_PITCH, NULL, NULL, Vector(), 0, pitch); }
125125

126126
// Limit pitch change to 8 octaves up or down
127127
m_GlobalPitch = Limit(pitch, 8, 0.125);
128-
if (!excludeMusic) { m_MusicChannelGroup->setPitch(m_GlobalPitch); }
128+
if (includeMusic) { m_MusicChannelGroup->setPitch(m_GlobalPitch); }
129129

130-
FMOD::ChannelGroup *channelGroupToUse = excludeImmobileSounds ? m_MobileSoundChannelGroup : m_SoundChannelGroup;
130+
FMOD::ChannelGroup *channelGroupToUse = includeImmobileSounds ? m_SoundChannelGroup : m_MobileSoundChannelGroup;
131131

132132
int numChannels;
133133
FMOD_RESULT result = channelGroupToUse->getNumChannels(&numChannels);
@@ -145,7 +145,7 @@ namespace RTE {
145145
if (result == FMOD_OK && isPlaying) {
146146
void *userData;
147147
result == FMOD_OK ? soundChannel->getUserData(&userData) : result;
148-
const SoundContainer *channelSoundContainer = (SoundContainer *)userData;
148+
const SoundContainer *channelSoundContainer = static_cast<SoundContainer *>(userData);
149149

150150
if (channelSoundContainer->IsAffectedByGlobalPitch()) { soundChannel->setPitch(pitch); }
151151
}
@@ -641,14 +641,14 @@ namespace RTE {
641641

642642
FMOD_RESULT F_CALLBACK AudioMan::SoundChannelEndedCallback(FMOD_CHANNELCONTROL *channelControl, FMOD_CHANNELCONTROL_TYPE channelControlType, FMOD_CHANNELCONTROL_CALLBACK_TYPE callbackType, void *unusedCommandData1, void *unusedCommandData2) {
643643
if (channelControlType == FMOD_CHANNELCONTROL_CHANNEL && callbackType == FMOD_CHANNELCONTROL_CALLBACK_END) {
644-
FMOD::Channel *channel = (FMOD::Channel *) channelControl;
644+
FMOD::Channel *channel = reinterpret_cast<FMOD::Channel *>(channelControl);
645645
int channelIndex;
646646
FMOD_RESULT result = channel->getIndex(&channelIndex);
647647

648648
// Remove this playing sound index from the SoundContainer if it has any playing sounds, i.e. it hasn't been reset before this callback happened.
649649
void *userData;
650650
result = (result == FMOD_OK) ? channel->getUserData(&userData) : result;
651-
SoundContainer *channelSoundContainer = (SoundContainer *)userData;
651+
SoundContainer *channelSoundContainer = static_cast<SoundContainer *>(userData);
652652
if (channelSoundContainer->IsBeingPlayed()) { channelSoundContainer->RemovePlayingChannel(channelIndex); }
653653
result = (result == FMOD_OK) ? channel->setUserData(NULL) : result;
654654

Managers/AudioMan.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ namespace RTE {
139139
double GetGlobalPitch() const { return m_GlobalPitch; }
140140

141141
/// <summary>
142-
/// Sets the global pitch multiplier for all sounds, optionally the music too.
142+
/// Sets the global pitch multiplier for mobile sounds, optionally setting it for immobile sounds and music.
143143
/// </summary>
144-
/// <param name="pitch">New global pitch, limited to 8 octaves up or down (i.e. 0.125 - 8).</param>
145-
/// <param name="excludeImmobileSounds">Whether to exclude immobile sounds (normally used for GUI and so on) from pitch modification.</param>
146-
/// <param name="excludeMusic">Whether to exclude the music from pitch modification.</param>
147-
void SetGlobalPitch(double pitch = 1.0, bool excludeImmobileSounds = false, bool excludeMusic = false);
144+
/// <param name="pitch">New global pitch, limited to 8 octaves up or down (i.e. 0.125 - 8). Defaults to 1.</param>
145+
/// <param name="includeImmobileSounds">Whether to include immobile sounds (normally used for GUI and so on) in global pitch modification. Defaults to false.</param>
146+
/// <param name="includeMusic">Whether to include the music in global pitch modification. Defaults to false.</param>
147+
void SetGlobalPitch(double pitch = 1.0, bool includeImmobileSounds = false, bool includeMusic = false);
148148
#pragma endregion
149149

150150
#pragma region Music Getters and Setters

Managers/FrameMan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1970,7 +1970,7 @@ void FrameMan::Update()
19701970
// TODO: Don't hardcode this coefficient - although it's a good defualt
19711971
float pitch = m_SimSpeed + (1.0f - m_SimSpeed) * 0.35;
19721972
// Set the pitch for all other applicable sounds other than music
1973-
g_AudioMan.SetGlobalPitch(pitch, true);
1973+
g_AudioMan.SetGlobalPitch(pitch);
19741974
// MUSIC PITCHING IS SUCK.. ruins the songs
19751975
// Only affect the music if it's really slow, so it doesn't sound funny and fluctuating
19761976
// TODO: Don't hardcode this threshold - although it's a good defualt

Managers/NetworkClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ namespace RTE
672672

673673
for (int msgIndex = 0; msgIndex < msg->SoundEventsCount; msgIndex++) {
674674
if (sndDataPtr->State == AudioMan::SOUND_SET_GLOBAL_PITCH) {
675-
g_AudioMan.SetGlobalPitch(sndDataPtr->Pitch, sndDataPtr->AffectedByGlobalPitch); //Note AffectedByGlobalPitch is hackily used to determine whether this affects music
675+
g_AudioMan.SetGlobalPitch(sndDataPtr->Pitch);
676676
} else {
677677
// The set of SoundContainers that have already been handled for this event, used to hopefully avoid repeating actions when iterating over provided sound channel indices
678678
std::unordered_set<SoundContainer *> alreadyHandledSoundContainers;

0 commit comments

Comments
 (0)