Skip to content
Permalink
Browse files

fix #6291

end sound{Start,Hit*}-volume babysitting
  • Loading branch information...
rtri
rtri committed Sep 4, 2019
1 parent a6cdd6d commit 79fcf9b7bb1f57f4ea87b407857b71a71b912283
@@ -58,8 +58,6 @@ CR_REG_METADATA(CWeapon, (
CR_MEMBER(sprayAngle),
CR_MEMBER(predictSpeedMod),

CR_MEMBER(fireSoundId),
CR_MEMBER(fireSoundVolume),
CR_MEMBER(hasBlockShot),
CR_MEMBER(hasTargetWeight),
CR_MEMBER(angleGood),
@@ -179,9 +177,8 @@ CWeapon::CWeapon(CUnit* owner, const WeaponDef* def):
salvoError(ZeroVector),
errorVector(ZeroVector),
errorVectorAdd(ZeroVector),
muzzleFlareSize(1),
fireSoundId(0),
fireSoundVolume(0)

muzzleFlareSize(1)
{
assert(weaponMemPool.alloced(this));
}
@@ -1098,8 +1095,8 @@ void CWeapon::Fire(bool scriptCall)
if (qfHasUnit)
quadField.RemoveUnitIf(currentTarget.unit, currentTargetPos);

if (fireSoundId > 0 && (salvoLeft == (salvoSize - 1) || !weaponDef->soundTrigger))
Channels::Battle->PlaySample(fireSoundId, owner, fireSoundVolume);
if (salvoLeft == (salvoSize - 1) || !weaponDef->soundTrigger)
Channels::Battle->PlayRandomSample(weaponDef->fireSound, owner);
}


@@ -196,8 +196,6 @@ class CWeapon : public CObject
float3 errorVectorAdd;

float muzzleFlareSize; // size of muzzle flare if drawn
int fireSoundId;
float fireSoundVolume;

protected:
SWeaponTarget currentTarget;
@@ -501,7 +501,7 @@ WeaponDef::WeaponDef(const LuaTable& wdTable, const std::string& name_, int id_)
wdTable.SubTable("customParams").GetMap(customParams);

// internal only
isNulled = (strcasecmp(name.c_str(), "noweapon") == 0);
isNulled = (STRCASECMP(name.c_str(), "noweapon") == 0);
isShield = (type == "Shield");
noAutoTarget = (manualfire || interceptor || isShield);
onlyForward = !turret && (projectileType != WEAPON_STARBURST_PROJECTILE);
@@ -513,32 +513,6 @@ void WeaponDef::ParseWeaponSounds(const LuaTable& wdTable) {
LoadSound(wdTable, "soundStart" , fireSound);
LoadSound(wdTable, "soundHitDry", hitSound);
LoadSound(wdTable, "soundHitWet", hitSound);

// FIXME: do we still want or need any of this?
const bool forceSetVolume =
(fireSound.getVolume(0) == -1.0f) ||
( hitSound.getVolume(0) == -1.0f) ||
( hitSound.getVolume(1) == -1.0f);

if (!forceSetVolume)
return;

if (damages.GetDefault() <= 50.0f) {
fireSound.setVolume(0, 5.0f);
hitSound.setVolume(0, 5.0f);
hitSound.setVolume(1, 5.0f);
return;
}

const float fireSoundVolume = math::sqrt(damages.GetDefault() * 0.5f) * ((type == "LaserCannon")? 0.5f: 1.0f);
const float hitSoundVolume = fireSoundVolume;

if (fireSound.getVolume(0) == -1.0f)
fireSound.setVolume(0, fireSoundVolume);
if (hitSound.getVolume(0) == -1.0f)
hitSound.setVolume(0, hitSoundVolume);
if (hitSound.getVolume(1) == -1.0f)
hitSound.setVolume(1, hitSoundVolume);
}


@@ -149,12 +149,6 @@ void CWeaponLoader::InitWeapon(CUnit* owner, CWeapon* weapon, const UnitDefWeapo
weapon->salvoDelay = int(weaponDef->salvodelay * GAME_SPEED);
weapon->projectilesPerShot = weaponDef->projectilespershot;

// TODO?
// weapon->dryHitSoundId = weaponDef->hitSound.getID(0);
// weapon->wetHitSoundId = weaponDef->hitSound.getID(1);
weapon->fireSoundId = weaponDef->fireSound.getID(0);
weapon->fireSoundVolume = weaponDef->fireSound.getVolume(0);

weapon->onlyForward = weaponDef->onlyForward;
weapon->maxForwardAngleDif = math::cos(weaponDef->maxAngle);
weapon->maxMainDirAngleDif = defWeapon->maxMainDirAngleDif;
@@ -42,7 +42,7 @@ class IAudioChannel {
virtual void PlaySample(size_t id, const CWorldObject* p, float volume = 1.0f) = 0;

virtual void PlayRandomSample(const GuiSoundSet& soundSet, const CWorldObject* obj) = 0;
virtual void PlayRandomSample(const GuiSoundSet& soundSet, const float3& pos) = 0;
virtual void PlayRandomSample(const GuiSoundSet& soundSet, const float3& pos, const float3& vel = ZeroVector) = 0;

/**
* @brief Start playing an ogg-file
@@ -68,8 +68,8 @@ class IAudioChannel {

protected:
virtual void FindSourceAndPlay(size_t id, const float3& p, const float3& velocity, float volume, bool relative) = 0;

virtual void SoundSourceFinished(CSoundSource* sndSource) = 0;

friend class CSoundSource;

public:
@@ -20,7 +20,7 @@ class NullAudioChannel : public IAudioChannel {
void PlaySample(size_t id, const CWorldObject* p, float volume = 1.0f) {}

void PlayRandomSample(const GuiSoundSet& soundSet, const CWorldObject* obj) {}
void PlayRandomSample(const GuiSoundSet& soundSet, const float3& pos) {}
void PlayRandomSample(const GuiSoundSet& soundSet, const float3& pos, const float3& vel = ZeroVector) {}

void StreamPlay(const std::string& path, float volume = 1.0f, bool enqueue = false) {}

@@ -30,9 +30,9 @@ class NullAudioChannel : public IAudioChannel {
float StreamGetPlayTime() { return 0.f; }

protected:
void FindSourceAndPlay(size_t id, const float3& p, const float3& velocity, float volume, bool relative) {}
void FindSourceAndPlay(size_t id, const float3& p, const float3& velocity, float volume, bool relative) override {}
void SoundSourceFinished(CSoundSource* sndSource) override {}

void SoundSourceFinished(CSoundSource* sndSource) {}
friend class CSoundSource;
};

@@ -27,8 +27,8 @@ void AudioChannel::SetVolume(float newVolume)

std::lock_guard<spring::recursive_mutex> lck(soundMutex);

for (auto it = curSources.begin(); it != curSources.end(); ++it) {
(*it)->UpdateVolume();
for (CSoundSource* src: curSources) {
src->UpdateVolume();
}

CheckError("AudioChannel::SetVolume");
@@ -63,14 +63,14 @@ void AudioChannel::SoundSourceFinished(CSoundSource* sndSource)

void AudioChannel::FindSourceAndPlay(size_t id, const float3& pos, const float3& velocity, float volume, bool relative)
{
if (id == 0 || volume <= 0.0f)
return;

std::lock_guard<spring::recursive_mutex> lck(soundMutex);

if (!enabled)
return;

if (volume <= 0.0f)
return;

// get the sound item, then find a source for it
const SoundItem* sndItem = sound->GetSoundItem(id);

@@ -99,9 +99,9 @@ void AudioChannel::FindSourceAndPlay(size_t id, const float3& pos, const float3&

int prio = INT_MAX;

for (auto it = curSources.begin(); it != curSources.end(); ++it) {
if ((*it)->GetCurrentPriority() < prio) {
src = *it;
for (CSoundSource* tmp: curSources) {
if (tmp->GetCurrentPriority() < prio) {
src = tmp;
prio = src->GetCurrentPriority();
}
}
@@ -129,6 +129,7 @@ void AudioChannel::FindSourceAndPlay(size_t id, const float3& pos, const float3&
curSources.insert(sndSource);
}


void AudioChannel::PlaySample(size_t id, float volume)
{
FindSourceAndPlay(id, -FwdVector, ZeroVector, volume, true);
@@ -144,28 +145,24 @@ void AudioChannel::PlaySample(size_t id, const float3& pos, const float3& veloci
FindSourceAndPlay(id, pos, velocity, volume, false);
}


void AudioChannel::PlaySample(size_t id, const CWorldObject* obj, float volume)
{
FindSourceAndPlay(id, obj->pos, obj->speed, volume, false);
}


void AudioChannel::PlayRandomSample(const GuiSoundSet& soundSet, const CWorldObject* obj)
void AudioChannel::PlayRandomSample(const GuiSoundSet& soundSet, const CWorldObject* obj) { PlayRandomSample(soundSet, obj->pos, obj->speed); }
void AudioChannel::PlayRandomSample(const GuiSoundSet& soundSet, const float3& pos, const float3& vel)
{
PlayRandomSample(soundSet, obj->pos);
}
int soundIdx = -1;

void AudioChannel::PlayRandomSample(const GuiSoundSet& soundSet, const float3& pos)
{
if (soundSet.NumSounds() == 0)
return;

const int soundIdx = guRNG.NextInt(soundSet.NumSounds());
const int soundID = soundSet.getID(soundIdx);
const float soundVol = soundSet.getVolume(soundIdx);
switch (soundSet.NumSounds()) {
case 0: { return; } break;
case 1: { soundIdx = 0; } break;
default: { soundIdx = guRNG.NextInt(soundSet.NumSounds()); } break;
}

PlaySample(soundID, pos, soundVol);
FindSourceAndPlay(soundSet.getID(soundIdx), pos, vel, soundSet.getVolume(soundIdx), false);
}


@@ -24,8 +24,6 @@ class AudioChannel : public IAudioChannel {
typedef std::pair<std::string, float> StreamQueueItem;

public:
AudioChannel(): curStreamSrc(nullptr) {}

void Enable(bool newState);
void SetVolume(float newVolume);

@@ -36,7 +34,7 @@ class AudioChannel : public IAudioChannel {
void PlaySample(size_t id, const CWorldObject* obj, float volume = 1.0f);

void PlayRandomSample(const GuiSoundSet& soundSet, const CWorldObject* obj);
void PlayRandomSample(const GuiSoundSet& soundSet, const float3& pos);
void PlayRandomSample(const GuiSoundSet& soundSet, const float3& pos, const float3& vel = ZeroVector);

void StreamPlay(const StreamQueueItem& item, bool enqueue) { StreamPlay(item.first, item.second, enqueue); }
void StreamPlay(const std::string& path, float volume = 1.0f, bool enqueue = false);
@@ -52,15 +50,14 @@ class AudioChannel : public IAudioChannel {
float StreamGetPlayTime();

protected:
void FindSourceAndPlay(size_t id, const float3& pos, const float3& velocity, float volume, bool relative);

void SoundSourceFinished(CSoundSource* sndSource);
void FindSourceAndPlay(size_t id, const float3& pos, const float3& velocity, float volume, bool relative) override;
void SoundSourceFinished(CSoundSource* sndSource) override;

private:
spring::unsynced_set<CSoundSource*> curSources;
std::deque<StreamQueueItem> streamQueue;

CSoundSource* curStreamSrc;
CSoundSource* curStreamSrc = nullptr;

static constexpr size_t MAX_STREAM_QUEUESIZE = 10;
};

0 comments on commit 79fcf9b

Please sign in to comment.
You can’t perform that action at this time.