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 | EnterNumber2(string prompt, number minX, number maxX, number stepX, number factoryX, number currentX, boolean isInverted, boolean isVertical, string arrowsLocation, table args, function onChangeFunc, DisplayValueMask) Ask the user to enter a number, in a more flexible and controlled way than the "EnterString" method, with possible realtime preview of result before validating the choice. |
| 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, boolean invertXaxis, boolean invertYaxis, table args, function onChangeFunc, string displayValuesMask) Ask the user 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 | LinearScaling(number x, number a, number b, number c, number d) Linear scaling (or linear transformation) of \(x\) from range \([a,b]\) to \([c,d]\). |
| number | NumberOfDecimals(number n) Get the number of decimals of a number |
| number | PowerScaling(number x, number n, number a, number b, number c, number d) Power scaling of \(x\) from range \([a,b]\) to \(x^n\) in \([c,d]\). |
| number | QuadraticScaling(number x, number a, number b, number c, number d) Quadratic scaling of \(x\) from range \([a,b]\) to \([c,d]\). |
| 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. |
| number | SMtonumber(string s) Convert the leading numeric chars of a string to number. |
| number | SquareRootScaling(number x, number a, number b, number c, number d) Square root scaling of \(x\) from range \([a,b]\) to \([c,d]\). |
| 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, number zoom) Convert a centimeter value into 72 DPI pixels. |
| number | dpi72_to_cm(number n, number zoom) 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
SMtonumber(string s)
Convert the leading numeric chars of a string to number.
Lua's tonumber can't convert "213abc" to number, it returns nil.
This function will return 123, ignoring the letters.
Note that it will break a scientific notation of a number "1.234e+56"
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | s |
- Return
- number: or
nil - Example
print(tonumber("123abc")) --> 123 print(tonumber("abc123")) --> nil print(tonumber("-.5x")) --> -0.5
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") --> ">"
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.MyrScriptVersionis lower, greater or equal to version.
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, number zoom)
Convert a 72 DPI pixel value into centimeters.
| Parameter | Type | Default | Description |
|---|---|---|---|
| n | number | n | |
| zoom | number | 100 | Printing zoom (scale), in %, greater than 0, PrintSetup.Scale. |
- Return
- number: number
- See
- cm_to_dpi72
cm_to_dpi72(number n, number zoom)
Convert a centimeter value into 72 DPI pixels.
| Parameter | Type | Default | Description |
|---|---|---|---|
| n | number | n | |
| zoom | number | 100% | Printing zoom (scale), in %, greater than 0, PrintSetup.Scale. |
- Return
- number: number
- See
- dpi72_to_cm
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.
Does not handle microtonal accidentals.
- 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
nilif user cancelled the choice. - See
- EnterNumber2
EnterNumber2(string prompt, number minX, number maxX, number stepX, number factoryX, number currentX, boolean isInverted, boolean isVertical, string arrowsLocation, table args, function onChangeFunc, DisplayValueMask)
Ask the user to enter a number, in a more flexible and controlled way than the "EnterString" method, with possible realtime preview of result before validating the choice.
| 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 |
| isInverted | boolean | false | Invert increase/decrease directions (arrows, keyboard, mouse wheel...), e.g. when asked number is for a top margin. |
| isVertical | boolean | true | false for Horizontal (left/right), true for vertical (up/down on the right) arrows |
| arrowsLocation | string | nil | Location of the arrows around the number.
|
| args | table | nil | Arguments for onChangeFunc |
| onChangeFunc | function | nil | Function called each time X is modified. It can show a preview of the new value on the score. Function will receive 2 arguments : x number and args table. |
| DisplayValueMask | "%x" | Display value mask string containing %x or function that accept an unique number argument x and returns a string. |
- Return
- number: X,
nilif user cancelled the dialog - Errors
- if minX >= maxX
- if factoryX or currentX are outside of [minX:maxX] range
- if factoryX and currentX are
nil. - Example
-- Create a new document with one staff, insert one note. -- This code moves the note horizontally -- X between -100% and 100%, step = 4. -- Factory position is 0, current is symbol head shift. Include "SMUtils" sym = FrontScore().FirstStaff.FirstSymbol x = EnterNumber2("Not horizontal shift", -100, 100, 4, 0, sym.HeadShift, false, false, nil, -- inverted, vertical, orientation {FrontScore(), sym}, function(x, args) args[2].HeadShift = x args[1].Update() end, "%x %" )
EnterXY(string prompt, number minX, number maxX, number stepX, number factoryX, number currentX, number minY, number maxY, number stepY, number factoryY, number currentY, boolean invertXaxis, boolean invertYaxis, table args, function onChangeFunc, string displayValuesMask)
Ask the user 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 |
| invertXaxis | boolean | false | If true, X axis is oriented from right to left. |
| invertYaxis | boolean | false | If true, Y axis is oriented from top to bottom. |
| args | table | nil | Arguments for onChangeFunc |
| onChangeFunc | function | nil | Function called each time X or Y is modified. It can show a preview of the new values on the score. Function will receive 3 arguments : x, y (numbers) and args table. |
| displayValuesMask | string | "%x, %y" | Mask for displaying values, %x and %y are replaced by values. |
- Returns
- number: X,
nilif user cancelled the dialog - number: Y,
nilif 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:
trueif 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)
LinearScaling(number x, number a, number b, number c, number d)
Linear scaling (or linear transformation) of \(x\) from range \([a,b]\) to \([c,d]\).
Use case: convert a MIDI value between 0 and 127 to a number between 0% and 100%.
| Parameter | Type | Default | Description |
|---|---|---|---|
| x | number | x | |
| a | number | a | |
| b | number | b | |
| c | number | c | |
| d | number | d |
- Return
- number: \[ c + \frac{(d - c)(x - a)}{b - a} \]
- Error
- if \(a > b\), or \(c > d\), or \(x\) not in range \([a,b]\).
QuadraticScaling(number x, number a, number b, number c, number d)
Quadratic scaling of \(x\) from range \([a,b]\) to \([c,d]\).
Low values of \(x\) are compressed, high values are expanded.
Use case: a knob or cursor for reverb duration between 0 and 5 s. We need a more precise short duration (0.1 s, 0.3 s) than a longer one (no big difference between 4.5 s and 4.7 s).
| Parameter | Type | Default | Description |
|---|---|---|---|
| x | number | x | |
| a | number | a | |
| b | number | b | |
| c | number | c | |
| d | number | d |
- Return
- number: \[ c + (d - c) \left(\frac{x - a}{b - a}\right)^2 \]
- Error
- if \(a > b\), or \(c > d\), or \(x\) not in range \([a,b]\).
- See
- SquareRootScaling
- PowerScaling
SquareRootScaling(number x, number a, number b, number c, number d)
Square root scaling of \(x\) from range \([a,b]\) to \([c,d]\).
Inverse of QuadraticScaling, high values of \(x\) are compressed, low values are expanded.
| Parameter | Type | Default | Description |
|---|---|---|---|
| x | number | x | |
| a | number | a | |
| b | number | b | |
| c | number | c | |
| d | number | d |
- Return
- number: \[ c + (d - c) \cdot \sqrt{\frac{x - a}{b - a}} \]
- Error
- if \(a > b\), or \(c > d\), or \(x\) not in range \([a,b]\).
- See
- QuadraticScaling
- PowerScaling
PowerScaling(number x, number n, number a, number b, number c, number d)
Power scaling of \(x\) from range \([a,b]\) to \(x^n\) in \([c,d]\).
Flexible control of the non-linearity. QuadraticScaling use \(x^2\), here we add \(n\) argument:
- if \(n > 1\), low values are compressed, high values are expanded,
- if \(0 < n < 1\) low values are expanded, high values are compressed,
- if \(n = 1\) the scaling is linear.
| Parameter | Type | Default | Description |
|---|---|---|---|
| x | number | x | |
| n | number | Power, positive number | |
| a | number | a | |
| b | number | b | |
| c | number | c | |
| d | number | d |
- Return
- number: \[ c + (d - c) \cdot \left(\frac{x - a}{b - a}\right)^n \]
- Error
- if \(n \leq 0\), or \(a > b\), or \(c > d\), or \(x\) not in range \([a,b]\).