csong@0: ''' csong@0: Author: Chunyang Song csong@0: Institution: Centre for Digital Music, Queen Mary University of London csong@1: ''' csong@0: csong@14: from basic_functions import concatenate, repeat, subdivide, ceiling, get_rhythm_category csong@0: csong@14: terminalNodes = [] # Global variable, storing all the terminal nodes from recursive tree structure in time order csong@0: csong@1: # Each terminnal node contains two properties: its node type (note or rest) and its metrical weight. csong@1: class Node: csong@14: def __init__(self,nodeType,metricalWeight): csong@14: self.nodeType = nodeType csong@14: self.metricalWeight = metricalWeight csong@0: csong@1: # This function will recurse the tree for a binary sequence and return a sequence containing the terminal nodes in time order. csong@14: def recursive_tree(binarySequence, subdivisionSequence, weightSequence, metricalWeight, level): csong@14: # If matching to a Note type, add to terminal nodes csong@14: if binarySequence == concatenate([1],repeat([0],len(binarySequence)-1)): csong@14: terminalNodes.append(Node('N',metricalWeight)) csong@0: csong@14: # If matching to a Rest type, add to terminal nodes csong@14: elif binarySequence == repeat([0],len(binarySequence)): csong@14: terminalNodes.append(Node('R',metricalWeight)) csong@0: csong@14: # Keep subdividing by the subdivisor of the next level csong@14: else: csong@14: subBinarySequences = subdivide(binarySequence, subdivisionSequence[level+1]) csong@14: subWeightSequences = concatenate([metricalWeight],repeat([weightSequence[level+1]],subdivisionSequence[level+1]-1)) csong@14: for a in range(len(subBinarySequences)): csong@14: recursive_tree(subBinarySequences[a], subdivisionSequence, weightSequence, subWeightSequences[a], level+1) csong@0: csong@14: def get_syncopation(bar, weightSequence = None, LMax = None): csong@14: ''' csong@14: The get_syncopation function calculates syncopation value . csong@14: ''' csong@14: #def get_syncopation(seq, subdivision_seq, weight_seq, prebar_seq, rhythm_category): csong@1: syncopation = None csong@14: csong@14: binarySequence = bar.get_binary_sequence() csong@14: subdivisionSequence = bar.get_subdivision_sequence() csong@14: csong@14: if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly': csong@14: print 'Warning: LHL model detects polyrhythms so returning None.' csong@1: else: csong@14: if weightSequence == None: csong@14: if LMax == None: csong@14: LMax = 5 csong@14: weightSequence = range(0,-LMax,-1) csong@14: csong@1: # If there is rhythm in previous bar, process its tree structure csong@14: if bar.get_previous_bar() != None: csong@14: prebarBinarySequence = bar.get_previous_bar().get_binary_sequence() csong@14: recursive_tree(ceiling(prebarBinarySequence), subdivisionSequence, weightSequence, weightSequence[0],0) csong@1: csong@1: # Only keep the last note-type node csong@14: while terminalNodes[-1].node_type != 'N': csong@14: del terminalNodes[-1] csong@14: del terminalNodes[0:-1] csong@0: csong@1: # For the rhythm in the current bar, process its tree structure and store the terminal nodes csong@14: recursive_tree(ceiling(binarySequence), subdivisionSequence, weightSequence, weightSequence[0],0) csong@1: csong@14: # for t in terminalNodes: csong@1: # print '<', t.node_type, t.metrical_weight, '>' csong@0: csong@14: # Search for the NR pairs that contribute to syncopation, csong@14: # then add the weight-difference to the NRpairSyncopation list csong@14: NRpairSyncopation = [] csong@14: for i in range(len(terminalNodes)-1,0,-1): csong@14: if terminalNodes[i].node_type == 'R': csong@1: for j in range(i-1, -1, -1): csong@14: if (terminalNodes[j].node_type == 'N') & (terminalNodes[i].metricalWeight >= terminalNodes[j].metricalWeight): csong@14: NRpairSyncopation.append(terminalNodes[i].metricalWeight - terminalNodes[j].metricalWeight) csong@1: break csong@14: #print NRpairSyncopation csong@0: csong@14: # If there are syncopation, sum all the local syncopation values stored in NRpairSyncopation list csong@14: if len(NRpairSyncopation) != 0: csong@14: syncopation = sum(NRpairSyncopation) csong@14: # If no syncopation, the value is -1; csong@14: elif len(terminalNodes) != 0: csong@1: syncopation = -1 csong@0: csong@1: return syncopation csong@14: csong@14: