From 3643f1c064ec02f33d982d7252b813a286bc12b5 Mon Sep 17 00:00:00 2001 From: Richard Harrison Date: Fri, 31 Jul 2020 01:31:27 +0200 Subject: [PATCH] XMLSound bugfixes - using expressions for pitch and volume didn't work - multiple volume elements not computed properly Discussion here: https://web.archive.org/web/20200730232940/http://fguk.me/forum/development-hangar/8360-supersonic-audio?start=40#46287 /a/v1 0.1 /a/v2 1 -3 /a/v3 1 -1 evaluates as follows: update(): Have 3 volume entries --> 0:prop /a[0]/v1[0] => 0.110000 factor = 0.100000 v=0.011000 offset 0.000000, now 0.000000, v=0.011000 vol=0.011000 ==> 0.011000 --> 1:prop /a[0]/v2[0] => 0.120000 factor = 3.000000 v=0.360000 -ve offset 1.000000 1.360000 = 0.014960 ==> 0.014960 --> 2:prop /a[0]/v3[0] => 0.130000 factor = 1.000000 v=0.130000 -ve offset 1.000000 1.130000 = 0.016905 ==> 0.016905 --- simgear/sound/xmlsound.cxx | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/simgear/sound/xmlsound.cxx b/simgear/sound/xmlsound.cxx index 9646a9c0..89331390 100644 --- a/simgear/sound/xmlsound.cxx +++ b/simgear/sound/xmlsound.cxx @@ -414,9 +414,11 @@ SGXmlSound::update (double dt) double v = 1.0; if (_volume[i].expr) { - v = _volume[i].expr->getValue(nullptr); - expr = true; - continue; + double expression_value = _volume[i].expr->getValue(nullptr); + if (expression_value >= 0) + volume *= expression_value; + expr = true; + continue; } if (_volume[i].prop) { @@ -437,19 +439,23 @@ SGXmlSound::update (double dt) if (_volume[i].max && (v > _volume[i].max)) v = _volume[i].max; - else if (v < _volume[i].min) v = _volume[i].min; - if (_volume[i].subtract) // Hack! - volume = _volume[i].offset - v; + if (_volume[i].subtract){ // Hack! + v = v + _volume[i].offset; + if (v >= 0) + volume = volume * v; + } else { - volume_offset += _volume[i].offset; - volume *= v; + if (v >= 0) { + volume_offset += _volume[i].offset; + volume = volume * v; + } } } - + // // Update the pitch // @@ -462,9 +468,9 @@ SGXmlSound::update (double dt) double p = 1.0; if (_pitch[i].expr) { - p = _pitch[i].expr->getValue(nullptr); - expr = true; - continue; + pitch *= _pitch[i].expr->getValue(nullptr); + expr = true; + continue; } if (_pitch[i].prop) {