annotate Syncopation models/synpy/LHL.py @ 45:6e9154fc58df

moving the code files to the synpy package directory
author christopherh <christopher.harte@eecs.qmul.ac.uk>
date Thu, 23 Apr 2015 23:52:04 +0100
parents
children e71028851131
rev   line source
christopher@45 1 '''
christopher@45 2 Author: Chunyang Song
christopher@45 3 Institution: Centre for Digital Music, Queen Mary University of London
christopher@45 4 '''
christopher@45 5
christopher@45 6 from basic_functions import concatenate, repeat, subdivide, ceiling, get_rhythm_category
christopher@45 7 from parameter_setter import are_parameters_valid
christopher@45 8
christopher@45 9 terminalNodes = [] # Global variable, storing all the terminal nodes from recursive tree structure in time order
christopher@45 10
christopher@45 11
christopher@45 12
christopher@45 13 # Each terminnal node contains two properties: its node type (note or rest) and its metrical weight.
christopher@45 14 class Node:
christopher@45 15 def __init__(self,nodeType,metricalWeight):
christopher@45 16 self.nodeType = nodeType
christopher@45 17 self.metricalWeight = metricalWeight
christopher@45 18
christopher@45 19 # This function will recurse the tree for a binary sequence and return a sequence containing the terminal nodes in time order.
christopher@45 20 def recursive_tree(binarySequence, subdivisionSequence, weightSequence, metricalWeight, level):
christopher@45 21 # If matching to a Note type, add to terminal nodes
christopher@45 22 if binarySequence == concatenate([1],repeat([0],len(binarySequence)-1)):
christopher@45 23 terminalNodes.append(Node('N',metricalWeight))
christopher@45 24
christopher@45 25 # If matching to a Rest type, add to terminal nodes
christopher@45 26 elif binarySequence == repeat([0],len(binarySequence)):
christopher@45 27 terminalNodes.append(Node('R',metricalWeight))
christopher@45 28
christopher@45 29 # Keep subdividing by the subdivisor of the next level
christopher@45 30 else:
christopher@45 31 subBinarySequences = subdivide(binarySequence, subdivisionSequence[level+1])
christopher@45 32 subWeightSequences = concatenate([metricalWeight],repeat([weightSequence[level+1]],subdivisionSequence[level+1]-1))
christopher@45 33 for a in range(len(subBinarySequences)):
christopher@45 34 recursive_tree(subBinarySequences[a], subdivisionSequence, weightSequence, subWeightSequences[a], level+1)
christopher@45 35
christopher@45 36
christopher@45 37 def get_syncopation(bar, parameters = None):
christopher@45 38 del terminalNodes[:]
christopher@45 39 syncopation = None
christopher@45 40
christopher@45 41 binarySequence = bar.get_binary_sequence()
christopher@45 42 subdivisionSequence = bar.get_subdivision_sequence()
christopher@45 43
christopher@45 44 # LHL can only measure monorhythms
christopher@45 45 if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly':
christopher@45 46 print 'Warning: LHL model detects polyrhythms so returning None.'
christopher@45 47 else:
christopher@45 48 # set defaults
christopher@45 49 Lmax = 5
christopher@45 50 weightSequence = range(0,-Lmax-1,-1)
christopher@45 51 # if parameters are specified by users, check their validities and update parameters if valid
christopher@45 52 if parameters!= None:
christopher@45 53 if 'Lmax' in parameters:
christopher@45 54 Lmax = parameters['Lmax']
christopher@45 55 if 'W' in parameters:
christopher@45 56 weightSequence = parameters['W']
christopher@45 57
christopher@45 58 if not are_parameters_valid(Lmax, weightSequence, subdivisionSequence):
christopher@45 59 print 'Error: the given parameters are not valid.'
christopher@45 60 else:
christopher@45 61 # If there is rhythm in previous bar, process its tree structure
christopher@45 62 prevbar = bar.get_previous_bar()
christopher@45 63 if prevbar != None and not prevbar.is_empty():
christopher@45 64 prebarBinarySequence = prevbar.get_binary_sequence()
christopher@45 65 recursive_tree(ceiling(prebarBinarySequence), subdivisionSequence, weightSequence, weightSequence[0],0)
christopher@45 66
christopher@45 67 if len(terminalNodes)>0:
christopher@45 68 # Only keep the last note-type node
christopher@45 69 while terminalNodes[-1].nodeType != 'N':
christopher@45 70 del terminalNodes[-1]
christopher@45 71 del terminalNodes[0:-1]
christopher@45 72
christopher@45 73 # For the rhythm in the current bar, process its tree structure and store the terminal nodes
christopher@45 74 recursive_tree(ceiling(binarySequence), subdivisionSequence, weightSequence, weightSequence[0],0)
christopher@45 75
christopher@45 76 # for t in terminalNodes:
christopher@45 77 # print '<', t.nodeType, t.metricalWeight, '>'
christopher@45 78
christopher@45 79 # Search for the NR pairs that contribute to syncopation,then add the weight-difference to the NRpairSyncopation list
christopher@45 80 NRpairSyncopation = []
christopher@45 81 for i in range(len(terminalNodes)-1,0,-1):
christopher@45 82 if terminalNodes[i].nodeType == 'R':
christopher@45 83 for j in range(i-1, -1, -1):
christopher@45 84 if (terminalNodes[j].nodeType == 'N') & (terminalNodes[i].metricalWeight >= terminalNodes[j].metricalWeight):
christopher@45 85 NRpairSyncopation.append(terminalNodes[i].metricalWeight - terminalNodes[j].metricalWeight)
christopher@45 86 break
christopher@45 87 #print NRpairSyncopation
christopher@45 88
christopher@45 89 # If there are syncopation, sum all the local syncopation values stored in NRpairSyncopation list
christopher@45 90 if len(NRpairSyncopation) != 0:
christopher@45 91 syncopation = sum(NRpairSyncopation)
christopher@45 92 # If no syncopation, the value is -1;
christopher@45 93 elif len(terminalNodes) != 0:
christopher@45 94 syncopation = -1
christopher@45 95
christopher@45 96 return syncopation
christopher@45 97
christopher@45 98