Mercurial > hg > syncopation-dataset
comparison Syncopation models/synpy/TMC.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 |
comparison
equal
deleted
inserted
replaced
44:144460f34b5e | 45:6e9154fc58df |
---|---|
1 ''' | |
2 Author: Chunyang Song | |
3 Institution: Centre for Digital Music, Queen Mary University of London | |
4 | |
5 ''' | |
6 | |
7 from basic_functions import get_H, ceiling, velocity_sequence_to_min_timespan, get_rhythm_category | |
8 from parameter_setter import are_parameters_valid | |
9 | |
10 # The get_metricity function calculates the metricity for a binary sequence with given sequence of metrical weights in a certain metrical level. | |
11 def get_metricity(binarySequence, H): | |
12 metricity = 0 | |
13 for m in range(len(binarySequence)): | |
14 metricity = metricity + binarySequence[m]*H[m] | |
15 return metricity | |
16 | |
17 # The get_max_metricity function calculates the maximum metricity for the same number of notes in a binary sequence. | |
18 def get_max_metricity(binarySequence, H): | |
19 maxMetricity = 0 | |
20 H.sort(reverse=True) # Sort the metrical weight sequence from large to small | |
21 for i in range(sum(binarySequence)): | |
22 maxMetricity = maxMetricity+H[i] | |
23 return maxMetricity | |
24 | |
25 # find the metrical level L that contains the same number of metrical positions as the length of the binary sequence | |
26 # if the given Lmax is not big enough to analyse the given sequence, request a bigger Lmax | |
27 def find_L(rhythmSequence, Lmax, weightSequence, subdivisionSequence): | |
28 L = Lmax | |
29 | |
30 # initially assuming the Lmax is not big enough | |
31 needBiggerLmax = True | |
32 | |
33 # from the lowest metrical level (Lmax) to the highest, find the matching metrical level that | |
34 # has the same length as the length of binary sequence | |
35 while L >= 0: | |
36 if len(get_H(weightSequence,subdivisionSequence, L)) == len(rhythmSequence): | |
37 needBiggerLmax = False | |
38 break | |
39 else: | |
40 L = L - 1 | |
41 | |
42 # if need a bigger Lmax, print error message and return None; otherwise return the matching metrical level L | |
43 if needBiggerLmax: | |
44 print 'Error: needs a bigger L_max (i.e. the lowest metrical level) to match the given rhythm sequence.' | |
45 L = None | |
46 | |
47 return L | |
48 | |
49 # The get_syncopation function calculates the syncopation value of the given sequence for TMC model. | |
50 #def get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category): | |
51 def get_syncopation(bar, parameters = None): | |
52 syncopation = None | |
53 binarySequence = bar.get_binary_sequence() | |
54 subdivisionSequence = bar.get_subdivision_sequence() | |
55 | |
56 if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly': | |
57 print 'Warning: TMC model detects polyrhythms so returning None.' | |
58 else: | |
59 | |
60 # set the defaults | |
61 Lmax = 5 | |
62 weightSequence = range(Lmax+1,0,-1) # i.e. [6,5,4,3,2,1] | |
63 | |
64 if parameters!= None: | |
65 if 'Lmax' in parameters: | |
66 Lmax = parameters['Lmax'] | |
67 if 'W' in parameters: | |
68 weightSequence = parameters['W'] | |
69 | |
70 if not are_parameters_valid(Lmax, weightSequence, subdivisionSequence): | |
71 print 'Error: the given parameters are not valid.' | |
72 else: | |
73 binarySequence = velocity_sequence_to_min_timespan(binarySequence) # converting to the minimum time-span format | |
74 L = find_L(binarySequence, Lmax, weightSequence, subdivisionSequence) | |
75 if L != None: | |
76 #? generate the metrical weights of the lowest level, | |
77 #? using the last matching_level number of elements in the weightSequence, to make sure the last element is 1 | |
78 H = get_H (weightSequence[-(L+1):], subdivisionSequence, L) | |
79 metricity = get_metricity(binarySequence, H) # converting to binary sequence then calculate metricity | |
80 maxMetricity = get_max_metricity(binarySequence, H) | |
81 | |
82 syncopation = maxMetricity - metricity | |
83 | |
84 return syncopation | |
85 |