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

Commit ba371ea

Browse files
committed
Tweaked network handling for sound data (and slightly for music data)
Made audioman sound event registration take and use the whole sound container, to make things cleaner NetworkSoundData now holds one channel and sound file hash, which is less efficient (but can be made a bit better) but less messy
1 parent 132cc28 commit ba371ea

File tree

5 files changed

+170
-175
lines changed

5 files changed

+170
-175
lines changed

Managers/AudioMan.cpp

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ namespace RTE {
136136
if (!m_AudioEnabled) {
137137
return;
138138
}
139-
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_GLOBAL_PITCH, nullptr, nullptr, Vector(), 0, 0.0F, pitch); }
139+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_GLOBAL_PITCH, nullptr); }
140140

141141
m_GlobalPitch = std::clamp(pitch, 0.125F, 8.0F);
142142
if (includeMusic) { m_MusicChannelGroup->setPitch(m_GlobalPitch); }
@@ -350,7 +350,7 @@ namespace RTE {
350350
if (!m_AudioEnabled || !soundContainer) {
351351
return false;
352352
}
353-
if (m_IsInMultiplayerMode) { RegisterSoundEvent(player, SOUND_STOP, soundContainer->GetPlayingChannels()); }
353+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(player, SOUND_STOP, soundContainer); }
354354

355355
FMOD_RESULT result;
356356
FMOD::Channel *soundChannel;
@@ -374,7 +374,7 @@ namespace RTE {
374374
if (!m_AudioEnabled || !soundContainer || !soundContainer->IsBeingPlayed()) {
375375
return;
376376
}
377-
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_FADE_OUT, soundContainer->GetPlayingChannels(), &soundContainer->GetSelectedSoundHashes(), Vector(), 0, 0, 0, false, 0, false, fadeOutTime); }
377+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_FADE_OUT, soundContainer, fadeOutTime); }
378378

