Mercurial > hg > syncopation-dataset
view Syncopation models/RhythmParser.py @ 2:031e2ccb1fb6
Added rhythm parser and parameter setter
author | Chunyang Song <csong@eecs.qmul.ac.uk> |
---|---|
date | Fri, 20 Mar 2015 18:28:08 +0000 |
parents | |
children | 1c99053c55cb |
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 import ParameterSetter class Bar: def __init__(self, rhythm_seq, subdivision_seq, beat_level): self.rhythm_seq = rhythm_seq self.subdivision_seq = subdivision_seq self.beat_level = beat_level def makeBar(self): return [self.rhythm_seq, self.subdivision_seq, self.beat_level] current_timesig = '' Piece = [] def readRhythm(fileName): try: f = file(fileName) bar = f.readline() # While the current line has contents while len(bar) > 0: rhythm_seq = [] info = bar.split(';') if isInfoValid(info): for i in info: if i[0] == 't': current_timesig = i[i.find('{')+1:i.find('}')] elif i[0] == 'q': rhythmString = i[i.find('{')+1:i.find('}')] rhythm_seq = map(int,rhythmString.split(',')) else: print 'Error: %s is not recognisable. Please check this rhythm file: %s.' %(i[0], fileName) #Piece.append(Bar(rhythm_seq, subdivision_seq, beat_level).makeBar()) else: break bar = f.readline() return Piece except: print 'Unexpected error.'#, sys.exc_info()[0] raise 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 print readRhythm("rhythmbase/clave.txt")