< Back to IRCAM Forum

Variables from MaxMSP through whenever

Hey, recently I’ve been having a slight problem for using the whenever function.

I use the prepend setvar $variable_name function in MaxMSP, and I’m able to receive the variable properly in Antescofo with a whenever function. However, when I try to use another function that should access this variable, it does not receive it.

An example of code:
whenever ($cue) @exclusive {
status “variable received”
status $cue
}

And another function
@proc_def ::testing($var1) {
switch ($cue) {
case 1:
$result := 2
case 2:
$result := 4
}

The result is always zero as the switch is not seeing any updated version of the $cue variable. Is there a specific reason for this? I’m on the latest release of Antescofo, Max 7.3.5 on OSX 10.13.6

Hello Maladie.

I suppose that you launch the process by a statment
::testing(10)
somewhere in the code. Your process does not last: the switch is not a durative action: it executes in the instant where the process is launched and the the process died. So the gloval variable $cue is tested only once, when the process is launched. You can launch periodically the process with a loop. But if you really want to react each time $cue is updated, the whenever is teh right control structure.

You may put a call to ::testing in the whenever:

whenever ($cue) @exclusive {  
     status “variable received”  
     status $cue  
     ::testing($cue)  
}

in this way, each time $cue is updated, the process ::testing is called.

You may also include a whenever in the process ::testing :

@proc_def ::testing($var1) {  
    whenever ($cue == $cue) {  
        switch ($cue) {  
          case 1: $result := 2  
          case 2: $result := 4  
    }  
}

This code call for several comments. When the process is launched, the whenever is launched as a sub-process of the process. This sub-process live forever (it must be explictly killed or scoped with a during clause) as for any whenever and will execute the switch each time $cue is updated somewhere

The second remark is that we use the condition ($cue == $cue) because we want to trigger the whenvevr body even if $cue is set to zero (or false): the whenever executes the body if the variable is updated and the condition evaluate sto ture. The expression ($x == $x) is teh simplest expression which evaluates to true each time the variable $x is updated.

The third comment is that we explicitly use the variable $cue in the condition of the whenever. The following code

@proc_def ::bad_testing($var1) {  
    whenever ($var1 == $var1) {  
        switch ($cue) {  
          case 1: $result := 2  
          case 2: $result := 4  
    }  
}  

::testing($cue)

does not do the intended job for the following reason: the whenever in ::bad_testing is watchu-ing the local variable $var1 (a process’s parameter is indeed a local variable of the process) not the variable given as argument of the call.

I thought that might be the issue but wasn’t really sure on how to solve it. Thanks a lot for the info, I’ve been able to fix it without any hitches. I loved how the two different solutions are useful in very different contexts.