379379
int sampleRate;
380380
m_AudioSystem->getSoftwareFormat(&sampleRate, nullptr, nullptr);
@@ -413,13 +413,13 @@ namespace RTE {
413413

414414
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
415415

416-
void AudioMan::RegisterMusicEvent(int player, NetworkMusicState state, const char *filepath, int loops, float position, float pitch) {
416+
void AudioMan::RegisterMusicEvent(int player, NetworkMusicState state, const char *filepath, int loopsOrSilence, float position, float pitch) {
417417
if (player == -1) {
418-
for (int i = 0; i < c_MaxClients; i++) { RegisterMusicEvent(i, state, filepath, loops, position, pitch); }
418+
for (int i = 0; i < c_MaxClients; i++) { RegisterMusicEvent(i, state, filepath, loopsOrSilence, position, pitch); }
419419
} else {
420420
NetworkMusicData musicData;
421421
musicData.State = state;
422-
musicData.Loops = loops;
422+
musicData.LoopsOrSilence = loopsOrSilence;
423423
musicData.Pitch = pitch;
424424
musicData.Position = position;
425425
if (filepath) {
@@ -440,43 +440,65 @@ namespace RTE {
440440
return;
441441
}
442442
list.clear();
443-
g_SoundEventsListMutex[player].lock();
444443

445-
for (const NetworkSoundData &soundEvent : m_SoundEvents[player]) { list.push_back(soundEvent); }
446-
m_SoundEvents[player].clear();
444+
g_SoundEventsListMutex[player].lock();
445+
const NetworkSoundData *lastSetGlobalPitchEvent = nullptr;
446+
for (const NetworkSoundData &soundEvent : m_SoundEvents[player]) {
447+
if (soundEvent.State == SOUND_SET_GLOBAL_PITCH) {
448+
lastSetGlobalPitchEvent = &soundEvent;
449+
} else {
450+
list.push_back(soundEvent);
451+
}
452+
}
453+
if (lastSetGlobalPitchEvent) { list.push_back(*lastSetGlobalPitchEvent); }
447454
g_SoundEventsListMutex[player].unlock();
448455
}
449456

450457
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
451458

452-
void AudioMan::RegisterSoundEvent(int player, NetworkSoundState state, const std::unordered_set<int> *channels, const std::vector<size_t> *soundFileHashes, const Vector &position, int loops, float volume, float pitch, bool affectedByGlobalPitch, float attenuationStartDistance, bool immobile, int fadeOutTime) {
459+
void AudioMan::RegisterSoundEvent(int player, NetworkSoundState state, const SoundContainer *soundContainer, int fadeoutTime) {
453460
if (player == -1) {
454-
for (int i = 0; i < c_MaxClients; i++) { RegisterSoundEvent(i, state, channels, soundFileHashes, position, loops, volume, pitch, affectedByGlobalPitch, attenuationStartDistance, immobile, fadeOutTime); }
461+
for (int i = 0; i < c_MaxClients; i++) { RegisterSoundEvent(i, state, soundContainer, fadeoutTime); }
455462
} else {
456-
if (player >= 0 && player < c_MaxClients) {
463+
FMOD_RESULT result = FMOD_OK;
464+
std::vector<NetworkSoundData> soundDataVector;
465+
466+
if (state == SOUND_SET_GLOBAL_PITCH) {
457467
NetworkSoundData soundData;
458468
soundData.State = state;
459-
460-
std::fill_n(soundData.Channels, c_MaxPlayingSoundsPerContainer, c_MaxVirtualChannels + 1);
461-
if (channels) { std::copy(channels->begin(), channels->end(), soundData.Channels); }
462-
463-
std::fill_n(soundData.SoundFileHashes, c_MaxPlayingSoundsPerContainer, 0);
464-
if (soundFileHashes) { std::copy(soundFileHashes->begin(), soundFileHashes->end(), soundData.SoundFileHashes); }
465-
466-
soundData.Position[0] = position.m_X;
467-
soundData.Position[1] = position.m_Y;
468-
soundData.Loops = loops;
469-
soundData.Volume = volume;
470-
soundData.Pitch = pitch;
471-
soundData.AffectedByGlobalPitch = affectedByGlobalPitch;
472-
soundData.AttenuationStartDistance = attenuationStartDistance;
473-
soundData.Immobile = immobile;
474-
soundData.FadeOutTime = fadeOutTime;
475-
476-
g_SoundEventsListMutex[player].lock();
477-
m_SoundEvents[player].push_back(soundData);
478-
g_SoundEventsListMutex[player].unlock();
469+
soundData.Pitch = m_GlobalPitch;
470+
soundDataVector.push_back(soundData);
471+
} else {
472+
for (int playingChannel : *soundContainer->GetPlayingChannels()) {
473+
FMOD::Channel *soundChannel;
474+
result = m_AudioSystem->getChannel(playingChannel, &soundChannel);
475+
FMOD::Sound *sound;
476+
result = (result == FMOD_OK) ? soundChannel->getCurrentSound(&sound) : result;
477+
478+
if (result != FMOD_OK) {
479+
continue;
480+
}
481+
NetworkSoundData soundData;
482+
soundData.State = state;
483+
soundData.SoundFileHash = soundContainer->GetSoundDataForSound(sound)->SoundFile.GetHash();
484+
soundData.Channel = playingChannel;
485+
soundData.Immobile = soundContainer->IsImmobile();
486+
soundData.AttenuationStartDistance = soundContainer->GetAttenuationStartDistance();
487+
soundData.Loops = soundContainer->GetLoopSetting();
488+
soundData.Priority = soundContainer->GetPriority();
489+
soundData.AffectedByGlobalPitch = soundContainer->IsAffectedByGlobalPitch();
490+
soundData.Position[0] = soundContainer->GetPosition().m_X;
491+
soundData.Position[1] = soundContainer->GetPosition().m_Y;
492+
soundData.Volume = soundContainer->GetVolume();
493+
soundData.Pitch = soundContainer->GetPitch();
494+
soundData.FadeOutTime = fadeoutTime;
495+
soundDataVector.push_back(soundData);
496+
}
479497
}
498+
499+
g_SoundEventsListMutex[player].lock();
500+
m_SoundEvents[player].insert(m_SoundEvents[player].end(), soundDataVector.begin(), soundDataVector.end());
501+
g_SoundEventsListMutex[player].unlock();
480502
}
481503
}
482504

@@ -534,9 +556,7 @@ namespace RTE {
534556
soundContainer->AddPlayingChannel(channelIndex);
535557
}
536558

537-
if (m_IsInMultiplayerMode) {
538-
RegisterSoundEvent(player, SOUND_PLAY, soundContainer->GetPlayingChannels(), &soundContainer->GetSelectedSoundHashes(), soundContainer->GetPosition(), soundContainer->GetLoopSetting(), soundContainer->GetVolume(), soundContainer->GetPitch(), soundContainer->IsAffectedByGlobalPitch(), soundContainer->GetAttenuationStartDistance(), soundContainer->IsImmobile());
539-
}
559+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(player, SOUND_PLAY, soundContainer); }
540560
return true;
541561
}
542562

@@ -546,7 +566,7 @@ namespace RTE {
546566
if (!m_AudioEnabled || !soundContainer) {
547567
return false;
548568
}
549-
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_POSITION, soundContainer->GetPlayingChannels(), &soundContainer->GetSelectedSoundHashes(), soundContainer->GetPosition()); }
569+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_POSITION, soundContainer); }
550570

551571
FMOD_RESULT result = FMOD_OK;
552572
FMOD::Channel *soundChannel;
@@ -573,7 +593,7 @@ namespace RTE {
573593
if (!m_AudioEnabled || !soundContainer || !soundContainer->IsBeingPlayed()) {
574594
return false;
575595
}
576-
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_VOLUME, soundContainer->GetPlayingChannels(), &soundContainer->GetSelectedSoundHashes(), Vector(), 0, soundContainer->GetVolume()); }
596+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_VOLUME, soundContainer); }
577597

