Processor

Overview

A processor does a plugin’s audio work: it takes the audio and the MIDI the host sends and produces the plugin’s output. The rest of the plugin supports it. The controller holds the parameter values it reads, the presenter edits those values, and the host adapter delivers the audio and MIDI to the processor and the parameter changes to the controller.

the processor and the parts that support it

The processor runs on the audio thread, where it must not block or allocate. QPlug handles the plugin format, the locks and the host’s event lists, so a processor reads audio, parameter values and MIDI, and writes samples.

It is a Q audio_stream_base, and process(in, out) is the same call a Q program gets from an audio device, over the same multi_buffer channels. DSP written for Q moves into a plugin as it is: the Anna examples are Q’s poly_synth example with a plugin around it, and the voice and the voice pool are the same code.

This is the gain example’s processor, in full:

#include <q_plug/processor.hpp>
#include <q/fx/lowpass.hpp>
...
namespace q_plug = cycfi::q_plug;
namespace q = cycfi::q;

class gain_controller;
...
class gain_processor : public q_plug::processor
{
public:

                        gain_processor(gain_controller& ctl);

   channel_config       channels() const override { return {1, 1}; }

   void                 activate() override;
   void                 reset() override;

   void                 process(
                           in_channels const& in
                         , out_channels const& out) override;

private:

   float                gain() const;

   gain_controller&     _ctl;
   q::one_pole_lowpass  _gain_lp{0.0f};
};

It declares which channels it runs in, sets up what depends on the sample rate in activate, clears its state in reset, and does its work in process. It reads its parameters from the controller it was made with, as the types they were declared with: see parameter. Around those functions, QPlug handles timing, threads and formats:

Timing

Parameter changes and MIDI messages take effect at the frame the host stamped them with. See Events at the Sample.

Threads

A parameter is an atomic value the audio thread reads without waiting. Edits made in the editor go out to the host from the audio thread only when that takes no wait, so the processor never blocks on the user interface.

Formats and dialects

The same processor runs as a CLAP, a VST3 and an AudioUnit. A midi_processor receives MIDI as MIDI 2.0 messages whether the host sent MIDI 1.0, MIDI 2.0 or CLAP’s own note events.

The work is split between two threads. activate and deactivate are called on the main thread, and reset right after activate. process and midi are called on the audio thread, where nothing may block or allocate, and so is reset when the host calls it.

Events at the Sample

The host delivers a block of frames together with the events that happen during it, parameter changes and MIDI, each stamped with its frame. QPlug cuts the block where the events fall, applies the events at each cut, and calls process once for each run of frames between the cuts. For a block of 512 frames with events at frames 128 and 400, the processor is called with:

a block cut at its events

Each call’s buffers begin at the first frame of its run, so a processor counts its frames from 0, whether or not the block was cut. Reading a parameter once, at the top of process, is enough: the value is the one in force for every frame of that call. The gain example does this:

void gain_processor::process(in_channels const& in, out_channels const& out)
{
   auto target = gain();
   auto frames = in.frames.size();

   for (std::size_t f = 0; f != frames; ++f)
   {
      auto g = _gain_lp(target);
      for (std::size_t ch = 0; ch != in.size(); ++ch)
         out[ch].begin()[f] = in[ch].begin()[f] * g;
   }
}

A few cases follow from cutting the block at its events:

  • Several events stamped with the same frame make one cut. They are all applied there, and process is never called with zero frames.

  • An event at frame 0 is applied before the first call, so it covers the whole block.

  • An event stamped at or past the end of the block is applied after the last call, so it takes effect from the start of the next block.

Rebuilding on Changes

Reading a parameter as it is used suits a value like a gain. Some processors compile their parameters into something instead: a synth that turns a patch into tables, a filter whose coefficients come from three parameters. For those, the processor overrides parameters_changed, and QPlug calls it on the audio thread, once, before a run of frames, when any parameter has changed value since the last run. A host’s automation, an edit in the editor, a preset and a restored state all count, and a burst of them, a preset moving a hundred parameters, is still one call.

A parameter the processor reads as it is used is marked live (see parameter), and its changes do not call parameters_changed: a volume under automation would otherwise rebuild everything on every block.

The dexter example compiles a DX7 patch from 145 parameters, and gives the new patch to the notes sounding, as a DX7 does. Its volume, glide and operator switches are live:

#include <q_plug/controller.hpp>
#include <q_plug/midi_processor.hpp>
#include <q/synth/fm/dx_patcher.hpp>
#include <q/synth/fm/fm_voice.hpp>
#include <optional>
#include <vector>
...
namespace q_plug = cycfi::q_plug;
namespace q = cycfi::q;

struct dexter_controller : q_plug::controller
{
   q::dx_patch          patch() const;
   ...
};

