Mercurial > hg > syncopation-dataset
comparison Syncopation models/TMC.py @ 0:76ce27beba95
Have uploaded the syncopation dataset and audio wavefies.
author | Chunyang Song <csong@eecs.qmul.ac.uk> |
---|---|
date | Fri, 21 Mar 2014 15:49:46 +0000 |
parents | |
children | b2da092dc2e0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:76ce27beba95 |
---|---|
1 ''' | |
2 Author: Chunyang Song | |
3 Institution: Centre for Digital Music, Queen Mary University of London | |
4 | |
5 ** Toussaint's Metric Complexity Model ** | |
6 | |
7 Algorithm: | |
8 | |
9 Only applicable to monorhythms. | |
10 | |
11 Define metrical hierarchy by given time signature; | |
12 Calculate how many onsets and determine Maximum Metricality Max_Metric; | |
13 Calculate the Metrical Simplicity - the weights of all onsets; | |
14 Syncopation = Max_Metric - Metric_simplicity. | |
15 Output the predicted syncopation score; -1 indicates non-applicable | |
16 | |
17 ''' | |
18 | |
19 from MeterStructure import MeterStructure | |
20 | |
21 def metricalModel(rhythm, time_sig, category, bar): | |
22 ms = MeterStructure(time_sig) | |
23 meter = ms.getLJWeights(bar) | |
24 if len(meter) !=0: | |
25 | |
26 metricalSimplicity = 0 # sum of weights of onsets per bar | |
27 maxMetrical = 0 # maximum metricity per bar | |
28 onsetCount = 0 # The number of onsets per bar | |
29 | |
30 if 'poly' in category: # not applicable to polyrhythms | |
31 return -1 | |
32 | |
33 # Calculate metricalSimplicity | |
34 else: | |
35 l = len(rhythm) | |
36 for i in range(l): | |
37 if rhythm[i] == 1: # onset detected | |
38 pos = int((float(i)/l) *len(meter)) # looking for the metrical position where this note locates | |
39 metricalSimplicity = metricalSimplicity+meter[pos] | |
40 onsetCount = onsetCount+1 | |
41 | |
42 # Calculate max metricity | |
43 meter.sort(reverse=True) | |
44 for i in range(0,onsetCount): | |
45 maxMetrical = maxMetrical+meter[i] | |
46 | |
47 #print 'test', onsetCount, maxMetrical, metricalSimplicity | |
48 syncopation = (maxMetrical - metricalSimplicity) | |
49 | |
50 return syncopation | |
51 else: | |
52 return -1 | |
53 | |
54 # Retrieve the stimuli | |
55 f = file('stimuli.txt') | |
56 #f = file('stimuli_34only.txt') | |
57 | |
58 #Calculate syncopation for each rhythm pattern | |
59 while True: | |
60 line = f.readline().split(';') | |
61 if len(line) == 1: | |
62 break | |
63 else: | |
64 sti_name = line[0] | |
65 rhythmString = line[1].split() | |
66 time_sig = line[2] | |
67 category = line[3] | |
68 bar = int(line[4]) | |
69 | |
70 rhythm = map(int,rhythmString[0].split(',')) | |
71 | |
72 print sti_name, metricalModel(rhythm, time_sig, category, bar) |