christopher@45: ''' christopher@45: Author: Chunyang Song christopher@45: Institution: Centre for Digital Music, Queen Mary University of London christopher@45: christopher@45: ''' christopher@45: christopher@45: from basic_functions import ceiling, find_divisor, is_prime, velocity_sequence_to_min_timespan christopher@45: christopher@45: def get_syncopation(bar, parameters = None): christopher@45: binarySequence = velocity_sequence_to_min_timespan(bar.get_binary_sequence()) christopher@45: sequenceLength = len(binarySequence) christopher@45: christopher@45: syncopation = 0 christopher@45: christopher@45: # if the length of b_sequence is 1 or a prime number, syncopation is 0; christopher@45: # otherwise the syncopation is calculated by adding up the number of off-beat notes christopher@45: if not ( (sequenceLength == 1) or (is_prime(sequenceLength)) ): christopher@45: # find all the divisors other than 1 and the length of this sequence christopher@45: divisors = find_divisor(sequenceLength) christopher@45: del divisors[0] christopher@45: del divisors[-1] christopher@45: christopher@45: # the on-beat/off-beat positions are the ones that can/cannot be subdivided by the sequenceLength; christopher@45: # the on-beat positions are set to be 0, off-beat positions are set to be 1 christopher@45: offbeatness = [1]*sequenceLength christopher@45: for index in range(sequenceLength): christopher@45: for d in divisors: christopher@45: if index % d == 0: christopher@45: offbeatness[index] = 0 christopher@45: break christopher@45: #print 'offbeatness', offbeatness christopher@45: # syncopation is the sum of the hadamard-product of the rhythm binary-sequence and the off-beatness christopher@45: syncopation += binarySequence[index]*offbeatness[index] christopher@45: christopher@45: return syncopation