class dexter_processor : public q_plug::midi_processor<dexter_processor>
{
   ...
   void                 parameters_changed() override;

   struct voice
   {
      q::fm_voice       fm;
      std::uint8_t      key = 0;
      float             velocity = 0.0f;     // 0 to 1, as it began
      ...
   };

   dexter_controller&   _ctl;
   std::optional<q::dx_patcher> _patcher;
   std::vector<voice>   _voices;
};
...
void dexter_processor::parameters_changed()
{
   _patcher.emplace(_ctl.patch(), float(sps()));
   for (auto& v : _voices)
   {
      if (v.fm.active())
         v.fm.update(*_patcher, v.key, v.velocity);
   }
}

It is not called for the values a processor starts with: activate builds from those, and the changes counted from then on are the ones that call it.

Include

#include <q_plug/processor.hpp>

Declaration

struct channel_config
{
   std::uint32_t           inputs;
   std::uint32_t           outputs;
};

class processor : public q::audio_stream_base
{
public:

   using channel_config = cycfi::q_plug::channel_config;

   virtual channel_config  channels() const;

   std::uint32_t           sps() const;
   std::uint32_t           max_frames() const;

   virtual void            activate();
   virtual void            deactivate();
   virtual void            reset();
   virtual void            parameters_changed();

   virtual bool            has_midi_input() const;
   virtual void            midi(q::midi_1_0::raw_message msg, std::size_t time);
   virtual void            midi(q::midi_2_0::packet const& p, std::size_t time);
};

using processor_ptr = std::unique_ptr<processor>;

Client Interface

The processor is a base class, meant to be derived from and not instantiated directly. The client writes a class derived from it and overrides process to do the plugin’s audio work. The other functions are optional: unless overridden, each does nothing or returns the default given below.

void process(in_channels const& in, out_channels const& out) override;

Process one run of frames from the host’s block, reading in and writing out. Called on the audio thread.

channel_config channels() const override;

The number of input and output channels. Stereo in and stereo out unless overridden. Example: {0, 2} for an instrument.

void activate() override;

Called on the main thread when the host activates the plugin. Build and size whatever depends on sps() and max_frames() here.

void deactivate() override;

Called on the main thread when the host deactivates the plugin.

void reset() override;

Clear any state carried over. Called right after activate, and by the host on the audio thread, as on a transport jump.

void parameters_changed() override;

Rebuild what is compiled from parameters. Called on the audio thread, once before a run of frames, when a parameter not marked live has changed value since the last run. See Rebuilding on Changes.

bool has_midi_input() const override;
void midi(q::midi_1_0::raw_message msg, std::size_t time) override;
void midi(q::midi_2_0::packet const& p, std::size_t time) override;

MIDI input: whether the processor takes MIDI (false unless overridden), and a MIDI 1.0 message or a MIDI 2.0 packet from the host at frame time of its block, on the audio thread. Overridden by midi_processor: see MIDI.

Process

in and out are Q multi_buffer channels. size() is the number of channels, frames is the range of frame indices, starting at 0, and out[ch] is one channel’s samples. An instrument’s in has no channels.

Of the three process overloads audio_stream_base declares, QPlug calls only process(in, out), so an instrument overrides it too and leaves in unread. See the Client Interface of audio_stream_base.

Channel Layout

A plugin has one channel layout, on one main port each way. QPlug declares a port of one channel to the host as mono and a port of two as stereo; a port of more than two has no spatial layout. A side with no channels has no port at all. An instrument has no audio input:

   // An instrument: no audio in, stereo out.
   channel_config       channels() const override { return {0, 2}; }

Life Cycle

which thread each call is made on

The delay example builds its delay line in activate, where the sample rate is known, and clears it in reset:

#include <q_plug/controller.hpp>
#include <q_plug/processor.hpp>
#include <q/fx/delay.hpp>
#include <q/fx/lowpass.hpp>
...
namespace q_plug = cycfi::q_plug;
namespace q = cycfi::q;
using namespace q::literals;

struct delay_controller : q_plug::controller
{
   static constexpr q::duration max_delay = q::duration{1.0};
   ...
};

class delay_processor : public q_plug::processor
{
   ...
   void                 activate() override;
   void                 reset() override;
   float                delay_samples() const;

   q::delay             _delay{std::size_t(1)};
   q::one_pole_lowpass  _delay_lp{0.0f};
};

constexpr auto smoothing = 4_Hz;               // the delay time's slew
...
void delay_processor::activate()
{
   _delay = q::delay{delay_controller::max_delay, float(sps())};
   _delay_lp.cutoff(smoothing, sps());
}

void delay_processor::reset()
{
   _delay.clear();
   _delay_lp = delay_samples();
}
reset runs on the audio thread when the host calls it, so it must not block or allocate. Allocate in activate.

