<aside> 💗 Contributed with love by Benoit Arbelot
</aside>
Filter scripts are JavaScript files that can be used as a filter in a mapping. They are created as separate JavaScript files and can be added in the filter tab by choosing the script option.
https://bkuperberg.gitbook.io/~gitbook/image?url=https%3A%2F%2F654853773-files.gitbook.io%2F~%2Ffiles%2Fv0%2Fb%2Fgitbook-legacy-files%2Fo%2Fassets%252F-M0J6klA7kDd2pwFQXYj%252F-M0J77Q7DvhqgPT4biK8%252F-M0J7KaAYsg_l9TByExO%252Ffilterscript_creation.gif%3Fgeneration%3D1581959448528514%26alt%3Dmedia&width=768&dpr=4&quality=100&sign=81e67b5768d7988dae6dd73e20d4fc7443d8e97c52a29d930db83e9753ea3918
Filter scripts are useful to add math functions or advanced filtering logic to your mappings.
When scripts are running as a filter, new function callbacks are called to process the filter.
Method | Description | Example |
---|---|---|
function filter(inputValue, min, max) | This function is called everytime the mapping is processed and goes through the filter chain. | |
inputValue is the value from the input or from the preceding filter. | ||
min and max are the range of the inputValue, if applicable. It's useful when filtering the value as a normalized value, or to avoid overshooting the values. |
This function must return a value ! | Explainfunction filter(inputValue, min, max) { var result = inputValue * myFloatParam.get(); return result; }
|
In this example, we consider a simple project with a looping audio track. We also have a masterVolume custom variable controlling the audio track volume through a mapping.
https://bkuperberg.gitbook.io/~gitbook/image?url=https%3A%2F%2F654853773-files.gitbook.io%2F~%2Ffiles%2Fv0%2Fb%2Fgitbook-legacy-files%2Fo%2Fassets%252F-M0J6klA7kDd2pwFQXYj%252F-M0J77Q7DvhqgPT4biK8%252F-M0J7KaCowVjYEfinEVO%252Ffilterscript_mastervolumeexample_presentation.gif%3Fgeneration%3D1581959448331101%26alt%3Dmedia&width=768&dpr=4&quality=100&sign=41dec80778082dcdf7409ee0c5b25de41ca1b8fe42ad00f1858e32628fe5122c
Now we would like to add the ability to fade in and out our audio track. We can do this using sequences which directly control the volume of the audio track. However, in order to respect the current value of the masterVolume, these sequences output need to be remapped from [0; 1] to [0; masterVolume].
This can be done with a simple filter script multiplying the output value of the fade sequences mappings by the masterVolume value. We create the following MultiplyByMasterVolume.js script :
<aside> ⚠️ This method is not updated, filters scripts can now handle multiplex and multi inputs !
</aside>
var multiplier = script.addFloatParameter("Multiplier", "The multiplication factor to apply to all input values", 2, 0, 10);
function filter(inputs, minValues, maxValues, multiplexIndex)
{
var result = [];
for(var i = 0; i < inputs.length; i++)
{
result[i] = inputs[i] * multiplier.get(); //Basic multiplication of all the inputs by the script parameter myFloatParam
}
return result;
}
This filter script returns the input value inputValue, multiplied by the custom variable masterVolume.
Reminder : You can quickly get the Script address of any variable in Chataigne by right-clicking on it and selecting Copy Script Control Address, then you can use the function get() to get the current value of this variable in a script.
We just have to assign this script as a filter script in our fade sequences mappings to get what we want : fading sequences that fade between 0 and the current masterVolume value.
https://bkuperberg.gitbook.io/~gitbook/image?url=https%3A%2F%2F654853773-files.gitbook.io%2F~%2Ffiles%2Fv0%2Fb%2Fgitbook-legacy-files%2Fo%2Fassets%252F-M0J6klA7kDd2pwFQXYj%252F-M0J77Q7DvhqgPT4biK8%252F-M0J7KaEdiHTjTrPT9hg%252Ffilterscript_mastervolumeexample_withfilterscript.gif%3Fgeneration%3D1581959448960899%26alt%3Dmedia&width=768&dpr=4&quality=100&sign=e1d83b93418956ce0fd4d293335c32882ae6ed1355d7824f87a5860b18a2164c