Contents

Melancholytron: Writing the Firmware, Part 2

A note arrives, a preset is read, and somewhere between the two the instrument has to decide which of six voices gets the note, how the note it replaces should end, and how far the pitch should drift while it sounds.

The Voices

There are six voices. Every note picks one of them, and the choice is not round robin. Each voice reports a score, built from weights that say how important it is to take that voice over another. An idle voice outranks one that still sounds, and an idle voice that has been idle longest outranks one that just finished. Among the voices that are still sounding, a release comes first, a sustain next, and an attack last. It is not voice stealing in the usual sense. It is a fixed order of preference, and it means the quietest candidate always goes first.

flowchart TD subgraph in[Where notes come from] key[Key matrix] midi[MIDI in] end key --> buffer midi --> buffer buffer[Note buffer<br/>20 slots] buffer --> lead{Queue has<br/>12 blocks lead?} lead -->|no| wait[Retry next loop] -.-> buffer lead -->|yes| pick[Voice with the highest score] subgraph out[How the note is played] pick --> mixin[Crossfade from<br/>the note it replaces] mixin --> mix[Six voices at a sixth,<br/>saturated to 16 bit] end

The amplitude envelope has four states and no curve. Attack and release are step sizes, computed once when the note is set up, so the time the knob promises is the time the envelope takes. A release shorter than a millisecond is stored as a step of zero, and a voice in that state goes straight to idle when the note ends. That is what the lowest position of the Release knob does. The pitch bend bends the pitch until the wheel reaches the end of its travel. Past that point it plays a special effect instead: a dedicated recording of an overbent sound, one for each direction, held until the wheel comes back. The notes that were sounding are released at that moment, so the effect replaces the chord instead of joining it. While it sounds the voice skips the envelope altogether, attack, wobble and pitch envelope included, and plays the sample at its recorded speed.

stateDiagram-v2 [*] --> Idle Idle --> Attack: note on, attack set Idle --> Sustain: note on, no attack Attack --> Sustain: attack done Sustain --> Release: note off Sustain --> [*]: note off, no release Release --> [*]: release done

Mixing is where the six voices meet. The engine clears a float buffer, asks every voice to add itself at a gain of one sixth, and saturates the sum to signed 16 bit at the very end. Six voices at a sixth each can add up to full scale with nothing to spare, so the saturation is the only safety net in the chain and no voice is allowed to add gain of its own.

Crossfading

When a voice is reused while it still sounds, the old note does not stop. The voice renders a short extra piece of it into a buffer, 132 samples of the note that is being replaced, and then crossfades from that piece into the new note over the same 132 samples. The crossfade curve is an S shape: the old note leaves slowly at first and quickly in the middle. At 44.1 kHz the whole handover lasts about three milliseconds, short enough to be inaudible and long enough that a jump in the waveform cannot be heard as a click. A handover that is already running is not restarted, so a fast repeated note still gets one clean transition.

/posts/melancholytron-2/melancholytron-crossfade.png

Pitch

A preset holds one recording per note and some settings. The recordings are at 44.1 kHz, but a voice does not play them back at that rate. It keeps a read position of its own, and that position can move faster or slower than the recordings. The read position advances once per output sample, by the base pitch times the pitch envelope times the wobble. A step of 1.0 plays the note at its recorded pitch. The effects are applied on top of the recordings while the note sounds, and the read position is where the pitch effects land. The darkness knob scales it from 1.0 at the top of its travel down to 0.125 at the bottom, three octaves below the recorded pitch. The pitch bend moves it by up to a quarter in either direction, and the pitch envelope and the wobble move it further still.

The output clock never changes, so only the speed of the read position through the decoded material changes. That position is a float, so it can sit between two samples. When it does, the voice returns a mix of the sample before it and the sample after it, in the proportion of how far the position has travelled. That is how the interpolation works, and it was part of the original engine that Bram Bos wrote.

/posts/melancholytron-2/melancholytron-interpolation.png

Darkness is one of the pitch effects. The bend, the envelope and the wobble move the pitch too, but darkness moves it furthest. What it costs is brightness, which is what the name describes, and at the bottom of its travel the interpolation spends eight output samples on every recorded one, turning them into a straight line instead of a staircase.

Pitch Envelope and Wobble

The pitch envelope is the other thing that moves the read position, and it is part of what gives the instrument its character. Every note holds a fixed recording, like a tape in a tape recorder, played back at whatever speed it is asked for. An old tape recorder never ran at a steady speed: the motor took a moment to come up and the speed wandered while the tape ran, and that unsteadiness is a good part of why those recordings sound alive. The envelope puts that motion back: a note starts a little below its pitch and climbs to it, and when it is released the pitch sags as it fades. That is what makes the instrument sound slightly unstable without anything being played out of tune.

The envelope is a fixed part of the preset, so the user cannot change it. What the user gets is the Context knob, and it does three things at once. It sets how long the attack takes, from nothing at the bottom to two seconds at the top. Above zero it uses the preset’s release; at zero the release is switched off, and the pitch holds where it is instead of sagging. And it decides how much of the wobble ends up in the sound.

The climb and the sag themselves come from the preset. For example, a note might start about a third of an octave below its pitch and the release might drop it by about a semitone. The knob only decides how long the climb takes, and whether the sag happens at all. At the top of its travel those are two seconds and eight seconds.

