Skip to content

Commit

Permalink
Ensure that the RPC curves are updated before the Cue is played. (Mon…
Browse files Browse the repository at this point in the history
…oGame#5709)

Fixed incorrect volume on simple sounds with RPC curves.
  • Loading branch information
tomspilman authored and nkast committed Jun 26, 2018
1 parent ab3cf74 commit f3f906c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
22 changes: 18 additions & 4 deletions MonoGame.Framework/Audio/Xact/Cue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ public void Play()
var index = XactHelpers.Random.Next(_sounds.Length);
_curSound = _sounds[index];

_curSound.Play(1.0f, _engine);
var volume = UpdateRpcCurves();

_curSound.Play(volume, _engine);
_played = true;
IsPrepared = false;
}
Expand Down Expand Up @@ -253,14 +255,22 @@ public void Apply3D(AudioListener listener, AudioEmitter emitter)

internal void Update(float dt)
{
if (_curSound != null)
_curSound.Update(dt);
if (_curSound == null)
return;

_curSound.Update(dt);

UpdateRpcCurves();
}

private float UpdateRpcCurves()
{
var volume = 1.0f;

// Evaluate the runtime parameter controls.
var rpcCurves = _curSound.RpcCurves;
if (rpcCurves.Length > 0)
{
var volume = 1.0f;
var pitch = 0.0f;
var reverbMix = 1.0f;
float? filterFrequency = null;
Expand Down Expand Up @@ -305,8 +315,12 @@ internal void Update(float dt)
}
}

pitch = MathHelper.Clamp(pitch, -1.0f, 1.0f);

_curSound.UpdateState(_engine, volume, pitch, reverbMix, filterFrequency, filterQFactor);
}

return volume;
}

/// <summary>
Expand Down
5 changes: 0 additions & 5 deletions MonoGame.Framework/Audio/Xact/SoundBank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ public SoundBank(AudioEngine audioEngine, string fileName)
stream.Seek(complexCuesOffset, SeekOrigin.Begin);
for (int i = 0; i < numComplexCues; i++)
{
//Cue cue;

byte flags = reader.ReadByte();
if (((flags >> 2) & 1) != 0)
{
Expand Down Expand Up @@ -348,9 +346,6 @@ private void Dispose(bool disposing)

if (disposing)
{
//foreach (var cue in _cues.Values)
// cue.Dispose();

IsInUse = false;

if (Disposing != null)
Expand Down
15 changes: 11 additions & 4 deletions MonoGame.Framework/Audio/Xact/XactSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public void Play(float volume, AudioEngine engine)
{
_cueVolume = volume;
var category = engine.Categories[_categoryID];
UpdateCategoryVolume(category._volume[0]);

var curInstances = category.GetPlayingInstanceCount();
if (curInstances >= category.maxInstances)
Expand All @@ -141,10 +140,17 @@ public void Play(float volume, AudioEngine engine)
}
}

float finalVolume = _volume * _cueVolume * category._volume[0];
float finalPitch = _pitch + _cuePitch;
float finalMix = _useReverb ? _cueReverbMix : 0.0f;

if (_complexSound)
{
foreach (XactClip clip in _soundClips)
{
clip.UpdateState(finalVolume, finalPitch, finalMix, _cueFilterFrequency, _cueFilterQFactor);
clip.Play();
}
}
else
{
Expand All @@ -160,9 +166,9 @@ public void Play(float volume, AudioEngine engine)
return;
}

_wave.Pitch = _pitch + _cuePitch;
_wave.Volume = _volume * _cueVolume * category._volume[0];
_wave.PlatformSetReverbMix(_useReverb ? _cueReverbMix : 0.0f);
_wave.Pitch = finalPitch;
_wave.Volume = finalVolume;
_wave.PlatformSetReverbMix(finalMix);
_wave.Play();
}
}
Expand Down Expand Up @@ -295,6 +301,7 @@ internal void UpdateState(AudioEngine engine, float volume, float pitch, float r
{
_wave.PlatformSetReverbMix(_useReverb ? _cueReverbMix : 0.0f);
_wave.Pitch = finalPitch;
_wave.Volume = finalVolume;
}
}

Expand Down

0 comments on commit f3f906c

Please sign in to comment.