From bc154bebbb4661b0e2383bd5279077b40f125f60 Mon Sep 17 00:00:00 2001 From: Tom Spilman Date: Sat, 13 May 2017 14:01:16 -0500 Subject: [PATCH] Ensure that the RPC curves are updated before the Cue is played. Fixed incorrect volume on simple sounds with RPC curves. --- MonoGame.Framework/Audio/Xact/Cue.cs | 22 ++++++++++++++++++---- MonoGame.Framework/Audio/Xact/SoundBank.cs | 5 ----- MonoGame.Framework/Audio/Xact/XactSound.cs | 15 +++++++++++---- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/MonoGame.Framework/Audio/Xact/Cue.cs b/MonoGame.Framework/Audio/Xact/Cue.cs index 138987a30b9..bdbe16e6db6 100644 --- a/MonoGame.Framework/Audio/Xact/Cue.cs +++ b/MonoGame.Framework/Audio/Xact/Cue.cs @@ -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; } @@ -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; @@ -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; } /// diff --git a/MonoGame.Framework/Audio/Xact/SoundBank.cs b/MonoGame.Framework/Audio/Xact/SoundBank.cs index e87db7a3bb2..f8c4de8410d 100644 --- a/MonoGame.Framework/Audio/Xact/SoundBank.cs +++ b/MonoGame.Framework/Audio/Xact/SoundBank.cs @@ -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) { @@ -348,9 +346,6 @@ private void Dispose(bool disposing) if (disposing) { - //foreach (var cue in _cues.Values) - // cue.Dispose(); - IsInUse = false; if (Disposing != null) diff --git a/MonoGame.Framework/Audio/Xact/XactSound.cs b/MonoGame.Framework/Audio/Xact/XactSound.cs index 6e9e17a1633..bfcc57fc414 100644 --- a/MonoGame.Framework/Audio/Xact/XactSound.cs +++ b/MonoGame.Framework/Audio/Xact/XactSound.cs @@ -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) @@ -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 { @@ -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(); } } @@ -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; } }