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/14 21:13] – [Include Snippets] Added more info and second documentation _kimozaic_tips_and_tricks [2021/07/01 21:41] (current) – Added „ About State-Saving“ section _ki
Line 1: Line 1:
 ====== Mozaic: Scripting Tips & Tricks ====== ====== Mozaic: Scripting Tips & Tricks ======
-~~NOTOC~~ {{tag>Mozaic midi_scripting}}+~~NOTOC~~ {{tag>Mozaic midi_scripting tips_and_tricks}}
  
 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=====
 +<html><p align = "right"><small><i>From -ki</i></small></p></html>
 +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 =====
Line 80: Line 93:
   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 116: Line 131:
 </code> </code>
  
 +
 +\\ 
 +===== Output Fixed Point Values in Labels =====
 +<html><p align = "right"><small><i>From -ki</i></small></p></html>
 +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>
  
 \\  \\ 
Line 143: Line 189:
 @End @End
 </code> </code>
 +
 +\\ 
 +===== About State-Saving =====
 +<html><p align = "right"><small><i>From -ki</i></small></p></html>
 +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 =====
 +<html><p align = "right"><small><i>From wim</i></small></p></html>
 +
 +<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 =====
 +<html><p align = "right"><small><i>From wim</i></small></p></html>
 +
 +<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>
  
 \\  \\ 
Line 157: Line 278:
   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 =====
 +<html><p align = "right"><small><i>From -ki</i></small></p></html>
 +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 279: Line 420:
  
  
 +\\ 
 +=====  Remove or add an entry inside an array ===== 
 +<html><p align = "right"><small><i>From -ki</i></small></p></html>
 +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> <html><p align = "right"><small><i>From -ki</i></small></p></html>
Line 386: Line 569:
 I tried to make them as 'developer-friendly' as possible and also did extensive testing on each of them. The documentation wiki pages show the snippets source at the bottom since its easier to read on the wiki than inside Mozaic. I tried to make them as 'developer-friendly' as possible and also did extensive testing on each of them. The documentation wiki pages show the snippets source at the bottom since its easier to read on the wiki than inside Mozaic.
  
-^PatchStorage  ^Wiki ^OnLoad ^Callbacks ^Info ^ +^PatchStorage  ^Wiki ^OnLoad ^Other ^Callbacks ^Info ^ 
-| [[https://patchstorage.com/pad-manager-include/|Pad & Shift Manager]] | [[mozaic_include_pad_and_shift_manager|Documentation]]| optional vars \\ one event | 2 | Detect single-/double-/triple-tap and hold interactions for pads or shift| +| [[https://patchstorage.com/pad-manager-include/|Pad & Shift Manager]] | [[mozaic_include_pad_and_shift_manager|Documentation]]| optional vars \\ one call| - | 2 | Detect single-/double-/triple-tap and hold interactions for pads or shift
-| [[https://patchstorage.com/migration-manager-include/|Migration Manager]] | [[mozaic_include_migration_mananger|Documentation]] | two events | 4 | Migrate script parameters between different script versions |+| [[https://patchstorage.com/active-notes-tracker/|Active Notes Tracker]] | [[mozaic_include_active_notes_tracker|Documentation]] | optional var \\ one call | note-on / note-off \\ events | - | Manages a list of active notes, velocities and durations for each channel 
 +| [[https://patchstorage.com/migration-manager-include/|Migration Manager]] | [[mozaic_include_migration_mananger|Documentation]] | two calls| - | 4 | Migrate script parameters between different script versions |
  • mozaic_tips_and_tricks.1594725230.txt.gz
  • Last modified: 2020/07/14 21:13
  • by _ki