It is currently October 9th, 2024, 7:15 pm

Reducing CPU usage?

Get help with creating, editing & fixing problems with skins
ItsJustRyan
Posts: 81
Joined: October 25th, 2019, 1:20 am

Reducing CPU usage?

Post by ItsJustRyan »

Hi!

Is there a way to reduce the CPU usage of this code? Its an excerpt from a skin I have that makes it look like a spinning vinyl. It has the outer vinyl and then a masked album cover in the middle. It appears that the mask method is very CPU intense because it has to keep doing the matrix calculations, so if I instead had two rotator meters, made the vinyl transparent where the album cover would go as opposed to the image meter maybe it'll improve? Or is there a smarter way I didn't think of to do it?

Code: Select all

[Rainmeter]
Update=20

[Variables]

[GetMusicPlayerStatus0]
Measure=Plugin
Plugin=NowPlaying
PlayerName=AIMP
PlayerType=STATUS

[GetMusicPlayerState0]
Measure=Plugin
Plugin=NowPlaying
PlayerName=AIMP
PlayerType=STATE

[MeasureStateButton]
Measure=Plugin
Plugin=NowPlaying.dll
PlayerName=AIMP
PlayerType=STATE
Substitute="0":"1","1":"0","2":"1"

[GetSongCover0]
Measure=Plugin
Plugin=NowPlaying
PlayerName=[GetMusicPlayerState0]
PlayerType=Cover
UpdateDivider=10

[MeasureRotate]
Measure=Calc
Formula=(GetMusicPlayerState0 = 1 ? (MeasureRotate % 360) + 1 : MeasureRotate)
MaxValue=360

[MeasureSinAngle]
Measure=Calc
Formula=SIN(MeasureRotate/360*2*PI)

[MeasureNegSinAngle]
Measure=Calc
Formula=-(MeasureSinAngle)

[MeasureCosAngle]
Measure=Calc
Formula=COS(MeasureRotate/360*2*PI)

[MeasureTXCover]
Measure=Calc
Formula=#XS#-MeasureCosAngle*#XS#-MeasureSinAngle*#XY#

[MeasureTYCover]
Measure=Calc
Formula=#XY#-MeasureNegSinAngle*#XS#-MeasureCosAngle*#XY#

[VinylRotate]
Meter=Rotator
MeasureName=MeasureRotate
ImageName=vinyl_bg_edit5MidLarger.png
X=1600
Y=765
OffsetX=225
OffsetY=225
W=450
H=450
AntiAlias=1
DynamicVariables=1

[CoverRotate]
Meter=Image
MeasureName=GetSongCover0
MaskImageName=vinyl_mask_small_full.png
X=1733
Y=898
W=186
H=186
TransformationMatrix=[MeasureCosAngle]; [MeasureSinAngle]; [MeasureNegSinAngle]; [MeasureCosAngle]; [MeasureTYCover]; [MeasureTXCover]
AntiAlias=1
DynamicVariables=1
User avatar
balala
Rainmeter Sage
Posts: 16645
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Reducing CPU usage?

Post by balala »

ItsJustRyan wrote: February 23rd, 2023, 8:52 pm Is there a way to reduce the CPU usage of this code? Its an excerpt from a skin I have that makes it look like a spinning vinyl. It has the outer vinyl and then a masked album cover in the middle. It appears that the mask method is very CPU intense because it has to keep doing the matrix calculations, so if I instead had two rotator meters, made the vinyl transparent where the album cover would go as opposed to the image meter maybe it'll improve? Or is there a smarter way I didn't think of to do it?
For first, sorry for the late reply.
Secondly there is something I think is mistaken (or at least weird) in your code. When you're playing something and get the rotating cover, most of the time the cover is not visible, being outside of the skin. Note that there is no way to get visible the meters going to positions with negative values of the coordinates. Both the horizontal and the vertical position have to be positive in order to can see the meter. But even if the [CoverRotate] has both of these options set to positive values (X=1733 and Y=898), due to the applied TransformationMatrix the image goes often to the not-visible regions of the skin. Is it sure this is what you want?
ItsJustRyan
Posts: 81
Joined: October 25th, 2019, 1:20 am

Re: Reducing CPU usage?

Post by ItsJustRyan »

