It is currently October 28th, 2020, 9:08 am

Measures/Meters Templates & Generators

Tips and Tricks from the Rainmeter Community
User avatar
eclectic-tech
Rainmeter Sage
Posts: 4144
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Measures/Meters Templates & Generators

Post by eclectic-tech »

TIP: How to create multiple measures and meters on-the-fly using ready-made measure and meter templates, 2 generator skins, and a few variables. Simply copy the 'TemplateGenerators' folder to your skin's root level folder, edit a few variables, add a few lines of code in your skin, and save having to create hundreds of measures and meters.

WHY DO THIS?
There are times when creating a skin requires coding hundreds of measures and meters that are identical except for an incremental value. Two of the most common situations are AudioLevel plugin band measures/meters and Webparser measures/meters with StringIndex numbers.

Besides being tedious to create those sections, all measures must exist in the skin (even if they are not used) and meters need to be managed (hidden/shown) when an end user desires a different quantities of results.

By using a template of the measure(s) and meter(s), and a Lua script to replace an incremental value, it is possible to automatically create as many measures and meters as needed. This saves having to create excess measures and having to hide/show corresponding meters; making the skin more efficient and adaptable.

TEMPLATE GENERATOR SKINS

'GenerateMeasures.ini' uses a defined measure(s) template file. The Lua script reads a template file and repeatedly replaces ‘Repeat#’ with an incremental value in every section of the template, as many times as defined, and saves the resulting measures as #@#My#Template#Measures.inc’.

AudioLevelMeasureTemplate.inc

Code: Select all

