comparison Syncopation models/syncopation.py @ 40:6371e8f21f7d

updating the syncopation functions to fix a few problems
author christopherh <christopher.harte@eecs.qmul.ac.uk>
date Thu, 23 Apr 2015 15:46:58 +0100
parents cc38b3047ed9
children
comparison
equal deleted inserted replaced
38:cc38b3047ed9 40:6371e8f21f7d
8 8
9 9
10 def sync_perbar_permodel (model, bar, parameters=None): 10 def sync_perbar_permodel (model, bar, parameters=None):
11 return model.get_syncopation(bar, parameters) 11 return model.get_syncopation(bar, parameters)
12 12
13 def syncopation_barlist_permodel(model, source, parameters=None): 13 def calculate_syncopation(model, source, parameters=None):
14 total = 0.0 14 total = 0.0
15 barResults = [] 15 barResults = []
16 numberOfNotes = 0 16 numberOfNotes = 0
17 17
18 barlist = None 18 barlist = None
19 19
20 if isinstance(source, BarList): 20 if isinstance(source, BarList):
21 barlist = source 21 barlist = source
22 sourceType = "bar list" 22 sourceType = "bar list"
23 elif isinstance(source, Bar):
24 barlist = BarList().append(source)
25 sourceType = "single bar"
23 elif isinstance(source, basestring): 26 elif isinstance(source, basestring):
24 #treat source as a filename 27 #treat source as a filename
25 sourceType = source 28 sourceType = source
26 if source[-4:]==".mid": 29 if source[-4:]==".mid":
27 import readmidi 30 import readmidi
35 print "Error in syncopation_barlist_permodel(): Unrecognised file type." 38 print "Error in syncopation_barlist_permodel(): Unrecognised file type."
36 else: 39 else:
37 print "Error in syncopation_barlist_permodel(): unrecognised source type." 40 print "Error in syncopation_barlist_permodel(): unrecognised source type."
38 41
39 barsDiscarded=0 42 barsDiscarded=0
43 discardedlist = []
44 includedlist = []
40 45
41 if barlist!=None: 46 if barlist!=None:
42 for bar in barlist: 47 for bar in barlist:
43 if not bar.is_empty(): 48 if not bar.is_empty():
44 barSyncopation = sync_perbar_permodel(model, bar, parameters) 49 barSyncopation = sync_perbar_permodel(model, bar, parameters)
48 53
49 barResults.append(barSyncopation) 54 barResults.append(barSyncopation)
50 if barSyncopation != None: 55 if barSyncopation != None:
51 total += barSyncopation 56 total += barSyncopation
52 numberOfNotes += sum(bar.get_binary_sequence()) 57 numberOfNotes += sum(bar.get_binary_sequence())
58 includedlist.append(barlist.index(bar))
53 else: 59 else:
54 barsDiscarded += 1 60 barsDiscarded += 1
61 discardedlist.append(barlist.index(bar))
55 print 'Model could not measure bar %d, returning None.' % barlist.index(bar) 62 print 'Model could not measure bar %d, returning None.' % barlist.index(bar)
56 63
57 import WNBD 64 import WNBD
58 if model is WNBD: 65 if model is WNBD:
59 total = total / numberOfNotes 66 total = total / numberOfNotes
60 67
61 average = total / (len(barResults)-barsDiscarded) 68 average = total / (len(barResults)-barsDiscarded)
62 69
63 return {"summed_syncopation":total, "average_syncopation_per_bar":average, "source":sourceType, "number_of_bars":len(barResults), "number_of_bars_not_measured":barsDiscarded, "syncopation_by_bar":barResults} 70 return {
71 "model_name":model.__name__ ,
72 "summed_syncopation":total,
73 "mean_syncopation_per_bar":average,
74 "source":sourceType,
75 "number_of_bars":len(barResults),
76 "number_of_bars_not_measured":barsDiscarded,
77 "bars_with_valid_output":includedlist,
78 "syncopation_by_bar":barResults
79 }
64 80
65 81
66 82
67 def results_to_xml(results, outputFilename): 83 def results_to_xml(results, outputFilename):
68 from xml.etree.ElementTree import Element, ElementTree 84 from xml.etree.ElementTree import Element, ElementTree