Using Mondrian

News

I recently noticed that I can't map the F4 function in emacs on my Mac. I had to shift my key mappings up by one, e.g., what used to be F4 is now F5. This page documents the current mappings.

Using the MIDI file backend

python mondrian.py [-s] [-o output.mid] [input.mon ...]

If you haven't patched the Python Midi Package, then Mondrian cannot write to stdout, so that you will always have to specify an output file. The -s switch runs Mondrian in strict mode, i.e., the slightest irregularity will cause Mondrian to quit with an exception. By default, Mondrian will try to recover from exceptions whenever this is reasonable.

If you have patched Python Midi, then you can use Mondrian in pipes, like this:

python mondrian.py fj.mon | timidity -

In order to translate MIDI files to Mondrian code, say

python demondrian.py [-c] [-q n] [-v m] [-o out.mon] [file.mid]

The -c switch enables the compression feature, which tries to extract macros by detecting regularities of the MIDI file (warning: the compression feature is still somewhat experimental and may take a long time to finish). The -q and -v options enable quantization; -q n rounds note lengths to the closest multiple of 2^-n, and -v m rounds velocities to the closest multiple of m.

In theory, the compression feature may be used to mine MIDI files for recurring pieces, such as drum patterns and bass lines; just feed in a MIDI file and see whether any large macros emerge. In practice, though, the compression feature seems to extract macros all over the place, and it's not always clear how to identify the pertinent ones.

Using MondrianLive

The basic idea is that MondrianLive keeps a queue of chunks of music (a chunk might be anything; a note, a measure, an entire song, etc.) that are played in sequence. Whenever the user adds a new chunk, it is added to this queue and will be played after all previous chunks have been played. When MondrianLive reaches the end of this queue, it keeps repeating the last chunk, unless the last chunk is empty, in which case playback will be paused until the next nonempty chunk arrives.

Vim plugin

If you have installed the vim plugin, then vim will load it and launch the MondrianLive sequencer when you open a file with a .mon extension. When you press F6, MondrianLive will list all devices that currently accept MIDI input as well as the current contents of the queue. You can connect the output port(s) of MondrianLive to those devices with tools like aconnect, or with the map command (see below).

If you press F5, vim sends the current line into MondrianLive, which will interpret it and append it to its queue of chunks of music. If you mark a range of lines (e.g., using vim's visual mode) and press F5, then the entire range will be sent to MondrianLive.

F7 will pause or continue playback, and F8 will stop MondrianLive and clear all current chunks. Playback will start again when you send in a new chunk of code. Of course, MondrianLive will terminate when you leave vim.

Emacs plugin

The emacs plugin will be loaded when you open a file with a .mon extension. It behaves much like the vim plugin, with two small differences. First, every time you press one of the special function keys, emacs will switch to a buffer that captures the output of MondrianLive, and you need to press the function key again in order to get back to your original buffer.

Second, when you press F5, the range of lines defined by the point and the mark is sent to MondrianLive, unless the mark hasn't been set yet, in which case the current line is sent to MondrianLive. (For instance, if you highlight a region with the mouse and press F5, then all the lines intersecting this region will be sent to MondrianLive, so that you don't have too aim too carefully; it's okay if the first and last lines aren't entirely selected)

Standalone use

In order to use MondrianLive outside of vim, launch the interactive Mondrian sequencer by typing

python mondrianlive.py
at your shell prompt. When started like this, MondrianLive creates a named pipe called /tmp/mondrianlive.pipe, and you can send code into MondrianLive by writing to this file. In order to terminate MondrianLive, just send it a single line containing a dot.

MondrianLive commands

MondrianLive interprets lines of the form

## command parameters
as commands for the sequencer. The following commands are available:

