midichord - generates adjusted-notes to conform to a fixed-voice


 perldoc midichord     # read the manual :-)
 # on a midi-file, which should contain both the fixed voice(s) on
 # channels 2 and 3, and adjustable chord-pattern 'notes' on channel 5:
 # (useful if 2,3 is a fast-moving solo, and 5 is a backing pattern)
 ~> midichord -f 2,3 -a 5 in.mid > out.mid
 # on real-time (raw) midi:
 # a chord-file plays on channels 4 and 5 which will be adjusted
 # to follow the keyboards on the fixed channels 0 and 1
 ~> midichord -a 4,5 -f 0,1 -i 14:0,32 -o 128
 ALSA client 129                midichord pid=2157
 Input port 129:0 is connected from 14:0,32:0
 Ouput port 129:1 is connected to 128:0
 Reigning chord is C,D,F,Ab
 ^C to Quit
 # Adjust channels 4,5 Closely to not dischord with On-notes in 0,1
 # (useful if 0,1 is a slow-moving chord, and 4,5 is a faster solo)
 ~> midichord -f 0,1 -aco 4,5 -i 14:0,32 -o 128
 # in Quiet mode, e.g. in a Makefile or script (14 is Midi Through):
 # where you play on your ProKeys on channels 0 and 1,
 # and the chord notes are on channels 4 and 5 in backing.mid
 ~> midichord -Q -f 0,1 -a 4,5 -i 14:0,ProKeys -o 14:1,TiMidity &
 ~> aplaymidi   -p midichord:0 backing.mid &
 ~> arecordmidi -p 14:1 -b 60 -t 1000 piece.mid
 # and then you play on your ProKeys


Notes input on the fixed-channels are transmitted unchanged to the output (unless -n), but notes received in the adjust-channels have their pitches changed before being output.   midichord can be used either:

  1. to synthesise chords on the adjust-channels to accompany a solo on a fixed-channels (the default mode),
  2. or to adjust the notes of a solo to not conflict with the notes on the fixed-channels (-aco mode).


By default, midichord remembers all non-semitone-dischord notes in a "fixed-channel" (or channels), generating a changing set of "chord" notes, which can be played by notes in a "adjust-channel". Any note in a fixed-channel remains as part of the Reigning Chord, and continues there until contradicted by a subsequent note in a fixed-channel, where "contradiction" means a semitone-dischord.

Notes input on the fixed-channels are also, by default, transmitted unchanged to the output. But notes received in the adjust-channels have their pitches changed, to conform to the Reigning Chord, before being output.

A black note in a adjust-channel plays the whole chord, in close harmony, inverted to start just above that note.

A white note in a adjust-channel plays one of the chord-notes in the same octave. The rules governing which one are a bit complicated; there are seven white keys in each octave, but the number of notes in the chord varies all the time, from one to six. The D key will always output the lowest chord-note in its octave, and the B key will usually output the higest. Between those two, the default mapping of key to chord-note ascends as smoothly as possible subject to the condition that each key produces a different chord-note from the previous key, so that it will avoid generating repeated notes unless that was intended.

The -m option enforces a simpler mapping: the note is chosen according to the modulus of the key-number, so that the white keys just cycle through the chord notes. This is useful for generating arpeggios, because if there are three or more chord-notes then three neighbouring white keys will generate three different notes.

When midichord starts, the Reigning Chord is empty. So, until the first note in a fixed-channel, the adjust-channels, whatever their input, will produce no output. The "all-notes-off" midi-controller (cc123) in one of the adjust-channels clears the Reigning Chord, resulting again in silence. This can be a graceful way to end a piece, to avoid having to fade out an endlessly recycling accompaniment.

If you are improvising in a fixed-channel, try to play your solo line a tiny bit ahead of the chord beat; it doesn't have to be audible - a millisecond will do.


If the -ao sub-option is used, the adjust-channel notes are adjusted to avoid any semitone-dischord with one of the currently On notes in a fixed-channel.

With the -ac sub-option there is no difference between how the white keys and the black keys are treated; the note is chosen Closest available pitch that is not discordant with a fixed-channel. This is usually used in conjunction with the o sub-option, i.e. -aco, with slow-moving fixed-channel notes and a faster-moving solo on the adjust-channel.

-a AND ITS SUB-OPTIONS :   -a,   -ac,   -am,   -ao,   -ar,   -at

The sub-options of -a affect the way the pitches in the adjust-channels get adjusted. They can be concatenated in the same argument, for example -aco which is a useful way to fix dischordant notes in a fast solo over a a slower channel which is setting the harmony; also -ato or -acot etc.

-a 4,5

In this example, the midi channels 4 and 5 will be used for the 'adjustable' voices. Notes input on those channels are not transmitted to the output; they are used to decide which of the current chord-notes will be output.

If the input-note is a black key, then all the current chord-notes are output, in a chord. The chord will be in close position, and inverted so that its lowest note is not lower than the input-note.

If the input-note is a white key, then only one of the current chord-notes will be output. If the input-note is a C then the lowest chord-note in that octave is chosen, and if the input-note is a D the the second-lowest, and thereafter the chosen chord-note meanders upwards (there are at most 6 chord-notes, but there are 7 white notes in an octave).

In either case, the chord-notes are output on the same channel as the input note came in on, and the note's duration and velocity (or volume) are also conserved.

This causes all adjust-channel keys to be translated differently into the choice of adjusted-note: by taking the closest available pitch that is not discordant with a fixed-channel. It is incompatible with the -am option.
This causes all adjust-channel keys to be translated by taking the closest available pitch that is part of the Reigning-Chord. This option was introduced in version 1.9

This causes adjust-channel white-keys to be translated differently into the choice of chord-note: by taking the Modulus of the key-number. If any input-note is a C then the lowest available chord-note in that octave is chosen, if the input-note is a D the the second-lowest, and so on, but returning to the lowest chord-note and starting again after reaching the highest; so cycling through the chord-notes. It is incompatible with the -ac option.


This causes the Reigning chord to consist of only the currently On notes of the fixed-channels; in other words the memory of a fixed-channel note will not persist. It allows a solo line more freedom, and is often used in combination with -ac  for example -aco

Do not confuse this -ao sub-option with the top-level -o option which sets the ALSA output port.


This introduces a tendancy to use Rests on white adjust-channel-notes, if they would otherwise repeat the adjusted-pitch most recently played. It is not very compatible with the -ao option.


This causes any chord-notes contradicted by a fixed-note to be immediately Terminated.   The default is to allow the chord-note to continue to its normal duration. Using -at gives a harmonically cleaner and lighter sound, but it can result in some ugly short notes in the adjust-channels. You may need to use -at if your chord-notes are very long notes.

OTHER OPTIONS :   -f,   -n,   -Q,   -i,   -o

-f 0,1

In this example, the midi channels 0 and 1 (out of 0...15) will be treated as the 'fixed' voices, or 'free' voices, and the adjustable-notes will be chosen so as not to be discordant with them. In this context, an interval of a semitone (plus or minus any number of octaves) is treated as a dischord. Notes on the fixed-channels (or any other non-adjust-channels) will by default also be transmitted unchanged to the output, though this can be changed with the -n option.


This causes midichord to not transmit the non-adjust-channels (including the fixed-channels, and all the other channels which are neither fixed nor adjusted).   This is useful because it allows midichord and midiecho to be run in parallel, for example both from 14:0 to 14:1 (where Midi Through is 14), without outputting the non-adjust-channels in duplicate.


This causes midichord to run in Quiet mode, in which nothing is printed to the screen; this is useful in scripts and Makefiles, and only makes sense in real-time mode (with -i).   In Quiet mode, midichord will terminate if one of its ALSA connections becomes disconnected.

-i 32:0 or -i ProKeys

This option puts midichord into raw-midi (or real-time, or midi-on-the-wire) mode, and takes the midi-data from the specified ALSA-port or ports.
If the ALSA port is specified as 0 then midichord will start up in real-time mode but without connecting from anything.
You can check out the available ports with the command arecordmidi -l or aconnect -il.

-o 128:0 or -o TiMidity

This option sets the ALSA-port or ports to which the midi output will be sent. You can check out the available ports with the command aplaymidi -l or aconnect -ol. The default ouput-port (if only -i option is present) is the environment variable $ALSA_OUTPUT_PORTS


For example, a quirky waltz such as
was produced by a chord pattern such as this waltz.txt (in muscript):

 midi channel 0 patch 71 pan 50  # solo clarinet
 midi channel 4 patch 21 pan 20  # accordeon
 midi channel 5 patch 48 pan 80  # arco bass
 | .5 .6 .5
 =1 bass cha5 4 E cha4 8 C# rest 4 C#
 =1 cha5 4 C cha4 8 C# rest 4 C#
 =1 cha5 4 E cha4 8 C# rest 4 C#
 =1 cha5 4 C cha4 8 C# rest 4 C#
 =1 cha5 4 E cha4 8 D# rest 4 D#
 =1 cha5 4 c cha4 8 D# rest 4 D#
 =1 cha4 8 C# C# C# rest C# rest
 =1 cha5 4 E rest rest
 =1 cha4 4 C# rest d
 =1 cha5 4 C rest rest

by improvising a solo over the top of it, on the ProKeys keyboard on channel 0, using these commands:

 aconnect -x
 aconnect Midi:1 Roland   # Roland is the synth
 midichord -f 0 -a 4,5 -i ProKeys,Midi:0 -o Midi:1 &
 arecordmidi -p Midi:1 -b 60 -t 1000 waltz.mid &
 muscript -midi waltz.txt | aplaymidi -p Midi:0 -
 pkill -INT midichord
 pkill -INT arecordmidi


The current version of midichord is available by http at www.pjb.com.au/midi/free/midichord
To install midichord, save it to disc, move it into your $PATH, make it executable, and if necessary edit the first line to reflect where perl is installed on your system.


 1.9 20150818 introduce -aC with $ClosestChord and key2closestchordnote()
 1.8 20150812 -aco unchanged, but -ac now adjusts to @ReigningChord
 1.7 20150809 an initially empty reigning-chord handled more gracefully
 1.6 20130805 bug with excessive note_offs in real-time mode fixed
 1.5 20130719 fixed-channel note-ons get output in real-time mode
 1.4 20130330 -i 0 forces realtime mode without connecting from anywhere
 1.3 20120811 move to -f and -a, with -a having suboptions c,m,o,r,t
 1.2 20111223 -n, no tranmission of fixed-channels
 1.1 20111217 real-time, midi-file; almost releaseable
 1.0 20111214 first working version


Peter J Billam http://www.pjb.com.au/comp/contact.html


Based on the MIDI::Perl CPAN module in midi-file mode, and the MIDI::ALSA CPAN module in real-time mode.