How to Play Music Written in MML

Here, we introduce two methods for playing music written in Pytakt MML. One method is to convert it to a standard MIDI file (aka a MIDI file) and play that. The other method is to send MIDI messages directly from Pytakt to a synthesizer (software or hardware for waveform synthesis) for playback.

Converting MML to MIDI files

Using the pytakt command, which becomes available after installing Pytakt, you can create MIDI files from music written in MML by entering the following line from the shell (Terminal, or PowerShell on Windows). The string cde corresponds to MML notation, indicating that the notes C, D, and E should be played in sequence with quarter notes. test.mid is the filename of the MIDI file that will be created.

$ pytakt -o test.mid -m 'cde'

To play the MIDI file, follow methods specific to each operating system.

The same task can also be performed interactively from the Python interpreter. Running the pytakt command without arguments launches the Python interpreter with the pytakt library loaded. From there, you can output MIDI files as shown below.

$ pytakt
pytakt version 1.1.0
>>> mml('cde').writesmf('test.mid')

In addition, you can store MML text in a file and convert it to a MIDI file. For example, using your preferred text editor (emacs in this example), enter the content cde and save it as test.mml.

emacs image

Then, you can convert it to a MIDI file using the pytakt command as below.

$ pytakt -o test.mid test.mml

Play directly from Pytakt

Playing directly from Pytakt eliminates the need for MIDI file conversion and also allows you to play music with piano roll display.

To play directly with Pytakt, you need either a software synthesizer built within your PC or an external synthesizer such as a MIDI keyboard. Below are examples of setup procedures to use a software synthesizer for each operating system.

Once the setup is complete, launch pytakt as shown below to display the piano roll window.

$ pytakt -m 'cde'  (For MML in the command-line)
$ pytakt test.mml  (For the file)

pianoroll window

Press the play button at the top right to start the performance, and press it again to stop. To play without displaying the piano roll, specify the -p option as shown below.

$ pytakt -p -m 'cde'  (For MML in the command-line)
$ pytakt -p test.mml  (For the file)

When running interactively from Python, use play() or show() as shown below.

$ pytakt
pytakt version 1.1.0
>>> play('cde')   # or mml('cde').play() to play
>>> mml('cde').show()   # to show the piano roll

Changing the MIDI output device

When playing directly from Pytakt, if multiple MIDI output devices are available, you may need to select which one to use. You can view the list of output devices using pytakt -l.

$ pytakt -l
MIDI Output Devices:
    [0] 14:0 Midi Through Port-0
 >  [1] 129:0 TiMidity port 0
    [2] 129:1 TiMidity port 1
    [3] 129:2 TiMidity port 2
    [4] 129:3 TiMidity port 3

MIDI Input Devices:
    [0] 14:0 Midi Through Port-0

'*': opened   '>': currently selected

From the display above, it can be seen that the currently selected output device is “[1] TiMidity port 0”. To change this to, for example, “[4] TiMidity port 3” for playback, use the -d option.

$ pytakt -d4 -m 'cde'
$ pytakt -d4 test.mml
$ pytakt -d4
pytakt version 1.1.0
>>> play('cde')

Specify the number displayed in the brackets after -d as shown above, or specify part of the device name, such as -d "port 3".

When running Python interactively, you can change the output device later using set_output_device().

>>> set_output_device(3)
>>> show_devices()   # Confirm that the output device has been changed.
MIDI Output Devices:
    [0] 14:0 Midi Through Port-0
    [1] 129:0 TiMidity port 0
    [2] 129:1 TiMidity port 1
 >  [3] 129:2 TiMidity port 2
    [4] 129:3 TiMidity port 3

MIDI Input Devices:
    [0] 14:0 Midi Through Port-0

'*': opened   '>': currently selected
>>> play('cde')   # Output to "[3] TiMidity port 2"


Last update: Feb 17 2026 16:18 JST     Copyright (C) 2026 Satoshi Nishimura Top Page