FM Synth

Overview

example/fm_synth/fm_synth.cpp plays four of the DX7’s factory patches through Q’s FM engine. It is the shortest path from a patch to a sound: a DX Patcher compiles a patch once, a pool of FM Voice plays it, and the four clips the FM Synthesis page plays are what it writes.

How the FM synth is put together
Figure 1. The pool, the file and the processor are the ones the virtual analog synth used. What is new is on the left.
  • DX Patcher compiles the patch once for the sample rate, and every slot plays from that one compiled copy.

  • A slot holds an FM Voice, its phase_iterator, and when it was last played, which is what voice stealing goes by.

  • The sum passes a 20 Hz high-pass, as the instrument’s output stage does.

  • Each clip is brought to the same peak before it is written.

Everything around the voice is what the Virtual Analog Synth left: a pool, a note taking a voice, a MIDI file for the notes. The voice itself is built the other way around. There, a patch was a handful of panel settings a voice read directly, and the sound came of taking harmonics away from a rich waveform. Here a patch is six operators in DX7 units, compiling it is a step of its own, and the harmonics are built rather than removed.

The Patch

A dx_patch is the numbers a DX7’s panel shows, operator by operator: output level, frequency ratio, detune, the four rates and levels of its envelope, then keyboard scaling and the sensitivities.

q::dx_patcher::config epiano_1()
{
   return
   {
      {  // OP1 to OP6: level, fixed, coarse, fine, detune, the
         // envelope, then the keyboard scaling and sensitivities
         {99, false,  1,  0, 10, {{96,25,25,67}, {99,75, 0, 0}},
             0,  0,  0, 0, 0, 3, 0, 2},
         // ... OP2 to OP6
      },
      {{94,67,95,60}, {50,50,50,50}},     // the pitch envelope
      {5, 6},                             // algorithm, feedback
      {34, 33, false, 4},                 // the LFO
      0, 0, 3,                            // LFO depths and sensitivity
      false, 24,                          // oscillator key sync, transpose
      "E.PIANO 1"
   };
}

Nothing in a patch is in seconds or hertz: rate 96 and level 99 are what the panel reads, and turning them into an envelope and a frequency is the patcher’s job. See DX Patcher for what each number means.

Compiling a Patch

A DX Patcher takes a patch and a sample rate and works out, once, what an FM Voice needs: the voice’s configuration and, per operator, what each key plays. Every voice in the pool shares one compiled patch.

auto patcher = std::make_shared<q::dx_patcher const>(cfg, sps);
std::vector<slot> pool;
pool.reserve(voices);
for (std::size_t i = 0; i != voices; ++i)
   pool.emplace_back(patcher);

A voice is then the patch plus the phase it is played at. The pitch is the caller’s, as it is for every oscillator in Q:

void attack(std::uint8_t key_, float velocity, std::uint64_t order_)
{
   key = key_;
   order = order_;
   master.set(q::frequency{440.0 * std::pow(2.0, (key - 69) / 12.0)}, sps);
   voice.attack(*patch, key, velocity);
}

Playing a MIDI File

The notes come from `q::midi_file`, which plays a standard MIDI file into a processor with each message timed in samples. The Virtual Analog Synth covers that side in full; the recorder here is the same.

Voices are taken free first, and the oldest is stolen when all are busy:

auto free = std::find_if(pool.begin(), pool.end(),
   [](auto const& s) { return !s.voice.active(); });
auto& s = free != pool.end()? *free
   : *std::min_element(pool.begin(), pool.end(),
      [](auto const& a, auto const& b)
      { return a.order < b.order; });   // steal the oldest
s.attack(e.key, e.velocity, ++order);

Knowing When to Stop

Rendering to a file needs an end, and a DX7 patch does not always give one: an envelope whose final level is above zero never dies away. The render therefore stops when every voice has gone quiet, or when the tail runs out, whichever comes first:

if (next == rec.events.size())
{
   if (!sounding)
      break;
   if (i > rec.events.back().time + std::size_t(tail * sps))
      break;
}

A last touch is the output stage: a 20 Hz one-pole high-pass, as the instrument has, which takes the rumble a stack of voices can accumulate.

The four clips are each brought to the same peak. The patches' own levels are more than 20 dB apart, BASS 1 being the quietest of these and BRASS 1 the loudest, which is how the instrument ships them.

Running

cmake -B build
cmake --build build --target example_fm_synth
build/example/fm_synth/example_fm_synth

With no argument the four clips play in turn through the default output device. Given a directory, they are written there as WAV files instead:

build/example/fm_synth/example_fm_synth ~/clips

Components Used

Component Role

DX Patcher

Compiles a patch in DX7 units for the sample rate

FM Voice

One voice, six operators under an algorithm

FM Operator

The operator each voice is built from

FM Algorithm

How the operators are wired

DX Envelope Generator

The rate and level envelope every operator carries

phase_iterator

The pitch, which the voice is given

`q::midi_file`

The notes, read from a standard MIDI file

Audio File

wav_writer, when the clips are written

Audio Stream

Playing through the default output device