Wednesday, March 18, 2015

Constructing a Virtual MIDI Sostenuto Pedal

This post explains how I created the sostenuto pedal that I used for this video.

An independent sostenuto pedal is a pedal that catches the dampers of any keys that are being played at the time the pedal is pressed. It works slightly differently from the sostenuto pedal on an acoustic piano. Normally, the damper pedal can interfere with the acoustic sostenuto—if the damper pedal is down, the sostenuto pedal can’t be pressed effectively until the damper pedal is raised again. The independent sostenuto pedal removes this limitation, allowing the two pedals to be used together freely and intuitively.

In an earlier post, I showed how to “hack” the acoustic mechanism to create an independent sostenuto pedal. This can be a great way to experience its potential on your own instrument, but the method can be complicated and time consuming and doesn’t work on every piano. Fortunately, the “mechanism” of an electronic keyboard is much simpler (at least until the point of sound production), so creating a sostenuto pedal on a keyboard is quick and reliable.

On most three-pedal keyboards, the middle pedal works like an independent sostenuto pedal by default. If you invested in something like this, you're already set.

However, a three pedal set for a keyboard can be a bit pricey, and it may not be worth it if you play mostly acoustic instruments. Here’s a simple way to construct a virtual sostenuto pedal that costs less than $20. You will need the following:
  • A laptop (if you don’t have one, this is still your cheapest option)
  • A MIDI to USB cable (available here for $7.15 at time of writing)
  • A USB foot pedal (available here for $10.75 at time of writing)

I still own a keyboard with a traditional MIDI hookup (Casio PX-120), but these are almost obsolete. More recent models have a USB connection, meaning the MIDI to USB cable would be swapped out for a normal USB cable, saving a few more dollars.

The Principle

My virtual sostenuto pedal works by sending the MIDI data from the keyboard to the laptop, altering that MIDI data, then sending it back to the keyboard. MIDI is a code that electronic instruments use to speak to one another and to computers. There’s MIDI code for a wide variety of things, including pushing the damper pedal (sends a value of 127 to controller 64), pitch bend (sends a value between 0 and 16,384 to the pitch wheel), or an emergency “reset” button that turns off all sound (“All Notes Off”, or a value of 0 to controller 123). In a typical keyboard performance, the vast majority of MIDI data are note events. Note On events occur when a note is pressed. They specify the key number (from 0 to 127, middle C is 60, A4 is 69) and how hard the key is struck (also called "velocity" with a value from 1 to 127). Note Off events occur when a key is released. They also specify a key number and have a velocity of 0.

At the time the sostenuto pedal is pressed, it checks which keys are being played. Our virtual sostenuto pedal will need to keep track of the MIDI note events so that it knows which keys to sustain. As long as the sostenuto pedal isn’t used, any MIDI data sent to the computer gets bounced straight back to the keyboard. When the sostenuto pedal is pressed, a “snapshot” of the state of the keyboard gets saved. Any notes that were being played will not be released as long as the sostenuto remains down. That means that the certain Note Off events coming from the keyboard won’t be passed back. When the sostenuto pedal is released, those Note Off events are sent all at once, with the exception of those that correspond to a note still being held at the time.

The Setup

Even though it’s not too difficult to manipulate the flow of MIDI data as outlined above, I don’t currently have a quick solution. My system is the result of hours of experimentation, and even once you’re equipped with all my tools it takes a while to set up. I’d welcome any ideas of how to streamline the process. I made a rudimentary patch using Pure Data, a free and open source visual programming language especially designed for musicians. I’ve posted my patch here.

Here are the steps to configuring your sostenuto pedal:

  1. Connect the keyboard to the computer using the MIDI to USB cable.
  2. Connect the USB pedal to the computer.
  3. Turn off the Key Repeat option on the computer. On my Mac, this option is in System Preferences->Keyboard. If you don’t do this, the USB pedal may not work properly.
  4. Download and install Pure Data, then download and open my thirdpedal.pd patch. Note that Pure Data is a powerful program with a steep learning curve. It's a valuable tool for musicians though, and I highly recommend learning the basics of its operation. This is a great tutorial.
  5. Check to see if the MIDI is working correctly. When you play notes on the keyboard, the keyboardStatus graph in the thirdpedal patch should change. If not, check the MIDI settings in Pure Data or in the system preferences. On my Mac, the MIDI preferences are hidden in a separate application “Audio MIDI Setup”.
  6. Click the “toggle sostenuto function” button at the top of the thirdpedal patch so that an X appears.
  7. Try out the USB pedal. When you push the pedal, it will send a keystroke to the computer. You can figure out which keystroke by checking the key number box at the top left of the thirdpedal patch. My USB pedal will function like a giant “B” key by default, which is key code 98.
  8. If your pedal sends a different keystroke, you can set the number by clicking and dragging in the number box below the “toggle sostenuto function” button. (The box that says “set the pedal number here”).
  9. Turn the local control off on the keyboard. This will cut the connection between the physical keyboard and the keyboard’s audio output. This step is important, because you want the MIDI data to be filtered through the computer before it comes back to the keyboard and is converted into audio, like in this diagram:

I haven’t tried to adapt any of this process to any setup other than my own. If anyone does attempt to set up their own virtual sostenuto, I would be happy to help where I can, and would welcome any feedback! Good luck.

What Happens

You can view the status of the keyboard (which notes are pressed, and at what velocity) in the keyboardStatus graph on the thirdpedal patch. Pushing the sostenuto pedal will copy the data in the keyboardStatus graph into the pedalStatus graph (which looks exactly the same, but inverted). The patch will filter out any Note Off MIDI commands that correspond to notes with a positive pedalStatus value. The number of blocked Note Off commands is recorded in the suppressedReleases graph. It is important to keep track of the suppressedReleases, because when the sostenuto pedal is lifted, the appropriate number of Note Off commands have to be sent for every pitch that was held on the sostenuto pedal.


  1. Hello! I was hoping that your guide will help me, but the link to your patch does not work :-(

  2. Hello! Yes I have moved to a new website since writing this guide. What setup do you have?