Mercurial > hg > syncopation-dataset
diff Syncopation models/LHL.py @ 28:5de1cb45c145
Parameters setting implemented.
author | csong <csong@eecs.qmul.ac.uk> |
---|---|
date | Sun, 12 Apr 2015 22:34:35 +0100 |
parents | b959c2acb927 |
children | f5abd2e8cafe |
line wrap: on
line diff
--- a/Syncopation models/LHL.py Sun Apr 12 15:55:12 2015 +0100 +++ b/Syncopation models/LHL.py Sun Apr 12 22:34:35 2015 +0100 @@ -4,6 +4,7 @@ ''' from basic_functions import concatenate, repeat, subdivide, ceiling, get_rhythm_category +from parameter_setter import are_parameters_valid terminalNodes = [] # Global variable, storing all the terminal nodes from recursive tree structure in time order @@ -30,10 +31,6 @@ for a in range(len(subBinarySequences)): recursive_tree(subBinarySequences[a], subdivisionSequence, weightSequence, subWeightSequences[a], level+1) -#!!!! needs fixing -def are_parameters_valid(parameters): - areValid = True -# if 'Lmax' not in parameters : def get_syncopation(bar, parameters = None): del terminalNodes[:] @@ -42,54 +39,55 @@ binarySequence = bar.get_binary_sequence() subdivisionSequence = bar.get_subdivision_sequence() + # LHL can only measure monorhythms if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly': print 'Warning: LHL model detects polyrhythms so returning None.' else: - # If the parameters are not given, use the default settings - if parameters == None: - Lmax = 5 - weightSequence = range(0,-Lmax,-1) # i.e. [0,-1,-2,-3,-4] + # set defaults + Lmax = 5 + weightSequence = range(0,-Lmax,-1) + # if parameters are specified by users, check their validities and update parameters if valid + if parameters!= None: + if 'Lmax' in parameters: + Lmax = parameters['Lmax'] + if 'W' in parameters: + weightSequence = parameters['W'] + + if not are_parameters_valid(Lmax, weightSequence, subdivisionSequence): + print 'Error: the given parameters are not valid.' else: - if are_parameters_valid(parameters): - Lmax = parameters['Lmax'] - weightSequence = parameters['W'] - else: - pass - #raise InvalidParameterError + # If there is rhythm in previous bar, process its tree structure + if bar.get_previous_bar() != None: + prebarBinarySequence = bar.get_previous_bar().get_binary_sequence() + recursive_tree(ceiling(prebarBinarySequence), subdivisionSequence, weightSequence, weightSequence[0],0) + + # Only keep the last note-type node + while terminalNodes[-1].nodeType != 'N': + del terminalNodes[-1] + del terminalNodes[0:-1] - # If there is rhythm in previous bar, process its tree structure - if bar.get_previous_bar() != None: - prebarBinarySequence = bar.get_previous_bar().get_binary_sequence() - recursive_tree(ceiling(prebarBinarySequence), subdivisionSequence, weightSequence, weightSequence[0],0) + # For the rhythm in the current bar, process its tree structure and store the terminal nodes + recursive_tree(ceiling(binarySequence), subdivisionSequence, weightSequence, weightSequence[0],0) - # Only keep the last note-type node - while terminalNodes[-1].nodeType != 'N': - del terminalNodes[-1] - del terminalNodes[0:-1] + # for t in terminalNodes: + # print '<', t.nodeType, t.metricalWeight, '>' - # For the rhythm in the current bar, process its tree structure and store the terminal nodes - recursive_tree(ceiling(binarySequence), subdivisionSequence, weightSequence, weightSequence[0],0) - - # for t in terminalNodes: - # print '<', t.nodeType, t.metricalWeight, '>' + # Search for the NR pairs that contribute to syncopation,then add the weight-difference to the NRpairSyncopation list + NRpairSyncopation = [] + for i in range(len(terminalNodes)-1,0,-1): + if terminalNodes[i].nodeType == 'R': + for j in range(i-1, -1, -1): + if (terminalNodes[j].nodeType == 'N') & (terminalNodes[i].metricalWeight >= terminalNodes[j].metricalWeight): + NRpairSyncopation.append(terminalNodes[i].metricalWeight - terminalNodes[j].metricalWeight) + break + #print NRpairSyncopation - # Search for the NR pairs that contribute to syncopation, - # then add the weight-difference to the NRpairSyncopation list - NRpairSyncopation = [] - for i in range(len(terminalNodes)-1,0,-1): - if terminalNodes[i].nodeType == 'R': - for j in range(i-1, -1, -1): - if (terminalNodes[j].nodeType == 'N') & (terminalNodes[i].metricalWeight >= terminalNodes[j].metricalWeight): - NRpairSyncopation.append(terminalNodes[i].metricalWeight - terminalNodes[j].metricalWeight) - break - #print NRpairSyncopation - - # If there are syncopation, sum all the local syncopation values stored in NRpairSyncopation list - if len(NRpairSyncopation) != 0: - syncopation = sum(NRpairSyncopation) - # If no syncopation, the value is -1; - elif len(terminalNodes) != 0: - syncopation = -1 + # If there are syncopation, sum all the local syncopation values stored in NRpairSyncopation list + if len(NRpairSyncopation) != 0: + syncopation = sum(NRpairSyncopation) + # If no syncopation, the value is -1; + elif len(terminalNodes) != 0: + syncopation = -1 return syncopation