csong@0: ''' csong@0: Author: Chunyang Song csong@0: Institution: Centre for Digital Music, Queen Mary University of London csong@0: ''' csong@0: csong@14: from basic_functions import repeat, subdivide, ceiling, get_min_timeSpan, get_rhythm_category csong@1: csong@14: def get_cost(sequence,nextSequence): csong@14: sequence = get_min_timeSpan(sequence) # converting to the minimum time-span format csong@1: csong@14: if sequence[1:] == repeat([0],len(sequence)-1): # null prototype csong@1: cost = 0 csong@14: elif sequence == repeat([1],len(sequence)): # filled prototype csong@1: cost = 1 csong@14: elif sequence[0] == 1 and sequence[-1] == 0: # run1 prototype csong@1: cost = 2 csong@14: elif sequence[0] == 1 and (nextSequence == None or nenextSequencext_seq[0] == 0): # run2 prototype csong@1: cost = 2 csong@14: elif sequence[0] == 1 and sequence[-1] == 1 and nextSequence != None and nextSequence[0] == 1: # upbeat prototype csong@1: cost = 3 csong@14: elif sequence[0] == 0: # syncopated prototype csong@1: cost = 5 csong@1: csong@1: return cost csong@1: csong@1: # This function calculates the syncopation value (cost) for the sequence with the postbar_seq for a certain level. csong@14: def syncopation_perlevel(subSequences): csong@1: total = 0 csong@14: for l in range(len(subSequences)-1): csong@14: total = total + get_cost(subSequences[l], subSequences[l+1]) csong@14: normalised = float(total)/(len(subSequences)-1) csong@1: csong@1: return normalised csong@1: csong@14: #def get_syncopation(seq,subdivision_seq, postbar_seq, rhythm_category): csong@14: def get_syncopation(bar, weightSequence = None, LMax = None): csong@14: ''' csong@14: The get_syncopation function calculates the overall syncopation value for a bar of sequence. csong@14: ''' csong@1: syncopation = None csong@14: csong@14: binarySequence = bar.get_binary_sequence() csong@14: subdivisionSequence = bar.get_subdivision_sequence() csong@14: csong@14: if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly': csong@14: print 'Warning: PRS model detects polyrhythms so returning None.' csong@0: else: csong@1: syncopation = 0 csong@0: csong@14: if bar.get_next_bar() != None: csong@14: nextbarBinarySequence = bar.get_next_bar().get_binary_sequence() csong@14: else: csong@14: nextbarBinarySequence = None csong@14: csong@14: # the initial number of sub-sequences at a certain metrical level csong@14: numberOfSubSeqs = 1 csong@14: for subdivisor in subdivisionSequence: csong@1: # the number of sub-sequence at the current level is product of all the subdivisors up to the current level csong@14: numberOfSubSeqs = numberOfSubSeqs * subdivisor csong@1: # recursion stops when the length of sub-sequence is less than 2 csong@14: if len(binarySequence)/numberOfSubSeqs >= 2: csong@1: # generate sub-sequences and append the next bar sequence csong@14: subSequences = subdivide(ceiling(binarySequence), numberOfSubSeqs) csong@14: subSequences.append(nextbarBinarySequence) csong@1: # adding syncopation at each metrical level to the total syncopation csong@14: syncopation += syncopation_perlevel(subSequences) csong@1: else: csong@1: break csong@0: csong@1: return syncopation