Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
mozaic_tips_and_tricks [2020/03/01 22:03] – Added Knob double-tap Support _ki | mozaic_tips_and_tricks [2020/11/17 02:15] – Formatting _ki | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Mozaic: Scripting Tips & Tricks ====== | ====== Mozaic: Scripting Tips & Tricks ====== | ||
- | {{tag> | + | ~~NOTOC~~ |
This wiki page contains programming tips & tricks for [[mozaic_plugin_engine|Mozaic]] | This wiki page contains programming tips & tricks for [[mozaic_plugin_engine|Mozaic]] | ||
Line 9: | Line 9: | ||
* [[#Store two positive Values into a single Variable]] | * [[#Store two positive Values into a single Variable]] | ||
* [[#Store three unsigned Bytes into a single Variable]] | * [[#Store three unsigned Bytes into a single Variable]] | ||
+ | * [[#Output Fixed Point Values in Labels]] | ||
* [[#Some Best Practice Tips]] | * [[#Some Best Practice Tips]] | ||
+ | * [[#Detect Long or Short Pad Taps]] | ||
+ | * [[#Detect Long or Short SHIFT Button Taps]] | ||
* [[# | * [[# | ||
+ | * [[#Dynamic Letters for Labeling]] | ||
* [[#Use the SHIFT Button to toggle to HELP View]] | * [[#Use the SHIFT Button to toggle to HELP View]] | ||
* [[#Use a Knob to toggle 16 Pads View to HELP View]] | * [[#Use a Knob to toggle 16 Pads View to HELP View]] | ||
* [[#Knob double-tap Support]] | * [[#Knob double-tap Support]] | ||
+ | * [[#Using Logic Operators in Expressions instead of IF cases]] | ||
+ | * [[#Using Inc and Dec in Expressions]] | ||
* [[# | * [[# | ||
+ | * [[#Include Snippets]] | ||
===== Two dimensional Arrays ===== | ===== Two dimensional Arrays ===== | ||
Line 113: | Line 120: | ||
</ | </ | ||
+ | |||
+ | \\ | ||
+ | ===== Output Fixed Point Values in Labels ===== | ||
+ | < | ||
+ | When outputting floating point values into labels (knobs, pads, titles) Moazic generates either a 4 digit fraction string | ||
+ | like 42.5000 (if a remainder present) or an integer value without fraction. | ||
+ | |||
+ | \\ | ||
+ | To always output a positive float with two digits (even when integer), one needs to split the input value and output the computed integer parts individually: | ||
+ | < | ||
+ | n = Round value * 100 | ||
+ | f = Div n, 100 | ||
+ | d = n-f*100 | ||
+ | r1 = Div d, 10 | ||
+ | r2 = d % 10 | ||
+ | LabelKnob 0, {B: | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | To always output a negative and positive float with a single digit, the split needs to work on the absolute value | ||
+ | and a conditional statement is used to output both variants: | ||
+ | < | ||
+ | n = Round Abs(value * 10) | ||
+ | f = Div n, 10 | ||
+ | r = (n-f*10) % 10 | ||
+ | if number> | ||
+ | LabelKnob 1, {N: +},f,{.},r | ||
+ | else | ||
+ | LabelKnob 1, {N: +},f,{.},r | ||
+ | endif | ||
+ | </ | ||
\\ | \\ | ||
Line 141: | Line 179: | ||
</ | </ | ||
+ | |||
+ | \\ | ||
+ | ===== Detect Long or Short Pad Taps ===== | ||
+ | < | ||
+ | |||
+ | < | ||
+ | @OnLoad | ||
+ | FillArray downStart, | ||
+ | pressTime = 250 | ||
+ | @End | ||
+ | |||
+ | @OnPadDown | ||
+ | downStart[LastPad] = SystemTime | ||
+ | @End | ||
+ | |||
+ | @OnPadUp | ||
+ | pad = LastPad | ||
+ | if SystemTime - downStart[pad] < pressTime | ||
+ | Log {Short tap pad }, pad | ||
+ | else | ||
+ | Log {Long tap pad }, pad | ||
+ | endif | ||
+ | @End | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ===== Detect Long or Short SHIFT Button Taps ===== | ||
+ | < | ||
+ | |||
+ | < | ||
+ | @OnLoad | ||
+ | shiftStart = 0 | ||
+ | pressTime | ||
+ | @End | ||
+ | |||
+ | @OnShiftDown | ||
+ | shiftStart = SystemTime | ||
+ | @End | ||
+ | |||
+ | @OnShiftUp | ||
+ | if SystemTime - shiftStart < pressTime | ||
+ | Log {Short tap SHIFT} | ||
+ | else | ||
+ | Log {Long tap SHIFT} | ||
+ | endif | ||
+ | @End | ||
+ | </ | ||
\\ | \\ | ||
Line 154: | Line 240: | ||
LabelPad 0,{Line 1},{ | LabelPad 0,{Line 1},{ | ||
LabelPad 1,{Upper},{ -------------- },{Lower} | LabelPad 1,{Upper},{ -------------- },{Lower} | ||
+ | @End | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | ===== Dynamic Letters for Labeling ===== | ||
+ | < | ||
+ | The NoteName function of Moazic can be used to dynamically output the letters A-G inside the label string definitions of Knobs, Pads or Logs. This allows to construct labels like ‚Bank A‘ to ‚Bank D‘, or ‚Preset A-1‘ to ‚Preset F-8‘. | ||
+ | |||
+ | < | ||
+ | @OnLoad | ||
+ | ShowLayout 2 | ||
+ | ABCDEFG | ||
+ | |||
+ | for i = 0 to 15 | ||
+ | row = 1 + (i>7) | ||
+ | id = i%8 | ||
+ | if (id < 7) | ||
+ | LabelPad i, {Pad },(NoteName ABCDEFG[id], | ||
+ | endif | ||
+ | endfor | ||
@End | @End | ||
</ | </ | ||
Line 224: | Line 330: | ||
- | If a knob is only used to toggle between two states like a switch, the code for double-tap detection would look like: | + | If a knob is only used to toggle between two states like a switch |
< | < | ||
+ | @OnKnobChange | ||
+ | _knob = LastKnob | ||
+ | _val = GetKnobValue _knob | ||
+ | | ||
+ | if _knob = TOGGLE_KNOB | ||
+ | if _val = 64 | ||
+ | isOn = not isOn | ||
+ | SetKnobValue TOGGLE_KNOB, | ||
+ | else | ||
+ | isOn = _val > 64 | ||
+ | endif | ||
+ | | ||
+ | if isOn | ||
+ | LabelKnob TOGGLE_KNOB, | ||
+ | else | ||
+ | LabelKnob TOGGLE_KNOB, | ||
+ | endif | ||
+ | endif | ||
+ | @End | ||
+ | |||
@OnLoad | @OnLoad | ||
ShowLayout 2 | ShowLayout 2 | ||
Line 246: | Line 372: | ||
else | else | ||
LabelKnob TOGGLE_KNOB, | LabelKnob TOGGLE_KNOB, | ||
- | endif | ||
- | @End | ||
- | |||
- | @OnKnobChange | ||
- | _knob = LastKnob | ||
- | _val = GetKnobValue _knob | ||
- | | ||
- | if _knob = TOGGLE_KNOB | ||
- | if _val = 64 | ||
- | isOn = not isOn | ||
- | SetKnobValue TOGGLE_KNOB, | ||
- | else | ||
- | isOn = _val > 64 | ||
- | endif | ||
- | | ||
- | if isOn | ||
- | LabelKnob TOGGLE_KNOB, | ||
- | else | ||
- | LabelKnob TOGGLE_KNOB, | ||
- | endif | ||
endif | endif | ||
@End | @End | ||
Line 275: | Line 381: | ||
* //isOn = _val > 64// assigns either 0 or 1 to //isOn// depending on _val | * //isOn = _val > 64// assigns either 0 or 1 to //isOn// depending on _val | ||
+ | |||
+ | |||
+ | \\ | ||
+ | ===== Using Logic Operators in Expressions instead of IF cases ===== | ||
+ | < | ||
+ | |||
+ | Boolean expressions compute to either 1 or 0, which are the internal values for TRUE/FALSE or YES/NO. | ||
+ | Using this behavior in expressions allows to get rid of some IFs, but on the other hand makes the script a bit more complicated to read and understand for novices. | ||
+ | |||
+ | Instead of the lengthy | ||
+ | < | ||
+ | // showHelp | ||
+ | |||
+ | If showHelp | ||
+ | ShowLayout 4 | ||
+ | Else | ||
+ | ShowLayout 2 | ||
+ | Endif | ||
+ | </ | ||
+ | |||
+ | On can write | ||
+ | < | ||
+ | ShowLayout 2 + 2*showHelp | ||
+ | </ | ||
+ | |||
+ | . | ||
+ | |||
+ | Here second example where in the 22 knob layout the left most 8 knobs of both rows are used as channel knobs for channels 1 to 16. Since there are 11 knobs per row, the channel computed for the lower row needs to be adjusted: | ||
+ | < | ||
+ | chan = LastKnob + 1 | ||
+ | If LastKnob >= 8 | ||
+ | chan = chan - 3 | ||
+ | Endif | ||
+ | </ | ||
+ | |||
+ | This can be written shorter as: | ||
+ | < | ||
+ | chan = LastKnob +1 - 3*(LastKnob> | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | ===== Using Inc and Dec in Expressions ===== | ||
+ | < | ||
+ | |||
+ | TheOriginalPaulB discovered that Inc and Dec are working as functions. Both Inc and Dec are ‚pre-increment‘ operations like ++var in C, as the function returns the already incremented value. This allows for several interesting language constructs: | ||
+ | |||
+ | In case of a ring buffer index, one could use | ||
+ | < | ||
+ | index = (Inc index) % 64 | ||
+ | </ | ||
+ | instead of the longer construct | ||
+ | |||
+ | < | ||
+ | Inc index | ||
+ | index = index % 64 | ||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | Sometimes this trick is also applicable in array initialization, | ||
+ | < | ||
+ | idx = -1 | ||
+ | for i = 10 to 19 | ||
+ | array[Inc idx] = 2*i | ||
+ | endfor | ||
+ | </ | ||
+ | The example fills array[0] to array[9] with the values 20 to 38. To compensate for pre-increment, | ||
+ | |||
+ | \\ | ||
+ | It is also possible to construct a post-increment operation by using the inc in the expression itself, but discarding it using multiplication with 0: | ||
+ | |||
+ | < | ||
+ | idx = 0 | ||
+ | for i = 10 to 19 | ||
+ | array[idx] = 2*i + 0*(Inc idx) | ||
+ | endfor | ||
+ | </ | ||
\\ | \\ | ||
Line 293: | Line 475: | ||
@End | @End | ||
</ | </ | ||
+ | |||
+ | |||
+ | \\ | ||
+ | ===== Include Snippets ===== | ||
+ | < | ||
+ | |||
+ | On PatchStorage you can find several ' | ||
+ | |||
+ | The snippets can be seen as 'well tested library functions' | ||
+ | The snippet in turn will later call event that you need to define in your code (callbacks). | ||
+ | All of the current available snippets need a timer event to do their work, the snippets are coded in a way that allows to also to work with a timer event that you defined in your script. | ||
+ | |||
+ | I tried to make them as ' | ||
+ | |||
+ | ^PatchStorage | ||
+ | | [[https:// | ||
+ | | [[https:// | ||
+ | | [[https:// |