comparison Syncopation models/PRS.py @ 20:b959c2acb927

Refactored all models except for KTH, all past testing except for SG.
author csong <csong@eecs.qmul.ac.uk>
date Tue, 07 Apr 2015 19:05:07 +0100
parents 9030967a05f8
children df1e7c378ee0
comparison
equal deleted inserted replaced
19:9030967a05f8 20:b959c2acb927
12 cost = 0 12 cost = 0
13 elif sequence == repeat([1],len(sequence)): # filled prototype 13 elif sequence == repeat([1],len(sequence)): # filled prototype
14 cost = 1 14 cost = 1
15 elif sequence[0] == 1 and sequence[-1] == 0: # run1 prototype 15 elif sequence[0] == 1 and sequence[-1] == 0: # run1 prototype
16 cost = 2 16 cost = 2
17 elif sequence[0] == 1 and (nextSequence == None or nenextSequencext_seq[0] == 0): # run2 prototype 17 elif sequence[0] == 1 and (nextSequence == None or nextSequence[0] == 0): # run2 prototype
18 cost = 2 18 cost = 2
19 elif sequence[0] == 1 and sequence[-1] == 1 and nextSequence != None and nextSequence[0] == 1: # upbeat prototype 19 elif sequence[-1] == 1 and nextSequence != None and nextSequence[0] == 1: # upbeat prototype
20 cost = 3 20 cost = 3
21 elif sequence[0] == 0: # syncopated prototype 21 elif sequence[0] == 0: # syncopated prototype
22 cost = 5 22 cost = 5
23 23
24 return cost 24 return cost
25 25
26 # This function calculates the syncopation value (cost) for the sequence with the postbar_seq for a certain level. 26 # This function calculates the syncopation value (cost) for the sequence with the postbar_seq for a certain level.
27 def syncopation_perlevel(subSequences): 27 def syncopation_perlevel(subSequences):
28 print 'subSequences', subSequences
28 total = 0 29 total = 0
29 for l in range(len(subSequences)-1): 30 for l in range(len(subSequences)-1):
31 print 'cost', get_cost(subSequences[l], subSequences[l+1])
30 total = total + get_cost(subSequences[l], subSequences[l+1]) 32 total = total + get_cost(subSequences[l], subSequences[l+1])
33 print 'total this level', total
31 normalised = float(total)/(len(subSequences)-1) 34 normalised = float(total)/(len(subSequences)-1)
32 35
33 return normalised 36 return normalised
34 37
35 #def get_syncopation(seq,subdivision_seq, postbar_seq, rhythm_category):
36 def get_syncopation(bar, parameters = None): 38 def get_syncopation(bar, parameters = None):
37 '''
38 The get_syncopation function calculates the overall syncopation value for a bar of sequence.
39 '''
40 syncopation = None 39 syncopation = None
41 40
42 binarySequence = bar.get_binary_sequence() 41 binarySequence = bar.get_binary_sequence()
43 subdivisionSequence = bar.get_subdivision_sequence() 42 subdivisionSequence = bar.get_subdivision_sequence()
44 43
44 # PRS does not handle polyrhythms
45 if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly': 45 if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly':
46 print 'Warning: PRS model detects polyrhythms so returning None.' 46 print 'Warning: PRS model detects polyrhythms so returning None.'
47 else: 47 else:
48 syncopation = 0 48 syncopation = 0
49 49
50 # retrieve the binary sequence in the next bar
50 if bar.get_next_bar() != None: 51 if bar.get_next_bar() != None:
51 nextbarBinarySequence = bar.get_next_bar().get_binary_sequence() 52 nextbarBinarySequence = bar.get_next_bar().get_binary_sequence()
52 else: 53 else:
53 nextbarBinarySequence = None 54 nextbarBinarySequence = None
54 55
55 # the initial number of sub-sequences at a certain metrical level 56 # numberOfSubSeqs is the number of sub-sequences at a certain metrical level, initialised to be 1 (at the bar level)
56 numberOfSubSeqs = 1 57 numberOfSubSeqs = 1
57 for subdivisor in subdivisionSequence: 58 for subdivisor in subdivisionSequence:
58 # the number of sub-sequence at the current level is product of all the subdivisors up to the current level 59 # numberOfSubSeqs is product of all the subdivisors up to the current level
59 numberOfSubSeqs = numberOfSubSeqs * subdivisor 60 numberOfSubSeqs = numberOfSubSeqs * subdivisor
61
60 # recursion stops when the length of sub-sequence is less than 2 62 # recursion stops when the length of sub-sequence is less than 2
61 if len(binarySequence)/numberOfSubSeqs >= 2: 63 if len(binarySequence)/numberOfSubSeqs >= 2:
62 # generate sub-sequences and append the next bar sequence 64 # generate sub-sequences and append the next bar sequence
63 subSequences = subdivide(ceiling(binarySequence), numberOfSubSeqs) 65 subSequences = subdivide(ceiling(binarySequence), numberOfSubSeqs)
64 subSequences.append(nextbarBinarySequence) 66 subSequences.append(nextbarBinarySequence)
65 # adding syncopation at each metrical level to the total syncopation 67 # adding syncopation at each metrical level to the total syncopation
68 #print 'per level', syncopation_perlevel(subSequences)
66 syncopation += syncopation_perlevel(subSequences) 69 syncopation += syncopation_perlevel(subSequences)
67 else: 70 else:
68 break 71 break
69 72
70 return syncopation 73 return syncopation