From cc406602062529086e4e77e07d9234ce059bcf0e Mon Sep 17 00:00:00 2001
From: ThorstenB <brehmt@gmail.com>
Date: Sun, 4 Dec 2011 21:43:21 +0100
Subject: [PATCH] Performance improvement Avoid frequently creating/dropping an
 identical sound sample, when the nav radio signal is flickering. Always keep
 most recent sound when signal is lost (only stop it/turn volume off).

---
 src/Instrumentation/kr_87.cxx |  4 +++-
 src/Sound/audioident.cxx      | 11 ++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/Instrumentation/kr_87.cxx b/src/Instrumentation/kr_87.cxx
index 0fa58012e..8ea39569c 100644
--- a/src/Instrumentation/kr_87.cxx
+++ b/src/Instrumentation/kr_87.cxx
@@ -524,7 +524,9 @@ void FGKR_87::search() {
 	    xyz = adf->cart();
 
 	    if ( _sgr->exists( "adf-ident" ) ) {
-		_sgr->remove( "adf-ident" );
+	        // stop is required! -- remove alone wouldn't stop immediately
+	        _sgr->stop( "adf-ident" );
+	        _sgr->remove( "adf-ident" );
 	    }
 	    SGSoundSample *sound;
         sound = FGMorse::instance()->make_ident( trans_ident, FGMorse::LO_FREQUENCY );
diff --git a/src/Sound/audioident.cxx b/src/Sound/audioident.cxx
index 782526c9b..829ce9ab6 100644
--- a/src/Sound/audioident.cxx
+++ b/src/Sound/audioident.cxx
@@ -70,7 +70,15 @@ void AudioIdent::setVolumeNorm( double volumeNorm )
 
 void AudioIdent::setIdent( const std::string & ident, double volumeNorm )
 {
-    if( _ident == ident ) {
+    // Signal may flicker very frequently (due to our realistic newnavradio...).
+    // Avoid recreating identical sound samples all the time, instead turn off
+    // volume when signal is lost, and save the most recent sample.
+    if (ident.empty())
+        volumeNorm = 0;
+
+    if(( _ident == ident )||
+       (volumeNorm == 0))  // don't bother with sounds when volume is OFF anyway...
+    {
         if( false == _ident.empty() )
             setVolumeNorm( volumeNorm );
         return;
@@ -88,6 +96,7 @@ void AudioIdent::setIdent( const std::string & ident, double volumeNorm )
             sound->set_volume( volumeNorm );
             if (!_sgr->add( sound, _fx_name )) {
                 SG_LOG(SG_SOUND, SG_WARN, "Failed to add sound '" << _fx_name << "' for ident '" << ident << "'" );
+                delete sound;
                 return;
             }