csong@9: ''' csong@9: Author: Chunyang Song csong@9: Institution: Centre for Digital Music, Queen Mary University of London csong@9: ''' csong@9: csong@9: # Parse the rhythm file and return a list of Bar objects csong@9: Piece = [] csong@9: csong@9: from ParameterSetter import timesigBase csong@9: csong@9: csong@9: comment_sign = '#' csong@9: def discardComments(line): csong@9: if comment_sign in line: csong@9: line = line[0:line.find(comment_sign)] csong@9: return line csong@9: csong@9: def discardSpaces(line): csong@9: line = line.replace(" ", '').replace("\t", '') csong@9: return line csong@9: csong@9: def extractInfo(line): csong@9: try: csong@9: if '{' not in line and '}' not in line: csong@9: raise RhythmSyntaxError(line) csong@9: else: csong@9: return line[line.find('{')+1 : line.find('}')] csong@9: except RhythmSyntaxError: csong@9: print 'Rhythmic information needs to be enclosed by "{" and "}"' csong@9: csong@9: csong@9: current_timesig = '' csong@9: current_timesigValue = [] csong@9: def process(line): csong@9: try: csong@9: if 't' in line: csong@9: current_timesig = extractInfo(line) csong@9: if current_timesig in timesigBase: csong@9: current_timesigValue = timesigBase[current_timesig] csong@9: else: csong@9: raise NoTimesigError(current_timesig) csong@9: csong@9: elif 'v' in line: csong@9: if current_timesig == '': csong@9: raise InitialTimesigError(line) csong@9: else: csong@9: rhythmString = extractInfo(line) csong@9: rhythm_seq = map(int,rhythmString.split(',')) csong@9: Piece.append(Bar(rhythm_seq, current_timesigValue[0], current_timesigValue[1])) csong@9: csong@9: else: csong@9: raise SymbolError(line) csong@9: csong@9: except InitialTimesigError: csong@9: print 'The initial time-signature is not given.' csong@9: except NoTimesigError: csong@9: print 'The time-signature is not recognised.' csong@9: except SymbolError: csong@9: print 'Unrecognised symbol.' csong@9: csong@9: csong@9: def readRhythm(fileName): csong@9: try: csong@9: f = file(fileName) csong@9: csong@9: # Clean up each line by discarding comments and spaces; extract time-signature or sequence information csong@9: isfinished = False csong@9: while(not isfinished): csong@9: line = f.readline() csong@9: if len(line) == 0: csong@9: isfinished = True csong@9: else: csong@9: cleanline = discardSpaces(discardComments(line)) csong@9: process(cleanline) csong@9: csong@9: # Extract time-signature and rhythm sequence information csong@9: csong@9: except IOError as e: csong@9: print "I/O error({0}): {1}".format(e.errno, e.strerror) csong@9: except: csong@9: print 'Unexpected error.'#, sys.exc_info()[0] csong@9: csong@9: # To check the csong@9: def isInfoValid(info): csong@9: # If two parts of information is not seperated by ; csong@9: # if 't' in info and 'q' in info: csong@9: # print 'Error: time-signature and rhythm sequence needs ";" to seperate. Please check this rhythm file: %s.' %fileName csong@9: # Piece = None csong@9: # break csong@9: # elif '{' not in info and '}' not in info: csong@9: # print 'Error: information needs to be enclosed by "{ }". Please check this rhythm file: %s.' %fileName csong@9: # Piece = None csong@9: # break csong@9: # else: csong@9: return True csong@9: csong@9: # To check whether the given time-signature is existed; if not then requires user to add in the new time-signature csong@9: #def checkTimesig(input): csong@9: csong@9: csong@9: # TESTING csong@9: 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' csong@9: # print discardSpaces(discardComments(line)) csong@9: print line[line.find('{')+1:line.find('}')] csong@9: #print readRhythm("rhythmbase/testrhythm.txt") csong@9: