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 [2020/07/23 02:31] – [Include Snippets] Added optional var _kimozaic_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]]
   * [[#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]]
 +  * [[#About State-Saving]]
 +  * [[#Detect Long or Short Pad Taps]]
 +  * [[#Detect Long or Short SHIFT Button Taps]]
   * [[#Multi-Line Pad Labels]]   * [[#Multi-Line Pad Labels]]
 +  * [[#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]]
 +  * [[#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 60: 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 71: 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 80: Line 96:
   varB = 124   // allowed range 0 .. ~16000   varB = 124   // allowed range 0 .. ~16000
      
-  combinedValue = valA + valB * maxA +  combinedValue = valA + (valB * maxA
 </code>  </code> 
 +
 +For single-byte MIDI values, use 128 for maxA
  
 To later extract the values from their packed format use To later extract the values from their packed format use
Line 91: 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 116: Line 135:
 </code> </code>
  
 +
 +\\ 
 +===== Output Fixed Point Values in Labels =====
 +<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 
 +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:
 +<code>
 +  n = Round value * 100
 +  f = Div n, 100
 +  d = n-f*100
 +  r1 = Div d, 10
 +  r2 = d % 10
 +  LabelKnob 0, {B:},f,{.},r1,r2
 +</code>
 +
 +\\ 
 +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:
 +<code>
 +  n = Round Abs(value * 10)
 +  f = Div n, 10
 +  r = (n-f*10) % 10
 +  if value >=0
 +    LabelKnob 1, {N: +},f,{.},r
 +  else
 +    LabelKnob 1, {N: },f,{.},r
 +  endif
 +</code>
  
 \\  \\ 
 ===== 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 144: Line 196:
 </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 =====
 +<WRAP group><WRAP right><sup>From wim</sup></WRAP></WRAP>
 +
 +<code>
 +@OnLoad
 +  FillArray downStart,0,16
 +  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
 +</code>
 +
 +\\ 
 +
 +===== Detect Long or Short SHIFT Button Taps =====
 +<WRAP group><WRAP right><sup>From wim</sup></WRAP></WRAP>
 +
 +<code>
 +@OnLoad
 +  shiftStart = 0
 +  pressTime  = 250
 +@End
 +
 +@OnShiftDown
 +  shiftStart = SystemTime
 +@End
 +
 +@OnShiftUp
 +  if SystemTime - shiftStart < pressTime
 +    Log {Short tap SHIFT}
 +  else
 +    Log {Long  tap SHIFT}
 +  endif
 +@End
 +</code>
  
 \\  \\ 
 ===== 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 157: Line 286:
   LabelPad 0,{Line 1},{                     },{Line 2}   LabelPad 0,{Line 1},{                     },{Line 2}
   LabelPad 1,{Upper},{ -------------- },{Lower}   LabelPad 1,{Upper},{ -------------- },{Lower}
 +@End
 +</code>
 +
 +\\ 
 +===== Dynamic Letters for Labeling =====
 +<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‘.
 +
 +<code>
 +@OnLoad
 +  ShowLayout 2
 +  ABCDEFG   = [9,11,0,2,4,5,7]
 +
 +  for i = 0 to 15
 +    row = 1 + (i>7)
 +    id  = i%8
 +    if (id < 7)
 +      LabelPad i, {Pad },(NoteName ABCDEFG[id],NO),    },{Row },row
 +    endif
 +  endfor
 @End @End
 </code> </code>
Line 162: 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 196: 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 221: 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 279: 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 320: 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 357: 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 376: 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.1595435505.txt.gz
  • Last modified: 2020/07/23 02:31
  • by _ki