annotate Syncopation models/PRS.py @ 19:9030967a05f8

Refactored parameter_setter, basic_functions. Halfway fixing parameter argument in LHL model.
author csong <csong@eecs.qmul.ac.uk>
date Fri, 03 Apr 2015 22:57:27 +0100
parents 4acddc008048
children b959c2acb927
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@12 6 from basic_functions import repeat, subdivide, ceiling, get_min_timeSpan, get_rhythm_category
csong@1 7
csong@12 8 def get_cost(sequence,nextSequence):
csong@12 9 sequence = get_min_timeSpan(sequence) # converting to the minimum time-span format
csong@1 10
csong@12 11 if sequence[1:] == repeat([0],len(sequence)-1): # null prototype
csong@1 12 cost = 0
csong@12 13 elif sequence == repeat([1],len(sequence)): # filled prototype
csong@1 14 cost = 1
csong@12 15 elif sequence[0] == 1 and sequence[-1] == 0: # run1 prototype
csong@1 16 cost = 2
csong@12 17 elif sequence[0] == 1 and (nextSequence == None or nenextSequencext_seq[0] == 0): # run2 prototype
csong@1 18 cost = 2
csong@12 19 elif sequence[0] == 1 and sequence[-1] == 1 and nextSequence != None and nextSequence[0] == 1: # upbeat prototype
csong@1 20 cost = 3
csong@12 21 elif sequence[0] == 0: # syncopated prototype
csong@1 22 cost = 5
csong@1 23
csong@1 24 return cost
csong@1 25
csong@1 26 # This function calculates the syncopation value (cost) for the sequence with the postbar_seq for a certain level.
csong@12 27 def syncopation_perlevel(subSequences):
csong@1 28 total = 0
csong@12 29 for l in range(len(subSequences)-1):
csong@12 30 total = total + get_cost(subSequences[l], subSequences[l+1])
csong@12 31 normalised = float(total)/(len(subSequences)-1)
csong@1 32
csong@1 33 return normalised
csong@1 34
csong@12 35 #def get_syncopation(seq,subdivision_seq, postbar_seq, rhythm_category):
csong@19 36 def get_syncopation(bar, parameters = None):
csong@12 37 '''
csong@12 38 The get_syncopation function calculates the overall syncopation value for a bar of sequence.
csong@12 39 '''
csong@1 40 syncopation = None
csong@12 41
csong@12 42 binarySequence = bar.get_binary_sequence()
csong@12 43 subdivisionSequence = bar.get_subdivision_sequence()
csong@12 44
csong@12 45 if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly':
csong@12 46 print 'Warning: PRS model detects polyrhythms so returning None.'
csong@0 47 else:
csong@1 48 syncopation = 0
csong@0 49
csong@12 50 if bar.get_next_bar() != None:
csong@12 51 nextbarBinarySequence = bar.get_next_bar().get_binary_sequence()
csong@12 52 else:
csong@12 53 nextbarBinarySequence = None
csong@12 54
csong@12 55 # the initial number of sub-sequences at a certain metrical level
csong@12 56 numberOfSubSeqs = 1
csong@12 57 for subdivisor in subdivisionSequence:
csong@1 58 # the number of sub-sequence at the current level is product of all the subdivisors up to the current level
csong@12 59 numberOfSubSeqs = numberOfSubSeqs * subdivisor
csong@1 60 # recursion stops when the length of sub-sequence is less than 2
csong@12 61 if len(binarySequence)/numberOfSubSeqs >= 2:
csong@1 62 # generate sub-sequences and append the next bar sequence
csong@12 63 subSequences = subdivide(ceiling(binarySequence), numberOfSubSeqs)
csong@12 64 subSequences.append(nextbarBinarySequence)
csong@1 65 # adding syncopation at each metrical level to the total syncopation
csong@12 66 syncopation += syncopation_perlevel(subSequences)
csong@1 67 else:
csong@1 68 break
csong@0 69
csong@1 70 return syncopation