< Back to IRCAM Forum

Table Creation and Macro Argument Default Value

2 questions/requetes :

1) Comment passer la création du tableau en argument d'une macro/fonction, au lieu d'une variable contenant le tableau ? Exemple, lors de l'appel de la macro @granularON, pouvoir écrire :

@granularON(tab [ “flute”, “basson”], 100, -10)
au lieu de

1) How can we pass the creation of a table in the argument of a macro/function, instead of a variable containing the table? Example: While calling the macro @granularON, I'd like to declare:

@granularON(tab [ “flute”, “basson”], 100, -10)
instead of

`$instruments_1 := tab [ "flute", "basson" ] @granularON($instruments_1, 100, -10)`
2) Comment donner des valeurs par défaut aux arguments d'une fonction/macro ?! (héhé...). Pouvoir définir une macro avec un truc du genre :
2) How to give provide the default value of an argument in a Function/Macro? To be able to define a macro like:
@macro_def @granularON($ITERSET = [ "flute", "basson" ], $GRAINSIZE = 100,  
$OUTGAIN = 0)  
{  
;$ITERSET contient par défaut un tableau avec les éléments "flute" et  
"basson"  
;$GRAINSIZE à comme valeur par défaut 100ms  
;$OUTGAIN à comme valeur par défaut 0db  
    forall $INSTRUMENT in $ITERSET  
     {  
            ; Mapping de l'instrument dans le filtre, 0db en 100ms  
              Mix $INSTRUMENT 1biq 0 100  
    }  

            ; Mapping du munger dans le filtre, 0db en 100ms  
              Mix 1mng1 1biq1 0 100  

            ; munger  
              1mng Outgain 0, grainSize $GRAINSIZE  

            ; filter  
              1biq Outgain $OUTGAIN, type bandPass, freq 500  

}

et appeler la macro comme ceci :
and call the macro as:
@granularON($instruments_1) ou comme cela / Or as : @granularON($instruments_1, 1556) ou encore / or still : @granularON()

For question 1:

The problem of passing a table (vector) directly in the argument of a macro is that a table definition includes comma (",") and each fragment of code between commas are interpreted as different macro argument. This behavior is the behavior intended because macros are expanded at a syntactic level.

You have not this problem with functions and processes. A function is used for expressions, while processes are used for actions. The macro you give in the second example shows that you need to refer to actions. So you can write a process definition very similar to the macro definition:

@proc_def ::granularON($ITERSET, $GRAINSIZE, $OUTGAIN)
{
forall $INSTRUMENT in $ITERSET
{
; Mapping de l’instrument dans le filtre, 0db en 100ms
Mix $INSTRUMENT 1biq 0 100
	; Mapping du munger dans le filtre, 0db en 100ms  
	Mix 1mng1 1biq1 0 100  

	; munger  
	1mng Outgain 0, grainSize $GRAINSIZE  

	; filter  
	1biq Outgain $OUTGAIN, type bandPass, freq 500  
    }  

}


then you can call the process as needed:
::granularON(tab [“flute”, “oboe”], 100, 0)

The expression tab [“flute”, “oboe”] is the way you create a tab by enumerating its values. Here the tab has two elements which are strings, but Antescofo tab can be heterogeneous if needed (e.g., holding elements of different types).

Antescofo processes and functions behave as usual: the arguments are evaluated (at the time of the call, which contrasts with macros where argument are expanded at file loading time) and the body is then evaluated with the parameters bound to the results of the argument’s evaluation.

Notice that processes are a new feature in v0.5. Their use should be preferred to that of macros, because they offer several advantages over macros:

  • they shorten the code (macros are expanded which increases the number of lines of code)
  • the argument of a process are evaluated only once per process call (the argument of a macro are textually copied, so there is an evaluation of each occurrences of the arguments in the macro body)
  • processes can be killed (using the abort action, you can stop all the instances of a processus or one specific instance)
  • processes can be recursive (recursive macros leads to an infinite expansion)
  • processes are high order values and can be passed as argument of other processes and functions
Do not hesitate to share your questions and design patterns with processes on the forum !

For question 2:

Currently, it is not possible to have function or process with default value of an argument.

Experiences in other programming languages shows that default values have little use if you cannot have labeled parameters (suppose in your example that you want to provide only the grain size and use the default value of the other two arguments). The introduction of these two features in Antescofo can be considered but for now it is not a priority.