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