Code Monkey home page Code Monkey logo

py-midi's Introduction

py-midi

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:

>>> 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.

py-midi's People

Contributors

edthrn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.