Mercurial > hg > syncopation-dataset
comparison Syncopation models/music_objects.py @ 23:df1e7c378ee0
fixed KTH, and WNBD
author | csong <csong@eecs.qmul.ac.uk> |
---|---|
date | Sun, 12 Apr 2015 13:06:17 +0100 |
parents | 2dbc09ca8013 |
children | 08c298f47917 |
comparison
equal
deleted
inserted
replaced
22:2dbc09ca8013 | 23:df1e7c378ee0 |
---|---|
59 | 59 |
60 def to_string(self): | 60 def to_string(self): |
61 return str(self)[1:-1].replace(" ","") | 61 return str(self)[1:-1].replace(" ","") |
62 | 62 |
63 | 63 |
64 def velocity_sequence_to_note_sequence(velocitySequence): | 64 def velocity_sequence_to_note_sequence(velocitySequence, nextbarVelocitySequence = None): |
65 | 65 |
66 noteSequence = NoteSequence() | 66 noteSequence = NoteSequence() |
67 | 67 |
68 for index in range(len(velocitySequence)): | 68 for index in range(len(velocitySequence)): |
69 if (velocitySequence[index]!= 0): # onset detected | 69 if (velocitySequence[index]!= 0): # onset detected |
78 # add the current note into note sequence | 78 # add the current note into note sequence |
79 noteSequence.append( Note(startTime, 0, velocity) ) | 79 noteSequence.append( Note(startTime, 0, velocity) ) |
80 | 80 |
81 # to set the duration for the last note | 81 # to set the duration for the last note |
82 if( len(noteSequence) > 0): | 82 if( len(noteSequence) > 0): |
83 previousNote = noteSequence[-1] | 83 lastNote = noteSequence[-1] |
84 previousNote.duration = len(velocitySequence) - previousNote.startTime | 84 |
85 if nextbarVelocitySequence == None: | |
86 lastNote.duration = len(velocitySequence) - lastNote.startTime | |
87 else: | |
88 nextNoteStartTime = next((index for index, v in enumerate(nextbarVelocitySequence) if v), None) | |
89 lastNote.duration = len(velocitySequence) + nextNoteStartTime-lastNote.startTime | |
90 | |
85 | 91 |
86 return noteSequence | 92 return noteSequence |
87 | 93 |
88 | 94 |
89 def note_sequence_to_velocity_sequence(noteSequence, timespanTicks = None): | 95 def note_sequence_to_velocity_sequence(noteSequence, timespanTicks = None): |
118 self[-1].set_next_bar(bar) | 124 self[-1].set_next_bar(bar) |
119 super(BarList, self).append(bar) | 125 super(BarList, self).append(bar) |
120 | 126 |
121 | 127 |
122 class Bar: | 128 class Bar: |
123 | |
124 def __init__(self, rhythmSequence, timeSignature, ticksPerQuarter=None, qpmTempo=None, nextBar=None, prevBar=None): | 129 def __init__(self, rhythmSequence, timeSignature, ticksPerQuarter=None, qpmTempo=None, nextBar=None, prevBar=None): |
125 if isinstance(rhythmSequence, NoteSequence): | 130 if isinstance(rhythmSequence, NoteSequence): |
126 self.noteSequence = rhythmSequence | 131 self.noteSequence = rhythmSequence |
127 self.velocitySequence = None | 132 self.velocitySequence = None |
128 elif isinstance(rhythmSequence, VelocitySequence): | 133 elif isinstance(rhythmSequence, VelocitySequence): |
135 self.nextBar = nextBar | 140 self.nextBar = nextBar |
136 self.prevBar = prevBar | 141 self.prevBar = prevBar |
137 | 142 |
138 def get_note_sequence(self): | 143 def get_note_sequence(self): |
139 if self.noteSequence == None: | 144 if self.noteSequence == None: |
140 self.noteSequence = velocitySequence_to_noteSequence(self.velocitySequence) | 145 nextbarVelocitySequence = None |
146 if self.nextBar != None: | |
147 nextbarVelocitySequence = self.nextBar.get_velocity_sequence() | |
148 self.noteSequence = velocity_sequence_to_note_sequence(self.velocitySequence, nextbarVelocitySequence) | |
141 return self.noteSequence | 149 return self.noteSequence |
142 | 150 |
143 def get_velocity_sequence(self): | 151 def get_velocity_sequence(self): |
144 if self.velocitySequence == None: | 152 if self.velocitySequence == None: |
145 self.velocitySequence = noteSequence_to_velocitySequence(self.noteSequence) | 153 self.velocitySequence = note_sequence_to_velocity_sequence(self.noteSequence) |
146 return self.velocitySequence | 154 return self.velocitySequence |
147 | 155 |
148 def get_binary_sequence(self): | 156 def get_binary_sequence(self): |
149 return ceiling(self.get_velocity_sequence()) | 157 return ceiling(self.get_velocity_sequence()) |
150 | 158 |
169 def get_time_signature(self): | 177 def get_time_signature(self): |
170 return self.timeSignature | 178 return self.timeSignature |
171 | 179 |
172 # return the length of a bar in time units (ticks) | 180 # return the length of a bar in time units (ticks) |
173 def get_bar_ticks(self): | 181 def get_bar_ticks(self): |
174 return calculate_bar_ticks(self.timeSignature.get_numerator(),self.timeSignature.get_denominator(), self.ticksPerQuarter) | 182 return calculate_bar_ticks(self.timeSignature.get_numerator(),self.timeSignature.get_denominator(), self.tpq) |
175 | 183 |
176 | 184 |
177 class TimeSignature(): | 185 class TimeSignature(): |
178 def __init__(self, inputString): | 186 def __init__(self, inputString): |
179 if inputString in parameter_setter.read_time_signature(): | 187 if inputString in parameter_setter.read_time_signature(): |