csong@9: ''' csong@9: Author: Chunyang Song csong@9: Institution: Centre for Digital Music, Queen Mary University of London csong@9: ''' csong@9: csong@28: # Set the parameters: time-signature, subdivision-sequence, strong-beat-level; Lmax; weight-sequence csong@28: # Important condition: Lmax needs to be no less than the length of subdivision-sequence and the length of weight-sequence csong@28: csong@9: csong@9: # {'key': time-signature} : csong@28: # {'value': [subdivision-sequence, theoretical beat-level represented by index in the subdivision-sequence list]} csong@19: timeSignatureBase = { csong@20: '2/2': [[1,2,2,2,2,2],1], csong@20: '3/2': [[1,3,2,2,2,2],1], csong@20: '4/2': [[1,2,2,2,2,2],1], csong@20: '2/4': [[1,2,2,2,2,2],1], csong@20: '3/4': [[1,3,2,2,2,2],1], csong@20: '4/4': [[1,2,2,2,2,2],2], csong@20: '5/4': [[1,5,2,2,2,2],1], csong@20: '7/4': [[1,7,2,2,2,2],1], csong@20: '3/8': [[1,3,2,2,2,2],1], csong@20: '5/8': [[1,5,2,2,2,2],1], csong@20: '6/8': [[1,2,3,2,2,2],1], csong@20: '9/8': [[1,3,3,2,2,2],1], csong@20: '12/8':[[1,2,2,3,2,2],2], csong@9: } csong@9: csong@28: csong@19: def add_time_signature(timeSignature, subdivisionSequence, beatLevel): csong@19: if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel): csong@19: if timeSignature in timesigBase: csong@9: print 'This time-signature is existed already.' csong@9: else: csong@19: timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel] csong@19: write_time_signature() csong@9: csong@19: def update_time_signature(timeSignature, subdivisionSequence, beatLevel): csong@19: if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel): csong@19: if timeSignature in timeSignatureBase: csong@19: print 'Original settings for ', timeSignature, ':',timeSignatureBase[timeSignature] csong@19: timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel] csong@19: print 'Changed into:',timeSignatureBase[timeSignature] csong@19: write_time_signature() csong@9: csong@19: def is_time_signature_valid(timeSignature, subdivisionSequence, beatLevel): csong@9: isValid = False csong@19: if ('/' not in timeSignature) or (not timeSignature.split('/')[0].isdigit()) or (not timeSignature.split('/')[1].isdigit()): csong@9: print 'Error: invalid time-signature. Please indicate in the form of fraction, e.g. 4/4, 6/8 or 3/4.' csong@19: elif subdivisionSequence != [s for s in subdivisionSequence if isinstance(s,int)]: csong@9: print 'Error: invalid subdivision sequence. Please indicate in the form of list of numbers, e.g [1,2,2,2,2].' csong@19: elif beatLevel >= len(subdivisionSequence): csong@9: print 'Error: beat-level exceeds the range of subdivision sequence list.' csong@9: else: csong@9: isValid = True csong@9: return isValid csong@9: csong@19: def write_time_signature(): csong@9: import cPickle as pickle csong@19: timeSigFile = open('TimeSignature.pkl', 'wb') csong@19: pickle.dump(timeSignatureBase, timeSigFile) csong@19: timeSigFile.close() csong@9: csong@19: def read_time_signature(): csong@9: import cPickle as pickle csong@19: timeSigFile = open('TimeSignature.pkl','rb') csong@19: data = pickle.load(timeSigFile) csong@9: return data csong@19: timeSigFile.close() csong@9: csong@19: def print_time_signature_base(): csong@19: data = read_time_signature() csong@19: for timeSignature, settings in data.items(): csong@19: print timeSignature, settings csong@9: csong@9: csong@28: def are_parameters_valid(Lmax, weightSequence, subdivisionSequence): csong@28: csong@28: # is_Lmax_valid() checks: csong@28: # 1. if Lmax is a non-negative integer csong@28: # 2. if Lmax is higher than the length of weightSequence and subdivisionSequence csong@28: def is_Lmax_valid(): csong@28: isValid = False csong@28: if isinstance(Lmax,int) and Lmax > 0: csong@28: if Lmax <= len(subdivisionSequence): csong@28: if Lmax <= len(weightSequence): csong@28: isValid = True csong@28: else: csong@28: print """Error: Lmax exceeds the length of subdivision-sequence. csong@28: Either reduce Lmax, or extend subdivision-sequence through updating time-signature (refer to update_time_signature function). """ csong@28: else: csong@28: print """Error: Lmax exceeds the length of weight-sequence. Either reduce Lmax, or provide a new weight-sequence csong@28: whose length is greater or equal to Lmax.""" csong@28: else: csong@28: print 'Error: Lmax needs to be a positive integer.' csong@28: return isValid csong@28: csong@28: # is_weight_sequence_valid() checks: csong@28: # 1. weightSequence is a list of numbers csong@28: # 2. the length of weightSequence is no less than Lmax csong@28: def is_weight_sequence_valid(): csong@28: isValid = False csong@28: if isinstance(weightSequence,list) and weightSequence == [i for i in weightSequence if isinstance(i,int)]: csong@28: if len(weightSequence) >= Lmax: csong@28: isValid = True csong@28: else: csong@28: print 'Error: the length of weight-sequence needs to be greater or equal to Lmax.' csong@28: else: csong@28: print 'Error: the weight-sequence needs to be a list of integers.' csong@28: return isValid csong@28: csong@28: csong@32: if is_weight_sequence_valid() and is_Lmax_valid(): csong@28: return True csong@28: else: csong@28: return False