annotate Syncopation models/synpy/SG.py @ 45:6e9154fc58df

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