예제 #1
0
 public void RegisterSoundPlayer(ISpectrumPlayer soundPlayer)
 {
     this.soundPlayer = soundPlayer;
     this.soundPlayer.PropertyChanged += soundPlayer_PropertyChanged;
     this.UpdateBarLayout();
     Running = true;
 }
예제 #2
0
 public void RegisterSoundPlayer(ISpectrumPlayer soundPlayer)
 {
     this.soundPlayer = soundPlayer;
     this.soundPlayer.PropertyChanged += soundPlayer_PropertyChanged;
     this.UpdateBarLayout();
     this.animationTimer.Start();
 }
예제 #3
0
 public void RegisterSoundPlayer(ISpectrumPlayer soundPlayer)
 {
     if (soundPlayer != null)
     {
         this.soundPlayer = soundPlayer;
         this.soundPlayer.PropertyChanged += soundPlayer_PropertyChanged;
         _visualTimer.Start();
     }
 }
예제 #4
0
        public void UnregisterSoundPlayer()
        {
            this.animationTimer.Stop();

            if (soundPlayer != null)
            {
                this.soundPlayer.PropertyChanged -= soundPlayer_PropertyChanged;
                this.soundPlayer = null;
            }
        }
예제 #5
0
        public void UnregisterSoundPlayer()
        {
            if (soundPlayer != null)
            {
                this.soundPlayer.PropertyChanged -= soundPlayer_PropertyChanged;
                this.soundPlayer = null;
            }

            this.Running = false;
        }
예제 #6
0
        private void UpdateSpectrum()
        {
            ISpectrumPlayer player = ActiveSpectrumPlayer;

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

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

            UpdateSpectrumShapes(player);
        }
예제 #7
0
 /// <summary>
 ///     Register a sound player from which the spectrum analyzer
 ///     can get the necessary playback data.
 /// </summary>
 /// <param name="soundPlayer">A sound player that provides spectrum data through the ISpectrumPlayer interface methods.</param>
 public void RegisterSoundPlayer(ISpectrumPlayer soundPlayer)
 {
     _soundPlayer = soundPlayer;
     soundPlayer.PropertyChanged += soundPlayer_PropertyChanged;
     UpdateBarLayout();
     _animationTimer.Start();
 }
 /// <summary>
 /// Register a sound player from which the spectrum analyzer
 /// can get the necessary playback data.
 /// </summary>
 /// <param name="soundPlayer">A sound player that provides spectrum data through the ISpectrumPlayer interface methods.</param>
 public void RegisterSoundPlayer(ISpectrumPlayer soundPlayer)
 {
     this.soundPlayer = soundPlayer;
     soundPlayer.PropertyChanged += soundPlayer_PropertyChanged;
     spectrumCanvas = GetTemplateChild("PART_SpectrumCanvas") as Canvas;
     UpdateBarLayout();
     animationTimer.Start();
 }
    private void UpdateSpectrumShapes(ISpectrumPlayer 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));
      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 || player.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++;
      }
    }
예제 #10
0
        private void UpdateSpectrumShapes(ISpectrumPlayer 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));

            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 || player.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++;
            }
        }
예제 #11
0
        private void InitializeBars()
        {
            ISpectrumPlayer 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;
        }