mozaic_tips_and_tricks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
mozaic_tips_and_tricks [2021/02/13 07:49] – [Store two positive Values into a single Variable] add parens for clarity andMIDIByte example espiegel123mozaic_tips_and_tricks [2024/04/23 19:07] (current) – Fixed depricated html snippets into WRAP _ki
Line 4: Line 4:
 This wiki page contains programming tips & tricks for [[mozaic_plugin_engine|Mozaic]] This wiki page contains programming tips & tricks for [[mozaic_plugin_engine|Mozaic]]
  
 +  * [[#NoteOn Velocity 0 Special Case]]
   * [[#Two dimensional Arrays]]   * [[#Two dimensional Arrays]]
   * [[#Multi dimensional Arrays]]   * [[#Multi dimensional Arrays]]
Line 11: Line 11:
   * [[#Output Fixed Point Values in Labels]]   * [[#Output Fixed Point Values in Labels]]
   * [[#Some Best Practice Tips]]   * [[#Some Best Practice Tips]]
 +  * [[#About State-Saving]]
   * [[#Detect Long or Short Pad Taps]]   * [[#Detect Long or Short Pad Taps]]
   * [[#Detect Long or Short SHIFT Button Taps]]   * [[#Detect Long or Short SHIFT Button Taps]]
Line 18: Line 19:
   * [[#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]]
 +  * [[#Remove or add an entry inside an array]]
   * [[#Using Logic Operators in Expressions instead of IF cases]]   * [[#Using Logic Operators in Expressions instead of IF cases]]
   * [[#Using Inc and Dec in Expressions]]   * [[#Using Inc and Dec in Expressions]]
   * [[#Calculate Standard Chords from a Root Note]]   * [[#Calculate Standard Chords from a Root Note]]
   * [[#Include Snippets]]   * [[#Include Snippets]]
 +
 +===== NoteOn Velocity 0 Special Case=====
 +<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
 +
 +The Midi Spec allows to use a NoteOn command with velocity zero instead of a NoteOff command. If this combination is received by Mozaic, this midi command is automatically converted to 
NoteOff vel 0 - even for the @OnMidiInput event and when checking MidiByte1. 
 +
 +Therefore scripts don‘t need to check for this ‚NoteOn Vel 0‘ case, as that special case is handled by Mozaic. Even when using MidiThrough, an incomming ‚NoteOn Vel 0‘ command will be converted to ‚NoteOff Vel 0‘
 +
  
 ===== Two dimensional Arrays ===== ===== Two dimensional Arrays =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP> 
 Each variable in Mozaic is in fact an array with a maximum of 1024 elements. Scripting often needs two dimensional arrays, but the variables only offer one dimensional access. Each variable in Mozaic is in fact an array with a maximum of 1024 elements. Scripting often needs two dimensional arrays, but the variables only offer one dimensional access.
  
Line 64: Line 75:
  
 ===== Multi dimensional Arrays ===== ===== Multi dimensional Arrays =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
  
 The above folding can be expanded to multiple dimension, here an example for a 3 dimensional array folded into one dimensions: The above folding can be expanded to multiple dimension, here an example for a 3 dimensional array folded into one dimensions:
Line 75: Line 86:
  
 ===== Store two positive Values into a single Variable ===== ===== Store two positive Values into a single Variable =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP> 
 It is possible to store two positive values in a single variable if their product is less than 16.777.216 It is possible to store two positive values in a single variable if their product is less than 16.777.216
 and they both have a known maximum value. and they both have a known maximum value.
Line 97: Line 109:
  
 ===== Store three unsigned Bytes into a single Variable ===== ===== Store three unsigned Bytes into a single Variable =====
-<html><p align = "right"><small><i>From <a href="https://forum.audiob.us/discussion/comment/724913/#Comment_724913">espiegel123 @ Audiobus Forum </a></i></small></p></html>+<WRAP group><WRAP right><sup> From [[https://forum.audiob.us/discussion/comment/724913/#Comment_724913
 +Comment of espiegel123 @ Audiobus Forum]]</sup></WRAP></WRAP>
  
 Storing of 3 unsigned bytes (J,K & L, each with the range of 0-255) into a single 24bit integer can be done by using the packing formular: Storing of 3 unsigned bytes (J,K & L, each with the range of 0-255) into a single 24bit integer can be done by using the packing formular:
Line 125: Line 138:
 \\  \\ 
 ===== Output Fixed Point Values in Labels ===== ===== Output Fixed Point Values in Labels =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP> 
 When outputting floating point values into labels (knobs, pads, titles) Moazic generates either a 4 digit fraction string  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. like 42.5000 (if a remainder present) or an integer value without fraction.
Line 147: Line 161:
   f = Div n, 10   f = Div n, 10
   r = (n-f*10) % 10   r = (n-f*10) % 10
-  if number>=0+  if value >=0
     LabelKnob 1, {N: +},f,{.},r     LabelKnob 1, {N: +},f,{.},r
   else   else
-    LabelKnob 1, {N: +},f,{.},r+    LabelKnob 1, {N: },f,{.},r
   endif   endif
 </code> </code>
Line 156: Line 170:
 \\  \\ 
 ===== Some Best Practice Tips ===== ===== Some Best Practice Tips =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP> 
   * Its helpfull to add the version number right into the title label. \\ And also add this version number to the script title and patchname if uploaded / updated on [[https://patchstorage.com/platform/mozaic/|PatchStorage]] so that script-users can notice the version change.   * Its helpfull to add the version number right into the title label. \\ And also add this version number to the script title and patchname if uploaded / updated on [[https://patchstorage.com/platform/mozaic/|PatchStorage]] so that script-users can notice the version change.
   * Add a short name to set the icon name. This helps when a project contains multiple Mozaic instances with different scripts   * Add a short name to set the icon name. This helps when a project contains multiple Mozaic instances with different scripts
Line 180: Line 195:
 @End @End
 </code> </code>
 +
 +\\ 
 +===== About State-Saving =====
 +<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
 +
 +All assigned variables of a script are automatically included in a saved preset and restored when reloading. To not overwrite them, use the 
 +<code>
 +  if Unassigned channel
 +    channel = 10
 +    bank = 1
 +    ....
 +  endif
 +</code>
 +construct in the scripts **OnLoad** that only set the default value if a script is freshly run using ‚Upload‘. If reloaded (and therefor some value is already assigned to ‚channel‘ ) the initialization block is skipped.
 +
 +
 +Mozaic stores some more more information in the presets/session and restores them when reloading:
 +  * Layout
 +  * Pad labels, color, latch-state
 +  * Knob labels and position
 +  * XY position
 +  * Title label above pads, knobs, XY
 +  * Rootnote and scale
 +
 +Things **not** restored during session reload:
 +  * ShortName
 +  * Timer state and settings
 +  * LFO settings
  
  
 \\  \\ 
 ===== Detect Long or Short Pad Taps ===== ===== Detect Long or Short Pad Taps =====
-<html><p align = "right"><small><i>From wim</i></small></p></html>+<WRAP group><WRAP right><sup>From wim</sup></WRAP></WRAP>
  
 <code> <code>
Line 209: Line 252:
  
 ===== Detect Long or Short SHIFT Button Taps ===== ===== Detect Long or Short SHIFT Button Taps =====
-<html><p align = "right"><small><i>From wim</i></small></p></html>+<WRAP group><WRAP right><sup>From wim</sup></WRAP></WRAP>
  
 <code> <code>
Line 232: Line 275:
 \\  \\ 
 ===== Multi-Line Pad Labels ===== ===== Multi-Line Pad Labels =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP> 
 You can force multiline pad labels by adding space-filled substrings.  You can force multiline pad labels by adding space-filled substrings. 
   * There must be enough spaces to fill the rest of the current line   * There must be enough spaces to fill the rest of the current line
Line 247: Line 291:
 \\  \\ 
 ===== Dynamic Letters for Labeling ===== ===== Dynamic Letters for Labeling =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP> 
 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‘. 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‘.
  
Line 267: Line 312:
 \\  \\ 
 ===== Use the SHIFT Button to toggle to HELP View ===== ===== Use the SHIFT Button to toggle to HELP View =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP> 
 <code> <code>
 @Description @Description
Line 301: Line 347:
 \\  \\ 
 ===== Use a Knob to toggle 16 Pads View to HELP View =====  ===== Use a Knob to toggle 16 Pads View to HELP View ===== 
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
  
 This script snippet is only applicable for the 16 pads layout (2) as this the only layout where the knobs 0-3 are at the same screen position as on the HELP page. This script snippet is only applicable for the 16 pads layout (2) as this the only layout where the knobs 0-3 are at the same screen position as on the HELP page.
Line 326: Line 372:
 \\  \\ 
 ===== Knob double-tap Support =====  ===== Knob double-tap Support ===== 
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
  
 The value returned from //**GetKnobValue**// is a floating point number, usually it will contain non-zero decimal places when the knob is turned manually. If double tapped the knob-value is set to 64 exactly.  The value returned from //**GetKnobValue**// is a floating point number, usually it will contain non-zero decimal places when the knob is turned manually. If double tapped the knob-value is set to 64 exactly. 
Line 384: Line 430:
  
  
 +\\ 
 +=====  Remove or add an entry inside an array ===== 
 +<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
 +
 +Instead of running through an array with a FOR loop to shift entries to either insert or remove an  intermedeate enry, it is way faster to use the CopyArray Mozaic function:
 +
 +<code> CopyArray source, dest, size</code>
  
 \\  \\ 
 +=== Remove an entry of an array (shift left) ===
 +
 +<code>
 +@OnLoad
 +  for i=0 to 6
 +    a[i] = i
 +  endfor
 +  Log {Before },a[0],{ },a[1],{ },a[2],{ },a[3],{ },a[4],{ },a[5],{ },a[6]
 +
 +  // Remove entry a[2] by left shift of the following entries
 +  CopyArray a[3], a[2], 6
 +  
 +  Log {After  },a[0],{ },a[1],{ },a[2],{ },a[3],{ },a[4],{ },a[5],{ },a[6]
 +@End
 +</code>
 +
 +\\ 
 +=== Insert an entry into array (shift right) ===
 +
 +Due to the inner working of CopyArray, the above trick doesn‘t work for right shifts - but one can use a temporary array to be more efficient than iterating over the array:
 +
 +<code>
 +@OnLoad
 +  for i=0 to 6
 +    a[i] = i
 +  endfor
 +  Log {Before  },a[0],{ },a[1],{ },a[2],{ },a[3],{ },a[4],{ },a[5],{ },a[6]
 +
 +  // Insert a new a[2]
 +  CopyArray a[2], _tmp, 6
 +  CopyArray _tmp, a[3], 6  
 +  a[2] = 9
 +  
 +  Log {After   },a[0],{ },a[1],{ },a[2],{ },a[3],{ },a[4],{ },a[5],{ },a[6]
 +@End
 +</code>
 =====  Using Logic Operators in Expressions instead of IF cases =====  =====  Using Logic Operators in Expressions instead of IF cases ===== 
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
  
 Boolean expressions compute to either 1 or 0, which are the internal values for TRUE/FALSE or YES/NO. Boolean expressions compute to either 1 or 0, which are the internal values for TRUE/FALSE or YES/NO.
Line 425: Line 514:
 \\  \\ 
 ===== Using Inc and Dec in Expressions =====  ===== Using Inc and Dec in Expressions ===== 
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
  
 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: 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:
Line 462: Line 551:
 \\  \\ 
 ===== Calculate Standard Chords from a Root Note ==== ===== Calculate Standard Chords from a Root Note ====
-<html><p align = "right"><small><i>From wim</i></small></p></html>+<WRAP group><WRAP right><sup>From wim</sup></WRAP></WRAP>
  
 This is a compact way to build standard chords from just a root note. It turns out that for standard western scales such as Major, Minor, Dorian, Phrygian, etc, normal chord notes can be found by starting at the root and counting up in steps of three semitones then quantizing up to the next scale tone. Repeat this for the number of notes you want in the chord (triad (3), seventh (4), ninth (5), eleventh (6), 13th (7). This is a compact way to build standard chords from just a root note. It turns out that for standard western scales such as Major, Minor, Dorian, Phrygian, etc, normal chord notes can be found by starting at the root and counting up in steps of three semitones then quantizing up to the next scale tone. Repeat this for the number of notes you want in the chord (triad (3), seventh (4), ninth (5), eleventh (6), 13th (7).
Line 481: Line 570:
 \\  \\ 
 ===== Include Snippets ===== ===== Include Snippets =====
-<html><p align = "right"><small><i>From -ki</i></small></p></html>+<WRAP group><WRAP right><sup>From -ki</sup></WRAP></WRAP>
  
 On PatchStorage you can find several 'Include Snippets' that cover specific programming problems and that are intended to be appended your own scipt code. On PatchStorage you can find several 'Include Snippets' that cover specific programming problems and that are intended to be appended your own scipt code.
  • mozaic_tips_and_tricks.1613162968.txt.gz
  • Last modified: 2021/02/13 07:49
  • by espiegel123