Mercurial > hg > syncopation-dataset
comparison Syncopation models/rhythm_parser.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 |
---|---|
17 return line | 17 return line |
18 | 18 |
19 def discard_spaces(line): | 19 def discard_spaces(line): |
20 line = line.replace(" ", '').replace("\t", '') | 20 line = line.replace(" ", '').replace("\t", '') |
21 return line | 21 return line |
22 | |
23 def discard_linereturns(line): | |
24 line = line.replace("\n","").replace("\r","") | |
25 return line | |
26 | |
22 | 27 |
23 # def extractInfo(line): | 28 # def extractInfo(line): |
24 # try: | 29 # try: |
25 # if '{' not in line and '}' not in line: | 30 # if '{' not in line and '}' not in line: |
26 # raise RhythmSyntaxError(line) | 31 # raise RhythmSyntaxError(line) |
52 if timeSignature==None: | 57 if timeSignature==None: |
53 (field, line) = get_next_field(line) | 58 (field, line) = get_next_field(line) |
54 # if there is a valid field, it should be a time signature | 59 # if there is a valid field, it should be a time signature |
55 if field!=None: | 60 if field!=None: |
56 [fieldname,value] = field | 61 [fieldname,value] = field |
57 if fieldname=="t": | 62 if fieldname.lower()=="t": |
58 timeSignature = TimeSignature(value) | 63 timeSignature = TimeSignature(value) |
59 else: | 64 else: |
60 print 'Error, first field in the file should set the time signature.' | 65 print 'Error, first field in the file should set the time signature.' |
61 | 66 |
62 # parse the line | 67 # parse the line |
74 else: | 79 else: |
75 return False | 80 return False |
76 | 81 |
77 def parse_line(line, timeSignature=None, tempo=None, ticksPerQuarter=None): | 82 def parse_line(line, timeSignature=None, tempo=None, ticksPerQuarter=None): |
78 | 83 |
79 #strip the line of spaces and comments | 84 #strip the line of line returns, spaces and comments |
80 line = discard_spaces(discard_comments(line)).replace("\n","") | 85 line = discard_linereturns(discard_spaces(discard_comments(line))) |
81 | 86 |
82 bars = BarList() | 87 bars = BarList() |
83 | 88 |
84 #work through each field in the line | 89 #work through each field in the line |
85 while len(line)>0: | 90 while len(line)>0: |
114 else: | 119 else: |
115 print 'Unrecognised field type: "' + fieldname + '"' | 120 print 'Unrecognised field type: "' + fieldname + '"' |
116 | 121 |
117 return bars, tempo, timeSignature, ticksPerQuarter | 122 return bars, tempo, timeSignature, ticksPerQuarter |
118 | 123 |
124 class RhythmSyntaxError(Exception): | |
125 def __init__(self, value): | |
126 self.value = value | |
127 def __str__(self): | |
128 return repr(self.value) | |
119 | 129 |
120 def get_next_field(line): | 130 def get_next_field(line): |
121 index = line.find("}") | 131 index = line.find("}") |
122 field = None | 132 field = None |
123 if index>=0: | 133 if index>=0: |
124 fieldtext = line[:index] | 134 fieldtext = line[:index] |
125 line = line[index+1:] | 135 line = line[index+1:] |
126 field = fieldtext.split("{") | 136 field = fieldtext.split("{") |
127 else: | 137 else: |
128 print 'Error, incorrect syntax: "'+line+'"' | 138 print 'Error, incorrect syntax: "'+line+'"' |
129 #raise RhythmSyntaxError(line) | 139 raise RhythmSyntaxError(line) |
130 | 140 |
131 return field,line | 141 return field,line |
132 | 142 |
133 | 143 |
134 | 144 |