Mercurial > hg > syncopation-dataset
view Syncopation models/rhythm_parser.py @ 23:df1e7c378ee0
fixed KTH, and WNBD
author | csong <csong@eecs.qmul.ac.uk> |
---|---|
date | Sun, 12 Apr 2015 13:06:17 +0100 |
parents | b959c2acb927 |
children | 7a1730bbf15a |
line wrap: on
line source
''' Author: Chunyang Song Institution: Centre for Digital Music, Queen Mary University of London ''' # Parse the rhythm file and return a list of Bar objects Piece = [] from parameter_setter import timeSignatureBase comment_sign = '#' def discardComments(line): if comment_sign in line: line = line[0:line.find(comment_sign)] return line def discardSpaces(line): line = line.replace(" ", '').replace("\t", '') return line def extractInfo(line): try: if '{' not in line and '}' not in line: raise RhythmSyntaxError(line) else: return line[line.find('{')+1 : line.find('}')] except RhythmSyntaxError: print 'Rhythmic information needs to be enclosed by "{" and "}"' current_timesig = '' current_timesigValue = [] def process(line): try: if 't' in line: current_timesig = extractInfo(line) if current_timesig in timesigBase: current_timesigValue = timesigBase[current_timesig] else: raise NoTimesigError(current_timesig) elif 'v' in line: if current_timesig == '': raise InitialTimesigError(line) else: rhythmString = extractInfo(line) rhythm_seq = map(int,rhythmString.split(',')) Piece.append(Bar(rhythm_seq, current_timesigValue[0], current_timesigValue[1])) else: raise SymbolError(line) except InitialTimesigError: print 'The initial time-signature is not given.' except NoTimesigError: print 'The time-signature is not recognised.' except SymbolError: print 'Unrecognised symbol.' def readRhythm(fileName): try: f = file(fileName) # Clean up each line by discarding comments and spaces; extract time-signature or sequence information isfinished = False while(not isfinished): line = f.readline() if len(line) == 0: isfinished = True else: cleanline = discardSpaces(discardComments(line)) process(cleanline) # Extract time-signature and rhythm sequence information except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except: print 'Unexpected error.'#, sys.exc_info()[0] # To check the def isInfoValid(info): # If two parts of information is not seperated by ; # if 't' in info and 'q' in info: # print 'Error: time-signature and rhythm sequence needs ";" to seperate. Please check this rhythm file: %s.' %fileName # Piece = None # break # elif '{' not in info and '}' not in info: # print 'Error: information needs to be enclosed by "{ }". Please check this rhythm file: %s.' %fileName # Piece = None # break # else: return True # To check whether the given time-signature is existed; if not then requires user to add in the new time-signature #def checkTimesig(input): # # TESTING # line = 't{4/4} q{1,0,0,1,0,0,1,0,0,0,1,0,0.8,0,0,0} t{2/4} # This is a comment' # # print discardSpaces(discardComments(line)) # print line[line.find('{')+1:line.find('}')] # #print readRhythm("rhythmbase/testrhythm.txt")