csong@0
|
1 '''
|
csong@0
|
2 Author: Chunyang Song
|
csong@0
|
3 Institution: Centre for Digital Music, Queen Mary University of London
|
csong@0
|
4
|
csong@0
|
5 '''
|
csong@0
|
6
|
csong@26
|
7 from basic_functions import get_H, velocity_sequence_to_min_timespan, get_rhythm_category, upsample_velocity_sequence
|
csong@32
|
8 from parameter_setter import are_parameters_valid
|
csong@0
|
9
|
csong@20
|
10 def get_syncopation(bar, parameters = None):
|
csong@1
|
11 syncopation = None
|
csong@20
|
12 velocitySequence = bar.get_velocity_sequence()
|
csong@20
|
13 subdivisionSequence = bar.get_subdivision_sequence()
|
csong@20
|
14
|
csong@20
|
15 if get_rhythm_category(velocitySequence, subdivisionSequence) == 'poly':
|
csong@20
|
16 print 'Warning: SG model detects polyrhythms so returning None.'
|
csong@0
|
17 else:
|
csong@26
|
18 #velocitySequence = velocity_sequence_to_min_timespan(velocitySequence) # converting to the minimum time-span format
|
csong@20
|
19
|
csong@28
|
20 # set the defaults
|
csong@28
|
21 Lmax = 5
|
csong@28
|
22 weightSequence = range(Lmax+1) # i.e. [0,1,2,3,4,5]
|
csong@28
|
23 if parameters!= None:
|
csong@28
|
24 if 'Lmax' in parameters:
|
csong@28
|
25 Lmax = parameters['Lmax']
|
csong@28
|
26 if 'W' in parameters:
|
csong@28
|
27 weightSequence = parameters['W']
|
csong@28
|
28
|
csong@28
|
29 if not are_parameters_valid(Lmax, weightSequence, subdivisionSequence):
|
csong@28
|
30 print 'Error: the given parameters are not valid.'
|
csong@20
|
31 else:
|
csong@28
|
32 # generate the metrical weights of level Lmax, and upsample(stretch) the velocity sequence to match the length of H
|
csong@28
|
33 H = get_H(weightSequence,subdivisionSequence, Lmax)
|
csong@28
|
34 velocitySequence = upsample_velocity_sequence(velocitySequence, len(H))
|
csong@0
|
35
|
csong@28
|
36 # The ave_dif_neighbours function calculates the (weighted) average of the difference between the note at a certain index and its neighbours in a certain metrical level
|
csong@28
|
37 def ave_dif_neighbours(index, level):
|
csong@0
|
38
|
csong@28
|
39 averages = []
|
csong@28
|
40 parameterGarma = 0.8
|
csong@28
|
41
|
csong@28
|
42 # The findPre function is to calculate the index of the previous neighbour at a certain metrical level.
|
csong@28
|
43 def find_pre(index, level):
|
csong@28
|
44 preIndex = (index - 1)%len(H) # using % is to restrict the index varies within range(0, len(H))
|
csong@28
|
45 while(H[preIndex] > level):
|
csong@28
|
46 preIndex = (preIndex - 1)%len(H)
|
csong@28
|
47 #print 'preIndex', preIndex
|
csong@28
|
48 return preIndex
|
csong@20
|
49
|
csong@28
|
50 # The findPost function is to calculate the index of the next neighbour at a certain metrical level.
|
csong@28
|
51 def find_post(index, level):
|
csong@28
|
52 postIndex = (index + 1)%len(H)
|
csong@28
|
53 while(H[postIndex] > level):
|
csong@28
|
54 postIndex = (postIndex + 1)%len(H)
|
csong@28
|
55 #print 'postIndex', postIndex
|
csong@28
|
56 return postIndex
|
csong@28
|
57
|
csong@28
|
58 # The dif function is to calculate a difference level factor between two notes (at note position index1 and index 2) in velocity sequence
|
csong@28
|
59 def dif(index1,index2):
|
csong@28
|
60 parameterBeta = 0.5
|
csong@28
|
61 dif_v = velocitySequence[index1]-velocitySequence[index2]
|
csong@28
|
62 dif_h = abs(H[index1]-H[index2])
|
csong@28
|
63 dif = dif_v*(parameterBeta*dif_h/4+1-parameterBeta)
|
csong@28
|
64 #print 'dif', dif
|
csong@28
|
65 return dif
|
csong@28
|
66
|
csong@28
|
67 # From the highest to the lowest metrical levels where the current note resides, calculate the difference between the note and its neighbours at that level
|
csong@28
|
68 for l in range(level, max(H)+1):
|
csong@28
|
69 ave = (parameterGarma*dif(index,find_pre(index,l))+dif(index,find_post(index,l)) )/(1+parameterGarma)
|
csong@28
|
70 averages.append(ave)
|
csong@28
|
71 #print 'averages', averages
|
csong@28
|
72 return averages
|
csong@28
|
73
|
csong@28
|
74 # if the upsampling was successfully done
|
csong@28
|
75 if velocitySequence != None:
|
csong@28
|
76 syncopation = 0
|
csong@28
|
77 # Calculate the syncopation value for each note
|
csong@28
|
78 for index in range(len(velocitySequence)):
|
csong@28
|
79 if velocitySequence[index] != 0: # Onset detected
|
csong@28
|
80 h = H[index]
|
csong@28
|
81 # Syncopation potential according to its metrical level, which is equal to the metrical weight
|
csong@28
|
82 potential = 1 - pow(0.5,h)
|
csong@28
|
83 level = h # Metrical weight is equal to its metrical level
|
csong@28
|
84 syncopation += min(ave_dif_neighbours(index, level))*potential
|
csong@32
|
85 else:
|
csong@32
|
86 print 'Try giving a bigger Lmax so that the rhythm sequence can be measured by the matching metrical weights sequence (H).'
|
csong@1
|
87 return syncopation
|