A container for notes. The NoteContainer provides a container for the mingus.containers.Note objects. It can be used to store single and multiple notes and is required for working with Bars.
| 25 | |
| 26 | |
| 27 | class NoteContainer(object): |
| 28 | |
| 29 | """A container for notes. |
| 30 | |
| 31 | The NoteContainer provides a container for the mingus.containers.Note |
| 32 | objects. |
| 33 | |
| 34 | It can be used to store single and multiple notes and is required for |
| 35 | working with Bars. |
| 36 | """ |
| 37 | |
| 38 | notes = [] |
| 39 | |
| 40 | def __init__(self, notes=None): |
| 41 | if notes is None: |
| 42 | notes = [] |
| 43 | self.empty() |
| 44 | self.add_notes(notes) |
| 45 | |
| 46 | def empty(self): |
| 47 | """Empty the container.""" |
| 48 | self.notes = [] |
| 49 | |
| 50 | def add_note(self, note, octave=None, dynamics=None): |
| 51 | """Add a note to the container and sorts the notes from low to high. |
| 52 | |
| 53 | The note can either be a string, in which case you could also use |
| 54 | the octave and dynamics arguments, or a Note object. |
| 55 | """ |
| 56 | if dynamics is None: |
| 57 | dynamics = {} |
| 58 | if isinstance(note, six.string_types): |
| 59 | if octave is not None: |
| 60 | note = Note(note, octave, dynamics) |
| 61 | elif len(self.notes) == 0: |
| 62 | note = Note(note, 4, dynamics) |
| 63 | else: |
| 64 | if Note(note, self.notes[-1].octave) < self.notes[-1]: |
| 65 | note = Note(note, self.notes[-1].octave + 1, dynamics) |
| 66 | else: |
| 67 | note = Note(note, self.notes[-1].octave, dynamics) |
| 68 | if not hasattr(note, "name"): |
| 69 | raise UnexpectedObjectError( |
| 70 | "Object '%s' was not expected. " |
| 71 | "Expecting a mingus.containers.Note object." % note |
| 72 | ) |
| 73 | if note not in self.notes: |
| 74 | self.notes.append(note) |
| 75 | self.notes.sort() |
| 76 | return self.notes |
| 77 | |
| 78 | def add_notes(self, notes): |
| 79 | """Feed notes to self.add_note. |
| 80 | |
| 81 | The notes can either be an other NoteContainer, a list of Note |
| 82 | objects or strings or a list of lists formatted like this: |
| 83 | >>> notes = [['C', 5], ['E', 5], ['G', 6]] |
| 84 |
no outgoing calls