From 641f96ba3fe7952a42657ee3bc9b8e4b5f385f8c Mon Sep 17 00:00:00 2001
From: Lightning303 <Lightning303@gmail.com>
Date: Wed, 5 Nov 2014 02:50:13 +0100
Subject: [PATCH] Fix for GetFFTData and implementation of GetChannelLevel

---
 .../Presentation/Players/ISpectrumPlayer.cs        |  8 ++++
 .../Source/UI/Players/BassPlayer/BassPlayer.cs     | 46 +++++++++++++++++++++-
 .../BassPlayer/PlayerComponents/PlaybackBuffer.cs  |  2 +-
 3 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/MediaPortal/Source/Core/MediaPortal.UI/Presentation/Players/ISpectrumPlayer.cs b/MediaPortal/Source/Core/MediaPortal.UI/Presentation/Players/ISpectrumPlayer.cs
index e8b0ae6..aa1881e 100644
--- a/MediaPortal/Source/Core/MediaPortal.UI/Presentation/Players/ISpectrumPlayer.cs
+++ b/MediaPortal/Source/Core/MediaPortal.UI/Presentation/Players/ISpectrumPlayer.cs
@@ -76,5 +76,13 @@ namespace MediaPortal.UI.Presentation.Players
     /// by method <see cref="GetFFTData"/>.</param>
     /// <returns><c>true</c>, if the FFT buffer was already created, else <c>false</c>.</returns>
     bool GetFFTFrequencyIndex(int frequency, out int frequencyIndex);
+
+    /// <summary>
+    /// Gets the channel levels in dB.
+    /// </summary>
+    /// <param name="dbLevelL">Field to copy the left channel level to</param>
+    /// <param name="dbLevelR">Field to copy the right channel level to</param>
+    /// <returns>True if data was written, otherwise false.</returns>
+    bool GetChannelLevel(out double dbLevelL, out double dbLevelR);
   }
 }
\ No newline at end of file
diff --git a/MediaPortal/Source/UI/Players/BassPlayer/BassPlayer.cs b/MediaPortal/Source/UI/Players/BassPlayer/BassPlayer.cs
index eaf5f8d..0febb62 100644
--- a/MediaPortal/Source/UI/Players/BassPlayer/BassPlayer.cs
+++ b/MediaPortal/Source/UI/Players/BassPlayer/BassPlayer.cs
@@ -33,6 +33,7 @@ using MediaPortal.UI.Players.BassPlayer.Utils;
 using MediaPortal.UI.Presentation.Players;
 using Un4seen.Bass;
 using Un4seen.Bass.AddOn.Tags;
+using Un4seen.BassWasapi;
 
 namespace MediaPortal.UI.Players.BassPlayer
 {
@@ -436,7 +437,21 @@ namespace MediaPortal.UI.Players.BassPlayer
       if (vizStream == null)
         return false;
 
-      return Bass.BASS_ChannelGetData(vizStream.Handle, fftDataBuffer, _maxFFT) > 0;
+      BASSActive status = Bass.BASS_ChannelIsActive(vizStream.Handle);
+      if (status == BASSActive.BASS_ACTIVE_PLAYING)
+      {
+        if (BassWasapi.BASS_WASAPI_IsStarted())
+        {
+          return BassWasapi.BASS_WASAPI_GetData(fftDataBuffer, _maxFFT) > 0;
+        }
+        else
+          // wird nach dem Beenden noch einmalig aufgerufen, bedeutet das Stop Event ist nicht schnell genug
+          // um den zugriff auf BASS_ChannelGetData zu unterbinden.. Da muss noch was gemacht werden!
+          // Nicht gerade fein wenn der Stream Wasapi Modus verwendet.
+          return Bass.BASS_ChannelGetData(vizStream.Handle, fftDataBuffer, _maxFFT) > 0;
+      }
+      else
+        return false;
     }
 
     public bool GetFFTFrequencyIndex(int frequency, out int frequencyIndex)
@@ -452,6 +467,35 @@ namespace MediaPortal.UI.Players.BassPlayer
       return true;
     }
 
+    public bool GetChannelLevel(out double dbLevelL, out double dbLevelR)
+    {
+      dbLevelL = 0f;
+      dbLevelR = 0f;
+
+      BassStream vizStream = _controller.PlaybackProcessor.VizStream;
+      if (vizStream == null)
+      {
+        return false;
+      }
+
+      BASSActive status = Bass.BASS_ChannelIsActive(vizStream.Handle);
+      if (status == BASSActive.BASS_ACTIVE_PLAYING)
+      {
+        int level;
+        if (BassWasapi.BASS_WASAPI_IsStarted())
+        {
+          level = BassWasapi.BASS_WASAPI_GetLevel();
+        }
+        else
+        {
+          level = Bass.BASS_ChannelGetLevel(vizStream.Handle);
+        }
+        dbLevelL = Un4seen.Bass.Utils.LevelToDB(Un4seen.Bass.Utils.LowWord32(level), 65535); // the left level
+        dbLevelR = Un4seen.Bass.Utils.LevelToDB(Un4seen.Bass.Utils.HighWord32(level), 65535); // the right level
+        return true;
+      }
+      return false;
+    }
     #endregion
   }
 }
diff --git a/MediaPortal/Source/UI/Players/BassPlayer/PlayerComponents/PlaybackBuffer.cs b/MediaPortal/Source/UI/Players/BassPlayer/PlayerComponents/PlaybackBuffer.cs
index 197cad6..7dd34f0 100644
--- a/MediaPortal/Source/UI/Players/BassPlayer/PlayerComponents/PlaybackBuffer.cs
+++ b/MediaPortal/Source/UI/Players/BassPlayer/PlayerComponents/PlaybackBuffer.cs
@@ -137,7 +137,7 @@ namespace MediaPortal.UI.Players.BassPlayer.PlayerComponents
 
       _inputStream = stream;
 
-      UpdateVizLatencyCorrection();
+      //UpdateVizLatencyCorrection();
 
       _buffer = new AudioRingBuffer(stream.SampleRate, stream.Channels, _bufferSize + _vizReadOffset);
       _streamEnded = false;
-- 
1.9.0.msysgit.0

