Author Topic: Programming for Musicians  (Read 12553 times)

Offline azslow3

  • Administrator
  • Hero Member
  • *****
  • Posts: 1679
Programming for Musicians
« on: May 29, 2014, 12:09:03 PM »
AZ Controller is very flexible in logic definition. This flexibility has it's price. The only way I know to describe arbitrary logic is to program this logic. Everyone with basic programming skills can skip this post. But if "conditional execution" sounds like a rocket science for you, please read on.

I will cover only bare minimum of knowledge needed to understand configuration of AZ Controller.

What is Digital Computer
Modern Digital Computer in fact still understand only 2 "digits": 0 (zero) and 1 (one). Something which can be either zero on one (and only zero or one) is called a Bit. For mathematical calculations, several Bits are used to represent one bigger number. For example, if we have 2 Bits, we can represent numbers from 0 to 3 ("0":"0" = 0, "0":"1"=1, "1":"0" = 2, "1":"1" = 3). Computers "know" how to do arithmetical operations with numbers represented by many bits, and they do that fast and precise. So they can "mix" your 100 auto tracks represented by 24bit*192000 samples pro second without problem (in simplest possible mixing that is just  a sum of 100s 24bit numbers, calculated 192000 times pro second).

But there is a big difference between very fast abacus and a computer. Computers can calculate something conditionally.  For example, it is possible to say computer: "If audio is louder then threshold, apply some gain reduction. But do not do this with silent material!"

When we look what computers can do in pure computational area, we can get feeling they are "smart". They show us complicated graphics, imitate complicated analog audio equipment,  make our voice sound better and so on. But when it comes to "thinking", we immediately understand how "stupid" they are. They are "living" in very simple world of...

Boolean logic
Just imagine everything is either TRUE of FALSE. No matter what it is. "Are you reading this text?" - "Yes, that is TRUE". "Have you already visited Mars?" - "No". "Is it going to rain next week?" - "Hmm... I do not know". It is impossible to "explain" the last answer to computer, it can not understand "may be", "i do not know", "that is better" and so on. It "understand" "Yes/True" or "No/False" only.

As soon as computer see any single, may be not even important, question on which the answer is not "True" of "False", it immediately CRASH. If that happens withing SONAR, your get SONAR crash. If that happens within the system, you get BSOD (Blue Screen Of Death).

Unlike mathematics,  where 1+1=2, the result of any logical calculations is still either true of false. There is no "more true","double true" of "less false". In programming for AZ Controller only one logical operation is used, operation "AND". And its meaning is the same as in the real world. For example:

You can record your voice if: microphone is connected to the audio interface AND audio interface is connected to computer AND computer is running recording program AND you have pressed "Record" button. So, you can record ONLY in case ALL conditions are TRUE. As soon as any single one (or several, or all) are not true (so, FALSE), the result is also FALSE. One more time, if you use logical operation AND, the result is TRUE in case (and only in case) all conditions are TRUE.

To be complete, boolean logic has 2 additional logical operations, "OR" and "NOT". But they are not used in AZ Controller.

Variables
When something is always TRUE or always FALSE, that is not very interesting. If some condition is always TRUE, we can skip the check. If it is always FALSE we can forget about consequences. We are interested in conditions which are some times valid and some times not. "Variable" is something which can have different meanings ("values") over time. But at any particular time, the variable has only one value. For example, "I_am_recording", "Microphone_is_connected", "Mixer_is_powered" are variables with possible values "True" and "False".

We are not computers and we use more that 2 words in our logic. While we can define many variables with "True" and "False" values only, "I_am_sleeping","I_am_eating","I_am_walking", more natural is to define variable "I_am" which can have values "sleeping","eating" and "walking". We should be careful there. A variable can have only one value at a time. But how can I describe that I am eating while walking? I can not just specify "I am" = "eating" and "walking", "I am" can not have 2 values at the same time. But I can define yet another value "eating_and_walking". So, the whole set of possible values will be "sleeping","eating","walking","eating_and_walking".

As I explained previously, computer logic understand only something which is either TRUE of FALSE. So, how we can use our variable "I_am" which has different values? While values are not "True" and "False", 'Is "I_am" currently "walking"?' has the answer either True or False.

In AZ Surface all variables and values correspond to some States. "SONAR" context is "Track","Console" or "Plugin", "Shift" is "Pressed" or "Not pressed", "ACT mapping" is "No mapping","EQ","Compressor","True Piano","Channel tools",etc. That is why I call each value a State. A complete list of all possible values for some variable is a State Set. Each State Set has one Current State. So, a variable is the "current state from the corresponding list of possible states". Using that terminology, a Condition is specified by just one State and can be interpreted as: "the current State in State Set 'Context' is 'Console'", which obviously can be either TRUE of FALSE.

Sequential operations
An analog mixer does everything in parallel. So, EQ setting, gain, etc. are applied at the same time to all inputs. But Digital Computers execute operations sequentially. So, it FIRST apply EQ to track ONE, THAN apply a gain to track ONE and only THEN apply EQ to track TWO and so on. That sometimes happens so fast you have no chance notice the difference (in Digital Mixers for example), but in case of DAW with many tracks and plug-ins that can take a while and you get significant delays. When we have several processors, they operate in parallel. But they are executing different programs.

