annotate Syncopation models/synpy/music_objects.py @ 45:6e9154fc58df

moving the code files to the synpy package directory
author christopherh <christopher.harte@eecs.qmul.ac.uk>
date Thu, 23 Apr 2015 23:52:04 +0100
parents
children e71028851131
rev   line source
christopher@45 1
christopher@45 2 from basic_functions import ceiling, string_to_sequence, calculate_bar_ticks, velocity_sequence_to_min_timespan
christopher@45 3 import parameter_setter
christopher@45 4 import rhythm_parser
christopher@45 5
christopher@45 6 class Note():
christopher@45 7 def __init__(self, firstarg = None, duration = None, velocity = None):
christopher@45 8 self.startTime = 0
christopher@45 9 self.duration = 0
christopher@45 10 self.velocity = 0
christopher@45 11
christopher@45 12 if firstarg != None:
christopher@45 13 if isinstance(firstarg,basestring):
christopher@45 14 intlist = string_to_sequence(firstarg,int)
christopher@45 15 self.startTime = intlist[0]
christopher@45 16 self.duration = intlist[1]
christopher@45 17 self.velocity = intlist[2]
christopher@45 18 elif isinstance(firstarg,int):
christopher@45 19 self.startTime = firstarg
christopher@45 20
christopher@45 21 if duration != None:
christopher@45 22 self.duration = duration
christopher@45 23 if velocity != None:
christopher@45 24 self.velocity = velocity
christopher@45 25
christopher@45 26
christopher@45 27 def to_string(self):
christopher@45 28 return "(%d,%d,%f)" %(self.startTime, self.duration, self.velocity)
christopher@45 29
christopher@45 30
christopher@45 31 # NoteSequence is a list of Note
christopher@45 32 class NoteSequence(list):
christopher@45 33 def __init__(self, noteSequenceString = None):
christopher@45 34 if noteSequenceString != None:
christopher@45 35 self.string_to_note_sequence(noteSequenceString)
christopher@45 36
christopher@45 37 def string_to_note_sequence(self, noteSequenceString):
christopher@45 38 noteSequenceString = rhythm_parser.discard_spaces(noteSequenceString)
christopher@45 39 # try:
christopher@45 40 # Turning "(1,2,3),(4,5,6),(7,8,9)" into ["1,2,3","4,5,6,","7,8,9"]
christopher@45 41 listStrings = noteSequenceString[1:-1].split("),(")
christopher@45 42 for localString in listStrings:
christopher@45 43 self.append(Note(localString))
christopher@45 44
christopher@45 45 def to_string(self):
christopher@45 46 noteSequenceString = ""
christopher@45 47 for note in self:
christopher@45 48 noteSequenceString += note.to_string() + ","
christopher@45 49 return noteSequenceString[:-1]
christopher@45 50
christopher@45 51
christopher@45 52 class NormalisedVelocityValueOutOfRange(Exception):
christopher@45 53 def __init__(self, value):
christopher@45 54 self.value = value
christopher@45 55 def __str__(self):
christopher@45 56 return repr(self.value)
christopher@45 57
christopher@45 58 # VelocitySequence is a list of float numbers
christopher@45 59 class VelocitySequence(list):
christopher@45 60 def __init__(self, velocitySequence = None):
christopher@45 61 if velocitySequence != None:
christopher@45 62 if isinstance(velocitySequence,basestring):
christopher@45 63 self.string_to_velocity_sequence(velocitySequence)
christopher@45 64 elif isinstance(velocitySequence, list):
christopher@45 65 self+=velocitySequence
christopher@45 66
christopher@45 67 def string_to_velocity_sequence(self,inputString):
christopher@45 68
christopher@45 69 def convert_velocity_value(argstring):
christopher@45 70 value = float(argstring)
christopher@45 71 if value>=0 and value<=1:
christopher@45 72 return value
christopher@45 73 else:
christopher@45 74 raise NormalisedVelocityValueOutOfRange("Value: "+argstring+" in " + inputString)
christopher@45 75
christopher@45 76 self.extend(string_to_sequence(inputString,convert_velocity_value))
christopher@45 77
christopher@45 78
christopher@45 79 def to_string(self):
christopher@45 80 return str(velocity_sequence_to_min_timespan(self))[1:-1].replace(" ","")
christopher@45 81
christopher@45 82
christopher@45 83 def velocity_sequence_to_note_sequence(velocitySequence, nextbarVelocitySequence = None):
christopher@45 84
christopher@45 85 noteSequence = NoteSequence()
christopher@45 86
christopher@45 87 for index in range(len(velocitySequence)):
christopher@45 88 if (velocitySequence[index]!= 0): # onset detected
christopher@45 89 startTime = index
christopher@45 90 velocity = velocitySequence[index]
christopher@45 91
christopher@45 92 # if there are previous notes added
christopher@45 93 if( len(noteSequence) > 0):
christopher@45 94 previousNote = noteSequence[-1]
christopher@45 95 previousNote.duration = startTime - previousNote.startTime
christopher@45 96
christopher@45 97 # add the current note into note sequence
christopher@45 98 noteSequence.append( Note(startTime, 0, velocity) )
christopher@45 99
christopher@45 100 # to set the duration for the last note
christopher@45 101 if( len(noteSequence) > 0):
christopher@45 102 lastNote = noteSequence[-1]
christopher@45 103
christopher@45 104 if nextbarVelocitySequence == None:
christopher@45 105 lastNote.duration = len(velocitySequence) - lastNote.startTime
christopher@45 106 else:
christopher@45 107 nextNoteStartTime = next((index for index, v in enumerate(nextbarVelocitySequence) if v), None)
christopher@45 108 lastNote.duration = len(velocitySequence) + nextNoteStartTime-lastNote.startTime
christopher@45 109
christopher@45 110
christopher@45 111 return noteSequence
christopher@45 112
christopher@45 113
christopher@45 114 def note_sequence_to_velocity_sequence(noteSequence, timespanTicks = None):
christopher@45 115
christopher@45 116 velocitySequence = VelocitySequence()
christopher@45 117
christopher@45 118 previousNoteStartTime = -1
christopher@45 119
christopher@45 120 for note in noteSequence:
christopher@45 121
christopher@45 122 interOnsetInterval = note.startTime - previousNoteStartTime
christopher@45 123 velocitySequence += [0]*(interOnsetInterval-1)
christopher@45 124 velocitySequence += [note.velocity]
christopher@45 125
christopher@45 126 previousNoteStartTime = note.startTime
christopher@45 127
christopher@45 128 if timespanTicks!=None:
christopher@45 129 velocitySequence += [0]*(timespanTicks - len(velocitySequence))
christopher@45 130 else:
christopher@45 131 velocitySequence += [0]*(noteSequence[-1].duration-1)
christopher@45 132
christopher@45 133 # normalising velocity sequence between 0-1
christopher@45 134 if max(velocitySequence)>0:
christopher@45 135 velocitySequence = VelocitySequence([float(v)/max(velocitySequence) for v in velocitySequence])
christopher@45 136
christopher@45 137 return velocitySequence
christopher@45 138
christopher@45 139
christopher@45 140 class BarList(list):
christopher@45 141 def append(self,bar):
christopher@45 142 if(len(self)>0):
christopher@45 143 bar.set_previous_bar(self[-1])
christopher@45 144 self[-1].set_next_bar(bar)
christopher@45 145 super(BarList, self).append(bar)
christopher@45 146
christopher@45 147 def concat(self, barList):
christopher@45 148 while(len(barList)!=0):
christopher@45 149 localbar = barList[0]
christopher@45 150 self.append(localbar)
christopher@45 151 barList.remove(localbar)
christopher@45 152
christopher@45 153
christopher@45 154 class Bar:
christopher@45 155 def __init__(self, rhythmSequence, timeSignature, ticksPerQuarter=None, qpmTempo=None, nextBar=None, prevBar=None):
christopher@45 156 if isinstance(rhythmSequence, NoteSequence):
christopher@45 157 self.noteSequence = rhythmSequence
christopher@45 158 self.velocitySequence = None
christopher@45 159 elif isinstance(rhythmSequence, VelocitySequence):
christopher@45 160 self.velocitySequence = rhythmSequence
christopher@45 161 self.noteSequence = None
christopher@45 162
christopher@45 163 self.tpq = ticksPerQuarter
christopher@45 164 self.qpm = qpmTempo
christopher@45 165 if isinstance(timeSignature, basestring):
christopher@45 166 self.timeSignature = TimeSignature(timeSignature)
christopher@45 167 else:
christopher@45 168 self.timeSignature = timeSignature
christopher@45 169 self.nextBar = nextBar
christopher@45 170 self.prevBar = prevBar
christopher@45 171
christopher@45 172 def get_note_sequence(self):
christopher@45 173 if self.noteSequence == None:
christopher@45 174 nextbarVelocitySequence = None
christopher@45 175 if self.nextBar != None:
christopher@45 176 nextbarVelocitySequence = self.nextBar.get_velocity_sequence()
christopher@45 177 self.noteSequence = velocity_sequence_to_note_sequence(self.velocitySequence, nextbarVelocitySequence)
christopher@45 178 return self.noteSequence
christopher@45 179
christopher@45 180 def get_velocity_sequence(self):
christopher@45 181 if self.velocitySequence == None:
christopher@45 182 self.velocitySequence = note_sequence_to_velocity_sequence(self.noteSequence, self.get_bar_ticks())
christopher@45 183 return self.velocitySequence
christopher@45 184
christopher@45 185 def get_binary_sequence(self):
christopher@45 186 return ceiling(self.get_velocity_sequence())
christopher@45 187
christopher@45 188 def get_next_bar(self):
christopher@45 189 return self.nextBar
christopher@45 190
christopher@45 191 def get_previous_bar(self):
christopher@45 192 return self.prevBar
christopher@45 193
christopher@45 194 def set_next_bar(self, bar):
christopher@45 195 self.nextBar = bar
christopher@45 196
christopher@45 197 def set_previous_bar(self, bar):
christopher@45 198 self.prevBar = bar
christopher@45 199
christopher@45 200 def get_subdivision_sequence(self):
christopher@45 201 return self.timeSignature.get_subdivision_sequence()
christopher@45 202
christopher@45 203 def get_beat_level(self):
christopher@45 204 return self.timeSignature.get_beat_level()
christopher@45 205
christopher@45 206 def get_time_signature(self):
christopher@45 207 return self.timeSignature
christopher@45 208
christopher@45 209 # return the length of a bar in time units (ticks)
christopher@45 210 def get_bar_ticks(self):
christopher@45 211 return calculate_bar_ticks(self.timeSignature.get_numerator(),self.timeSignature.get_denominator(), self.tpq)
christopher@45 212
christopher@45 213 def is_empty(self):
christopher@45 214 if max(self.get_velocity_sequence())>0:
christopher@45 215 return False
christopher@45 216 else:
christopher@45 217 return True
christopher@45 218
christopher@45 219 def to_string(self, sequenceType=None):
christopher@45 220 output = "t{"+self.timeSignature.to_string()+"}"
christopher@45 221 prev = self.get_previous_bar()
christopher@45 222 if prev!=None:
christopher@45 223 if prev.get_time_signature()==self.get_time_signature():
christopher@45 224 output=""
christopher@45 225
christopher@45 226 if sequenceType==None or sequenceType=="v":
christopher@45 227 output += "v{"+self.get_velocity_sequence().to_string()+"}"
christopher@45 228 else:
christopher@45 229 output += "y{"+self.get_note_sequence().to_string()+"}"
christopher@45 230 return output
christopher@45 231
christopher@45 232
christopher@45 233 class TimeSignature():
christopher@45 234 def __init__(self, inputString):
christopher@45 235 if inputString in parameter_setter.read_time_signature():
christopher@45 236 self.tsString = inputString
christopher@45 237 else:
christopher@45 238 print "Error: undefined time-signature: ", inputString
christopher@45 239 raise NullTimeSignatureError
christopher@45 240
christopher@45 241 def get_subdivision_sequence(self):
christopher@45 242 return parameter_setter.timeSignatureBase[self.tsString][0]
christopher@45 243
christopher@45 244 def get_beat_level(self):
christopher@45 245 return parameter_setter.timeSignatureBase[self.tsString][1]
christopher@45 246
christopher@45 247 def get_numerator(self):
christopher@45 248 return int(self.tsString.split('/')[0])
christopher@45 249
christopher@45 250 def get_denominator(self):
christopher@45 251 return int(self.tsString.split('/')[1])
christopher@45 252
christopher@45 253 def to_string(self):
christopher@45 254 return self.tsString
christopher@45 255
christopher@45 256