MCPcopy Create free account
hub / github.com/bspaans/python-mingus / MidiTrack

Class MidiTrack

mingus/midi/midi_track.py:37–285  ·  view source on GitHub ↗

A class used to generate MIDI events from the objects in mingus.containers.

Source from the content-addressed store, hash-verified

35
36
37class MidiTrack(object):
38
39 """A class used to generate MIDI events from the objects in
40 mingus.containers."""
41
42 track_data = b""
43 delta_time = b"\x00"
44 delay = 0
45 bpm = 120
46 change_instrument = False
47 instrument = 1
48
49 def __init__(self, start_bpm=120):
50 self.track_data = b""
51 self.set_tempo(start_bpm)
52
53 def end_of_track(self):
54 """Return the bytes for an end of track meta event."""
55 return b"\x00\xff\x2f\x00"
56
57 def play_Note(self, note):
58 """Convert a Note object to a midi event and adds it to the
59 track_data.
60
61 To set the channel on which to play this note, set Note.channel, the
62 same goes for Note.velocity.
63 """
64 channel = note.channel
65 velocity = note.velocity
66 if self.change_instrument:
67 self.set_instrument(channel, self.instrument)
68 self.change_instrument = False
69
70 assert 0 <= velocity <= 0x7F
71
72 self.track_data += self.note_on(channel, int(note) + 12, velocity)
73
74 def play_NoteContainer(self, notecontainer):
75 """Convert a mingus.containers.NoteContainer to the equivalent MIDI
76 events and add it to the track_data.
77
78 Note.channel and Note.velocity can be set as well.
79 """
80 if len(notecontainer) <= 1:
81 [self.play_Note(x) for x in notecontainer]
82 else:
83 self.play_Note(notecontainer[0])
84 self.set_deltatime(0)
85 [self.play_Note(x) for x in notecontainer[1:]]
86
87 def play_Bar(self, bar):
88 """Convert a Bar object to MIDI events and write them to the
89 track_data."""
90 self.set_deltatime(self.delay)
91 self.delay = 0
92 self.set_meter(bar.meter)
93 self.set_deltatime(0)
94 self.set_key(bar.key)

Callers 5

write_NoteFunction · 0.90
write_NoteContainerFunction · 0.90
write_BarFunction · 0.90
write_TrackFunction · 0.90
write_CompositionFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected