Skip to content

Processors

Processors are an advanced type of generator exclusive to SynthEnginePro. They allow you to output custom data per sample by using a callback function. Care should be taken with regards to performance when implementing processors, as their callback function will be called thousands of times each frame.

gml
// Generates a pure square wave
synth_add_processor(synth, function(note, amp, data) {
    var _output = 0;

    // Calculate the full cycle of a wave using synth rate and note frequency
    var _period = data.sample_rate / note.frequency;

    // Toggle output between -1 and 1 at the halfway point of the wave
    if ((note.i % _period) > (_period * 0.5)) {
        _output = -1;
    } else {
        _output = 1;
    }

    // Multiply the output by the amp value so it's the right volume
    return _output * amp;
});

Processor functions

Processor generators only take one parameter, and that's the callback function to execute for each sample. The callback function is passed three arguments:

  • note - This is a struct containing information about the note being played. It can be used to get the frequency of the note with note.frequency. It can also be used to get a ticker value note.i that increments every sample, which can be necessary for tracking wave positions.
  • amp - This is a real between 0 and 1 that represents the amplitude that the synth wants the note to play at, accounting for things like polyphony and amp envelope. This value should multiplied with your sample output.
  • data - This is a struct containing all of the data associated with your synth. It can be used to get some useful information, such as the following:
gml
var _sample_rate = data.sample_rate;
var _is_stereo = data.channels == audio_stereo;
var _polyphony = data.polyphony;
var _panning = data.panning;
var _lfos = data.lfos;

Return value

The return value from your callback should be a value between -1 and 1, and represents one sample of audio. The value you return is not processed any further, and will be directly added to the audio output. It is therefor very important that you multiply your return value by the provided amp argument, otherwise you may encounter audio clipping issues if you have other generators attached to the synth.