Mercurial > hg > syncopation-dataset
comparison Syncopation models/music_objects.py @ 29:7a1730bbf15a
updating files with new text reading code
author | christopherh <christopher.harte@eecs.qmul.ac.uk> |
---|---|
date | Sun, 12 Apr 2015 22:37:56 +0100 |
parents | 08c298f47917 |
children | d9ac6e0d1daf |
comparison
equal
deleted
inserted
replaced
28:5de1cb45c145 | 29:7a1730bbf15a |
---|---|
1 | 1 |
2 from basic_functions import ceiling, string_to_sequence, calculate_bar_ticks | 2 from basic_functions import ceiling, string_to_sequence, calculate_bar_ticks, velocity_sequence_to_min_timespan |
3 import parameter_setter | 3 import parameter_setter |
4 import rhythm_parser | 4 import rhythm_parser |
5 | 5 |
6 class Note(): | 6 class Note(): |
7 def __init__(self, firstarg = None, duration = None, velocity = None): | 7 def __init__(self, firstarg = None, duration = None, velocity = None): |
33 def __init__(self, noteSequenceString = None): | 33 def __init__(self, noteSequenceString = None): |
34 if noteSequenceString != None: | 34 if noteSequenceString != None: |
35 self.string_to_note_sequence(noteSequenceString) | 35 self.string_to_note_sequence(noteSequenceString) |
36 | 36 |
37 def string_to_note_sequence(self, noteSequenceString): | 37 def string_to_note_sequence(self, noteSequenceString): |
38 noteSequenceString = rhythm_parser.discardSpaces(noteSequenceString) | 38 noteSequenceString = rhythm_parser.discard_spaces(noteSequenceString) |
39 # try: | 39 # try: |
40 # Turning "(1,2,3),(4,5,6),(7,8,9)" into ["1,2,3","4,5,6,","7,8,9"] | 40 # Turning "(1,2,3),(4,5,6),(7,8,9)" into ["1,2,3","4,5,6,","7,8,9"] |
41 listStrings = noteSequenceString[1:-1].split("),(") | 41 listStrings = noteSequenceString[1:-1].split("),(") |
42 for localString in listStrings: | 42 for localString in listStrings: |
43 self.append(Note(localString)) | 43 self.append(Note(localString)) |
48 noteSequenceString += note.to_string() + "," | 48 noteSequenceString += note.to_string() + "," |
49 return noteSequenceString[:-1] | 49 return noteSequenceString[:-1] |
50 | 50 |
51 # VelocitySequence is a list of float numbers | 51 # VelocitySequence is a list of float numbers |
52 class VelocitySequence(list): | 52 class VelocitySequence(list): |
53 def __init__(self, velocitySequenceString = None): | 53 def __init__(self, velocitySequence = None): |
54 if velocitySequenceString != None: | 54 if velocitySequence != None: |
55 self.string_to_velocity_sequence(velocitySequenceString) | 55 if isinstance(velocitySequence,basestring): |
56 self.string_to_velocity_sequence(velocitySequence) | |
57 elif isinstance(velocitySequence, list): | |
58 self+=velocitySequence | |
56 | 59 |
57 def string_to_velocity_sequence(self,inputString): | 60 def string_to_velocity_sequence(self,inputString): |
58 self.extend(string_to_sequence(inputString)) | 61 self.extend(string_to_sequence(inputString)) |
59 | 62 |
60 def to_string(self): | 63 def to_string(self): |
61 return str(self)[1:-1].replace(" ","") | 64 return str(velocity_sequence_to_min_timespan(self))[1:-1].replace(" ","") |
62 | 65 |
63 | 66 |
64 def velocity_sequence_to_note_sequence(velocitySequence, nextbarVelocitySequence = None): | 67 def velocity_sequence_to_note_sequence(velocitySequence, nextbarVelocitySequence = None): |
65 | 68 |
66 noteSequence = NoteSequence() | 69 noteSequence = NoteSequence() |
110 velocitySequence += [0]*(timespanTicks - len(velocitySequence)) | 113 velocitySequence += [0]*(timespanTicks - len(velocitySequence)) |
111 else: | 114 else: |
112 velocitySequence += [0]*(noteSequence[-1].duration-1) | 115 velocitySequence += [0]*(noteSequence[-1].duration-1) |
113 | 116 |
114 # normalising velocity sequence between 0-1 | 117 # normalising velocity sequence between 0-1 |
115 velocitySequence = [float(v)/max(velocitySequence) for v in velocitySequence] | 118 velocitySequence = VelocitySequence([float(v)/max(velocitySequence) for v in velocitySequence]) |
116 | 119 |
117 return velocitySequence | 120 return velocitySequence |
118 | 121 |
119 | 122 |
120 class BarList(list): | 123 class BarList(list): |
121 def append(self,bar): | 124 def append(self,bar): |
122 if(len(self)>0): | 125 if(len(self)>0): |
123 bar.set_previous_bar(self[-1]) | 126 bar.set_previous_bar(self[-1]) |
124 self[-1].set_next_bar(bar) | 127 self[-1].set_next_bar(bar) |
125 super(BarList, self).append(bar) | 128 super(BarList, self).append(bar) |
129 | |
130 def concat(self, barList): | |
131 while(len(barList)!=0): | |
132 localbar = barList[0] | |
133 self.append(localbar) | |
134 barList.remove(localbar) | |
126 | 135 |
127 | 136 |
128 class Bar: | 137 class Bar: |
129 def __init__(self, rhythmSequence, timeSignature, ticksPerQuarter=None, qpmTempo=None, nextBar=None, prevBar=None): | 138 def __init__(self, rhythmSequence, timeSignature, ticksPerQuarter=None, qpmTempo=None, nextBar=None, prevBar=None): |
130 if isinstance(rhythmSequence, NoteSequence): | 139 if isinstance(rhythmSequence, NoteSequence): |
179 | 188 |
180 # return the length of a bar in time units (ticks) | 189 # return the length of a bar in time units (ticks) |
181 def get_bar_ticks(self): | 190 def get_bar_ticks(self): |
182 return calculate_bar_ticks(self.timeSignature.get_numerator(),self.timeSignature.get_denominator(), self.tpq) | 191 return calculate_bar_ticks(self.timeSignature.get_numerator(),self.timeSignature.get_denominator(), self.tpq) |
183 | 192 |
193 def to_string(self, sequenceType=None): | |
194 if sequenceType==None or sequenceType=="v": | |
195 output = "v{"+self.get_velocity_sequence().to_string()+"}" | |
196 else: | |
197 output = "y{"+self.get_note_sequence().to_string()+"}" | |
198 return output | |
199 | |
184 | 200 |
185 class TimeSignature(): | 201 class TimeSignature(): |
186 def __init__(self, inputString): | 202 def __init__(self, inputString): |
187 if inputString in parameter_setter.read_time_signature(): | 203 if inputString in parameter_setter.read_time_signature(): |
188 self.tsString = inputString | 204 self.tsString = inputString |