map
Maps tracks to MIDI devices. It takes up to four parameters: The client ID (or name) of the target device, the port number (or name) of the target device, a floating point number giving the latency of the target device (measured in milliseconds), and the track to be mapped to the device. The last three parameters are optional. If the track number is omitted, then the device will be the default device, i.e., it will be the target for all tracks that are not mapped to another device. The default latency is zero, as is the default port. Names don't have to match exactly; case doesn't matter, spaces and underscores are the same, and it suffices to specify a substring of the name that uniquely determines the device/port. If called without parameters, the map command will list all potential target devices.
For example, ## map timidity 0 400 2 will map track 2 to port 0 of timidity, with a latency of 400ms. The command ## map 129 will make port 0 of client 129 the default target, with latency 0.
Note that it is generally a bad idea to remap a track while events from this track are being played because events that have already been scheduled at the time of remapping will be lost.
unmap
If called with a track number, unmap will remove the mapping of this track, i.e., events for this track will be sent to the default device. If called without parameters, unmap will list all current track assignments.
imap
Connects the input port of MondrianLive to a MIDI input device, typically a keyboard. The imap command takes the same parameters as the map command, i.e., client ID number/name, client port number/name, device latency in milliseconds, and the number of the track to which events from this device will be recorded. Port and latency are optional and default to zero. If the track number is omitted, then events from this device will be recorded to the current track (as indicated by the state of Mondrian).
When called without parameters, it will print the current list of MIDI input devices.
iunmap
If called with a client name/number and an (optional) port name/number, iunmap will remove the connection from this device. If called without parameters, it will list all incoming connections.
record
Converts MIDI events that arrived at MondrianLive's input port while the most recent loop was being played (and before the record command was issued) to Mondrian code. If you are using an editor plugin, the resulting code will be appended to your file. If you are using MondrianLive outside of an editor, then the write command will output the recording.
This command takes two optional parameters, a note length quantization (interpreted as a negative exponent of two) and a velocity quantization. For instance, ## record 3 16 will limit note lengths to multiples of eighth notes (since 2^-3=1/8) and velocities to multiples of 16.
write
Writes a transcript of the current session to a file. The parameter is a file name. For instance, ## write spam.mon will write the current session to spam.mon. The transcript indicates how many times each loop has been played, so that you can reconstruct the entire performance from this file.
skip
If called with one integer parameter n, the next n chunks in the queue will be skipped, unless n is zero, in which case the entire queue will be cleared. If called without parameters, it lists the current contents of the queue.
state
Prints current state of Mondrian interpreter; no parameters.
reset
Resets the Mondrian interpreter to its initial state; no parameters.
pause, continue
Pretty much what you'd expect; no parameters.
clear
Stops playback and clears all current segments; no parameters.

Using MondrianPM

The basic behavior of MondrianPM is the same as that of MondrianLive.

Vim plugin

When you open a file with a .mpm extension, vim will load the MondrianPM plugin and launch the MondrianPM sequencer. If you press F6, the plugin will print the list of devices currently accepting MIDI input as well as the current contents of the queue. Press F5 in order to send the current line or range to MondrianPM. The contents of this line or range will be repeated until you send in the next chunk; sending it a single empty line will pause the sequencer once it is done playing the chunks that came before.

F7 will pause or continue playback, and F8 will stop MondrianPM and clear all current chunks. Playback will start again when you send in a new chunk of code. Of course, MondrianPM will terminate when you leave vim.

Emacs plugin

The emacs plugin for MondrainPM behaves much like the vim plugin. It, too, will be loaded when you open a file with a .mpm extension. In addition, the comments regarding the emacs plugin for MondrianLive apply.

Standalone use

If you want to use MondrianPM outside of vim or emacs, just say python mondrianpm.py and write Mondrian code to the file /tmp/mondrianpm.pipe.

MondrianPM commands

Like MondrianLive commands, MondrianPM commands are of the form ## command parameters.

map
Assigns MIDI tracks to devices. If called without parameters, this command will list all devices that currently accept MIDI input. The map command takes up to three parameters: the index or name of the target device, the latency of the target device (in milliseconds), and the track to be mapped to this device. If you specify the target device by name, then the name doesn't have to match exactly; an unambiguous substring of the name is enough, and case doesn't matter. If the desired name contains spaces, you need to replace them by underscores. For instance, ## map timidity_port_0 will make the device called 'TiMidity port 0' the default device.
The last two parameters are optional. If the track number is missing, then the device given by the first parameter will be the default device, i.e., the outlet for all tracks that haven't been mapped to a device. If the second parameter is missing as well, then the latency will be one millisecond.
write
Writes a transcript of the current session to a file. The parameter is a file name.
skip
If called with one integer parameter n, the next n chunks in the queue will be skipped. If called without parameters, it lists the current contents of the queue.
state
Prints current state of Mondrian interpreter; no parameters.
reset
Resets Mondrian interpreter to its initial state; no parameters.
pause, continue
Pretty much what you'd expect; no parameters.
clear
Stops playback and clears all current segments; no parameters.

Peter Brinkmann
$Date: 2006/11/21 02:52:31 $