Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions mingus/midi/sequencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from mingus.containers.instrument import MidiInstrument
from six.moves import range

import six

class Sequencer(object):

Expand Down Expand Up @@ -110,10 +110,22 @@ def notify_listeners(self, msg_type, params):

def set_instrument(self, channel, instr, bank=0):
"""Set the channel to the instrument _instr_."""
self.instr_event(channel, instr, bank)
if isinstance(instr, MidiInstrument):
try:
instr_i = instr.names.index(instr.name)
except ValueError:
instr_i = 1
elif isinstance(instr, six.string_types):
try:
instr_i = MidiInstrument.names.index(instr)
except ValueError:
instr_i = 1
else:
instr_i = instr
self.instr_event(channel, instr_i, bank)
self.notify_listeners(
self.MSG_INSTR,
{"channel": int(channel), "instr": int(instr), "bank": int(bank)},
{"channel": int(channel), "instr": int(instr_i), "bank": int(bank)},
)

def control_change(self, channel, control, value):
Expand All @@ -139,10 +151,11 @@ def play_Note(self, note, channel=1, velocity=100):
you can set the Note.velocity and Note.channel attributes, which
will take presedence over the function arguments.
"""
if hasattr(note, "velocity"):
velocity = note.velocity
if hasattr(note, "channel"):
channel = note.channel
if hasattr(note, '__dict__'):
if "velocity" in note.__dict__:
velocity = note.velocity
if "channel" in note.__dict__:
channel = note.channel
self.play_event(int(note) + 12, int(channel), int(velocity))
self.notify_listeners(
self.MSG_PLAY_INT,
Expand All @@ -164,7 +177,7 @@ def stop_Note(self, note, channel=1):
If Note.channel is set, it will take presedence over the channel
argument given here.
"""
if hasattr(note, "channel"):
if hasattr(note, '__dict__') and "channel" in note.__dict__:
channel = note.channel
self.stop_event(int(note) + 12, int(channel))
self.notify_listeners(self.MSG_STOP_INT, {"channel": int(channel), "note": int(note) + 12})
Expand Down Expand Up @@ -306,6 +319,9 @@ def play_Bars(self, bars, channels, bpm=120):
def play_Track(self, track, channel=1, bpm=120):
"""Play a Track object."""
self.notify_listeners(self.MSG_PLAY_TRACK, {"track": track, "channel": channel, "bpm": bpm})
instr = track.instrument
if instr is not None:
self.set_instrument(channel, instr)
for bar in track:
res = self.play_Bar(bar, channel, bpm)
if res != {}:
Expand All @@ -327,14 +343,8 @@ def play_Tracks(self, tracks, channels, bpm=120):
# Set the right instruments
for x in range(len(tracks)):
instr = tracks[x].instrument
if isinstance(instr, MidiInstrument):
try:
i = instr.names.index(instr.name)
except:
i = 1
self.set_instrument(channels[x], i)
else:
self.set_instrument(channels[x], 1)
if instr is not None:
self.set_instrument(channels[x], instr)
current_bar = 0
max_bar = len(tracks[0])

Expand Down