Esempio n. 1
0
        private double[] OnNewVUMeter()
        {
            double[] dbLevel = new double[] { -200.0, -200.0 };

            IPlayerContextManager playerContextManager = ServiceRegistration.Get <IPlayerContextManager>(false);

            if (playerContextManager == null)
            {
                return(dbLevel);
            }

            IPlayerContext playerContext = playerContextManager.GetPlayerContext(PlayerChoice.PrimaryPlayer);

            if (playerContext == null)
            {
                return(dbLevel);
            }

            IAudioPlayerAnalyze player = (playerContext.CurrentPlayer as IAudioPlayerAnalyze);

            if (player == null)
            {
                return(dbLevel);
            }

            double dbLevelL;
            double dbLevelR;

            if (player.GetChannelLevel(out dbLevelL, out dbLevelR))
            {
                dbLevel[0] = dbLevelL;
                dbLevel[1] = dbLevelR;
            }
            return(dbLevel);
        }
Esempio n. 2
0
        private void UpdateSpectrum()
        {
            IAudioPlayerAnalyze player      = ActiveSpectrumPlayer;
            IAudioPlayer        audioPlayer = player as IAudioPlayer;

            if (!_refreshValues || audioPlayer == null || player == null || _spectrumCanvas == null)
            {
                return;
            }

            if (audioPlayer.State != PlayerState.Active || !player.GetFFTData(_channelData))
            {
                return;
            }

            UpdateSpectrumShapes(player);
        }
Esempio n. 3
0
        private void UpdateSpectrumShapes(IAudioPlayerAnalyze player)
        {
            double fftBucketHeight = 0f;
            double barHeight       = 0f;
            double lastPeakHeight  = 0f;
            double height          = _spectrumCanvas.ActualHeight;
            int    barIndex        = 0;
            double peakDotHeight   = Math.Max(_barWidth / 2.0f, 1);
            double barHeightScale  = (height - peakDotHeight);
            int    minIndex        = Math.Max(0, Math.Min(_minimumFrequencyIndex, _channelData.Length));
            int    maxIndex        = Math.Max(0, Math.Min(_maximumFrequencyIndex, _channelData.Length));

            IAudioPlayer audioPlayer = player as IAudioPlayer;

            for (int i = minIndex; i <= maxIndex; i++)
            {
                // If we're paused, keep drawing, but set the current height to 0 so the peaks fall.
                if (player == null || audioPlayer != null && audioPlayer.State != PlayerState.Active)
                {
                    barHeight = 0f;
                }
                else // Draw the maximum value for the bar's band
                {
                    switch (BarHeightScaling)
                    {
                    case BarHeightScalingStyles.Decibel:
                        double dbValue = 20 * Math.Log10(_channelData[i]);
                        fftBucketHeight = ((dbValue - MIN_DB_VALUE) / DB_SCALE) * barHeightScale;
                        break;

                    case BarHeightScalingStyles.Linear:
                        fftBucketHeight = (_channelData[i] * SCALE_FACTOR_LINEAR) * barHeightScale;
                        break;

                    case BarHeightScalingStyles.Sqrt:
                        fftBucketHeight = (((Math.Sqrt(_channelData[i])) * SCALE_FACTOR_SQR) * barHeightScale);
                        break;
                    }

                    if (barHeight < fftBucketHeight)
                    {
                        barHeight = fftBucketHeight;
                    }
                    if (barHeight < 0f)
                    {
                        barHeight = 0f;
                    }
                }

                // If this is the last FFT bucket in the bar's group, draw the bar.
                int currentIndexMax = IsFrequencyScaleLinear ? _barIndexMax[barIndex] : _barLogScaleIndexMax[barIndex];
                if (i != currentIndexMax)
                {
                    continue;
                }

                // Peaks can't surpass the height of the control.
                if (barHeight > height)
                {
                    barHeight = height;
                }

                if (AveragePeaks && barIndex > 0)
                {
                    barHeight = (lastPeakHeight + barHeight) / 2;
                }

                double peakYPos = barHeight;

                if (_channelPeakData[barIndex] < peakYPos)
                {
                    _channelPeakData[barIndex] = (float)peakYPos;
                }
                else
                {
                    _channelPeakData[barIndex] = (float)(peakYPos + (PeakFallDelay * _channelPeakData[barIndex])) / (PeakFallDelay + 1);
                }

                Control bar = _barShapes[barIndex];
                bar.RenderTransform = new ScaleTransform
                {
                    CenterX = bar.ActualWidth / 2,
                    CenterY = bar.ActualHeight,
                    ScaleY  = barHeight / height
                };

                Control peak = _peakShapes[barIndex];
                peak.RenderTransform = new TranslateTransform
                {
                    Y = -_channelPeakData[barIndex]
                };

                lastPeakHeight = barHeight;
                barHeight      = 0f;
                barIndex++;
            }
        }
Esempio n. 4
0
        private void InitializeBars()
        {
            IAudioPlayerAnalyze player = ActiveSpectrumPlayer;

            if (player == null)
            {
                _barWidth = 1;
                _maximumFrequencyIndex = -1;
                _minimumFrequencyIndex = 0;
                return;
            }

            if (!_refreshShapes || _spectrumCanvas == null)
            {
                return;
            }

            int  maxIndex;
            int  minIndex;
            bool res = player.GetFFTFrequencyIndex(MaximumFrequency, out maxIndex);

            res |= player.GetFFTFrequencyIndex(MinimumFrequency, out minIndex);
            if (!res)
            {
                return;
            }
            _maximumFrequencyIndex = Math.Min(maxIndex + 1, FREQUENCY_BUFFER_SIZE - 1);
            _minimumFrequencyIndex = Math.Min(minIndex, FREQUENCY_BUFFER_SIZE - 1);

            _barWidth = Math.Max(((_spectrumCanvas.ActualWidth - (BarSpacing * (BarCount + 1))) / BarCount), 1);
            int actualBarCount = _barWidth >= 1.0d ? BarCount : Math.Max((int)((_spectrumCanvas.ActualWidth - BarSpacing) / (_barWidth + BarSpacing)), 1);

            _channelPeakData = new float[actualBarCount];

            int        indexCount            = _maximumFrequencyIndex - _minimumFrequencyIndex;
            int        linearIndexBucketSize = (int)Math.Round(indexCount / (double)actualBarCount, 0);
            List <int> maxIndexList          = new List <int>();
            List <int> maxLogScaleIndexList  = new List <int>();
            double     maxLog = Math.Log(actualBarCount, actualBarCount);

            for (int i = 1; i < actualBarCount; i++)
            {
                maxIndexList.Add(_minimumFrequencyIndex + (i * linearIndexBucketSize));
                int logIndex = (int)((maxLog - Math.Log((actualBarCount + 1) - i, (actualBarCount + 1))) * indexCount) + _minimumFrequencyIndex;
                maxLogScaleIndexList.Add(logIndex);
            }
            maxIndexList.Add(_maximumFrequencyIndex);
            maxLogScaleIndexList.Add(_maximumFrequencyIndex);
            _barIndexMax         = maxIndexList.ToArray();
            _barLogScaleIndexMax = maxLogScaleIndexList.ToArray();

            FrameworkElementCollection canvasChildren = _spectrumCanvas.Children;

            canvasChildren.StartUpdate();
            try
            {
                canvasChildren.Clear();

                double height        = _spectrumCanvas.ActualHeight;
                double peakDotHeight = Math.Max(_barWidth / 2.0f, 1);
                for (int i = 0; i < actualBarCount; i++)
                {
                    // Deep copy the styles to each bar
                    Style barStyleCopy  = MpfCopyManager.DeepCopyCutLVPs(BarStyle);
                    Style peakStyleCopy = MpfCopyManager.DeepCopyCutLVPs(PeakStyle);

                    double  xCoord     = BarSpacing + (_barWidth * i) + (BarSpacing * i) + 1;
                    Control barControl = new Control
                    {
                        Width  = _barWidth,
                        Height = height,
                        Style  = barStyleCopy
                    };
                    Canvas.SetLeft(barControl, xCoord);
                    Canvas.SetBottom(barControl, height);
                    _barShapes.Add(barControl);
                    canvasChildren.Add(barControl);

                    Control peakControl = new Control
                    {
                        Width  = _barWidth,
                        Height = peakDotHeight,
                        Style  = peakStyleCopy
                    };
                    Canvas.SetLeft(peakControl, xCoord);
                    Canvas.SetBottom(peakControl, height);
                    _peakShapes.Add(peakControl);
                    canvasChildren.Add(peakControl);
                }
            }
            finally
            {
                canvasChildren.EndUpdate();
            }
            ActualBarWidth = _barWidth;
            _refreshShapes = false;
        }
    private void UpdateSpectrumShapes(IAudioPlayerAnalyze player)
    {
      double fftBucketHeight = 0f;
      double barHeight = 0f;
      double lastPeakHeight = 0f;
      double height = _spectrumCanvas.ActualHeight;
      int barIndex = 0;
      double peakDotHeight = Math.Max(_barWidth / 2.0f, 1);
      double barHeightScale = (height - peakDotHeight);
      int minIndex = Math.Max(0, Math.Min(_minimumFrequencyIndex, _channelData.Length));
      int maxIndex = Math.Max(0, Math.Min(_maximumFrequencyIndex, _channelData.Length));

      IAudioPlayer audioPlayer = player as IAudioPlayer;
      for (int i = minIndex; i <= maxIndex; i++)
      {
        // If we're paused, keep drawing, but set the current height to 0 so the peaks fall.
        if (player == null || audioPlayer != null && audioPlayer.State != PlayerState.Active)
          barHeight = 0f;
        else // Draw the maximum value for the bar's band
        {
          switch (BarHeightScaling)
          {
            case BarHeightScalingStyles.Decibel:
              double dbValue = 20 * Math.Log10(_channelData[i]);
              fftBucketHeight = ((dbValue - MIN_DB_VALUE) / DB_SCALE) * barHeightScale;
              break;
            case BarHeightScalingStyles.Linear:
              fftBucketHeight = (_channelData[i] * SCALE_FACTOR_LINEAR) * barHeightScale;
              break;
            case BarHeightScalingStyles.Sqrt:
              fftBucketHeight = (((Math.Sqrt(_channelData[i])) * SCALE_FACTOR_SQR) * barHeightScale);
              break;
          }

          if (barHeight < fftBucketHeight)
            barHeight = fftBucketHeight;
          if (barHeight < 0f)
            barHeight = 0f;
        }

        // If this is the last FFT bucket in the bar's group, draw the bar.
        int currentIndexMax = IsFrequencyScaleLinear ? _barIndexMax[barIndex] : _barLogScaleIndexMax[barIndex];
        if (i != currentIndexMax) 
          continue;

        // Peaks can't surpass the height of the control.
        if (barHeight > height)
          barHeight = height;

        if (AveragePeaks && barIndex > 0)
          barHeight = (lastPeakHeight + barHeight) / 2;

        double peakYPos = barHeight;

        if (_channelPeakData[barIndex] < peakYPos)
          _channelPeakData[barIndex] = (float) peakYPos;
        else
          _channelPeakData[barIndex] = (float) (peakYPos + (PeakFallDelay * _channelPeakData[barIndex])) / (PeakFallDelay + 1);

        Control bar = _barShapes[barIndex];
        bar.RenderTransform = new ScaleTransform
          {
              CenterX = bar.ActualWidth / 2,
              CenterY = bar.ActualHeight,
              ScaleY = barHeight / height
          };

        Control peak = _peakShapes[barIndex];
        peak.RenderTransform = new TranslateTransform
          {
              Y = -_channelPeakData[barIndex]
          };

        lastPeakHeight = barHeight;
        barHeight = 0f;
        barIndex++;
      }
    }