I'll be honest I didn't fully understand what you meant, but it seems to be working perfectly fine for me, and I've been using it for quite a while. Just to be clear, what makes it outside of the skin? Is it the large coordinate values (which are all positive to my knowledge). Also I just realised when getting the excerpt for the code to post here I also deleted two important variables for the matrix calculation.

Code: Select all

XS=(898+93)
XY=(1733+93)
These make it so that the album cover spins around its center, which I think is what you were referring to when you said it spends most of the time not being visible. If not this, then I'm not sure lol.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5539
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Reducing CPU usage?

Post by eclectic-tech »

ItsJustRyan wrote: February 26th, 2023, 12:41 am I'll be honest I didn't fully understand what you meant, but it seems to be working perfectly fine for me, and I've been using it for quite a while. Just to be clear, what makes it outside of the skin? Is it the large coordinate values (which are all positive to my knowledge). Also I just realised when getting the excerpt for the code to post here I also deleted two important variables for the matrix calculation.

Code: Select all

XS=(898+93)
XY=(1733+93)
These make it so that the album cover spins around its center, which I think is what you were referring to when you said it spends most of the time not being visible. If not this, then I'm not sure lol.
In general the only way to reduce the load when animating images is to increase the skin update as high as possible without causing appreciable reduction in the animation effect. Thankfully the eye and mind allow for less than perfect transitions appearances to seem alright.

If you set the skin update to 100 or 150 and increase the additive in your [MeasureRotate] to say +5, this will cause the rotation to appear similar and the load is reduced from ~25% to less than ~5-10% on my tests.

Code: Select all

[MeasureRotate]
Measure=Calc
Formula=(GetMusicPlayerState0 = 1 ? (MeasureRotate % 360) + 5 : MeasureRotate)
MaxValue=360
You can play with those values if the animation is not as smooth as desired, but it is going to be a trade-off to reduce the load and not degrade the animation.

Hopefully this gives you some ideas to optimize your skin.
User avatar
balala
Rainmeter Sage
Posts: 16645
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Reducing CPU usage?

Post by balala »

ItsJustRyan wrote: February 23rd, 2023, 8:52 pm Is there a way to reduce the CPU usage of this code?
As eclectic-tech said, you should increase the Update value of the code. The higher this value is, the more easier is running the skin. This is why the default Update=1000 is one of the best values for this option, meaning one update per second, which is a quite good compromise between not loading too much the CPU, but running the skin in a proper way. This is many times true, excepting skins which have an animation, like this one. In such cases the Update has to be set a lower value in order to have the animation ran normally, not updated just once per second (or even more rarely in some cases). But this loads form time to time even extremely much the CPU, causing lags in running. So it seems there is no solution to this limitation. But is it sure there is not?
No, this is not sure at all. There might be some solutions. One of this solutions is the ActionTimer plugin. Using this plugin you could use the default Update=1000 option for your skin, which reduces the loading of CPU. When the animation has to be ran, so while the media player is playing, you can update the appropriate measures and meters to the same frequency as on the skin with the low update. But there are two important differences, namely:
  • Only those measures and meters are updated often which have to be. The others are updated only on the normal amount, set by the Update value, so once per second.
  • Even the measures and meters which have to be updated more frequently, are updated so, only while the player is playing. When it is paused or stopped, or even closed, all measures and meters are updated once per second, so at the normal amount, or even less.
This way you can reduce the CPU loading.

