comparison Syncopation models/LHL.py @ 20:b959c2acb927

Refactored all models except for KTH, all past testing except for SG.
author csong <csong@eecs.qmul.ac.uk>
date Tue, 07 Apr 2015 19:05:07 +0100
parents 9030967a05f8
children 5de1cb45c145
comparison
equal deleted inserted replaced
19:9030967a05f8 20:b959c2acb927
28 subBinarySequences = subdivide(binarySequence, subdivisionSequence[level+1]) 28 subBinarySequences = subdivide(binarySequence, subdivisionSequence[level+1])
29 subWeightSequences = concatenate([metricalWeight],repeat([weightSequence[level+1]],subdivisionSequence[level+1]-1)) 29 subWeightSequences = concatenate([metricalWeight],repeat([weightSequence[level+1]],subdivisionSequence[level+1]-1))
30 for a in range(len(subBinarySequences)): 30 for a in range(len(subBinarySequences)):
31 recursive_tree(subBinarySequences[a], subdivisionSequence, weightSequence, subWeightSequences[a], level+1) 31 recursive_tree(subBinarySequences[a], subdivisionSequence, weightSequence, subWeightSequences[a], level+1)
32 32
33 #!!!! needs fixing
33 def are_parameters_valid(parameters): 34 def are_parameters_valid(parameters):
34 areValid = False 35 areValid = True
35 if 'Lmax' not in parameters : 36 # if 'Lmax' not in parameters :
36 37
37 def get_syncopation(bar, parameters = None): 38 def get_syncopation(bar, parameters = None):
38 ''' 39 del terminalNodes[:]
39 The get_syncopation function calculates syncopation value .
40 '''
41 #def get_syncopation(seq, subdivision_seq, weight_seq, prebar_seq, rhythm_category):
42 syncopation = None 40 syncopation = None
43 41
44 binarySequence = bar.get_binary_sequence() 42 binarySequence = bar.get_binary_sequence()
45 subdivisionSequence = bar.get_subdivision_sequence() 43 subdivisionSequence = bar.get_subdivision_sequence()
46 44
48 print 'Warning: LHL model detects polyrhythms so returning None.' 46 print 'Warning: LHL model detects polyrhythms so returning None.'
49 else: 47 else:
50 # If the parameters are not given, use the default settings 48 # If the parameters are not given, use the default settings
51 if parameters == None: 49 if parameters == None:
52 Lmax = 5 50 Lmax = 5
53 weightSequence = range(0,-Lmax,-1) 51 weightSequence = range(0,-Lmax,-1) # i.e. [0,-1,-2,-3,-4]
54 else: 52 else:
55 if are_parameters_valid(parameters): 53 if are_parameters_valid(parameters):
56 Lmax = parameters['Lmax'] 54 Lmax = parameters['Lmax']
57 weightSequence = parameters['W'] 55 weightSequence = parameters['W']
58 else: 56 else:
63 if bar.get_previous_bar() != None: 61 if bar.get_previous_bar() != None:
64 prebarBinarySequence = bar.get_previous_bar().get_binary_sequence() 62 prebarBinarySequence = bar.get_previous_bar().get_binary_sequence()
65 recursive_tree(ceiling(prebarBinarySequence), subdivisionSequence, weightSequence, weightSequence[0],0) 63 recursive_tree(ceiling(prebarBinarySequence), subdivisionSequence, weightSequence, weightSequence[0],0)
66 64
67 # Only keep the last note-type node 65 # Only keep the last note-type node
68 while terminalNodes[-1].node_type != 'N': 66 while terminalNodes[-1].nodeType != 'N':
69 del terminalNodes[-1] 67 del terminalNodes[-1]
70 del terminalNodes[0:-1] 68 del terminalNodes[0:-1]
71 69
72 # For the rhythm in the current bar, process its tree structure and store the terminal nodes 70 # For the rhythm in the current bar, process its tree structure and store the terminal nodes
73 recursive_tree(ceiling(binarySequence), subdivisionSequence, weightSequence, weightSequence[0],0) 71 recursive_tree(ceiling(binarySequence), subdivisionSequence, weightSequence, weightSequence[0],0)
74 72
75 # for t in terminalNodes: 73 # for t in terminalNodes:
76 # print '<', t.node_type, t.metrical_weight, '>' 74 # print '<', t.nodeType, t.metricalWeight, '>'
77 75
78 # Search for the NR pairs that contribute to syncopation, 76 # Search for the NR pairs that contribute to syncopation,
79 # then add the weight-difference to the NRpairSyncopation list 77 # then add the weight-difference to the NRpairSyncopation list
80 NRpairSyncopation = [] 78 NRpairSyncopation = []
81 for i in range(len(terminalNodes)-1,0,-1): 79 for i in range(len(terminalNodes)-1,0,-1):
82 if terminalNodes[i].node_type == 'R': 80 if terminalNodes[i].nodeType == 'R':
83 for j in range(i-1, -1, -1): 81 for j in range(i-1, -1, -1):
84 if (terminalNodes[j].node_type == 'N') & (terminalNodes[i].metricalWeight >= terminalNodes[j].metricalWeight): 82 if (terminalNodes[j].nodeType == 'N') & (terminalNodes[i].metricalWeight >= terminalNodes[j].metricalWeight):
85 NRpairSyncopation.append(terminalNodes[i].metricalWeight - terminalNodes[j].metricalWeight) 83 NRpairSyncopation.append(terminalNodes[i].metricalWeight - terminalNodes[j].metricalWeight)
86 break 84 break
87 #print NRpairSyncopation 85 #print NRpairSyncopation
88 86
89 # If there are syncopation, sum all the local syncopation values stored in NRpairSyncopation list 87 # If there are syncopation, sum all the local syncopation values stored in NRpairSyncopation list