578598
FMOD_RESULT result = FMOD_OK;
579599
FMOD::Channel *soundChannel;
@@ -605,7 +625,7 @@ namespace RTE {
605625
if (!m_AudioEnabled || !soundContainer || !soundContainer->IsBeingPlayed()) {
606626
return false;
607627
}
608-
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_PITCH, soundContainer->GetPlayingChannels(), &soundContainer->GetSelectedSoundHashes(), Vector(), 0, 0.0F, soundContainer->GetPitch()); }
628+
if (m_IsInMultiplayerMode) { RegisterSoundEvent(-1, SOUND_SET_PITCH, soundContainer); }
609629

610630
FMOD_RESULT result = FMOD_OK;
611631
FMOD::Channel *soundChannel;

Managers/AudioMan.h

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace RTE {
4949
struct NetworkMusicData {
5050
unsigned char State;
5151
char Path[256];
52-
int Loops;
52+
int LoopsOrSilence;
5353
float Position;
5454
float Pitch;
5555
};
@@ -58,12 +58,12 @@ namespace RTE {
5858
/// Sound event states for sending sound data from the server to clients during multiplayer games.
5959
/// </summary>
6060
enum NetworkSoundState {
61-
SOUND_PLAY = 0,
61+
SOUND_SET_GLOBAL_PITCH = 0,
62+
SOUND_PLAY,
6263
SOUND_STOP,
6364
SOUND_SET_POSITION,
6465
SOUND_SET_VOLUME,
6566
SOUND_SET_PITCH,
66-
SOUND_SET_GLOBAL_PITCH,
6767
SOUND_FADE_OUT
6868
};
6969

@@ -72,15 +72,16 @@ namespace RTE {
7272
/// </summary>
7373
struct NetworkSoundData {
7474
unsigned char State;
75-
int Channels[c_MaxPlayingSoundsPerContainer];
76-
size_t SoundFileHashes[c_MaxPlayingSoundsPerContainer];
77-
float Position[2];
75+
std::size_t SoundFileHash;
76+
int Channel;
77+
bool Immobile;
78+
float AttenuationStartDistance;
7879
int Loops;
80+
int Priority;
81+
bool AffectedByGlobalPitch;
82+
float Position[2];
7983
float Volume;
8084
float Pitch;
81-
bool AffectedByGlobalPitch;
82-
float AttenuationStartDistance;
83-
bool Immobile;
8485
int FadeOutTime;
8586
};
8687

@@ -348,34 +349,26 @@ namespace RTE {
348349
/// <param name="player">Player(s) for which the event happened.</param>
349350
/// <param name="state">NetworkMusicState for the event.</param>
350351
/// <param name="filepath">Music file path to transmit to client.</param>
351-
/// <param name="loops">Loops counter.</param>
352+
/// <param name="loops">LoopsOrSilence counter or, if state is silence, the length of the silence.</param>
352353
/// <param name="position">Music playback position.</param>
353354
/// <param name="pitch">Pitch value.</param>
354-
void RegisterMusicEvent(int player, NetworkMusicState state, const char *filepath, int loops = 0, float position = 0, float pitch = 1);
355+
void RegisterMusicEvent(int player, NetworkMusicState state, const char *filepath, int loopsOrSilence = 0, float position = 0, float pitch = 1);
355356

356357
/// <summary>
357358
/// Fills the list with sound events happened for the specified network player.
358359
/// </summary>
359360
/// <param name="player">Player to get events for.</param>
360361
/// <param name="list">List with events for this player.</param>
361-
void GetSoundEvents(int player, std::list<NetworkSoundData> & list);
362+
void GetSoundEvents(int player, std::list<NetworkSoundData> &list);
362363

363364
/// <summary>
364-
/// Adds the sound event to internal list of sound events for the specified player.
365+
/// Adds the sound event to the internal list of sound events for the specified player.
365366
/// </summary>
366367
/// <param name="player">Player(s) for which the event happened.</param>
367-
/// <param name="state">NetworkSoundState for the event .</param>
368-
/// <param name="channels">Pointer to an unordered_set of channels this sound event applies to on the server.</param>
369-
/// <param name="soundFileHashes">Pointer to a vector of hashes describing sound file locations to transmit to client.</param>
370-
/// <param name="position">Sound position.</param>
371-
/// <param name="loops">Loops counter.</param>
372-
/// <param name="volume">Volume value.</param>
373-
/// <param name="pitch">Pitch value.</param>
374-
/// <param name="attenuationStartDistance">The distance at which the sound will start attenuating away.</param>
375-
/// <param name="affectedByGlobalPitch">Whether the sound is affected by pitch.</param>
376-
/// <param name="immobile">Whether the sound is immobile or not.</param>
377-
/// <param name="fadeOutTime">The amount of time, in ms, to fade out over.</param>
378-
void RegisterSoundEvent(int player, NetworkSoundState state, const std::unordered_set<int> *channels = nullptr, const std::vector<size_t> *soundFileHashes = nullptr, const Vector &position = Vector(), int loops = 0, float volume = 1.0F, float pitch = 1.0F, bool affectedByGlobalPitch = false, float attenuationStartDistance = 0, bool immobile = false, int fadeOutTime = 0);
368+
/// <param name="state">NetworkSoundState for the event.</param>
369+
/// <param name="soundContainer">A pointer to the SoundContainer this event is happening to, or a null pointer for global events.</param>
370+
/// <param name="fadeOutTime">THe amount of time, in MS, to fade out over. This data isn't contained in SoundContainer, so it needs to be passed in separately.</param>
371+
void RegisterSoundEvent(int player, NetworkSoundState state, const SoundContainer *soundContainer, int fadeOutTime = 0);
379372
#pragma endregion
380373

381374
#pragma region Class Info

0 commit comments

Comments
 (0)