If you are still interested, here is the solution:
  • Set the Update value in the [Rainmeter] section, to the default Update=1000, by replacing the previous Update=20 by this.
  • Add the following Measure to your code:

    Code: Select all

    [MeasureRotateUpdate]
    Measure=Plugin
    Plugin=ActionTimer
    Group=Sliders
    ActionList1=Repeat Rotate,20,360
    Rotate=[!UpdateMeasure "MeasureRotate"][!UpdateMeasure "MeasureSinAngle"][!UpdateMeasure "MeasureNegSinAngle"][!UpdateMeasure "MeasureCosAngle"][!UpdateMeasure "MeasureTXCover"][!UpdateMeasure "MeasureTYCover"][!UpdateMeter "CoverRotate"][!Redraw]
    DynamicVariables=1
    I added this measure right after the [GetSongCover0] measure, but finally doesn't really matter where you add it.
  • Add the following IfCondition and the appropriate IfTrueAction and IfFalseAction options to the existing [GetMusicPlayerState0] measure. Don1t alter the existing options, just add these:

    Code: Select all

    [GetMusicPlayerState0]
    ...
    IfCondition=(#CURRENTSECTION#=1)
    IfTrueAction=[!CommandMeasure "MeasureRotateUpdate" "Execute 1"]
    IfFalseAction=[!CommandMeasure "MeasureRotateUpdate" "Stop 1"]
  • Finally add the following options to the [MeasureRotate] measure and once again make sure not to alter the existing options:

    Code: Select all

    [MeasureRotate]
    ...
    IfCondition=(#CURRENTSECTION#>=358)
    IfTrueAction=[!CommandMeasure "MeasureRotateUpdate" "Stop 1"][!CommandMeasure "MeasureRotateUpdate" "Execute 1"]
Some other improvements are also possible, and even needed, but for first please try out this modifications of your code, to see if they meat your needs. If they do, I'm gonna be back with the following and needed updates of the code.
ItsJustRyan
Posts: 81
Joined: October 25th, 2019, 1:20 am

Re: Reducing CPU usage?

Post by ItsJustRyan »

Hi!

Thanks for the suggestion. However, I was more looking for ways to reduce the CPU usage of the animation, as I wanted to try make a fullscreen turntable type skin, and found that scaling up the animation drastically increased the CPU usage. The code you suggested is useful, although I have music playing most of the time anyways :D . If I instead used a rotator meter instead of using the transformation matrix on an image meter, could it reduce the usage or does it work on a similar principle anyways. Or are there better ways of making the animation? Thanks!
User avatar
balala
Rainmeter Sage
Posts: 16645
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Reducing CPU usage?

Post by balala »

ItsJustRyan wrote: February 28th, 2023, 11:28 am and found that scaling up the animation drastically increased the CPU usage.
For sure, this is definitely right.
ItsJustRyan wrote: February 28th, 2023, 11:28 am If I instead used a rotator meter instead of using the transformation matrix on an image meter, could it reduce the usage or does it work on a similar principle anyways. Or are there better ways of making the animation? Thanks!
It could, I assume, however has to be tried out. Am not entirely sure, but I think it would be much better, so suggest to try out such a solution as well. Anyway, scaling could also increase much the CPU usage, even with a Rotator meter, especially that there is no good solution for scaling this type of meter, other than the TransformationMatrix.
However, if you don't really know how to do, please let me know for further help.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5539
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Reducing CPU usage?

Post by eclectic-tech »

ItsJustRyan wrote: February 28th, 2023, 11:28 am Hi!

Thanks for the suggestion. However, I was more looking for ways to reduce the CPU usage of the animation, as I wanted to try make a fullscreen turntable type skin, and found that scaling up the animation drastically increased the CPU usage. The code you suggested is useful, although I have music playing most of the time anyways :D . If I instead used a rotator meter instead of using the transformation matrix on an image meter, could it reduce the usage or does it work on a similar principle anyways. Or are there better ways of making the animation? Thanks!
:???: https://forum.rainmeter.net/viewtopic.php?p=213048#p213005 :confused:

The larger the image the greater the load on the CPU will be no matter the method is employed... so reducing the speed and number of calculations of the animation by these methods is about all that may help reduce the load while the animation is running.
User avatar
Active Colors
Moderator
Posts: 1318
Joined: February 16th, 2012, 3:32 am
Location: Berlin, Germany

Re: Reducing CPU usage?

Post by Active Colors »

eclectic-tech wrote: February 28th, 2023, 1:59 pm :???: https://forum.rainmeter.net/viewtopic.php?p=213048#p213005 :confused:

The larger the image the greater the load on the CPU will be no matter the method is employed... so reducing the speed and number of calculations of the animation by these methods is about all that may help reduce the load while the animation is running.
Just to add to it. Large images are sort of ok, of course depening how large they are. But changing the W and H values of a large image is a very resource intensive operation. To reduce the load, you might want to edit your picture by resizing it to your needs and avoid resizing it through Rainmeter. This might not reduce it completely, but with other optimizations this will have a cumulative effect.
User avatar
balala
Rainmeter Sage
Posts: 16645
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Reducing CPU usage?

Post by balala »

Active Colors wrote: February 28th, 2023, 9:47 pm To reduce the load, you might want to edit your picture by resizing it to your needs and avoid resizing it through Rainmeter.
Not a viable option, because the images are cover arts, as get by a NowPlaying measure. So those images are different sized and additionally can't even be manually edited.