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

Class Instrument

mingus/containers/instrument.py:26–98  ·  view source on GitHub ↗

An instrument object. The Instrument class is pretty self explanatory. Instruments can be used with Tracks to define which instrument plays what, with the added bonus of checking whether the entered notes are in the range of the instrument. It's probably easiest to subclass you

Source from the content-addressed store, hash-verified

24
25
26class Instrument(object):
27
28 """An instrument object.
29
30 The Instrument class is pretty self explanatory. Instruments can be used
31 with Tracks to define which instrument plays what, with the added bonus
32 of checking whether the entered notes are in the range of the
33 instrument.
34
35 It's probably easiest to subclass your own Instruments (see Piano and
36 Guitar for examples).
37 """
38
39 name = "Instrument"
40 range = (Note("C", 0), Note("C", 8))
41 clef = "bass and treble"
42 tuning = None # optional StringTuning object
43
44 def __init__(self):
45 pass
46
47 def set_range(self, range):
48 """Set the range of the instrument.
49
50 A range is a tuple of two Notes or note strings.
51 """
52 if isinstance(range[0], six.string_types):
53 range[0] = Note(range[0])
54 range[1] = Note(range[1])
55 if not hasattr(range[0], "name"):
56 raise UnexpectedObjectError(
57 "Unexpected object '%s'. "
58 "Expecting a mingus.containers.Note object" % range[0]
59 )
60 self.range = range
61
62 def note_in_range(self, note):
63 """Test whether note is in the range of this Instrument.
64
65 Return True if so, False otherwise.
66 """
67 if isinstance(note, six.string_types):
68 note = Note(note)
69 if not hasattr(note, "name"):
70 raise UnexpectedObjectError(
71 "Unexpected object '%s'. "
72 "Expecting a mingus.containers.Note object" % note
73 )
74 if note >= self.range[0] and note <= self.range[1]:
75 return True
76 return False
77
78 def notes_in_range(self, notes):
79 """An alias for can_play_notes."""
80 return self.can_play_notes(notes)
81
82 def can_play_notes(self, notes):
83 """Test if the notes lie within the range of the instrument.

Callers 2

setUpMethod · 0.90
setUpMethod · 0.90

Calls 1

NoteClass · 0.90

Tested by 2

setUpMethod · 0.72
setUpMethod · 0.72