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

Commit 16f6e83

Browse files
committed
Added SoundSet to project (forgot to do that before, oops)
Cleaned up some typos and issues in SoundSet Properly reimplemented sound data and sub sound set selection in SoundSet Added const and non-const versions of SoundSet::GetFlattenedSound and used them appropriately in SoundContainer and AudioMan Changed class enums to regular enums so luabind doesn't get all uppity Minor tweaks and cleanup
1 parent 34f1e28 commit 16f6e83

File tree

7 files changed

+113
-61
lines changed

7 files changed

+113
-61
lines changed

Entities/SoundContainer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ namespace RTE {
5959

6060
int SoundContainer::ReadProperty(std::string propName, Reader &reader) {
6161
if (propName == "AddSound") {
62-
m_TopLevelSoundSet.AddSoundData(SoundSet::ReadAndGetSound(reader));
62+
m_TopLevelSoundSet.AddSoundData(SoundSet::ReadAndGetSoundData(reader));
6363
} else if (propName == "AddSoundSet") {
6464
SoundSet subSoundSet;
6565
reader >> subSoundSet;
@@ -141,7 +141,7 @@ namespace RTE {
141141

142142
std::vector<std::size_t> SoundContainer::GetSelectedSoundHashes() const {
143143
std::vector<size_t> soundHashes;
144-
std::vector<SoundSet::SoundData *> flattenedSoundData;
144+
std::vector<const SoundSet::SoundData *> flattenedSoundData;
145145
m_TopLevelSoundSet.GetFlattenedSoundData(flattenedSoundData, false);
146146
for (const SoundSet::SoundData *selectedSoundData : flattenedSoundData) {
147147
soundHashes.push_back(selectedSoundData->SoundFile.GetHash());
@@ -152,7 +152,7 @@ namespace RTE {
152152
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
153153

154154
const SoundSet::SoundData * SoundContainer::GetSoundDataForSound(const FMOD::Sound *sound) const {
155-
std::vector<SoundSet::SoundData *> flattenedSoundData;
155+
std::vector<const SoundSet::SoundData *> flattenedSoundData;
156156
m_TopLevelSoundSet.GetFlattenedSoundData(flattenedSoundData, false);
157157
for (const SoundSet::SoundData *soundData : flattenedSoundData) {
158158
if (sound == soundData->SoundObject) {

Entities/SoundContainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace RTE {
2222
/// <summary>
2323
/// How the SoundContainer should behave when it tries to play again while already playing.
2424
/// </summary>
25-
enum class SoundOverlapMode {
25+
enum SoundOverlapMode {
2626
OVERLAP = 0,
2727
RESTART = 1,
2828
IGNORE_PLAY = 2

Entities/SoundSet.cpp

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
#include "SoundSet.h"
2+
#include "AudioMan.h"
3+
#include "RTETools.h"
4+
#include "RTEError.h"
25

36
namespace RTE {
47

58
const std::string SoundSet::m_sClassName = "SoundSet";
69

7-
const std::unordered_map<std::string, SoundContainer::SoundCycleMode> SoundContainer::c_SoundCycleModeMap = {
8-
{"Random", SoundSet::SoundCycleMode::RANDOM},
9-
{"Forwards", SoundSet::SoundCycleMode::FORWARDS},
10-
{"All", SoundSet::SoundCycleMode::ALL}
10+
const std::unordered_map<std::string, SoundSet::SoundSelectionCycleMode> SoundSet::c_SoundSelectionCycleModeMap = {
11+
{"Random", SoundSelectionCycleMode::RANDOM},
12+
{"Forwards", SoundSelectionCycleMode::FORWARDS},
13+
{"All", SoundSelectionCycleMode::ALL}
1114
};
1215

1316
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1417

1518
void SoundSet::Clear() {
1619
m_SoundSelectionCycleMode = SoundSelectionCycleMode::RANDOM;
17-
m_CurrentSelection.clear();
20+
m_CurrentSelection = {false, 0};
1821

1922
m_SoundData.clear();
2023
m_SubSoundSets.clear();
@@ -28,7 +31,7 @@ namespace RTE {
2831
for (SoundData referenceSoundData : reference.m_SoundData) {
2932
m_SoundData.push_back(referenceSoundData);
3033
}
31-
for (const SoundSet &referenceSoundSet : reference.m_SoundSets) {
34+
for (const SoundSet &referenceSoundSet : reference.m_SubSoundSets) {
3235
SoundSet soundSet;
3336
soundSet.Create(referenceSoundSet);
3437
m_SubSoundSets.push_back(soundSet);
@@ -43,7 +46,7 @@ namespace RTE {
4346
if (propName == "SoundSelectionCycleMode" || propName == "CycleMode") {
4447
m_SoundSelectionCycleMode = ReadSoundSelectionCycleMode(reader);
4548
} else if (propName == "AddSound") {
46-
AddSoundData(ReadAndGetSound(reader));
49+
AddSoundData(ReadAndGetSoundData(reader));
4750
} else if (propName == "AddSoundSet" || propName == "AddSubSoundSet") {
4851
SoundSet subSoundSet;
4952
reader >> subSoundSet;
@@ -56,14 +59,14 @@ namespace RTE {
5659

5760
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5861

59-
static SoundData ReadAndGetSound(Reader &reader) {
60-
SoundData soundData;
62+
SoundSet::SoundData SoundSet::ReadAndGetSoundData(Reader &reader) {
63+
SoundSet::SoundData soundData;
6164

6265
/// <summary>
6366
/// 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.
6467
/// </summary>
6568
/// <param name="soundPath">The path to the sound file.</param>
66-
auto readSoundDirectlyFromPath = [&soundData, &reader](const std::string &soundPath) {
69+
auto readSoundFromPath = [&soundData, &reader](const std::string &soundPath) {
6770
ContentFile soundFile(soundPath.c_str());
6871
soundFile.SetFormattedReaderPosition("in file " + reader.GetCurrentFilePath() + " on line " + std::to_string(reader.GetCurrentFileLine()));
6972
FMOD::Sound *soundObject = soundFile.GetAsSound();
@@ -75,14 +78,14 @@ namespace RTE {
7578

7679
std::string propValue = reader.ReadPropValue();
7780
if (propValue != "Sound" && propValue != "ContentFile") {
78-
readSoundDirectlyFromPath(propValue);
81+
readSoundFromPath(propValue);
7982
return soundData;
8083
}
8184

8285
while (reader.NextProperty()) {
8386
std::string soundSubPropertyName = reader.ReadPropName();
8487
if (soundSubPropertyName == "FilePath" || soundSubPropertyName == "Path") {
85-
readSound(reader.ReadPropValue());
88+
readSoundFromPath(reader.ReadPropValue());
8689
} else if (soundSubPropertyName == "Offset") {
8790
reader >> soundData.Offset;
8891
} else if (soundSubPropertyName == "MinimumAudibleDistance") {
@@ -97,15 +100,16 @@ namespace RTE {
97100

98101
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99102

100-
static SoundSelectionCycleMode SoundSet::ReadSoundSelectionCycleMode(const Reader &reader) {
101-
std::string cycleModeString = reader.ReadPropValue();
102-
SoundSelectionCycleMode soundSelectionCycleMode = SoundSelectionCycleMode::RANDOM;
103-
if (c_SoundCycleModeMap.find(cycleModeString) != c_SoundCycleModeMap.end()) {
104-
soundSelectionCycleMode = c_SoundCycleModeMap.find(cycleModeString)->second;
105-
} else {
106-
reader.ReportError("Cycle mode " + cycleModeString + " is invalid.");
103+
SoundSet::SoundSelectionCycleMode SoundSet::ReadSoundSelectionCycleMode(Reader &reader) {
104+
std::string soundSelectionCycleModeString = reader.ReadPropValue();
105+
106+
std::unordered_map<std::string, SoundSelectionCycleMode>::const_iterator soundSelectionCycleMode = c_SoundSelectionCycleModeMap.find(soundSelectionCycleModeString);
107+
if (soundSelectionCycleMode != c_SoundSelectionCycleModeMap.end()) {
108+
return soundSelectionCycleMode->second;
107109
}
108-
return soundSelectionCycleMode;
110+
111+
reader.ReportError("Sound selection cycle mode " + soundSelectionCycleModeString + " is invalid.");
112+
return SoundSelectionCycleMode::RANDOM;
109113
}
110114

111115
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -114,7 +118,7 @@ namespace RTE {
114118
Serializable::Save(writer);
115119

116120
writer.NewProperty("SoundSelectionCycleMode");
117-
SaveSoundSelectionCycleMode(m_SoundSelectionCycleMode);
121+
SaveSoundSelectionCycleMode(writer, m_SoundSelectionCycleMode);
118122

119123
for (const SoundData &soundData : m_SoundData) {
120124
writer.NewProperty("AddSound");
@@ -144,9 +148,9 @@ namespace RTE {
144148

145149
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146150

147-
static void SoundSet::SaveSoundSelectionCycleMode(Writer &writer, SoundSelectionCycleMode soundSelectionCycleMode) {
148-
std::list<std::pair<const std::string, SoundCycleMode>>::const_iterator cycleModeMapEntry = std::find_if(c_SoundCycleModeMap.begin(), c_SoundCycleModeMap.end(), [&soundSelectionCycleMode = soundSelectionCycleMode](auto element) { return element.second == soundSelectionCycleMode; });
149-
if (cycleModeMapEntry != c_SoundCycleModeMap.end()) {
151+
void SoundSet::SaveSoundSelectionCycleMode(Writer &writer, SoundSelectionCycleMode soundSelectionCycleMode) {
152+
std::list<std::pair<const std::string, SoundSelectionCycleMode>>::const_iterator cycleModeMapEntry = std::find_if(c_SoundSelectionCycleModeMap.begin(), c_SoundSelectionCycleModeMap.end(), [&soundSelectionCycleMode = soundSelectionCycleMode](auto element) { return element.second == soundSelectionCycleMode; });
153+
if (cycleModeMapEntry != c_SoundSelectionCycleModeMap.end()) {
150154
writer << cycleModeMapEntry->first;
151155
} else {
152156
RTEAbort("Tried to write invalid SoundSelectionCycleMode when saving SoundContainer/SoundSet.");
@@ -168,8 +172,8 @@ namespace RTE {
168172
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
169173

170174
bool SoundSet::HasAnySounds(bool includeSubSoundSets) const {
171-
bool hasAnySounds = !m_SoundSets.empty();
172-
if (!hasAySounds && includeSubSoundSets) {
175+
bool hasAnySounds = !m_SoundData.empty();
176+
if (!hasAnySounds && includeSubSoundSets) {
173177
for (const SoundSet &subSoundSet : m_SubSoundSets) {
174178
hasAnySounds = subSoundSet.HasAnySounds();
175179
if (hasAnySounds) { break; }
@@ -180,55 +184,96 @@ namespace RTE {
180184

181185
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
182186

183-
void SoundSet::GetFlattenedSoundData(std::vector<SoundData *> &flattenedSoundData, bool onlyGetSelectedSoundData) const {
187+
void SoundSet::GetFlattenedSoundData(std::vector<SoundData *> &flattenedSoundData, bool onlyGetSelectedSoundData) {
184188
if (!onlyGetSelectedSoundData || m_SoundSelectionCycleMode == SoundSelectionCycleMode::ALL) {
185189
for (SoundData &soundData : m_SoundData) { flattenedSoundData.push_back(&soundData); }
190+
for (SoundSet &subSoundSet : m_SubSoundSets) { subSoundSet.GetFlattenedSoundData(flattenedSoundData, onlyGetSelectedSoundData); }
191+
} else {
192+
if (m_CurrentSelection.first == false) {
193+
flattenedSoundData.push_back(&m_SoundData[m_CurrentSelection.second]);
194+
} else {
195+
m_SubSoundSets[m_CurrentSelection.second].GetFlattenedSoundData(flattenedSoundData, onlyGetSelectedSoundData);
196+
}
197+
}
198+
}
199+
200+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201+
202+
void SoundSet::GetFlattenedSoundData(std::vector<const SoundData *> &flattenedSoundData, bool onlyGetSelectedSoundData) const {
203+
if (!onlyGetSelectedSoundData || m_SoundSelectionCycleMode == SoundSelectionCycleMode::ALL) {
204+
for (const SoundData &soundData : m_SoundData) { flattenedSoundData.push_back(&soundData); }
186205
for (const SoundSet &subSoundSet : m_SubSoundSets) { subSoundSet.GetFlattenedSoundData(flattenedSoundData, onlyGetSelectedSoundData); }
187206
} else {
188207
if (m_CurrentSelection.first == false) {
189208
flattenedSoundData.push_back(&m_SoundData[m_CurrentSelection.second]);
190209
} else {
191-
m_CurrentSelection.second.GetFlattenedSoundData(flattenedSoundData, onlyGetSelectedSoundData);
210+
m_SubSoundSets[m_CurrentSelection.second].GetFlattenedSoundData(flattenedSoundData, onlyGetSelectedSoundData);
192211
}
193212
}
194213
}
195214

196215
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
197216

198-
bool SoundSet::SelectNextSoundSet() const {
199-
int soundSetCount = m_SoundSets.size();
200-
switch (soundSetCount) {
217+
bool SoundSet::SelectNextSounds() {
218+
if (m_SoundSelectionCycleMode == SoundSelectionCycleMode::ALL) {
219+
return true;
220+
}
221+
int selectedVectorSize = m_CurrentSelection.first == false ? m_SoundData.size() : m_SubSoundSets.size();
222+
int unselectedVectorSize = m_CurrentSelection.first == true ? m_SoundData.size() : m_SubSoundSets.size();
223+
int soundDataAndSetSize = selectedVectorSize + unselectedVectorSize;
224+
225+
/// <summary>
226+
/// Internal lambda function to pick a random sound that's not the previously played sound. Done to avoid scoping issues inside the switch below.
227+
/// </summary>
228+
auto selectSoundRandom = [&selectedVectorSize, &unselectedVectorSize, this]() {
229+
if (selectedVectorSize == 1 || RandomNum(0, 1) == 1) {
230+
std::swap(selectedVectorSize, unselectedVectorSize);
231+
m_CurrentSelection = {!m_CurrentSelection.first, RandomNum(0, selectedVectorSize - 1)};
232+
} else {
233+
size_t soundToSelect = RandomNum(0, selectedVectorSize - 1);
234+
while (soundToSelect == m_CurrentSelection.second) {
235+
soundToSelect = RandomNum(0, selectedVectorSize - 1);
236+
}
237+
m_CurrentSelection.second = soundToSelect;
238+
}
239+
};
240+
241+
/// <summary>
242+
/// Internal lambda function to pick the next sound in the forwards direction.
243+
/// </summary>
244+
auto selectSoundForwards = [&selectedVectorSize, &unselectedVectorSize, this]() {
245+
m_CurrentSelection.second++;
246+
if (m_CurrentSelection.second > selectedVectorSize) {
247+
m_CurrentSelection = {!m_CurrentSelection.first, 0};
248+
std::swap(selectedVectorSize, unselectedVectorSize);
249+
if (selectedVectorSize == 0) {
250+
m_CurrentSelection.first = !m_CurrentSelection.first;
251+
std::swap(selectedVectorSize, unselectedVectorSize);
252+
}
253+
}
254+
};
255+
256+
switch (soundDataAndSetSize) {
201257
case 0:
202258
return false;
203259
case 1:
204260
return true;
205261
case 2:
206-
m_SelectedSoundSet = (m_SelectedSoundSet + 1) % 2;
262+
selectSoundForwards();
207263
break;
208264
default:
209-
/// <summary>
210-
/// Internal lambda function to pick a random sound that's not the previously played sound. Done to avoid scoping issues inside the switch below.
211-
/// </summary>
212-
auto selectRandomSound = [&soundSetCount, this]() {
213-
size_t soundToSelect = RandomNum(0, soundSetCount - 1);
214-
while (soundToSelect == m_SelectedSoundSet) {
215-
soundToSelect = RandomNum(0, soundSetCount - 1);
216-
}
217-
m_SelectedSoundSet = soundToSelect;
218-
};
219-
220265
switch (m_SoundSelectionCycleMode) {
221-
case RANDOM:
222-
selectRandomSound();
266+
case SoundSelectionCycleMode::RANDOM:
267+
selectSoundRandom();
223268
break;
224-
case FORWARDS:
225-
m_SelectedSoundSet = (m_SelectedSoundSet + 1) % soundSetCount;
269+
case SoundSelectionCycleMode::FORWARDS:
270+
selectSoundForwards();
226271
break;
227272
default:
228-
RTEAbort("Invalid sound cycle mode " + m_SoundSelectionCycleMode);
273+
RTEAbort("Invalid sound selection sound cycle mode.");
229274
break;
230275
}
231-
RTEAssert(m_SelectedSoundSet >= 0 && m_SelectedSoundSet < soundSetCount, "Failed to select next sound, either none was selected or the selected sound was invalid.");
276+
RTEAssert(m_CurrentSelection.second >= 0 && m_CurrentSelection.second < selectedVectorSize, "Failed to select next sound, either none was selected or the selected sound was invalid.");
232277
}
233278
return true;
234279
}

Entities/SoundSet.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace RTE {
1818
/// <summary>
1919
/// How the SoundSet should choose the next sound or SoundSet to play when SelectNextSound is called.
2020
/// </summary>
21-
enum class SoundSelectionCycleMode {
21+
enum SoundSelectionCycleMode {
2222
RANDOM = 0,
2323
FORWARDS,
2424
ALL
@@ -68,14 +68,14 @@ namespace RTE {
6868
/// </summary>
6969
/// <param name="reader">A Reader lined up to the value of the property to be read.</param>
7070
/// <returns>SoundData for the newly read sound.</returns>
71-
static SoundData ReadAndGetSound(Reader &reader);
71+
static SoundData ReadAndGetSoundData(Reader &reader);
7272

7373
/// <summary>
7474
/// Handles turning a SoundCelectionCycleMode from its user-friendly name in INI to its enum value, using the static SoundSelectionCycleMap.
7575
/// </summary>
7676
/// <param name="reader">A Reader lined up to the value of the property to be read.</param>
7777
/// <returns>The appropriate SoundSelectionCycleMode for the given INI value.</returns>
78-
static SoundSelectionCycleMode ReadSoundSelectionCycleMode(const Reader &reader);
78+
static SoundSelectionCycleMode ReadSoundSelectionCycleMode(Reader &reader);
7979

8080
/// <summary>
8181
/// Handles writing the given SoundSelectionCycleMode out to the given Writer, using the static SoundSelectionCycleMap.
@@ -148,14 +148,21 @@ namespace RTE {
148148
/// Sets the SoundSelectionCycleMode for this SoundSet, which is used to determine what SoundSet to select next time SelectNextSounds is called.
149149
/// </summary>
150150
/// <param name="newSoundSelectionCycleMOde">The new SoundSelectionCycleMode for this SoundSet.</param>
151-
void SetSoundSelectionCycleMode(SoundSelectionCycleMode newSoundSelectionCycleMOde) { m_SoundSelectionCycleMode = newSoundSelectionCycleMOde; }
151+
void SetSoundSelectionCycleMode(SoundSelectionCycleMode newSoundSelectionCycleMode) { m_SoundSelectionCycleMode = newSoundSelectionCycleMode; }
152152

153153
/// <summary>
154154
/// Filles the passed in vector with the flattened SoundData in the SoundSet, optionally only getting currently selected SoundData.
155155
/// </summary>
156156
/// <param name="flattenedSoundData">A reference vector of SoundData references to be filled with this SoundSet's flattened SoundData.</param>
157157
/// <param name="onlyGetSelectedSoundData">Whether to only get SoundData that is currently selected, or to get all SoundData in this SoundSet.</param>
158-
void GetFlattenedSoundData(std::vector<SoundData *> &flattenedSoundData, bool onlyGetSelectedSoundData) const;
158+
void GetFlattenedSoundData(std::vector<SoundData *> &flattenedSoundData, bool onlyGetSelectedSoundData);
159+
160+
/// <summary>
161+
/// Filles the passed in vector with the flattened SoundData in the SoundSet, optionally only getting currently selected SoundData.
162+
/// </summary>
163+
/// <param name="flattenedSoundData">A reference vector of SoundData references to be filled with this SoundSet's flattened SoundData.</param>
164+
/// <param name="onlyGetSelectedSoundData">Whether to only get SoundData that is currently selected, or to get all SoundData in this SoundSet.</param>
165+
void GetFlattenedSoundData(std::vector<const SoundData *> &flattenedSoundData, bool onlyGetSelectedSoundData) const;
159166

160167
/// <summary>
161168
/// Gets the vector of SubSoundSets for this SoundSet.

Managers/AudioMan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ namespace RTE {
524524
FMOD::ChannelGroup *channelGroupToPlayIn = soundContainer->IsImmobile() ? m_ImmobileSoundChannelGroup : m_MobileSoundChannelGroup;
525525
FMOD::Channel *channel;
526526
int channelIndex;
527-
std::vector<SoundSet::SoundData *> selectedSoundData;
527+
std::vector<const SoundSet::SoundData *> selectedSoundData;
528528
soundContainer->GetTopLevelSoundSet().GetFlattenedSoundData(selectedSoundData, true);
529529
for (const SoundSet::SoundData *soundData : selectedSoundData) {
530530
result = (result == FMOD_OK) ? m_AudioSystem->playSound(soundData->SoundObject, channelGroupToPlayIn, true, &channel) : result;

Managers/AudioMan.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
namespace RTE {
1616

17-
class SoundContainer;
18-
1917
/// <summary>
2018
/// The singleton manager of the WAV sound effects and OGG music playback.
2119
/// </summary>

RTEA.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
<ClInclude Include="Entities\MetaSave.h" />
265265
<ClInclude Include="Entities\PEmitter.h" />
266266
<ClInclude Include="Entities\Round.h" />
267+
<ClInclude Include="Entities\SoundSet.h" />
267268
<ClInclude Include="GUI\Interface.h" />
268269
<ClInclude Include="GUI\GUISound.h" />
269270
<ClInclude Include="Managers\NetworkClient.h" />
@@ -417,6 +418,7 @@
417418
<ClCompile Include="Entities\MetaSave.cpp" />
418419
<ClCompile Include="Entities\PEmitter.cpp" />
419420
<ClCompile Include="Entities\Round.cpp" />
421+
<ClCompile Include="Entities\SoundSet.cpp" />
420422
<ClCompile Include="GUI\GUISound.cpp" />
421423
<ClCompile Include="Managers\NetworkClient.cpp" />
422424
<ClCompile Include="Managers\NetworkServer.cpp" />

0 commit comments

Comments
 (0)