Mercurial > hg > syncopation-dataset
comparison Syncopation models/TMC.py @ 28:5de1cb45c145
Parameters setting implemented.
author | csong <csong@eecs.qmul.ac.uk> |
---|---|
date | Sun, 12 Apr 2015 22:34:35 +0100 |
parents | df1e7c378ee0 |
children | f5abd2e8cafe |
comparison
equal
deleted
inserted
replaced
27:ed29ed80635c | 28:5de1cb45c145 |
---|---|
3 Institution: Centre for Digital Music, Queen Mary University of London | 3 Institution: Centre for Digital Music, Queen Mary University of London |
4 | 4 |
5 ''' | 5 ''' |
6 | 6 |
7 from basic_functions import get_H, ceiling, velocity_sequence_to_min_timespan, get_rhythm_category | 7 from basic_functions import get_H, ceiling, velocity_sequence_to_min_timespan, get_rhythm_category |
8 from parameter_setter import are_parameters_valid | |
8 | 9 |
9 # The get_metricity function calculates the metricity for a binary sequence with given sequence of metrical weights in a certain metrical level. | 10 # The get_metricity function calculates the metricity for a binary sequence with given sequence of metrical weights in a certain metrical level. |
10 def get_metricity(binarySequence, H): | 11 def get_metricity(binarySequence, H): |
11 metricity = 0 | 12 metricity = 0 |
12 for m in range(len(binarySequence)): | 13 for m in range(len(binarySequence)): |
53 subdivisionSequence = bar.get_subdivision_sequence() | 54 subdivisionSequence = bar.get_subdivision_sequence() |
54 | 55 |
55 if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly': | 56 if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly': |
56 print 'Warning: TMC model detects polyrhythms so returning None.' | 57 print 'Warning: TMC model detects polyrhythms so returning None.' |
57 else: | 58 else: |
58 binarySequence = velocity_sequence_to_min_timespan(binarySequence) # converting to the minimum time-span format | 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'] | |
59 | 69 |
60 # If the parameters are not given, use the default settings | 70 if not are_parameters_valid(Lmax, weightSequence, subdivisionSequence): |
61 if parameters == None: | 71 print 'Error: the given parameters are not valid.' |
62 Lmax = 5 | |
63 weightSequence = range(Lmax+1,0,-1) # i.e. [6,5,4,3,2,1] | |
64 else: | 72 else: |
65 if are_parameters_valid(parameters): | 73 binarySequence = velocity_sequence_to_min_timespan(binarySequence) # converting to the minimum time-span format |
66 Lmax = parameters['Lmax'] | 74 L = find_L(binarySequence, Lmax, weightSequence, subdivisionSequence) |
67 weightSequence = parameters['W'] | 75 if L != None: |
68 else: | 76 #? generate the metrical weights of the lowest level, |
69 pass | 77 #? using the last matching_level number of elements in the weightSequence, to make sure the last element is 1 |
70 #raise InvalidParameterError | 78 H = get_H (weightSequence[-(L+1):], subdivisionSequence, L) |
79 | |
80 metricity = get_metricity(binarySequence, H) # converting to binary sequence then calculate metricity | |
81 maxMetricity = get_max_metricity(binarySequence, H) | |
71 | 82 |
72 L = find_L(binarySequence, Lmax, weightSequence, subdivisionSequence) | 83 syncopation = maxMetricity - metricity |
73 if L != None: | |
74 #? generate the metrical weights of the lowest level, | |
75 #? using the last matching_level number of elements in the weightSequence, to make sure the last element is 1 | |
76 H = get_H (weightSequence[-(L+1):], subdivisionSequence, L) | |
77 | |
78 metricity = get_metricity(binarySequence, H) # converting to binary sequence then calculate metricity | |
79 maxMetricity = get_max_metricity(binarySequence, H) | |
80 | |
81 syncopation = maxMetricity - metricity | |
82 | 84 |
83 return syncopation | 85 return syncopation |
84 | 86 |