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
Next revisionBoth sides next revision
mozaic_tips_and_tricks [2020/01/12 20:46] – Added 'Store two positive values into a single combined variable' _kimozaic_tips_and_tricks [2020/01/12 21:16] – [Store two positive Values into a single Variable] Fixed typo _ki
Line 3: Line 3:
 This wiki page contains programming tips & tricks for [[mozaic_plugin_engine|Mozaic]] This wiki page contains programming tips & tricks for [[mozaic_plugin_engine|Mozaic]]
  
 +
 +  * [[mozaic_tips_and_tricks#Two dimensional Arrays]]
 +  * [[mozaic_tips_and_tricks#Multi dimensional Arrays]]
 +  * [[mozaic_tips_and_tricks#Store two positive Values into a single Variable]]
   * [[mozaic_tips_and_tricks#Store three unsigned Bytes into a single Variable]]   * [[mozaic_tips_and_tricks#Store three unsigned Bytes into a single Variable]]
   * [[mozaic_tips_and_tricks#Some Best Practice Tips]]   * [[mozaic_tips_and_tricks#Some Best Practice Tips]]
Line 9: Line 13:
   * [[mozaic_tips_and_tricks#Use a Knob to toggle 16 Pads View to HELP View]]   * [[mozaic_tips_and_tricks#Use a Knob to toggle 16 Pads View to HELP View]]
  
-===== Store two positive values into a single Variable =====+===== Two dimensional Arrays ===== 
 +<html><p align = "right"><small><i>From -ki</i></small></p></html> 
 +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. 
 + 
 +Its is very easy to 'fold' two or more dimensions into a normal array if the row length is fixed: 
 + 
 +<code> 
 +  xMax = 16 
 + 
 +  x = 2 
 +  y = 5 
 +  value = 42 
 + 
 +  // write the value to position x,y :  
 +  twoDimStorage[ x + xMax *y] = value 
 +</code> 
 + 
 +Retrieval of values is done using the same folding: 
 +<code>   
 +  // retrieve value from 5,3 
 +  x = 5 
 +  y = 3 
 +  value =   twoDimStorage[ x + xMax *y ] 
 +</code> 
 + 
 +Mozaics array length is limited to 1024, so 32x32 just fits - but 128x16 (often needed in midi programming) doesn‘t. 
 + 
 +\\  
 + 
 +There is a single two dimensional array in Mozaic that fits this dimensions, the note state array. Since it is special, there are separate functions: 
 +<code> 
 +  ResetNoteState initValue         // Fills all 16x128 elements with the given value 
 +   
 +  SetNoteState row, column, value  // Sets a value 
 +  value = GetNoteState row, colum  // Retrieves the value 
 +</code> 
 +There is only one array of this kind, but sometimes you Need to store multiple values. In such a case, its possible to fold these values into a single value - see the tricks below: 
 + 
 +\\  
 + 
 +===== Multi dimensional Arrays ===== 
 +<html><p align = "right"><small><i>From -ki</i></small></p></html> 
 + 
 +The above folding can be expanded to multiple dimension, here an example for a 3 dimensional array folded into one dimensions: 
 +<code> 
 +  array[x + xMax * y + xMax*yMax *z] = value 
 +</code> 
 + 
 +Since Mozaic arrays are limited to 1024 elements, xMax * yMax * zMax needs to be less or equal 1024. 
 +\\  
 + 
 +===== Store two positive Values into a single Variable =====
 <html><p align = "right"><small><i>From -ki</i></small></p></html> <html><p align = "right"><small><i>From -ki</i></small></p></html>
 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
Line 15: Line 70:
  
 <code> <code>
-  offset = 1000  // Maximum value for B plus one +  maxB = 1000  // Maximum value for B plus one 
-  valA   = 723   // allowed range 0 .. ~16000 +  valA = 723   // allowed range 0 .. ~16000 
-  varB   = 124   // allowed range 0 .. 999+  varB = 124   // allowed range 0 .. 999
      
-  combinedValue = valA * offset + valB+  combinedValue = valA * maxB + valB
 </code>  </code> 
  
 To later extract the values from their packed format use To later extract the values from their packed format use
 <code> <code>
-  valA = RoundDown combinedValue / offset +  valA = RoundDown combinedValue / maxB 
-  valB = combinedValue % offset+  valB = combinedValue % maxB
 </code> </code>
 \\  \\ 
  • mozaic_tips_and_tricks.txt
  • Last modified: 2024/04/23 19:07
  • by _ki