Section Numbers
numbers utility functions.
Summary
Return type | Function and summary |
---|---|
int | BinNot(int a, int bits) NOT bitwise operation MyrScript comes with BinAnd(...), BinOr(...), BinXor(...), BinShl(...), BinShr(...) but not the NOT bitwise operation. |
string | CompareMyrScriptVersion(string version) Is MyrScript =, < or > version x.y.z? |
string | CompareVersions(string a, string b) Compare two version numbers in x.y.z string format. |
number | EnterNumber(string text, number defaultValue, number minValue, number maxValue) Ask the user to enter a number. |
number, number | EnterXY(string prompt, number minX, number maxX, number stepX, number factoryX, number currentX, number minY, number maxY, number stepY, number factoryY, number currentY, string displayValuesMask) Ask to enter X,Y coordinates in a plotter with X and Y axis. |
boolean | IsPowerOfTwo(number n) Is n a number power of 2? |
number | NumberOfDecimals(number n) Get the number of decimals of a number |
string, int | SMPitchToString(int pitch, int preferredAccidental, int compatible, string preferredNote) Extension and bugfix for builtin PitchToString function. |
int, int | SMStringToPitch(string name) Extension and bugfix for builtin StringToPitch function. |
int | bin2int(string s, boolean signed) Converts binary number (string containing only "0" and "1") to number. |
number | bound(number n, number mini, number maxi) Ensure number n is within bounds [mini:maxi]. |
number | cm_to_dpi72(number n) Convert a centimeter value into 72 DPI pixels. |
number | dpi72_to_cm(number n) Convert a 72 DPI pixel value into centimeters. |
int | hex2int(string s, boolean signed) Converts hexadecimal number (string containing 0-9a-fA-f) to number. |
number | round(number num, int numberOfDecimals) Round a number. |
string | tobin(int n, int size, string separator) Converts a number to a binary string (containing only 0 and 1) |
string | tohex(int n, int size, string separator) Converts a number to hexadecimal string Shortcut to format("%x", n) with more options and no 32-bit limit |
Functions
CompareMyrScriptVersion(string version)
Is MyrScript =, < or > version x.y.z?
As Application.MyrScriptVersion
is a string, comparison may fail in the future, "10.1.1" is greater than "9.9.9" but string comparison will stay it's lower (because "1" < "9").
Parameter | Type | Default | Description |
---|---|---|---|
version | string | Version to compare, in x.y.z form (e.g.: "1.6.9") |
- Return
- string:
"<", ">"
or"="
ifApplication.MyrScriptVersion
is lower, greater or equal to version.
CompareVersions(string a, string b)
Compare two version numbers in x.y.z string format.
Parameter | Type | Default | Description |
---|---|---|---|
a | string | a | |
b | string | b |
- Return
- string:
"<", ">"
or"="
if a is lower, greater or equal to b. - Example
CompareVersions("9.9.7", "9.9.6") --> ">"
round(number num, int numberOfDecimals)
Round a number.
Parameter | Type | Default | Description |
---|---|---|---|
num | number | Number to round() | |
numberOfDecimals | int | 0 | Number of decimals |
- Return
- number: the rounded number, i.e.
floor(number + 0.5)
for an integer (no decimals)
NumberOfDecimals(number n)
Get the number of decimals of a number
Parameter | Type | Default | Description |
---|---|---|---|
n | number | n |
- Return
- number: number
bound(number n, number mini, number maxi)
Ensure number n is within bounds [mini:maxi].
bound(n, mini, maxi)
is a syntaxic sugar for max(min(n, maxi), mini)
Parameter | Type | Default | Description |
---|---|---|---|
n | number | n | |
mini | number | Minimal value | |
maxi | number | Maximal value |
- Return
- number: Bounded n.
dpi72_to_cm(number n)
Convert a 72 DPI pixel value into centimeters.
Parameter | Type | Default | Description |
---|---|---|---|
n | number | n |
- Return
- number: number
cm_to_dpi72(number n)
Convert a centimeter value into 72 DPI pixels.
Parameter | Type | Default | Description |
---|---|---|---|
n | number | n |
- Return
- number: number
SMPitchToString(int pitch, int preferredAccidental, int compatible, string preferredNote)
Extension and bugfix for builtin PitchToString function.
- Allow pitches out of 0..127 bounds, but keep in mind that HA can't handle them!
PitchToString(11)
gives 11,SMPitchToString(11)
returns B-1
Parameter | Type | Default | Description |
---|---|---|---|
pitch | int | pitch | |
preferredAccidental | int | AUTO | Preferred accidental in MSDefine. HA can handle A## and Bbb as note names. If a string is provided, then if it contains a "b" or a "#", it's interpreted as FLAT or SHARP constants. |
compatible | int | ffalse | If true return value that HA can handle, 11 instead of B-1, a value in bounds. |
preferredNote | string | "" | English note "A" to "G", if no preferred accidental or AUTO, select the enharmonic from the preferred note. If result is "A" and preferred note is "G" then result will be "G##", unless allowDoubleAccidental is false. |
- Returns
- string: Anglo-saxon note name followed by octave, e.g. "Ab5".
- int: Accidental for the note, one of MSDefine NATURAL,FLAT,SHARP,DOUBLE_FLAT,DOUBLE_SHARP constants.
SMStringToPitch(string name)
Extension and bugfix for builtin StringToPitch function.
- Allow pitches out of 0..127 bounds
StringToPitch("B-1")
gives the strange 71 result,SMStringToPitch("B-1")
returns 11- Handles double-sharp (## or x) and double-flat (bb)
Parameter | Type | Default | Description |
---|---|---|---|
name | string | English not name followed by octave. If octave is missing, considers it's the 4th octave. |
- Returns
- int: May be out of 0..127 range.
- int: Preferred accidental, one of AUTO, SHARP, FLAT... constants from MSDefine
EnterNumber(string text, number defaultValue, number minValue, number maxValue)
Ask the user to enter a number.
Use EnterString(...)
, and checks that the entered is a number within bounds. In case of error, ask again.
Parameter | Type | Default | Description |
---|---|---|---|
text | string | Text to dispaly in prompt popup | |
defaultValue | number | nil | A pre-defined value, or nil to force user to enter a value. |
minValue | number | nil | If specified, entered value is bound by min and max. |
maxValue | number | nil | If specified, entered value is bound by min and max. |
- Return
- number: or
nil
if user cancelled the choice.
EnterXY(string prompt, number minX, number maxX, number stepX, number factoryX, number currentX, number minY, number maxY, number stepY, number factoryY, number currentY, string displayValuesMask)
Ask to enter X,Y coordinates in a plotter with X and Y axis.
It's handy when you ask for 2 values, as X and Y offsets.
Parameter | Type | Default | Description |
---|---|---|---|
prompt | string | A prompt text above the plotter, nil means no prompt text. | |
minX | number | 0 | Minimal value for X |
maxX | number | 0 | Maximal value for X |
stepX | number | 1 | Step between two consecutive X possible values. e.g. 1 for a value from 0 to 127, 0.01 for fine adjustement of object position in the score... The bigger the faster mouse wheel and keyboard will change the value. |
factoryX | number | currentX | Default (factory) value for X, a dual click on the slider goes back to this value |
currentX | number | factoryX | Current displayed X value |
minY | number | 0 | Minimal value for Y |
maxY | number | 0 | Maximal value for Y |
stepY | number | 1 | Step between two consecutive Y possible values. e.g. 1 for a value from 0 to 127, 0.01 for fine adjustement of object position in the score... The bigger the faster mouse wheel and keyboard will change the value. |
factoryY | number | currentY | Default (factory) value for Y, a dual click on the slider goes back to this value |
currentY | number | factoryY | Current displayed Y value |
displayValuesMask | string | "%x, %y" | Mask for displaying values, %x and %y are replaced by values. |
- Returns
- number: X,
nil
if user cancelled the dialog - number: Y,
nil
if user cancelled the dialog - Errors
- if minX >= maxX or minY >= maxY
- if factoryX or currentX are outside of [minX:maxX] range
- if factoryY or currentY are outside of [minY:maxY] range
- if factoryX and currentX, or factoryY and currentY are
nil
. - Example
-- Create a new document with one staff, go to Page mode. -- This code moves first staff name position -- X between -50 and 50, Y between -30 and 30, steps are 1 -- Negative Y go up, positive Y go down. -- Factory position is 0,0, current is 5,-24 Include "SMUtils" x,y = EnterXY("Test XY", -50, 50, 1, 0, 5, -30, 30, 1, 0, -24, false, true, {FrontScore()}, function(x,y,args) local staff = args[1].FirstStaff staff.TitleXOffset, staff.TitleYOffset = x, y Application.UpdateScores() end, "%x px, %y px" -- not mandatory, just show that unit is pixel (in 72 DPI) )
IsPowerOfTwo(number n)
Is n a number power of 2?
Parameter | Type | Default | Description |
---|---|---|---|
n | number | n |
- Return
- boolean:
true
if n equals 1, 2, 4, 8, 16...,nil
(for false) if not. - Error
- if n is not a number
bin2int(string s, boolean signed)
Converts binary number (string containing only "0" and "1") to number.
Shortcut to tonumber(s, 2)
with protection and no 32-bit limit
Parameter | Type | Default | Description |
---|---|---|---|
s | string | s | |
signed | boolean | false | Signed or unsigned integer? |
- Return
- int: 0 or positive integer
- Error
- if s is not a binary string
hex2int(string s, boolean signed)
Converts hexadecimal number (string containing 0-9a-fA-f) to number.
Shortcut to tonumber(s, 16)
with protection and no 32-bit limit.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | s | |
signed | boolean | false | Signed or unsigned integer? |
- Return
- int: 0 or positive integer
- Error
- if s is not an hexadecimal string
tohex(int n, int size, string separator)
Converts a number to hexadecimal string Shortcut to format("%x", n)
with more options and no 32-bit limit
Parameter | Type | Default | Description |
---|---|---|---|
n | int | Positive, 0 or negative. | |
size | int | 1 | Pad result with zeros to ensure result string length is a multiple of size |
separator | string | "" | Separate groups of size hex chars? |
- Return
- string: string
- Error
- if n is not a number or size < 1 or separator is not a string.
tobin(int n, int size, string separator)
Converts a number to a binary string (containing only 0 and 1)
Parameter | Type | Default | Description |
---|---|---|---|
n | int | n | |
size | int | 1 | Pad result with zeros to ensure result string length is a multiple of size |
separator | string | "" | Separate groups of size binary chars? |
- Return
- string: string
- Error
- if n is not a number or size < 1 or separator is not a string.
BinNot(int a, int bits)
NOT bitwise operation MyrScript comes with BinAnd(...), BinOr(...), BinXor(...), BinShl(...), BinShr(...)
but not the NOT bitwise operation.
Here it is!
Parameter | Type | Default | Description |
---|---|---|---|
a | int | a | |
bits | int | 32 |
- Return
- int: int
- Example
local a = 5 -- 0101 print(bit_not(a, 4)) --> 10 (1010)