csong@0: ''' csong@0: Author: Chunyang Song csong@0: Institution: Centre for Digital Music, Queen Mary University of London csong@0: csong@0: ''' csong@0: csong@20: from basic_functions import get_H, get_min_timeSpan, get_rhythm_category csong@20: from TMC import find_L csong@0: csong@20: #def get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category): csong@20: def get_syncopation(bar, parameters = None): csong@1: syncopation = None csong@20: velocitySequence = bar.get_velocity_sequence() csong@20: subdivisionSequence = bar.get_subdivision_sequence() csong@20: csong@20: if get_rhythm_category(velocitySequence, subdivisionSequence) == 'poly': csong@20: print 'Warning: SG model detects polyrhythms so returning None.' csong@0: else: csong@20: velocitySequence = get_min_timeSpan(velocitySequence) # converting to the minimum time-span format csong@20: csong@20: # If the parameters are not given, use the default settings csong@20: if parameters == None: csong@20: Lmax = 5 csong@20: weightSequence = range(Lmax+1) # i.e. [0,1,2,3,4,5] csong@20: else: csong@20: if are_parameters_valid(parameters): csong@20: Lmax = parameters['Lmax'] csong@20: weightSequence = parameters['W'] csong@1: else: csong@20: pass csong@20: #raise InvalidParameterError csong@0: csong@20: L = find_L(velocitySequence, Lmax, weightSequence, subdivisionSequence) csong@20: print 'L', L csong@20: if L != None: csong@1: syncopation = 0 csong@1: # generate the metrical weights of the lowest level csong@20: H = get_H(weightSequence,subdivisionSequence, L) csong@20: print 'H', H csong@0: csong@20: # 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@20: def ave_dif_neighbours(index, level): csong@20: csong@0: averages = [] csong@20: parameterGarma = 0.8 csong@0: csong@1: # The findPre function is to calculate the index of the previous neighbour at a certain metrical level. csong@20: def find_pre(index, level): csong@20: preIndex = (index - 1)%len(H) # using % is to restrict the index varies within range(0, len(H)) csong@20: while(H[preIndex] > level): csong@20: preIndex = (preIndex - 1)%len(H) csong@20: print 'preIndex', preIndex csong@20: return preIndex csong@0: csong@1: # The findPost function is to calculate the index of the next neighbour at a certain metrical level. csong@20: def find_post(index, level): csong@20: postIndex = (index + 1)%len(H) csong@20: while(H[postIndex] > level): csong@20: postIndex = (postIndex + 1)%len(H) csong@20: print 'postIndex', postIndex csong@20: return postIndex csong@0: csong@1: # The dif function is to calculate a difference level factor between two notes (at note position index1 and index 2) in velocity sequence csong@0: def dif(index1,index2): csong@20: parameterBeta = 0.5 csong@20: dif_v = velocitySequence[index1]-velocitySequence[index2] csong@1: dif_h = abs(H[index1]-H[index2]) csong@20: dif = dif_v*(parameterBeta*dif_h/4+1-parameterBeta) csong@20: print 'dif', dif csong@0: return dif csong@0: csong@1: # 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@1: for l in range(level, max(H)+1): csong@20: ave = (parameterGarma*dif(index,find_pre(index,l))+dif(index,find_post(index,l)) )/(1+parameterGarma) csong@0: averages.append(ave) csong@20: print 'averages', averages csong@0: return averages csong@0: csong@1: # Calculate the syncopation value for each note csong@20: for index in range(len(velocitySequence)): csong@20: if velocitySequence[index] != 0: # Onset detected csong@20: h = H[index] csong@20: # Syncopation potential according to its metrical level, which is equal to the metrical weight csong@20: potential = 1 - pow(0.5,h) csong@20: level = h # Metrical weight is equal to its metrical level csong@20: syncopation += min(ave_dif_neighbours(index, level))*potential csong@1: csong@1: return syncopation