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

Commit 7c59e25

Browse files
committed
Reorganized NetworkSoundEnum and Data positioning in AudioMan header, added a couple missing states. Reorganized NetworkSoundData so it's consistent and changed some of its types around slightly.
Reorganized RegisterMusicEvent and RegisterSoundEvent so they're consistent and changed all callers accordingly Updated RegisterSoundEvent so it copies channels and sound hashes properly, and generally works correctly. Updated NetworkServer to match with these changes.
1 parent 3f7bb3e commit 7c59e25

File tree

3 files changed

+56
-43
lines changed

3 files changed

+56
-43
lines changed

Managers/AudioMan.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ namespace RTE {
120120
return;
121121
}
122122

123-
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_PITCH, 0, 0, std::unordered_set<short int>(), 0, pitch, excludeMusic); }
123+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_GLOBAL_PITCH, NULL, NULL, 0, 0, pitch, excludeMusic); }
124124

125125
m_GlobalPitch = Limit(pitch, 8, 0.125); //Limit pitch change to 8 octaves up or down
126126

@@ -226,6 +226,7 @@ namespace RTE {
226226
return false;
227227
}
228228

229+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_ATTENUATION, pSoundContainer->GetPlayingChannels(), &pSoundContainer->GetSelectedSoundHashes(), attenuation); }
229230

230231
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)
231232

@@ -251,9 +252,7 @@ namespace RTE {
251252
return false;
252253
}
253254

254-
if (IsInMultiplayerMode() && pSoundContainer) {
255-
RegisterSoundEvent(-1, SOUND_SET_PITCH, pSoundContainer->GetHash(), 0, pSoundContainer->GetPlayingChannels(), pSoundContainer->GetLoopSetting(), pitch, pSoundContainer->IsAffectedByGlobalPitch());
256-
}
255+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_PITCH, pSoundContainer->GetPlayingChannels(), &pSoundContainer->GetSelectedSoundHashes(), 0, 0, pitch); }
257256

258257
pitch = Limit(pitch, 8, 0.125); //Limit pitch change to 8 octaves up or down
259258

