A track object. The Track class can be used to store Bars and to work on them. The class is also designed to be used with Instruments, but this is optional. Tracks can be stored together in Compositions.
| 27 | |
| 28 | |
| 29 | class Track(object): |
| 30 | |
| 31 | """A track object. |
| 32 | |
| 33 | The Track class can be used to store Bars and to work on them. |
| 34 | |
| 35 | The class is also designed to be used with Instruments, but this is |
| 36 | optional. |
| 37 | |
| 38 | Tracks can be stored together in Compositions. |
| 39 | """ |
| 40 | |
| 41 | bars = [] |
| 42 | instrument = None |
| 43 | name = "Untitled" # Will be looked for when saving a MIDI file. |
| 44 | tuning = None # Used by tablature |
| 45 | |
| 46 | def __init__(self, instrument=None): |
| 47 | self.bars = [] |
| 48 | self.instrument = instrument |
| 49 | |
| 50 | def add_bar(self, bar): |
| 51 | """Add a Bar to the current track.""" |
| 52 | self.bars.append(bar) |
| 53 | return self |
| 54 | |
| 55 | def add_notes(self, note, duration=None): |
| 56 | """Add a Note, note as string or NoteContainer to the last Bar. |
| 57 | |
| 58 | If the Bar is full, a new one will automatically be created. |
| 59 | |
| 60 | If the Bar is not full but the note can't fit in, this method will |
| 61 | return False. True otherwise. |
| 62 | |
| 63 | An InstrumentRangeError exception will be raised if an Instrument is |
| 64 | attached to the Track, but the note turns out not to be within the |
| 65 | range of the Instrument. |
| 66 | """ |
| 67 | if self.instrument != None: |
| 68 | if not self.instrument.can_play_notes(note): |
| 69 | raise InstrumentRangeError( |
| 70 | "Note '%s' is not in range of the instrument (%s)" |
| 71 | % (note, self.instrument) |
| 72 | ) |
| 73 | if duration == None: |
| 74 | duration = 4 |
| 75 | |
| 76 | # Check whether the last bar is full, if so create a new bar and add the |
| 77 | # note there |
| 78 | if len(self.bars) == 0: |
| 79 | self.bars.append(Bar()) |
| 80 | last_bar = self.bars[-1] |
| 81 | if last_bar.is_full(): |
| 82 | self.bars.append(Bar(last_bar.key, last_bar.meter)) |
| 83 | # warning should hold note if it doesn't fit |
| 84 | |
| 85 | return self.bars[-1].place_notes(note, duration) |
| 86 |
no outgoing calls