MIDI

A processor that takes MIDI derives from MIDI, which overrides has_midi_input and both midi functions, and dispatches every message to the processor’s Q overloads, whichever dialect the host sent.

MIDI is sample accurate with no scheduling in the processor. The adapter calls midi at the cut for the message’s frame, as Events at the Sample describes, and the next call to process starts on that frame. A processor acts on a message as soon as it arrives. The anna_1 example starts a voice when a note-on comes in, and ignores time:

#include <q_plug/midi_processor.hpp>
...
namespace q_plug = cycfi::q_plug;
namespace midi = cycfi::q::midi_2_0;

class anna_processor : public q_plug::midi_processor<anna_processor>
{
public:

   using midi_processor::operator();

   void operator()(midi::note_on msg, std::size_t time);
   void note_on(std::uint8_t key, float velocity);
   ...
};
...
void anna_processor::operator()(midi::note_on msg, std::size_t)
{
   // Sixteen bits. A MIDI 1.0 note-on of zero velocity never gets here:
   // the translation makes it the note-off MIDI 1.0 means by it.
   note_on(msg.key(), float(msg.velocity()) / 65535);
}
void anna_processor::note_on(std::uint8_t key, float velocity)
{
   allocate(key).on(midi::note_frequency(key), sensed(velocity));
}

Its process then renders every active voice from the first frame of the run, and for a voice just started, that frame is the note’s frame:

void anna_processor::process(in_channels const& /*in*/
 , out_channels const& out)
{
   update_envelopes();

   auto const volume = q::lin_float(_ctl.volume());
   auto left = out[0];
   auto right = out[1];
   for (auto frame : out.frames)
   {
      // Bend and vibrato, in semitones, become one ratio every voice
      // multiplies its pitch by. Twelve semitones is a doubling.
      auto const vibrato = _lfo().first * _wheel * vibrato_depth;
      auto const pitch_factor = std::exp2((_bend + vibrato) / 12.0f);

      auto mix = 0.0f;
      for (auto& v : _voices)
         if (v.active())
            mix += v(pitch_factor);
      left[frame] = right[frame] = mix * headroom * _volume(volume);
   }
}
time is the message’s frame in the host’s block. The next call’s buffers start at 0 on that frame, so a processor that queues messages and plays them at time within process applies the offset twice. Act on a message when it arrives.

Expressions

As a subclass of audio_stream_base, processor inherits all the publicly accessible member functions, member variables, and types of its base class.

Notation

proc

Object of a type derived from processor.

c

Object of type channel_config.

Type Definitions

Expression Semantics Type

processor::channel_config

Get the channel layout type.

channel_config

processor::in_channels

Get the input channels type.

q::multi_buffer<float const>

processor::out_channels

Get the output channels type.

q::multi_buffer<float>

Access

Expression Semantics Return Type

c.inputs

Get the number of input channels.

std::uint32_t

c.outputs

Get the number of output channels.

std::uint32_t

Accessors

Expression Semantics Return Type

proc.sps()

Get the sample rate the host activated the plugin at, in samples per second.

std::uint32_t

proc.max_frames()

Get the largest number of frames a call to process is given.

std::uint32_t

Both are set before activate is called, and hold until the plugin is activated again.

Example

Make a gain effect, mono in and mono out, that reads its level in decibels from the parameter at index 0 of the plugin’s controller, and smooths it against zipper noise:

#include <q_plug/controller.hpp>  // For controller
#include <q/fx/lowpass.hpp>        // For one_pole_lowpass

namespace q = cycfi::q;
namespace q_plug = cycfi::q_plug;
using namespace q::literals;

class gain_processor : public q_plug::processor
{
public:

   gain_processor(q_plug::controller& ctl)
    : _ctl{ctl}
   {}

   channel_config channels() const override { return {1, 1}; }   (1)

   void activate() override
   {
      _gain_lp.cutoff(4_Hz, sps());                                (2)
   }

   void reset() override
   {
      _gain_lp = gain();                                           (3)
   }

   void process(in_channels const& in, out_channels const& out) override  (4)
   {
      auto target = gain();
      for (auto f : in.frames)
         out[0][f] = in[0][f] * _gain_lp(target);
   }

private:

   float gain() const
   {
      return q::lin_float(_ctl.get_parameter<q::decibel>(0));
   }

   q_plug::controller&  _ctl;
   q::one_pole_lowpass  _gain_lp{0.0f};
};
1 Declare one input channel and one output channel.
2 Set up the smoothing filter in activate, where sps() is known.
3 Start the filter at the current gain, so the output begins at that level.
4 Process one run of frames: read the parameter once, then scale each input sample by the smoothed gain.

See Client Interface for more info.