@@ -274,7 +273,7 @@ namespace RTE {
274273

275274
void AudioMan::PlayMusic(const char *filePath, int loops, double volumeOverrideIfNotMuted) {
276275
if (m_AudioEnabled) {
277-
if (m_IsInMultiplayerMode) { RegisterMusicEvent(-1, MUSIC_PLAY, filePath, loops, 0.0, 1.0); }
276+
if (m_IsInMultiplayerMode) { RegisterMusicEvent(-1, MUSIC_PLAY, filePath, loops); }
278277

279278
FMOD_RESULT result = m_MusicChannelGroup->stop();
280279
if (result != FMOD_OK) {
@@ -343,7 +342,7 @@ namespace RTE {
343342
bool isPlaying;
344343
FMOD_RESULT result = m_MusicChannelGroup->isPlaying(&isPlaying);
345344
if (result == FMOD_OK && isPlaying) {
346-
RegisterMusicEvent(-1, MUSIC_SILENCE, 0, seconds, 0.0, 1.0);
345+
if (m_IsInMultiplayerMode) { RegisterMusicEvent(-1, MUSIC_SILENCE, NULL, seconds); }
347346
result = m_MusicChannelGroup->stop();
348347
}
349348
if (result != FMOD_OK) {
@@ -462,7 +461,7 @@ namespace RTE {
462461

463462
// 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.
464463
if (m_IsInMultiplayerMode) {
465-
RegisterSoundEvent(player, SOUND_PLAY, pSoundContainer->GetHash(), attenuation, pSoundContainer->GetPlayingChannels(), pSoundContainer->GetLoopSetting(), pitch, pSoundContainer->IsAffectedByGlobalPitch());
464+
RegisterSoundEvent(player, SOUND_PLAY, pSoundContainer->GetPlayingChannels(), &pSoundContainer->GetSelectedSoundHashes(), attenuation, pSoundContainer->GetLoopSetting(), pitch, pSoundContainer->IsAffectedByGlobalPitch());
466465
}
467466

468467
return true;
@@ -475,6 +474,8 @@ namespace RTE {
475474
return false;
476475
}
477476

477+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(player, SOUND_STOP, pSoundContainer->GetPlayingChannels()); }
478+
478479
FMOD_RESULT result;
479480
FMOD::Channel *soundChannel;
480481
bool anySoundsPlaying = pSoundContainer->IsBeingPlayed();
@@ -502,6 +503,8 @@ namespace RTE {
502503
return;
503504
}
504505

506+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_FADE_OUT, pSoundContainer->GetPlayingChannels(), &pSoundContainer->GetSelectedSoundHashes(), 0, 0, 0, false, fadeOutTime); }
507+
505508
int sampleRate;
506509
m_AudioSystem->getSoftwareFormat(&sampleRate, nullptr, nullptr);
507510
int fadeOutTimeAsSamples = fadeOutTime * sampleRate / 1000;
@@ -544,7 +547,7 @@ namespace RTE {
544547

545548
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
546549

547-
void AudioMan::RegisterMusicEvent(int player, unsigned char state, const char *filepath, int loops, double position, float pitch) {
550+
void AudioMan::RegisterMusicEvent(int player, NetworkMusicState state, const char *filepath, int loops, double position, float pitch) {
548551
if (player == -1) {
549552
for (int i = 0; i < c_MaxClients; i++) {
550553
RegisterMusicEvent(i, state, filepath, loops, position, pitch);
@@ -587,21 +590,26 @@ namespace RTE {
587590

588591
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
589592

590-
void AudioMan::RegisterSoundEvent(int player, unsigned char state, size_t hash, short int attenuation, std::unordered_set<short int> const &channels, short int loops, float pitch, bool affectedByPitch) {
593+
void AudioMan::RegisterSoundEvent(int player, NetworkSoundState state, std::unordered_set<unsigned short int> const *channels, std::vector<size_t> const *soundFileHashes, short int attenuation, short int loops, float pitch, bool affectedByGlobalPitch, short int fadeOutTime) {
591594
if (player == -1) {
592595
for (int i = 0; i < c_MaxClients; i++) {
593-
RegisterSoundEvent(i, state, hash, attenuation, channels, loops, pitch, affectedByPitch);
596+
RegisterSoundEvent(i, state, channels, soundFileHashes, attenuation, loops, pitch, affectedByGlobalPitch, fadeOutTime);
594597
}
595598
} else {
596599
if (player >= 0 && player < c_MaxClients) {
597600
NetworkSoundData soundData;
598601
soundData.State = state;
602+
if (channels) {
603+
std::copy(channels->begin(), channels->end(), soundData.Channels);
604+
}
605+
if (soundFileHashes) {
606+
std::copy(soundFileHashes->begin(), soundFileHashes->end(), soundData.SoundFileHashes);
607+
}
599608
soundData.Distance = attenuation;
600-
soundData.SoundHash = hash;
601-
soundData.Channels = channels;
602609
soundData.Loops = loops;
603610
soundData.Pitch = pitch;
604-
soundData.AffectedByPitch = affectedByPitch ? 1 : 0;
611+
soundData.AffectedByGlobalPitch = affectedByGlobalPitch;
612+
soundData.FadeOutTime = fadeOutTime;
605613

606614
g_SoundEventsListMutex[player].lock();
607615
m_SoundEvents[player].push_back(soundData);

Managers/AudioMan.h

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,6 @@ namespace RTE {
3030
PRIORITY_COUNT
3131
};
3232

33-
enum NetworkSoundState {
34-
SOUND_PLAY = 0,
35-
SOUND_STOP,
36-
SOUND_SET_PITCH,
37-
SOUND_SET_ATTENUATION
38-
};
39-
40-
struct NetworkSoundData {
41-
unsigned char State;
42-
size_t SoundHash;
43-
unsigned char AffectedByPitch;
44-
short int Distance;
45-
std::unordered_set<short int> Channels;
46-
short int Loops;
47-
float Pitch;
48-
};
49-
5033
enum NetworkMusicState {
5134
MUSIC_PLAY = 0,
5235
MUSIC_STOP,
@@ -62,6 +45,26 @@ namespace RTE {
6245
float Pitch;
6346
};
6447

48+
enum NetworkSoundState {
49+
SOUND_PLAY = 0,
50+
SOUND_STOP,
51+
SOUND_SET_ATTENUATION,
52+
SOUND_SET_PITCH,
53+
SOUND_SET_GLOBAL_PITCH,
54+
SOUND_FADE_OUT
55+
};
56+
57+
struct NetworkSoundData {
58+
unsigned char State;
59+
unsigned short int Channels[c_MaxPlayingSoundsPerContainer]{c_NumberOfAudioChannels + 1};
60+
size_t SoundFileHashes[c_NumberOfAudioChannels]{0};
61+
short int Distance;
62+
short int Loops;
63+
float Pitch;
64+
bool AffectedByGlobalPitch;
65+
short int FadeOutTime;
66+
};
67+
6568
#pragma region Creation
6669
/// <summary>
6770
/// Constructor method used to instantiate a AudioMan object in system memory.
@@ -355,13 +358,13 @@ namespace RTE {
355358
/// <summary>
356359
/// Adds the music event to internal list of music events for the specified player.
357360
/// </summary>
358-
/// <param name="player">Player for which the event happened.</param>
359-
/// <param name="state">Music state.</param>
361+
/// <param name="player">Player(s) for which the event happened.</param>
362+
/// <param name="state">NetworkMusicState for the event.</param>
360363
/// <param name="filepath">Music file path to transmit to client.</param>
361364
/// <param name="loops">Loops counter.</param>
362365
/// <param name="position">Music playback position.</param>
363366
/// <param name="pitch">Pitch value.</param>
364-
void RegisterMusicEvent(int player, unsigned char state, const char *filepath, int loops, double position, float pitch);
367+
void RegisterMusicEvent(int player, NetworkMusicState state, const char *filepath, int loops = 0, double position = 0, float pitch = 1);
365368

366369
/// <summary>
367370
/// Fills the list with sound events happened for the specified network player.
@@ -373,15 +376,16 @@ namespace RTE {
373376
/// <summary>
374377
/// Adds the sound event to internal list of sound events for the specified player.
375378
/// </summary>
376-
/// <param name="player">Player for which the event happened.</param>
377-
/// <param name="state">Sound state.</param>
378-
/// <param name="hash">Sound file hash to transmit to client.</param>
379-
/// <param name="distance">Sound distance.</param>
380-
/// <param name="channels">Channels where sound was played.</param>
379+
/// <param name="player">Player(s) for which the event happened.</param>
380+
/// <param name="state">NetworkSoundState for the event .</param>
381+
/// <param name="channels">Pointer to an unordered_set of channels this sound event applies to on the server.</param>
382+
/// <param name="soundFileHashes">Pointer to a vector of hashes describing sound file locations to transmit to client.</param>
383+
/// <param name="attenuation">Sound attenuation.</param>
381384
/// <param name="loops">Loops counter.</param>
382385
/// <param name="pitch">Pitch value.</param>
383-
/// <param name="affectedByPitch">Whether the sound is affected by pitch.</param>
384-
void RegisterSoundEvent(int player, unsigned char state, size_t hash, short int distance, std::unordered_set<short int> const &channels, short int loops, float pitch, bool affectedByPitch);
386+
/// <param name="affectedByGlobalPitch">Whether the sound is affected by pitch.</param>
387+
/// <param name="fadeOutTime">The amount of time, in ms, to fade out over.</param>
388+
void RegisterSoundEvent(int player, NetworkSoundState state, std::unordered_set<unsigned short int> const *channels = NULL, std::vector<size_t> const *soundFileHashes = NULL, short int attenuation = 0, short int loops = 0, float pitch = 1, bool affectedByGlobalPitch = false, short int fadeOutTime = 0);
385389
#pragma endregion
386390

387391
protected:

Managers/NetworkServer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,12 +1226,13 @@ namespace RTE
12261226
for (std::list<AudioMan::NetworkSoundData>::iterator eItr = events.begin(); eItr != events.end(); ++eItr)
12271227
{
12281228
sndDataPtr->State = (*eItr).State;
1229-
sndDataPtr->Channels = (*eItr).Channels;
1229+
std::copy(std::begin(eItr->Channels), std::end(eItr->Channels), sndDataPtr->Channels);
1230+
std::copy(std::begin(eItr->SoundFileHashes), std::end(eItr->SoundFileHashes), sndDataPtr->SoundFileHashes);
12301231
sndDataPtr->Distance = (*eItr).Distance;
1231-
sndDataPtr->SoundHash = (*eItr).SoundHash;
12321232
sndDataPtr->Loops = (*eItr).Loops;
12331233
sndDataPtr->Pitch = (*eItr).Pitch;
1234-
sndDataPtr->AffectedByPitch = (*eItr).AffectedByPitch;
1234+
sndDataPtr->AffectedByGlobalPitch = (*eItr).AffectedByGlobalPitch;
1235+
sndDataPtr->FadeOutTime = (*eItr).FadeOutTime;
12351236

12361237
msg->SoundEventsCount++;
12371238
sndDataPtr++;

0 commit comments

Comments
 (0)