The wobble is the slow drift of a tape that cannot hold its speed, and it is made with Perlin noise. Each voice has its own generator, so two voices playing the same sample drift apart instead of moving together, and the same note played twice is never quite the same note. The noise moves the pitch a little either way, multiplied into the playback rate next to the pitch envelope. The Context knob sets how far, but not from the bottom of its travel: the wobble is off at the low end and comes in over the upper half. Even there it stays within a few percent, so it is a slow wavering rather than a bend.

/posts/melancholytron-2/melancholytron-pitch-envelope.png

The Knobs

The eight knobs, the slider and the octave buttons are read through an analog multiplexer on the board Edwin designed for it. The board carries the eight pot footprints in two rows, the two switches under the octave buttons, the multiplexer and a connector for the ribbon to the main board. Four address lines select a channel and one analog pin reads it. Twelve of the sixteen channels are in use: eight knobs, the slider and three buttons. The other four are brought out to headers on the board.

/posts/melancholytron-2/melancholytron-knobs.jpg

The firmware does not count up through the channels in order. It visits them in Gray code order, where every step changes exactly one address line, because counting would sometimes change three at once and leave the multiplexer briefly pointing at a channel no one asked for. Then it waits for the address lines to settle and the analog to digital converter to settle after them, and reads the value.

Each channel has its own filtered reading. The filter takes the noise off the wiper, snaps to the ends when a knob sits close to them, and reports nothing while nothing moves, so a still instrument sends no stream of control changes. A thousand raw counts map onto 0 to 127 with room left at the top, so an input that overshoots still clamps at 127. At startup the scanner reads all sixteen channels twenty times and throws the results away.

Those settings were the tricky part. Too little filtering and a resting knob reports movement that is not there. Too much and the reading lags behind the hand, so a fast turn arrives late and a slow one arrives in steps.

The knobs and the slider cap are printed parts on top of that board. The knob is 18 millimetres across the foot and 16.5 millimetres tall, fluted and tapered to 11 millimetres at the top, so a finger can turn it from any side. The slider cap is 24 millimetres wide and 10 deep at the base, with a rib over it, because a slider is pushed rather than turned and the hand has to find it without looking.

Three of those channels are not knobs. The buttons are read as analog values like the rest, against a threshold rather than as contacts. Pressed is above it and released below it, and the two are not the same point: there is a gap around the middle to cross either way. That gap is hysteresis, and it is needed because a closed contact still wanders a little. Pressing both octave buttons resets the octave to the one in the preset, and the offset is clamped so the 25 keys stay inside the MIDI range.

The third button is the paradigm button. It has a channel of its own, and the firmware reports it as the sustain control, which is the control a MIDI pedal sends. A press on the panel and a press on a pedal reach the rest of the firmware as the same message.

The Keyboard

The classic instrument uses an existing MIDI keyboard, so its firmware does not scan keys and only reads the notes the keyboard sends. The production instrument has no MIDI keyboard: the keys are a matrix of contacts with no microcontroller of their own, so the firmware drives the shift register and reads out the rows and columns itself. Eight rows and four columns give 32 positions, of which the keys use the 25 in the middle. The whole scan produces a single 32 bit word, and the firmware compares it with the previous word and reports only the keys that changed, so a held chord costs nothing and a key that is not touched is never looked at again.

/posts/melancholytron-2/melancholytron-key-matrix.png

The teal row is the one the scan is driving. The other seven are left alone, and the four column lines are read before the register moves to the next row.

Every key remembers the note number it produced when it went down, because the octave can change while a key is held and the release still has to find the voice that started. Key position plus the octave offset is the note number, and that number goes into a buffer of twenty notes rather than straight into a voice. Notes that arrive over MIDI get the same offset and are remembered per channel, so a note off finds its voice even when the octave moved in between.

/posts/melancholytron-2/melancholytron-keyboard.jpg

What It Meant

I did not expect the Melancholytron to become this successful. It started as a small firmware job I could do for Allard, a few changes to help his project along, and it reached more people than I could have expected.

It was more than a few changes in the end. I began with what I was asked to do, reading the keys and the knobs and managing the presets. By the end I had developed an instrument together with my brother, and it had become my project as well. I wanted it to be as good as I could make it, and most of the last year went into the sound: taking the clicks out one at a time until I could not hear one.

I was happy with how the instrument was received. It was well liked at Superbooth, in the press and on the forums, and that motivated me to keep working on the details.

Most of what I have written here is about the firmware, but that was not the only part I had a hand in. My part in the case drawings and the packaging is described in the first part, and the knobs on the panel I designed with Allard, modelled and printed for the classic version. Seeing all of that come together was one of the parts I enjoyed a lot, and I learned a good deal from it. I saw a big part of the process of making a product first hand, where in my work as a software designer I usually only see it second hand.

My discussions were mostly with Allard. He had the idea and the concept, and a good part of what the instrument does came out of the conversations we had about how it should behave and how it should work. Allard has said that thinking it through together shaped how the instrument works now. The rest of the instrument came from the others: Edwin designed the boards and Roy the enclosure, Emiel helped with the packaging, and underneath all of it is Bram’s audio engine.

I enjoyed the work, and I am ready for the next one.

Credits

Allard Krijger took the photograph of the instrument in front of the grand piano. It comes from melancholytron.com.

Edwin van den Oetelaar designed the multiplexer board. The photographs of the board and of the keyboard are mine.