When you write some Program you define a Sequence of operations and it will be executed in sequence, one operation after an other. It is important in case you can modify what next operation should do in previous operation. Small mathematical example:
1. A equal 1.
2. A equal A plus 1.
3. B equal A plus 1.
What is B? If (2) and (3) are executed in parallel, B is equal "2". But (3) is executed AFTER (2), so B is equal 3.

Example from AZ Controller logic
Let say I have one Button and one Rotor. I want to use the Button as a "Shift", so the Rotor control 'Volume' without Shift and 'Pan' with Shift. To make example more interesting, I want that the (same) Button act like "CapsLock" in case I pressed it without touching the Rotor.

One possible solution is:
1) I define a State Set (Variable) "SHIFT" with 2 possible States (Values): "OFF" and "ON"
2) I define a State Set "ROTOR_MOVED" with 2 possible States: "NO" and "YES"
3) On Button pressed event I execute the following program (a list of Actions in AZ Controller):
  * "ROTOR_MOVED" is set to "NO" (we have not moved rotor after pressing Shift)
  * If "SHIFT" is "OFF" then set "SHIFT" to "ON" and FINISH execution (no CapsLock, indicate Shift is ON, do not execute next instructions)
  * If "SHIFT" is "ON" then set "SHIFT" to "OFF" (that was CapsLock mode, indicate Shift is OFF now)
4) On Button unpressed event I execute the following:
 * If "SHIFT" is "OFF" AND "ROTOR_MOVED" is "YES" then set "SHIFT" to "ON" and FINISH (we used Shift in CapsLock mode)
 * If "SHIFT" is "ON" AND "ROTOR_MOVED" is "YES" then set "SHIFT" to "OFF" and FINISH (normal Shift)
 * If "SHIFT" is "OFF" AND "ROTOR_MOVED" is "NO" then do nothing (we unset CapsLock, since we do nothing, we could skip this operation)
 * If "SHIFT" is "ON" AND "ROTOR_MOVED" is "NO" then do nothing (we enter CapsLock mode, we could skip this operation as well)
4) On Rotor moved event:
 * Set "ROTOR_MOVED" to "YES"
 * If "SHIFT" is "ON" then change Pan
 * If "SHIFT" is "OFF" then change Volume

Do you understand how it works? Do you understand why we need "and FINISH" instruction? If yes, you are ready to program complex logic in AZ Controller. If not, do not worry. After reading User manual you should be able to really program this example. And then you can see what happens on each event/operation.

I hope your brain is still in tact. I am happy I am not near you otherwise  :)
« Last Edit: May 30, 2014, 03:41:14 PM by azslow3 »

Offline azslow3

  • Administrator
  • Hero Member
  • *****
  • Posts: 1679
Re: Programming for Musicians
« Reply #1 on: May 31, 2014, 01:47:56 PM »
Recently I have introduced a bit more mathematically/logically complicated concepts. So here is some explanations.

Functions
Let say you always write:
Quote
Cheers,
 Mr. N,

Germany, Berlin, tel. +49xxxxx
to the end of you mail. You can type it every time, but most mailers/forums will allow you to define it once and add automatically.

When we write complicated logic, at some point we see that we repeat the same "code" again and again. Why not define it once and then just "call" when needed? Such block of code, usually named for clarity, is a function (or subroutine). Defined once and "Call"ed from many places. In AZ Controller any Logical Control can be used as a function from other controls.

Mathematic
Let say you have a State Set "Mode" with states "Play","Record","Sleep". We can look at that states from the ordering perspective: "Play" is the first, "Record" is the second and "Sleep" is the third (possible state in "Mode" set). Programmers are normally counting from "0" (zero), in that case "Play" is ZERO, "Record" is the first and "Sleep" is the second.

Why you want count from zero? That is really handy if you Shift something. Let say you want use first track when you Play, the second track when you Record and the third (most probably empty one...) when you sleep. But in case you count states from "1", "First track + Mode" will be 1+1 = 2! So, you do not want "Play" is counted as 1, but as 0.

AZ Controller always use zero based counting of state position which states are used as a shift. That can be quite confusing, in case the state name is not zero based. Let say we have "Bank": "1","2","3". In this case bank "1" has position 0 (zero), bank "2" has position 1 and so on.

But do not worry, the most complicated mathematical formula used in AZ Controller is "5+<Bank>*8". In case you understand why the result for "Bank":"1" is 5 and for "Bank":"2" is 13, you have sufficient skills already. Please read this section once more otherwise.

Hex numbers
Humans are normally counting in decimal numbers, so 5+5=10. Programmers are sometimes counting in hex numbers, so 5+5 = A. And yes, "A" is valid hex digit.

In configuring AZ Controller, you use hex numbers when you specify SysEx messages. In documentation for controllers, such messages are described in hex in any case, so you should learn what they are and how to use them in case (and only in case) you need SysEx.

In short, decimal numbers are written with "0,1,2,3,4,5,6,7,8,9" and hex numbers are written with "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f", where "a...f" correspond to "10...15". So, "b" is 11.

To write numbers more then 9, we use 2 digits. For example, 56 is 5*10+6. Since we have 16 digits in hex, 4b is 4*16+b = (decimal) 75. While the biggest number representable with 2 decimal digits is 99 ( = 9*10 + 9), in hex it is ff ( f*16+f = (decimal)15*16+15 =(decimal) 255). For SysEx you normally do not need to understand more.
« Last Edit: April 09, 2015, 07:24:08 PM by azslow3 »