Mercurial > hg > syncopation-dataset
comparison Syncopation models/music_objects.py @ 40:6371e8f21f7d
updating the syncopation functions to fix a few problems
author | christopherh <christopher.harte@eecs.qmul.ac.uk> |
---|---|
date | Thu, 23 Apr 2015 15:46:58 +0100 |
parents | cc38b3047ed9 |
children |
comparison
equal
deleted
inserted
replaced
38:cc38b3047ed9 | 40:6371e8f21f7d |
---|---|
9 self.duration = 0 | 9 self.duration = 0 |
10 self.velocity = 0 | 10 self.velocity = 0 |
11 | 11 |
12 if firstarg != None: | 12 if firstarg != None: |
13 if isinstance(firstarg,basestring): | 13 if isinstance(firstarg,basestring): |
14 intlist = string_to_sequence(firstarg) | 14 intlist = string_to_sequence(firstarg,int) |
15 self.startTime = intlist[0] | 15 self.startTime = intlist[0] |
16 self.duration = intlist[1] | 16 self.duration = intlist[1] |
17 self.velocity = intlist[2] | 17 self.velocity = intlist[2] |
18 elif isinstance(firstarg,int): | 18 elif isinstance(firstarg,int): |
19 self.startTime = firstarg | 19 self.startTime = firstarg |
45 def to_string(self): | 45 def to_string(self): |
46 noteSequenceString = "" | 46 noteSequenceString = "" |
47 for note in self: | 47 for note in self: |
48 noteSequenceString += note.to_string() + "," | 48 noteSequenceString += note.to_string() + "," |
49 return noteSequenceString[:-1] | 49 return noteSequenceString[:-1] |
50 | |
51 | |
52 class NormalisedVelocityValueOutOfRange(Exception): | |
53 def __init__(self, value): | |
54 self.value = value | |
55 def __str__(self): | |
56 return repr(self.value) | |
50 | 57 |
51 # VelocitySequence is a list of float numbers | 58 # VelocitySequence is a list of float numbers |
52 class VelocitySequence(list): | 59 class VelocitySequence(list): |
53 def __init__(self, velocitySequence = None): | 60 def __init__(self, velocitySequence = None): |
54 if velocitySequence != None: | 61 if velocitySequence != None: |
56 self.string_to_velocity_sequence(velocitySequence) | 63 self.string_to_velocity_sequence(velocitySequence) |
57 elif isinstance(velocitySequence, list): | 64 elif isinstance(velocitySequence, list): |
58 self+=velocitySequence | 65 self+=velocitySequence |
59 | 66 |
60 def string_to_velocity_sequence(self,inputString): | 67 def string_to_velocity_sequence(self,inputString): |
61 self.extend(string_to_sequence(inputString)) | 68 |
69 def convert_velocity_value(argstring): | |
70 value = float(argstring) | |
71 if value>=0 and value<=1: | |
72 return value | |
73 else: | |
74 raise NormalisedVelocityValueOutOfRange("Value: "+argstring+" in " + inputString) | |
75 | |
76 self.extend(string_to_sequence(inputString,convert_velocity_value)) | |
77 | |
62 | 78 |
63 def to_string(self): | 79 def to_string(self): |
64 return str(velocity_sequence_to_min_timespan(self))[1:-1].replace(" ","") | 80 return str(velocity_sequence_to_min_timespan(self))[1:-1].replace(" ","") |
65 | 81 |
66 | 82 |
94 | 110 |
95 return noteSequence | 111 return noteSequence |
96 | 112 |
97 | 113 |
98 def note_sequence_to_velocity_sequence(noteSequence, timespanTicks = None): | 114 def note_sequence_to_velocity_sequence(noteSequence, timespanTicks = None): |
99 | 115 |
100 velocitySequence = VelocitySequence() | 116 velocitySequence = VelocitySequence() |
101 | 117 |
102 previousNoteStartTime = -1 | 118 previousNoteStartTime = -1 |
103 | 119 |
104 for note in noteSequence: | 120 for note in noteSequence: |