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