Skip to content

Commit

Permalink
Merge pull request #55 from TwinFan/next
Browse files Browse the repository at this point in the history
v3.0.1 Fix Audio Crackling
  • Loading branch information
TwinFan authored Dec 10, 2022
2 parents 04b4952 + 1334fea commit 50309d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
10 changes: 3 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ else()
endif()

project(XPMP2
VERSION 3.0.0
VERSION 3.0.1
DESCRIPTION "Multiplayer library for X-Plane 11 and 12")

# Provide compile macros from the above project version definition
Expand Down Expand Up @@ -120,13 +120,9 @@ else()
if(MSVC)
# Fast code, symbols into separate .pdb file, no global optimization (as it produces huge files)
add_compile_options(/Zi /O2 /GL-)
elseif(APPLE)
add_compile_options(-O3 -fPIC)
elseif (UNIX OR MINGW)
# Use position-independent code and highest optimization level (FPS!).
else()
# Use position-independent code and highest optimization level
add_compile_options(-O3 -fPIC)
# Strip symbols during linking
add_link_options(-s)
endif()
endif()

Expand Down
42 changes: 20 additions & 22 deletions src/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ constexpr int EXP_COMP_SKIP_CYCLES = 10; ///< In how many cycles to s
constexpr int FMOD_NUM_VIRT_CHANNELS = 1000; ///< Number of virtual channels during initialization
constexpr float FMOD_3D_MAX_DIST = 10000.0f; ///< Value used for 3D max distance, which doesn't have much of a function for the inverse roll-off model used here
constexpr float FMOD_LOW_PASS_GAIN = 0.2f; ///< Gain used when activating Low Pass filter
constexpr int FMOD_NEW_SOUNDS_PER_FRAME = 1; ///< How many new sounds do we allow per frame (to reduce risk of cracks)?

static FMOD_SYSTEM* gpFmodSystem = nullptr; ///< FMOD system
static unsigned int gFmodVer = 0; ///< FMOD version
static int gbFmodSoundsThisFrame = 0; ///< How many sounds did we create this frame already?

/// Use pre-v2 FMOD version structures?
inline bool UseOldFmod() { return gFmodVer < 0x00020000; }
Expand Down Expand Up @@ -456,7 +454,8 @@ FMOD_CHANNEL* Aircraft::SoundPlay (const std::string& sndName, float vol)
// Have FMOD create the sound
try {
SoundFile& snd = mapSound.at(sndName);
FMOD_CHANNEL* pChn = snd.play();
// start paused to avoid cracking, will be unpaused only in the next call to Aircraft::SoundUpdate()
FMOD_CHANNEL* pChn = snd.play(nullptr, true);
if (pChn) {
chnList.push_back(pChn); // add to managed list of sounds
FMOD_LOG(FMOD_Channel_Set3DMinMaxDistance(pChn, (float)sndMinDist, FMOD_3D_MAX_DIST));
Expand All @@ -468,7 +467,6 @@ FMOD_CHANNEL* Aircraft::SoundPlay (const std::string& sndName, float vol)
if (bChnMuted) { // if currently muted, then mute
FMOD_LOG(FMOD_Channel_SetMute(pChn, true));
}
FMOD_LOG(FMOD_Channel_SetPaused(pChn, false)); // un-pause
}
return pChn;
}
Expand Down Expand Up @@ -620,18 +618,15 @@ void Aircraft::SoundUpdate ()
float vol = std::clamp<float>((fVal - def.valMin) / (def.valMax - def.valMin), 0.0f, 1.0f);
// If there hasn't been a sound triggered do so now
if (!sndCh.pChn) {
// Are we allowed to add more sounds?
if (++gbFmodSoundsThisFrame <= FMOD_NEW_SOUNDS_PER_FRAME) {
// Get Sound's name and volume adjustment
const std::string sndName = SoundGetName(eSndEvent, sndCh.volAdj);
if (!sndName.empty()) {
vol *= sndCh.volAdj;
sndCh.pChn = SoundPlay(sndName, vol);
if (sndCh.pChn) {
LOG_MATCHING(logINFO, "Aircraft %08X (%s): Looping sound '%s' at volume %.2f for '%s'",
modeS_id, GetFlightId().c_str(),
sndName.c_str(), vol, SoundEventTxt(eSndEvent));
}
// Get Sound's name and volume adjustment
const std::string sndName = SoundGetName(eSndEvent, sndCh.volAdj);
if (!sndName.empty()) {
vol *= sndCh.volAdj;
sndCh.pChn = SoundPlay(sndName, vol);
if (sndCh.pChn) {
LOG_MATCHING(logINFO, "Aircraft %08X (%s): Looping sound '%s' at volume %.2f for '%s'",
modeS_id, GetFlightId().c_str(),
sndName.c_str(), vol, SoundEventTxt(eSndEvent));
}
}
} else {
Expand Down Expand Up @@ -661,9 +656,7 @@ void Aircraft::SoundUpdate ()
{
sndCh.lastDRVal = fVal; // Remember this current value
// If there hasn't been a sound triggered do so now
if (!sndCh.pChn &&
// And we are allowed to add more this frame
++gbFmodSoundsThisFrame <= FMOD_NEW_SOUNDS_PER_FRAME)
if (!sndCh.pChn)
{
const std::string sndName = SoundGetName(eSndEvent, sndCh.volAdj);
if (!sndName.empty()) {
Expand Down Expand Up @@ -726,6 +719,12 @@ void Aircraft::SoundUpdate ()
if (pSnd && pSnd->hasConeInfo())
pSnd->setConeOrientation(*iter, *this);
}
// Unpause the channel if still paused (always starts paused to avoid cracking)
FMOD_BOOL bPaused = false;
FMOD_LOG(FMOD_Channel_GetPaused(*iter, &bPaused));
if (bPaused) {
FMOD_LOG(FMOD_Channel_SetPaused(*iter, false));
}
// Next sound channel
iter++;
}
Expand Down Expand Up @@ -812,8 +811,7 @@ bool SoundInit ()
// Prepare for this frame's updates, which are about to start
void SoundUpdatesBegin()
{
// Reset counter of allowed new sounds
gbFmodSoundsThisFrame = 0;
// Currently, there's nothing in here
}

// Tell FMOD that all updates are done
Expand Down Expand Up @@ -980,4 +978,4 @@ const char* XPMPSoundEnumerate (const char* prevName, const char** ppFilePath)
}
}

#endif // INCLUDE_FMOD_SOUND
#endif // INCLUDE_FMOD_SOUND

0 comments on commit 50309d3

Please sign in to comment.