2018-05-23 19:49:54 +02:00
|
|
|
import rtmidi
|
|
|
|
import time
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import Plugin, action
|
2018-05-23 19:49:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MidiPlugin(Plugin):
|
|
|
|
"""
|
2018-06-25 00:49:45 +02:00
|
|
|
Virtual MIDI controller plugin. It allows you to send custom MIDI messages
|
|
|
|
to any connected devices.
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
|
|
|
* **python-rtmidi** (``pip install python-rtmidi``)
|
2018-05-23 19:49:54 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
_played_notes = set()
|
|
|
|
|
|
|
|
def __init__(self, device_name='Platypush virtual MIDI output',
|
|
|
|
*args, **kwargs):
|
2018-06-25 00:49:45 +02:00
|
|
|
"""
|
|
|
|
:param device_name: MIDI virtual device name (default: *Platypush virtual MIDI output*)
|
|
|
|
:type device_name: str
|
|
|
|
"""
|
|
|
|
|
2018-05-23 19:49:54 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self.device_name = device_name
|
|
|
|
self.midiout = rtmidi.MidiOut()
|
|
|
|
available_ports = self.midiout.get_ports()
|
|
|
|
|
|
|
|
if available_ports:
|
|
|
|
self.midiout.open_port(0)
|
2018-06-06 20:09:18 +02:00
|
|
|
self.logger.info('Initialized MIDI plugin on port 0')
|
2018-05-23 19:49:54 +02:00
|
|
|
else:
|
|
|
|
self.open_virtual_port(self.device_name)
|
2018-06-06 20:09:18 +02:00
|
|
|
self.logger.info('Initialized MIDI plugin on virtual device {}'.
|
2018-05-23 19:49:54 +02:00
|
|
|
format(self.device_name))
|
|
|
|
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-05-23 19:49:54 +02:00
|
|
|
def send_message(self, values, *args, **kwargs):
|
|
|
|
"""
|
2018-06-25 00:49:45 +02:00
|
|
|
:param values: Values is expected to be a list containing the MIDI command code and the command parameters - see reference at https://ccrma.stanford.edu/~craig/articles/linuxmidi/misc/essenmidi.html
|
|
|
|
:type values: list[int]
|
2018-05-23 19:49:54 +02:00
|
|
|
|
|
|
|
Available MIDI commands:
|
2018-06-25 00:49:45 +02:00
|
|
|
* ``0x80`` Note Off
|
|
|
|
* ``0x90`` Note On
|
|
|
|
* ``0xA0`` Aftertouch
|
|
|
|
* ``0xB0`` Continuous controller
|
|
|
|
* ``0xC0`` Patch change
|
|
|
|
* ``0xD0`` Channel Pressure
|
|
|
|
* ``0xE0`` Pitch bend
|
|
|
|
* ``0xF0`` Start of system exclusive message
|
|
|
|
* ``0xF1`` MIDI Time Code Quarter Frame (Sys Common)
|
|
|
|
* ``0xF2`` Song Position Pointer (Sys Common)
|
|
|
|
* ``0xF3`` Song Select
|
|
|
|
* ``0xF6`` Tune Request (Sys Common)
|
|
|
|
* ``0xF7`` End of system exclusive message
|
|
|
|
* ``0xF8`` Timing Clock (Sys Realtime)
|
|
|
|
* ``0xFA`` Start (Sys Realtime)
|
|
|
|
* ``0xFB`` Continue (Sys Realtime)
|
|
|
|
* ``0xFC`` Stop (Sys Realtime)
|
|
|
|
* ``0xFE`` Active Sensing (Sys Realtime)
|
|
|
|
* ``0xFF`` System Reset (Sys Realtime)
|
|
|
|
|
|
|
|
:param args: Extra args that will be passed to ``rtmidi.send_message``
|
|
|
|
:param kwargs: Extra kwargs that will be passed to ``rtmidi.send_message``
|
2018-05-23 19:49:54 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
self.midiout.send_message(values, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-05-23 19:49:54 +02:00
|
|
|
def play_note(self, note, velocity, duration=0):
|
|
|
|
"""
|
2018-06-25 00:49:45 +02:00
|
|
|
Play a note with selected velocity and duration.
|
|
|
|
|
|
|
|
:param note: MIDI note in range 0-127 with #60 = C4
|
|
|
|
:type note: int
|
|
|
|
|
|
|
|
:param velocity: MIDI note velocity in range 0-127
|
|
|
|
:type velocity: int
|
|
|
|
|
|
|
|
:param duration: Note duration in seconds. Pass 0 if you don't want the note to get off
|
|
|
|
:type duration: float
|
2018-05-23 19:49:54 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
self.send_message([0x90, note, velocity]) # Note on
|
|
|
|
self._played_notes.add(note)
|
|
|
|
|
|
|
|
if duration:
|
|
|
|
time.sleep(duration)
|
|
|
|
self.send_message([0x80, note, 0]) # Note off
|
|
|
|
self._played_notes.remove(note)
|
|
|
|
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-05-23 19:49:54 +02:00
|
|
|
def release_note(self, note):
|
2018-06-25 00:49:45 +02:00
|
|
|
"""
|
|
|
|
Release a played note.
|
|
|
|
|
|
|
|
:param note: MIDI note in range 0-127 with #60 = C4
|
|
|
|
:type note: int
|
|
|
|
"""
|
|
|
|
|
2018-05-23 19:49:54 +02:00
|
|
|
self.send_message([0x80, note, 0]) # Note off
|
|
|
|
self._played_notes.remove(note)
|
|
|
|
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-05-23 19:49:54 +02:00
|
|
|
def release_all_notes(self):
|
2018-06-25 00:49:45 +02:00
|
|
|
"""
|
|
|
|
Release all the notes being played.
|
|
|
|
"""
|
|
|
|
|
2018-05-23 19:49:54 +02:00
|
|
|
played_notes = self._played_notes.copy()
|
|
|
|
for note in played_notes:
|
|
|
|
self.release_note(note)
|
|
|
|
|
2018-06-25 00:49:45 +02:00
|
|
|
|
2018-05-23 19:49:54 +02:00
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|