Digital Music Programming II: squelch

This lab demonstrates how to filter out the feedback notes from a PianoDisc MIDI player piano. The piano will return notes played by the computer, so it is difficult to figure out whether the note was played by a performer or is just a feedback note which should be ignored by the computer.

The return time of a note depends on two variables: how hard the note is played, and the pitch of the note.

The following figure displays the callibration data used in this object. The grayscale value of each pixel indicates how long it took for a note with a given attack velocity to return to the computer. White means that the note to a very long time to return (about 250 milliseconds), while dark gray means that the note took a short time to return (about 47 milliseconds). Black indicates that either the note never returned (in low velocities, for example), or that the MIDI note was outside the bounds of the 88-key keyboard. Note the alternating horizontal bands which are caused by the differences in weight/length of black and white keys on the piano. Note also the decreased sensitivity of the mid-range of the piano where the manufacturer of piano made the action more stiff since this is the region of the piano played most often (the measured piano was less than one year old when measured).

Return Times by MIDI Note v MIDI Velocity

The above plot shows the average return time for any given note/velocity. Usually louder notes return faster and have less variation in their return times. Therefore a smaller window of time is necessary to squelch loud notes. Softer notes have a more variable return time, and the squelching window must be made larger. Notice in the plot below that the lower dynamic notes cause a more jagged appearance to the plot. This is because the softer notes are played with less force. The previous momentum of the key before it was played starts to become significant, as well as the motion of other nearby keys and other vibrations in the piano.

Below is a plot of the squelching tolerance window based on the velocity of the outgoing MIDI note. Loud notes have a +/- 5 millsecond windows around their expected return time, while low notes have a +/- 30 millisecond window around their expected return time. If returned notes fall within the squelch window, then it will be identified as a note the computer had previously sent out. This tolerance window is only approximate. See the extra-extra credit question in the exercises for more information.


How does the squelch object work? Outgoing note on messages are timestamped before they leave the computer. A circular buffer is used to store the note number that went out, as well as the minimum and maximum time that the note is expected to return.

When a note-on message comes into the computer from the piano, the squelch object checks through its circular buffer for a note of the same pitch that was sent out. If a note of the same pitch is found, then the object checks to see if a squelch note is expected in the given time window specified by the minimum and maximum arrival times stored if and when the note was first sent out of the computer.

Note-off messages are not squelched and are not a major concern since they will not cause feedback (unless perhaps you send note to the piano based on note-offs received from the piano).

Source Code

  • squelchc.c -- constant time squelching.
  • returntimes -- array of measured return times.
  • linear interpolation for non-constant squelching.
  • squelch.c -- optimized return time squelching.


  1. Compile the squelch object and test it by transposing the Kawai piano input by a perfect fifth up and sending it back to the piano to play. Use the following patch:

    Play notes at the Kawai piano keyboard. The computer should reply with exactly one note a fifth above the notes you play. If more than one note is played by the computer, then the note got past the squelch object when it was not supposed to. If the computer does not reply with a note a perfect fifth above the note you play, then the squelch object was too agressive because it squelched a note from the performer. Try playing one note repeatedly. Try playing soft and loud.

  2. Redesign the input/output ports into a more memorable layout such as:

  3. Replace the function getTimeRange with the following definition:
    void getTimeRange(long curtime, long key, long vel, 
          long *mintime, long *maxtime) {
       *mintime = curtime + 40;
       *maxtime = curtime + 250;

    How does this change the squelch object? Try out the squelch object with this new definition for the getTimeRange function. How does the behavior of the object change when you do the same performance tests as in exercise 1?

  4. Extra Credit: write an object which measures the return times of notes sent from Max and returned by the piano. This sort of data was used to generate the callibration array for optimized squelching. Only note-on messsages need to be timed. If note-offs are accidently not squelched, then there is not as much of a problem than if note-ons are accidently not squelched.

  5. Extra-Extra Credit: write a variation on the squelch object which is super-optimized. Notice that the squelching tolerance window is just an approximation. Why might this be a waste of time? Answer: There could be timing glitches on the timestamps for the incoming MIDI messages, depending on how dependable the data transfer rate is from the MIDI input drivers into the Max program.

  6. Why is it more appropriate for the squelching mechanism to be located inside the piano rather than inside of the computer? Why has the company who manufactures the PianoDisc apparatus not put the squelching feature into their controlling software? [Or, alternatively, why is it impossible to find the squelching feature documented in the user manual?].