Section Numbers

numbers utility functions.

Summary

Return typeFunction and summary
intBinNot(int a, int bits)
NOT bitwise operation MyrScript comes with BinAnd(...), BinOr(...), BinXor(...), BinShl(...), BinShr(...) but not the NOT bitwise operation.
stringCompareMyrScriptVersion(string version)
Is MyrScript =, < or > version x.y.z?
stringCompareVersions(string a, string b)
Compare two version numbers in x.y.z string format.
numberEnterNumber(string text, number defaultValue, number minValue, number maxValue)
Ask the user to enter a number.
number, numberEnterXY(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.
booleanIsPowerOfTwo(number n)
Is n a number power of 2?
numberNumberOfDecimals(number n)
Get the number of decimals of a number
string, intSMPitchToString(int pitch, int preferredAccidental, int compatible, string preferredNote)
Extension and bugfix for builtin PitchToString function.
int, intSMStringToPitch(string name)
Extension and bugfix for builtin StringToPitch function.
intbin2int(string s, boolean signed)
Converts binary number (string containing only "0" and "1") to number.
numberbound(number n, number mini, number maxi)
Ensure number n is within bounds [mini:maxi].
numbercm_to_dpi72(number n)
Convert a centimeter value into 72 DPI pixels.
numberdpi72_to_cm(number n)
Convert a 72 DPI pixel value into centimeters.
inthex2int(string s, boolean signed)
Converts hexadecimal number (string containing 0-9a-fA-f) to number.
numberround(number num, int numberOfDecimals)
Round a number.
stringtobin(int n, int size, string separator)
Converts a number to a binary string (containing only 0 and 1)
stringtohex(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").

ParameterTypeDefaultDescription
versionstring Version to compare, in x.y.z form (e.g.: "1.6.9")
Return
string: "<", ">" or "=" if Application.MyrScriptVersion is lower, greater or equal to version.

CompareVersions(string a, string b)

Compare two version numbers in x.y.z string format.

ParameterTypeDefaultDescription
astring a
bstring 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.

ParameterTypeDefaultDescription
numnumber Number to round()
numberOfDecimalsint0Number 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

ParameterTypeDefaultDescription
nnumber 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)

ParameterTypeDefaultDescription
nnumber n
mininumber Minimal value
maxinumber Maximal value
Return
number: Bounded n.

dpi72_to_cm(number n)

Convert a 72 DPI pixel value into centimeters.

ParameterTypeDefaultDescription
nnumber n
Return
number: number

cm_to_dpi72(number n)

Convert a centimeter value into 72 DPI pixels.

ParameterTypeDefaultDescription
nnumber 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

ParameterTypeDefaultDescription
pitchint pitch
preferredAccidentalintAUTOPreferred 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.
compatibleintffalseIf true return value that HA can handle, 11 instead of B-1, a value in bounds.
preferredNotestring""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)

ParameterTypeDefaultDescription
namestring 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.

ParameterTypeDefaultDescription
textstring Text to dispaly in prompt popup
defaultValuenumbernilA pre-defined value, or nil to force user to enter a value.
minValuenumbernilIf specified, entered value is bound by min and max.
maxValuenumbernilIf 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.

See sample code below

ParameterTypeDefaultDescription
promptstring A prompt text above the plotter, nil means no prompt text.
minXnumber0Minimal value for X
maxXnumber0Maximal value for X
stepXnumber1Step 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.
factoryXnumbercurrentXDefault (factory) value for X, a dual click on the slider goes back to this value
currentXnumberfactoryXCurrent displayed X value
minYnumber0Minimal value for Y
maxYnumber0Maximal value for Y
stepYnumber1Step 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.
factoryYnumbercurrentYDefault (factory) value for Y, a dual click on the slider goes back to this value
currentYnumberfactoryYCurrent displayed Y value
displayValuesMaskstring"%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?

ParameterTypeDefaultDescription
nnumber 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

ParameterTypeDefaultDescription
sstring s
signedbooleanfalseSigned 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.

ParameterTypeDefaultDescription
sstring s
signedbooleanfalseSigned 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

ParameterTypeDefaultDescription
nint Positive, 0 or negative.
sizeint1Pad result with zeros to ensure result string length is a multiple of size
separatorstring""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)

ParameterTypeDefaultDescription
nint n
sizeint1Pad result with zeros to ensure result string length is a multiple of size
separatorstring""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!

ParameterTypeDefaultDescription
aint a
bitsint32 
Return
int: int
Example


 local a = 5 -- 0101
 print(bit_not(a, 4)) --> 10 (1010)