[MeasureAudioRepeat#]
Group=Audio
Measure=Plugin
Plugin=AudioLevel
Parent=MeasureAudioParent
Type=Band
BandIdx=Repeat#
'GenerateMeters.ini' uses a defined meter(s) template file. It's Lua script reads a template file and repeatedly replaces ‘Repeat#’ with an incremental value in every section of the template as many times as defined, and saves the resulting meters as ‘#@#My#Template#Meters.inc’.

AudioLevelMeterTemplate.inc

Code: Select all

[MeterbarRepeat#]
Meter=Bar
MeasureName=MeasurebandRepeat#
BarOrientation=Vertical
X=#Gap#R
W=#BarWidth#
H=#BarHeight#
BarColor=#MyBarColor#
REPEATING CONTROL VARIABLES

The generator skins Lua scripts are setup and controlled by 'RepeatingVariables.inc' file.
It contains only 4 required variables:
• the name of your template,
• the number for the first increment (zero or one),
• the number of repeating measures,
• and the number of repeating meters.

RepeatingVariables.inc

Code: Select all

; Repeating incremental number sections variables
; Create a new template file, with measure or meter section(s), in 'Templates' folder, using normal Rainmeter formatting
; Then anywhere you want an incremental number, replace that with 'Repeat#' (without quote marks!)

[Variables]
; Include any skin variables that are used in the variables below
@includeSkinVariables=#@#VisualizerVariables.inc

; All 4 variables below are required by the script in order to generate measures and meters

; The Name of your Measure/Meter template file you created (or pre-made template name)
; Pre-made Templates: Webparser, AudioLevel, AudioAnalyzer
Template=AudioAnalyzer

; The first number in your repeating incremental sequence
; This is usually zero for audio measures/meters, or 1 for webparsers measures/meters
FirstIndex=0

; Total number of measure sections to create
; Add 1 to toal if FirstIndex=1
TotalMeasures=#TotalBars#

; Total number of meter sections to create
; This should match 'TotalMeasures' above
TotalMeters=#TotalBars#
When you load 'GenerateMeasures.ini', the script will read your template, replace all instances of 'Repeat#' with the 'FirstIndex' value, then increment the value by one, and repeat the process as many times as set by 'TotalMeasures'. It will save the resulting file in @Resources with the name 'My#Template#Measures.inc' and deactivate itself.

The process to automatically generate meters is the same as above. When you load 'GenerateMeters.ini', it creates 'My#Template#Meters.inc' in the @Resources folder (based on your meter template), and deactivates itself.

These generator skins uses 'OnCloseAction=' to open the newly written file in your default text editor; so you can verify they are correct.

However, when you use this method in your skin, you do not want to open the files, but instead read the new files for use in your skin. So be sure to remove (or comment out) 'OnCloseAction=' when using in your skin. ** See the 2nd sentence below! **

USING THE GENERATORS IN A SKIN

I made the TemplateGenerators folder 'modular' so to use them, copy the entire ‘TemplateGenerators’ folder to your skin's root level folder. Everything you need to automatically generate measures and meters from templates is in that folder.

IMPORTANT: Comment out (add a ; to the beginning of the line) of 'OnCloseAction=…' in the [Rainmeter] sections of both generator skins to prevent opening the editor. This was already done in the GoogleNews and Visualizer demo packages.

Then create your skin with the 'OnCloseAction', 'RunOnce' measure, and the '@includeyadah-yadah=#@#My...' for the generated measures and meters files. Create your template files in the 'TemplateGenerators\Templates' folder, if you are not using one of the ready-made templates. Then set your variables in 'RepeatingVariables.inc'.

That's it!

The visualizer skin below shows how to integrate this method into almost any skin. This is the entire working code for a skin that may have hundreds of measures and meters.

Code: Select all

[Rainmeter]
AccurateText=1
Update=16
MiddleMouseUpAction=[!Refresh]
OnRefreshAction=[!ZPos -1]

; This resets the skin so that measures/meters are generated when the skin is loaded
OnCloseAction=[!WriteKeyValue RunOnce Disabled 0]

ContextTitle="Edit Visualizer Variables"
ContextAction=["#@#VisualizerVariables.inc"]
ContextTitle2="Update Visualizer"
ContextAction2=[!DeactivateConfig][!ActivateConfig "#RootConfig#" "bars.ini]

[Variables]
@includeVars=#@#VisualizerVariables.inc

; This measure runs the skins to generate the number of measures and meters defined in your variables, then disables itself and refreshes the skin
[RunOnce]
Measure=Calc
Formula=Counter
IfEqualValue=1
IfEqualAction=[!ActivateConfig "#RootConfig#\TemplateGenerators" "GenerateMeasures.ini"][!ActivateConfig "#RootConfig#\TemplateGenerators" "GenerateMeters.ini"]
IfAboveValue=1
IfAboveAction=[!WriteKeyValue RunOnce Disabled 1][!Refresh]
Disabled=1

[MeasureaudioParent]
Measure=Plugin
Type=Parent
Plugin=AudioAnalyzer_1_1
Processing=proc1
Processing_proc1=channels auto | handlers fft,resampler,blur,transform,fir,mapper
Handler_fft=type fft |attack 50 |decay 300 |binWidth 25 |cascadesCount 5 | testRandom 0
Handler_resampler=type BandResampler |source fft |freqList flist | proportionalValues 1| minCascade 0 | maxCascade 2
Handler_blur=type weightedBlur |source resampler | minRadius 0 | maxRadius 5 | MinRadiusAdaptation 1
Handler_transform=type BandCascadeTransformer |source blur | minWeight 0 | mixFunction product | weightFallback 0 | targetWeight 100
Handler_fir=type FiniteTimeFilter |source transform | smoothingFactor 5 | smoothingCurve flat
Handler_mapper=type LogarithmicValueMapper |source fir | offset -0.1| sensitivity 40
FreqList_flist=log #TotalBars# 50 17000

@includeBarMeasures=#@#MyAudioAnalyzerMeasures.inc

@includeBarMeters=#@#MyAudioAnalyzerMeters.inc

[MeterDummy]
Meter=String
#@#VisualizerVariables.inc

Code: Select all

; Unload/Re-Load 'TemplateVisualizer\Visualizer\Bars.ini' skin after making changes
[Variables]
Gap=2
BarWidth=4
BarHeight=300
MyBarColor=222,222,222
TotalBars=(#ScreenAreaWidth#/(#BarWidth#+#Gap#))
valID=mapper
NOTES ON USE
** Notice that there is a [MeterDummy] section that MUST be in your skin since initially there are no meters until the generator skin creates them.

You should include the 'OnCloseAction' in the [Rainmeter] section so the skin is ready to generate measures and meters when it is reloaded, since it is possible the ‘RepeatingVariables.inc’ file may be edited separately.

The [RunOnce] measure does all the work and must be included. Along with the @includes for the generated measures and meters.

Those are the only required items, but the rest of your skin can contain other items such as backgrounds, controls, etc. If there are other meters in your skin then you do not need the [MeterDummy] section.

TWO DEMONSTRATION PACKAGES

I created 2 example packages showing how you might use the repeating template generators in your own skins.

AUDIO ANALYZER VISUALIZER
For audio visualizers which usually use 1 incremented measure and 1 related meter, it can create @include files with the number of repeating measures and meters, on-the-fly, for use in a visualizer. The demo creates a bar visualizer with as many bars and gaps as needed to fill your screen area width. This could be hundreds of measures and meters depending on your screen resolution.
See 'TemplateVisualizer' demo package.

GOOGLE NEWS WEBPARSER
For webparser skins, the demo will create title/link/pubdate (3 indexes) of measures and meters, but as many as needed can be created. It uses a few variables and a formula to automatically calculate the StringIndex values.

You know how many captures in each item, and each capture is given a descending 'index' value starting with the last capture of the item (ex.: TitleIndex=-2, LinkIndex=-1, DateIndex=0)). Those values are used in a formula for that capture's StringIndex; ex. StringIndex=(#Captures#*Repeat#+#TitleIndex#) etc.. It can dynamically create any number of measures and meters as needed on-the-fly.
See 'TemplateGoogleNews' package.


Any ideas for improvement are welcome.
You do not have the required permissions to view the files attached to this post.