tjscience / audion.cscore

An audio visualization and processing framework for WPF
MIT License
33 stars 7 forks source link

CPU Usage #1

Closed Mitra-M closed 7 years ago

Mitra-M commented 7 years ago

First, Thank you for sharing. It is Greate.

But CPU Usage is very high. (I mean CPU consumption for the spectrum analyzer, not the waveform )

Source : mp3 16bit 48khz Windows 10 : CPU Usage = %38 Windows 8.1: CPU Usage = %27

Thanks again!

Mitra-M commented 7 years ago

The problem is in this loop:

for (var i = 0; i < FrequencyBarCount; i++)
            {
                var b = new Border();
                b.Width = barWidth;
                b.BorderBrush = FrequencyBarBorderBrush;
                b.CornerRadius = FrequencyBarCornerRadius;
                b.BorderThickness = FrequencyBarBorderThickness;
                b.Height = (dataPoints[i] / 100) * height;
                b.HorizontalAlignment = HorizontalAlignment.Left;
                b.VerticalAlignment = VerticalAlignment.Bottom;
                b.Margin = new Thickness(i * barSpacing, 0, 0, 0);
                b.Background = FrequencyBarBrush;
                spectrumGrid.Children.Add(b);
            }

I think resize elements is not a good idea to do that. (resize in WPF is expensive)

The best method is to create a collection (ObservableCollection) of bars and show / hide them in the loop.

Like this:

public static readonly DependencyProperty ValueProperty = 
        DependencyProperty.Register("Value", typeof(int), typeof(VUMeterControl), new UIPropertyMetadata(0, 
          (sender, e) => 
          { 
            VUMeterControl control = (VUMeterControl)sender; 
            int value = (int)e.NewValue; 
            for (int i = TOTAL_COUNT - 1; i >= 0; i--) 
            { 
              FrameworkElement element = (FrameworkElement)control.PART_ItemsPresenter.ItemContainerGenerator.ContainerFromIndex(i); 
              if (TOTAL_COUNT - i > value / (control.MaxValue / TOTAL_COUNT)) 
                element.Visibility = Visibility.Hidden; 
              else 
                element.Visibility = Visibility.Visible; 
            } 
          }) 
        );

Please have a look at this.

tjscience commented 7 years ago

Hi, Thanks for reporting this and for the suggestion. I was already going down that path among other things like freezing brushes and decreasing the frame rate. I will check in my fixes shortly.

tjscience commented 7 years ago

Another thing that can be done to improve performance of the spectrum analyzer is to decrease the FrequencyBarCount property which is at its max, 100, in the sample app. decreasing this to 50 should cut the CPU usage in half theoretically.

Mitra-M commented 7 years ago

I tried it. A lot better now

Thanks