Skip to main content

A package for sending and receiving MIDI messages easily.

Project description

Simply send and receive MIDI messages using Python3.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.

Warning: To use with Python3 only. The library doesn’t work with Python2.

1. Purpose

This Python3 library has been made in order to communicate easily with any MIDI devices. The only requirement is to have a serial interface on your machine. It’s on this interface that you must connect MIDI cables to establish a communication between your machine and the MIDI devices.

The py-midi library allows users to build and/or read MIDI messages without having to worry on formating them before sending and/or after receiving.

MIDI (Musical Instrument Digital Interface) is a standard used for making easy for instruments, computers and other electronic devices to communicate.

This library is able to deal with any kind of MIDI messages, on any of the 16 possible channels (for channel-type messages) or with any kind of SysEx messages.

For more details on the MIDI standard, see Wikipedia MIDI page https://en.wikipedia.org/wiki/MIDI

2. Installation

Easily install the package using pip:

$ pip3 install py-midi

Then you can import the package to your program:

>>> import midi

3. Getting started

Creates an interface between your program and the serial port of the machine. You instanciate by giving the path to the serial port. Example:

>>> from midi import MidiConnector
>>> conn = MidiConnector('/dev/serial0')  # path to use on RaspberryPi 3

If you don’t want the MidiConnector.read() method to block forever if it receives nothing, use the keyword argument timeout to set up a maximum duration (seconds) of blocking:

>>> conn = MidiConnector('/dev/serial0', timeout=5)

The timeout kwarg is only used for reading, not for writing.

To send a MIDI message, you first need to instantiate a MidiMessageType. There are 8 differents types of MIDI message. Here they are, with there instanciation parameters:

  • NoteOff(note_number, velocity)

  • NoteOn(note_number, velocity)

  • PolyphonicAftertouch(note_number, pressure)

  • ChannelAftertouch(pressure)

  • ControlChange(control_number, value)

  • ProgramChange(program_number)

  • PitchWheel(lsbyte, msbyte)

  • SysEx(manufacturer_id, data1, data2..., dataN)

NOTE All instanciation parameters must be included within [0, 127] except for SysEx data (0 to 255) or ProgramChange’ number (1 to 128).

Example: create a type ControlChange:

>>> from midi import ControlChange
>>> cc = ControlChange(100, 127)

Now build the full message, providing a channel:

>>> from midi import Message
>>> msg = Message(cc, channel=1)

You can access the attributes of your message directly:

>>> msg.control_number
100
>>> msg.value
127

Send the message to MIDI OUT, using the connector:

>>> conn.write(msg)  # returns the number of bytes sent
3

For reading messages received via MIDI IN, use the method read() as follow: (here, we )

>>> msg = conn.read()  # read on ANY channel by default
>>> # Pretend to receive a ProgramChange message, on channel 2
>>> msg
Message(ProgramChange(35), 2)
>>> msg.channel
2
>>> msg.type
ProgramChange(35)
>>> msg.program_number
35

By default, the connector’s read() method reads in OMNI mode. To specify a channel, add the channel number as a parameter:

>>> msg = conn.read(8)  # read only on channel 8, ignore the rest

As per the MIDI standard, there are 16 channels you can read from, numbered from 1 to 16.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

py_midi-2.0-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file py_midi-2.0-py3-none-any.whl.

File metadata

  • Download URL: py_midi-2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.5

File hashes

Hashes for py_midi-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f2531bd51241ebc7732430a8688568890a36e76e8e074d7185786b55a273408
MD5 5e52407fa3612c592f841f1e27dcf9cf
BLAKE2b-256 4eabb26c05d4c73dad6a395d9e47e6f30074e1bc3cf37460a8f265fb768ff36c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page