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

Commit cc298e2

Browse files
committed
Changed SoundContainer::GetSounds to return a pointer to m_Sounds and GetPlayingChannels to return a pointer to m_PlayingChannels, to avoid copying stuff. Made it so Soundcontainers can have multiple hashes (one for each sound) and added methods to get vectors of selected sound hashes and selected sound objects
Added a limit to the number of playing channels a SoundContainer can have (defined in constants), for sanity and to support multiplayer Fixed various types not being unsigned, added various safety checks, etc.
1 parent d9661ae commit cc298e2

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

Entities/SoundContainer.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ namespace RTE {
99

1010
void SoundContainer::Clear() {
1111
m_Sounds.clear();
12-
m_CurrentSound = 0;
12+
m_SelectedSounds.clear();
1313
m_PlayingChannels.clear();
1414
m_Loops = 0;
1515
m_Priority = AudioMan::PRIORITY_LOW;
1616
m_AffectedByGlobalPitch = true;
17-
m_Hash = 0;
1817
}
1918

2019
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -26,13 +25,11 @@ namespace RTE {
2625
m_Sounds.push_back(*itr);
2726
}
2827

29-
m_CurrentSound = reference.m_CurrentSound;
28+
m_SelectedSounds = reference.m_SelectedSounds;
3029
m_PlayingChannels.clear();
3130
m_Loops = reference.m_Loops;
3231
m_Priority = reference.m_Priority;
3332
m_AffectedByGlobalPitch = reference.m_AffectedByGlobalPitch;
34-
m_Hash = reference.m_Hash;
35-
3633

3734
return 0;
3835
}
@@ -43,7 +40,6 @@ namespace RTE {
4340
if (propName == "AddSample" || propName == "AddSound") {
4441
ContentFile newFile;
4542
reader >> newFile;
46-
m_Hash = newFile.GetHash();
4743

4844
FMOD::Sound *pNewSample = newFile.GetAsSample();
4945
if (!pNewSample) {
@@ -77,6 +73,24 @@ namespace RTE {
7773
}
7874
}
7975

76+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77+
78+
std::vector<std::size_t> SoundContainer::GetSelectedSoundHashes() {
79+
std::vector<std::size_t> soundHashes;
80+
for (std::size_t selectedSoundIndex : m_SelectedSounds) {
81+
soundHashes.push_back(m_Sounds[selectedSoundIndex].first.GetHash());
82+
}
83+
return soundHashes;
84+
}
85+
86+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87+
88+
std::vector<FMOD::Sound *> SoundContainer::GetSelectedSoundObjects() {
89+
std::vector<FMOD::Sound *> soundObjects;
90+
for (std::size_t selectedSoundIndex : m_SelectedSounds) {
91+
soundObjects.push_back(m_Sounds[selectedSoundIndex].second);
92+
}
93+
return soundObjects;
8094
}
8195

8296
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Entities/SoundContainer.h

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ namespace RTE {
105105
/// Gets the current list of sounds in the SoundContainer.
106106
/// </summary>
107107
/// <returns>A reference to the list.</returns>
108-
std::vector<std::pair<ContentFile, FMOD::Sound *>> GetSounds() const { return m_Sounds; }
108+
const std::vector<std::pair<ContentFile, FMOD::Sound *>> *GetSounds() const { return &m_Sounds; }
109109

110110
/// <summary>
111111
/// Shows whether this SoundContainer has been initialized at all yet and loaded with any samples.
@@ -132,28 +132,34 @@ namespace RTE {
132132
int GetPlayingSoundCount() { return m_PlayingChannels.size(); }
133133

134134
/// <summary>
135-
/// Gets the index in the internal sound group of the currently playing sound in this SoundContainer.
135+
/// Gets a vector of hashes of the sounds selected to be played next in this SoundContainer.
136136
/// </summary>
137-
/// <returns>The currently playing sound.</returns>
138-
int GetCurrentSound() { return m_CurrentSound; }
137+
/// <returns>The currently playing sounds hashes.</returns>
138+
std::vector<std::size_t> GetSelectedSoundHashes();
139+
140+
/// <summary>
141+
/// Gets a vector of the sounds objects selected to be played next in this SoundContainer.
142+
/// </summary>
143+
/// <returns>The currently playing sound objects.</returns>
144+
std::vector<FMOD::Sound *> GetSelectedSoundObjects();
139145

140146
/// <summary>
141147
/// Gets the channels playing sounds from this SoundContainer.
142148
/// </summary>
143149
/// <returns>The channels currently being used.</returns>
144-
std::unordered_set<short int> GetPlayingChannels() { return m_PlayingChannels; }
150+
std::unordered_set<unsigned short int> *GetPlayingChannels() { return &m_PlayingChannels; }
145151

146152
/// <summary>
147153
/// Adds a channel index to the SoundContainer's collection of playing channels.
148154
/// </summary>
149155
/// <param name="channel">The channel index to add.</param>
150-
void AddPlayingChannel(int channel) { m_PlayingChannels.insert(channel); }
156+
void AddPlayingChannel(unsigned short int channel) { m_PlayingChannels.insert(channel); RTEAssert(m_PlayingChannels.size() <= c_MaxPlayingSoundsPerContainer, "Tried to play more than " + std::to_string(c_MaxPlayingSoundsPerContainer) + " sounds in SoundContainer " + GetPresetName()); }
151157

152158
/// <summary>
153159
/// Removes a channel index from the SoundContainer's collection of playing channels.
154160
/// </summary>
155161
/// <param name="channel">The channel index to remove.</param>
156-
void RemovePlayingChannel(int channel) { m_PlayingChannels.erase(channel); }
162+
void RemovePlayingChannel(unsigned short int channel) { m_PlayingChannels.erase(channel); }
157163

158164
/// <summary>
159165
/// Updates the distance attenuation of the SoundContainer's sounds while they're playing.
@@ -198,12 +204,6 @@ namespace RTE {
198204
/// </summary>
199205
/// <param name="pitched">Whether the sounds in this SoundContainer should be affected by global pitch or not.</param>
200206
void SetAffectedByGlobalPitch(bool affectedByGlobalPitch) { m_AffectedByGlobalPitch = affectedByGlobalPitch; }
201-
202-
/// <summary>
203-
/// Gets the hash for this SoundContainer, used for multiplayer data
204-
/// </summary>
205-
/// <returns>The hash for this SoundContainer</returns>
206-
size_t GetHash() { return m_Hash; }
207207
#pragma endregion
208208

209209
#pragma region Playback Controls
@@ -239,36 +239,33 @@ namespace RTE {
239239
/// </summary>
240240
/// <param name="player">Player to stop playback of this SoundContainer for.</param>
241241
/// <returns>Whether this SoundContainer successfully stopped playing.</returns>
242-
bool Stop(int player) { return HasAnySounds() ? g_AudioMan.StopSound(this, player) : false; }
242+
bool Stop(int player) { return (HasAnySounds() && IsBeingPlayed()) ? g_AudioMan.StopSound(this, player) : false; }
243243

244244
/// <summary>
245-
/// Selects, saves and returns the next sound of this SoundContainer to be played.
245+
/// Selects the next sounds of this SoundContainer to be played.
246246
/// </summary>
247-
/// <returns>A pointer to the sound to be played. Ownership is NOT transferred.</returns>
248-
FMOD::Sound * SelectNextSound();
247+
bool SelectNextSounds();
249248

250249
/// <summary>
251250
/// Fades out playback of the SoundContainer to 0 volume.
252251
/// </summary>
253252
/// <param name="fadeOutTime">How long the fadeout should take.</param>
254-
void FadeOut(int fadeOutTime = 1000) { if (HasAnySounds()) { return g_AudioMan.FadeOutSound(this, fadeOutTime); } }
253+
void FadeOut(int fadeOutTime = 1000) { if (IsBeingPlayed()) { return g_AudioMan.FadeOutSound(this, fadeOutTime); } }
255254
#pragma endregion
256255

257256
protected:
257+
258258
static Entity::ClassInfo m_sClass; //!< ClassInfo for this class.
259259

260260
std::vector<std::pair<ContentFile, FMOD::Sound *>> m_Sounds; // All the FMOD Sound objects in this SoundContainer, paired with their appropriate ContentFile. The sound objects within are owned by the ContentFile static maps
261+
std::vector<size_t> m_SelectedSounds; //!< Vector of the indices of all selected sounds
261262

262-
//TODO change this to be a list/vector of sounds
263-
int m_CurrentSound; //!< Sound group index of the current (or last played if nothing is being played) sound being played
264-
std::unordered_set<short int> m_PlayingChannels; //!< The channels this SoundContainer is currently using
263+
std::unordered_set<unsigned short int> m_PlayingChannels; //!< The channels this SoundContainer is currently using
265264

266265
int m_Loops; //!< Number of loops (repeats) the SoundContainer's sounds should play when played. 0 means it plays once, -1 means it plays until stopped
267266
int m_Priority; //!< The mixing priority of this SoundContainer's sounds. Higher values are more likely to be heard
268267
bool m_AffectedByGlobalPitch; //!< Whether this SoundContainer's sounds should be able to be altered by global pitch changes
269268

270-
size_t m_Hash; //!< Sound file hash for network transmission.
271-
272269
private:
273270
/// <summary>
274271
/// Clears all the member variables of this SoundContainer, effectively resetting the members of this abstraction level only.

Managers/AudioMan.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ namespace RTE {
258258
FMOD_RESULT result;
259259
FMOD::Channel *soundChannel;
260260

261-
std::unordered_set<short int> channels = pSoundContainer->GetPlayingChannels();
262-
for (std::unordered_set<short int>::iterator channelIterator = channels.begin(); channelIterator != channels.end(); ++channelIterator) {
261+
std::unordered_set<unsigned short int> const *channels = pSoundContainer->GetPlayingChannels();
262+
for (std::unordered_set<unsigned short int>::iterator channelIterator = channels->begin(); channelIterator != channels->end(); ++channelIterator) {
263263
result = m_AudioSystem->getChannel((*channelIterator), &soundChannel);
264264
if (result == FMOD_OK) {
265265
soundChannel->setPitch(pitch);
@@ -471,8 +471,8 @@ namespace RTE {
471471
bool anySoundsPlaying = pSoundContainer->IsBeingPlayed();
472472

473473
if (anySoundsPlaying) {
474-
std::unordered_set<short int> channels = pSoundContainer->GetPlayingChannels();
475-
for (std::unordered_set<short int>::iterator channelIterator = channels.begin(); channelIterator != channels.end(); ++channelIterator) {
474+
std::unordered_set<unsigned short int> const *channels = pSoundContainer->GetPlayingChannels();
475+
for (std::unordered_set<unsigned short int>::iterator channelIterator = channels->begin(); channelIterator != channels->end();) {
476476
result = m_AudioSystem->getChannel((*channelIterator), &soundChannel);
477477
result = result == FMOD_OK ? soundChannel->stop() : result;
478478
if (result != FMOD_OK) {
@@ -509,8 +509,8 @@ namespace RTE {
509509
unsigned long long parentClock;
510510
float currentVolume;
511511

512-
std::unordered_set<short int> channels = pSoundContainer->GetPlayingChannels();
513-
for (std::unordered_set<short int>::iterator channelIterator = channels.begin(); channelIterator != channels.end(); ++channelIterator) {
512+
std::unordered_set<unsigned short int> const *channels = pSoundContainer->GetPlayingChannels();
513+
for (std::unordered_set<unsigned short int>::iterator channelIterator = channels->begin(); channelIterator != channels->end(); ++channelIterator) {
514514
result = m_AudioSystem->getChannel((*channelIterator), &soundChannel);
515515
result = result == FMOD_OK ? soundChannel->getDSPClock(nullptr, &parentClock) : result;
516516
result = result == FMOD_OK ? soundChannel->getVolume(&currentVolume) : result;

System/Constants.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ namespace RTE {
3333
static constexpr float c_SixteenthPI = 0.1963495;
3434
#pragma endregion
3535

36+
#pragma region Audio Constants
37+
static constexpr unsigned short int c_NumberOfAudioChannels = 512;
38+
#pragma endregion
39+
3640
#pragma region Network Constants
3741
static constexpr unsigned short int c_MaxClients = 4;
3842
#pragma endregion

0 commit comments

Comments
 (0)