File: __init__.py

package info (click to toggle)
python-mido 1.3.3-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 920 kB
  • sloc: python: 4,006; makefile: 127; sh: 4
file content (179 lines) | stat: -rw-r--r-- 4,643 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# SPDX-FileCopyrightText: 2013 Ole Martin Bjorndalen <ombdalen@gmail.com>
#
# SPDX-License-Identifier: MIT

"""
MIDI Objects for Python

Mido is a library for working with MIDI messages and ports. It's
designed to be as straight forward and Pythonic as possible.

Creating messages:

    Message(type, **parameters) -- create a new message
    MetaMessage(type, **parameters) -- create a new meta message
    UnknownMetaMessage(type_byte, data=None, time=0)

Ports:

    open_input(name=None, virtual=False, callback=None) -- open an input port
    open_output(name=None, virtual=False,               -- open an output port
                autoreset=False)
    open_ioport(name=None, virtual=False,        -- open an I/O port (capable
                callback=None, autoreset=False)     of both input and output)

    get_input_names() -- return a list of names of available input ports
    get_output_names() -- return a list of names of available output ports
    get_ioport_names() -- return a list of names of available I/O ports

MIDI files:

    MidiFile(filename, **kwargs) -- open a MIDI file
    MidiTrack()  -- a MIDI track
    bpm2tempo()  -- convert beats per minute to MIDI file tempo
    tempo2bpm()  -- convert MIDI file tempo to beats per minute
    merge_tracks(tracks)  -- merge tracks into one track

SYX files:

    read_syx_file(filename)  -- read a SYX file
    write_syx_file(filename, messages,
                   plaintext=False)  -- write a SYX file
Parsing MIDI streams:

    parse(bytes) -- parse a single message bytes
                    (any iterable that generates integers in 0..127)
    parse_all(bytes) -- parse all messages bytes
    Parser -- MIDI parser class

Parsing objects serialized with str(message):

    parse_string(string) -- parse a string containing a message
    parse_string_stream(iterable) -- parse strings from an iterable and
                                     generate messages

Sub modules:

    ports -- useful tools for working with ports

For more on MIDI, see:

    http://www.midi.org/


Getting started:

    >>> import mido
    >>> m = mido.Message('note_on', note=60, velocity=64)
    >>> m
    <message note_on channel=0, note=60, velocity=64, time=0>
    >>> m.type
    'note_on'
    >>> m.channel = 6
    >>> m.note = 19
    >>> m.copy(velocity=120)
    <message note_on channel=0, note=60, velocity=64, time=0>
    >>> s = mido.Message('sysex', data=[byte for byte in range(5)])
    >>> s.data
    (0, 1, 2, 3, 4)
    >>> s.hex()
    'F0 00 01 02 03 04 F7'
    >>> len(s)
    7

    >>> default_input = mido.open_input()
    >>> default_input.name
    'MPK mini MIDI 1'
    >>> output = mido.open_output('SD-20 Part A')
    >>>
    >>> for message in default_input:
    ...     output.send(message)

    >>> get_input_names()
    ['MPK mini MIDI 1', 'SH-201']
"""

from . import ports, sockets
from .backends.backend import Backend
from .messages import (
    MAX_PITCHWHEEL,
    MAX_SONGPOS,
    MIN_PITCHWHEEL,
    MIN_SONGPOS,
    Message,
    format_as_string,
    parse_string,
    parse_string_stream,
)
from .midifiles import (
    KeySignatureError,
    MetaMessage,
    MidiFile,
    MidiTrack,
    UnknownMetaMessage,
    bpm2tempo,
    merge_tracks,
    second2tick,
    tempo2bpm,
    tick2second,
)
from .parser import Parser, parse, parse_all
from .syx import read_syx_file, write_syx_file
from .version import version_info

__all__ = [
    "KeySignatureError",
    "MAX_PITCHWHEEL",
    "MAX_SONGPOS",
    "MIN_PITCHWHEEL",
    "MIN_SONGPOS",
    "Message",
    "MetaMessage",
    "MidiFile",
    "MidiTrack",
    "Parser",
    "UnknownMetaMessage",
    "bpm2tempo",
    "format_as_string",
    "merge_tracks",
    "parse",
    "parse_all",
    "parse_string",
    "parse_string_stream",
    "ports",
    "read_syx_file",
    "second2tick",
    "sockets",
    "tempo2bpm",
    "tick2second",
    "version_info",
    "write_syx_file",
]


def set_backend(name=None, load=False):
    """Set current backend.

    name can be a module name like 'mido.backends.rtmidi' or
    a Backend object.

    If no name is passed, the default backend will be used.

    This will replace all the open_*() and get_*_name() functions
    in top level mido module. The module will be loaded the first
    time one of those functions is called."""

    glob = globals()

    if isinstance(name, Backend):
        backend = name
    else:
        backend = Backend(name, load=load, use_environ=True)
    glob['backend'] = backend

    for name in dir(backend):
        if name.split('_')[0] in ['open', 'get']:
            glob[name] = getattr(backend, name)


set_backend()