annotate Syncopation models/SG.py @ 26:d9d22e6f396d

fixed SG!
author csong <csong@eecs.qmul.ac.uk>
date Sun, 12 Apr 2015 15:53:58 +0100
parents df1e7c378ee0
children 5de1cb45c145
rev   line source
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@20 8 from TMC import find_L
csong@0 9
csong@20 10 #def get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category):
csong@20 11 def get_syncopation(bar, parameters = None):
csong@1 12 syncopation = None
csong@20 13 velocitySequence = bar.get_velocity_sequence()
csong@20 14 subdivisionSequence = bar.get_subdivision_sequence()
csong@20 15
csong@20 16 if get_rhythm_category(velocitySequence, subdivisionSequence) == 'poly':
csong@20 17 print 'Warning: SG model detects polyrhythms so returning None.'
csong@0 18 else:
csong@26 19 #velocitySequence = velocity_sequence_to_min_timespan(velocitySequence) # converting to the minimum time-span format
csong@20 20
csong@20 21 # If the parameters are not given, use the default settings
csong@20 22 if parameters == None:
csong@20 23 Lmax = 5
csong@20 24 weightSequence = range(Lmax+1) # i.e. [0,1,2,3,4,5]
csong@20 25 else:
csong@20 26 if are_parameters_valid(parameters):
csong@20 27 Lmax = parameters['Lmax']
csong@20 28 weightSequence = parameters['W']
csong@1 29 else:
csong@20 30 pass
csong@20 31 #raise InvalidParameterError
csong@0 32
csong@26 33 syncopation = 0
csong@26 34 # generate the metrical weights of level Lmax, and upsample(stretch) the velocity sequence to match the length of H
csong@26 35 H = get_H(weightSequence,subdivisionSequence, Lmax)
csong@26 36 velocitySequence = upsample_velocity_sequence(velocitySequence, len(H))
csong@0 37
csong@26 38 # 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@26 39 def ave_dif_neighbours(index, level):
csong@20 40
csong@26 41 averages = []
csong@26 42 parameterGarma = 0.8
csong@26 43
csong@26 44 # The findPre function is to calculate the index of the previous neighbour at a certain metrical level.
csong@26 45 def find_pre(index, level):
csong@26 46 preIndex = (index - 1)%len(H) # using % is to restrict the index varies within range(0, len(H))
csong@26 47 while(H[preIndex] > level):
csong@26 48 preIndex = (preIndex - 1)%len(H)
csong@26 49 #print 'preIndex', preIndex
csong@26 50 return preIndex
csong@0 51
csong@26 52 # The findPost function is to calculate the index of the next neighbour at a certain metrical level.
csong@26 53 def find_post(index, level):
csong@26 54 postIndex = (index + 1)%len(H)
csong@26 55 while(H[postIndex] > level):
csong@26 56 postIndex = (postIndex + 1)%len(H)
csong@26 57 #print 'postIndex', postIndex
csong@26 58 return postIndex
csong@26 59
csong@26 60 # The dif function is to calculate a difference level factor between two notes (at note position index1 and index 2) in velocity sequence
csong@26 61 def dif(index1,index2):
csong@26 62 parameterBeta = 0.5
csong@26 63 dif_v = velocitySequence[index1]-velocitySequence[index2]
csong@26 64 dif_h = abs(H[index1]-H[index2])
csong@26 65 dif = dif_v*(parameterBeta*dif_h/4+1-parameterBeta)
csong@26 66 #print 'dif', dif
csong@26 67 return dif
csong@0 68
csong@26 69 # 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@26 70 for l in range(level, max(H)+1):
csong@26 71 ave = (parameterGarma*dif(index,find_pre(index,l))+dif(index,find_post(index,l)) )/(1+parameterGarma)
csong@26 72 averages.append(ave)
csong@26 73 #print 'averages', averages
csong@26 74 return averages
csong@0 75
csong@26 76 # Calculate the syncopation value for each note
csong@26 77 for index in range(len(velocitySequence)):
csong@26 78 if velocitySequence[index] != 0: # Onset detected
csong@26 79 h = H[index]
csong@26 80 # Syncopation potential according to its metrical level, which is equal to the metrical weight
csong@26 81 potential = 1 - pow(0.5,h)
csong@26 82 level = h # Metrical weight is equal to its metrical level
csong@26 83 syncopation += min(ave_dif_neighbours(index, level))*potential
csong@26 84
csong@1 85 return syncopation