Writing your first audio plugin with Scripteffect
Before you start with this tutorial make sure you have installed everything correctly.
Start your audio host and configure some audio source to be processed by Scripteffect. This could be a audio input if you want to use Scripteffect live or some recorded audio. You could also use a vst instrument. Make sure you can hear your audio source at the hosts audio output. Then insert Scripteffect in the hosts signal path. If you are unsure how to do this, consult your hosts manual.
Open the editor of Scripteffect if you host has not already opened it.
The Scripteffect editor contains three parts. The first part contains buttons to load, run and save scripts. The second part has four sliders to control the scripts parameter and the third part is a text editor to edit the script.
Of course the most important part is the text editor.
If you have not loaded another script, it contains the default script:
|
import org.jvaptools.scripteffect.*;
org.jvaptools.VstPluginImpl plugin;
public void init(org.jvaptools.VstPluginImpl owner) {this.plugin=owner; }
public void close() {}
public void onMidiEvent(javax.sound.midi.MidiMessage message, long timeStamp) {}
public void processReplacing(vstmain plugin,float a,float b,float c,float d,float[][] inputs, float[][] outputs, int sampleFrames)
{
float[] out1 = outputs[0];
float[] out2 = outputs[1];
float[] in1 = inputs[0];
float[] in2 = inputs[1];
for (int i = 0; i < sampleFrames; i++) {
out1[i] = in1[i];
out2[i] = in2[i];
} } |
Understanding the default script
The default script contains one import statement, one variable declaration and four functions.
The import statement tells the compiler to look in the Java package org.jvaptools.scripteffect when looking for unknown classes. You can add more import statements when you want to use other Java packages.
The variable declaration declares a variable with name plugin and type org.jvaptools.VstPluginImpl. You can use this variable whenever you need access to the vst plugin.
The function init is called whenever the script is initialised. In the default script it only saves the plugin for later use.
The function onMidiEvent is called whenever the plugin recieves a midi event. If you want to know more about the message, look in: MidiMessage
The function close is called when a script is not longer used by Scripteffect. This could be because you changed the script or because Scripteffect itself is closed.
The most importand funtion is processReplacing. It has eight parameters. The parameter plugin contains the vst plugin again. The parameters a, b, c and d contain the vst parameters that you can control from the host or from the upper part of the Scripteffect. The array input contains audio input values and after you have processed them you must save them in the outputs array. Since Scripteffect is stereo it has two input and two output channels. The parameter sampleFrames contains the number of samples to process.
Since the default script should do nothing we only copy the input to the output:
out1[i] = in1[i];
out2[i] = in2[i];
Modifing the default script
Let's now suppose you want to write a volume plugin. Parameter "a" should controll the volume of the audio output.
To achieve this we just have to change the lines
out1[i] = in1[i];
out2[i] = in2[i];
in the processReplacing function with
out1[i] = in1[i]*a;
out2[i] = in2[i]*a;
After you changed the lines you have to press the run button in the upper part of the plugin. The new script is compiled and you can test it. Try playing some audio. Since the parameter "a" has a default value of zero you should hear nothing now. If you increase the value of "a" in the host or in the upper part of the plugin you signal should come back. You can find the modified default script here.
Setting a default value for parameter "a"
It would be better if the parameter "a" had a default value of one. To achieve this we modify the init function.
After this.plugin=owner; we insert the text this.plugin.setParameter("a","1.0");.
Now the init function should look like this:
public void init(org.jvaptools.VstPluginImpl owner) { this.plugin=owner;
this.plugin.setParameter("a","1.0");
}
Don't forget to press run after you changed the script. Now the parameter "a" is set to one.
You can find the script here.
Adding a lowpass filter
Let's now suppose you want to add a simple lowpass filter. Parameter b should controll the cutoff frequency.
We program a one pole IIR lowpass. To do this we need a buffer that holds the last output sample. Since we have two channels we need two varables:
float y1,y2;
Just insert the declarations after the first variable declaration:
org.jvaptools.VstPluginImpl plugin;
float y1,y2;
public void init(org.jvaptools.VstPluginImpl owner) {this.plugin=owner;
Then we initialise the filter buffer in the init method:
public void init(org.jvaptools.VstPluginImpl owner) {
this.plugin=owner;
this.plugin.setParameter("a","1.0");
y1=0.0f;
y2=0.0f;
}
Now we have to change our processReplacing function. Change the lines
out1[i] = in1[i]*a;
out2[i] = in2[i]*a;
to
out1[i] = in1[i]*a*b+y1*(1-b);
out2[i] = in2[i]*a*b+y2*(1-b);
y1=out1[i];
y2=out2[i];
This means that the new output is generated by a mixture of the last output value and the input. If parameter "b" is set to zero you should hear nothing. If parameter "b" is set to one the our changes in the script have no effect. But if we set "b" to a value lower than one the sound should become very dull. Press run and try setting "b" to 0.04. You can find the script here.
Congratulations you just wrote your first audio plugin with Scripteffect.
You can now look at the other scripts supplied with Scripteffect to find out how to use midi messages or create other effects. You can use all Java classes in Scripteffect, so there is no limit about what you can do with Scripteffect.
If you want to know more about the Java classes look at the J2SE 5.0 API
If you want to know more about JVAPTools you can download the source here.
If you want to know more about the jVSTwRapper you can look here.
If you want to know more about DSP you could look at www.musicdsp.org.
|