comparison Syncopation models/syncopation.py @ 38:cc38b3047ed9

updated syncopation.y to allow output of sync for a bar list also fixed some problems in models and other modules
author christopherh <christopher.harte@eecs.qmul.ac.uk>
date Mon, 13 Apr 2015 23:06:49 +0100
parents 3a878de00d19
children 6371e8f21f7d
comparison
equal deleted inserted replaced
37:fefbe0d853c6 38:cc38b3047ed9
1 ''' 1 '''
2 Author: Chunyang Song 2 Author: Chunyang Song
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 from rhythm_parser import *
7 from music_objects import *
8
6 9
7 def sync_perbar_permodel (model, bar, parameters=None): 10 def sync_perbar_permodel (model, bar, parameters=None):
8 return model.get_syncopation(bar, parameters) 11 return model.get_syncopation(bar, parameters)
9 12
10 def syncopation_barlist_permodel(model, source, parameters=None): 13 def syncopation_barlist_permodel(model, source, parameters=None):
11 total = 0 14 total = 0.0
15 barResults = []
12 numberOfNotes = 0 16 numberOfNotes = 0
13 17
18 barlist = None
19
20 if isinstance(source, BarList):
21 barlist = source
22 sourceType = "bar list"
23 elif isinstance(source, basestring):
24 #treat source as a filename
25 sourceType = source
26 if source[-4:]==".mid":
27 import readmidi
28 midiFile = readmidi.read_midi_file(source)
29 barlist = readmidi.get_bars_from_midi(midiFile)
30
31 elif source[-4:]==".rhy":
32 #import rhythm_parser
33 barlist = read_rhythm(source)
34 else:
35 print "Error in syncopation_barlist_permodel(): Unrecognised file type."
36 else:
37 print "Error in syncopation_barlist_permodel(): unrecognised source type."
14 38
39 barsDiscarded=0
15 40
16 for bar in barlist: 41 if barlist!=None:
17 if sync_perbar_permodel(model, bar, parameters) != None: 42 for bar in barlist:
18 total += sync_perbar_permodel(model, bar, parameters) 43 if not bar.is_empty():
19 numberOfNotes += sum(bar.get_binary_sequence()) 44 barSyncopation = sync_perbar_permodel(model, bar, parameters)
20 else: 45 else:
21 print 'Bar %d cannot be measured, returning None.' % barlist.index(bar) 46 barSyncopation = None
47 print 'Bar %d cannot be measured because it is empty, returning None.' % barlist.index(bar)
48
49 barResults.append(barSyncopation)
50 if barSyncopation != None:
51 total += barSyncopation
52 numberOfNotes += sum(bar.get_binary_sequence())
53 else:
54 barsDiscarded += 1
55 print 'Model could not measure bar %d, returning None.' % barlist.index(bar)
22 56
23 if model is WNBD: 57 import WNBD
24 total = (float) total/ numberOfNotes 58 if model is WNBD:
59 total = total / numberOfNotes
25 60
26 # def sync_perbar_permodel(seq, model, timesig = None, subdivision_seq = None, weight_seq = None, L_max = 5, prebar_seq = None, postbar_seq = None, strong_beat_level = None): 61 average = total / (len(barResults)-barsDiscarded)
27 # syncopation = None
28 62
29 # if seq == None or model == None: 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}
30 # print 'Error: please indicate rhythm sequence and syncopation model.'
31 64
32 # elif timesig == None and subdivision_seq == None:
33 # print 'Error: please indicate either time signature or subdivision sequence.'
34
35 # else:
36 # while subdivision_seq == None:
37 # from basic_functions import get_subdivision_seq
38 # subdivision_seq = get_subdivision_seq(timesig, L_max)
39 65
40 # # The get_rhythm_category function is used to detect rhythm category: monorhythm or polyrhythm.
41 # # For monorhythms, all prime factors of the length of minimum time-span representation of this sequence are
42 # # elements of its subdivision_seq, otherwise it is polyrhythm;
43 # # e.g. prime_factors of polyrhythm 100100101010 in 4/4 is [2,3] but subdivision_seq = [1,2,2] for 4/4
44 # def get_rhythm_category():
45 # rhythm_category = 'mono'
46 # from basic_functions import get_min_timeSpan, find_prime_factors
47 # for f in find_prime_factors(len(get_min_timeSpan(seq))):
48 # if not (f in subdivision_seq):
49 # rhythm_category = 'poly'
50 # break
51 # return rhythm_category
52
53 # rhythm_category = get_rhythm_category()
54 66
55 # if model == 'LHL': 67 def results_to_xml(results, outputFilename):
56 # import LHL 68 from xml.etree.ElementTree import Element, ElementTree
57 # if weight_seq == None:
58 # weight_seq = range(0,-L_max,-1)
59 # syncopation = LHL.get_syncopation(seq, subdivision_seq, weight_seq, prebar_seq, rhythm_category)
60 # elif model == 'PRS':
61 # import PRS
62 # syncopation = PRS.get_syncopation(seq, subdivision_seq, postbar_seq, rhythm_category)
63 # elif model == 'TMC':
64 # import TMC
65 # if weight_seq == None:
66 # weight_seq = range(L_max+1,0,-1)
67 # syncopation = TMC.get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category)
68 # elif model == 'SG':
69 # import SG
70 # if weight_seq == None:
71 # weight_seq = range(L_max+1)
72 # syncopation = SG.get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category)
73 # elif model == 'KTH':
74 # import KTH
75 # syncopation = KTH.get_syncopation(seq, timesig, postbar_seq)
76 # elif model == 'TOB':
77 # import TOB
78 # syncopation = TOB.get_syncopation(seq)
79 # elif model == 'WNBD':
80 # import WNBD
81 # if strong_beat_level == None:
82 # if timesig == '4/4':
83 # strong_beat_level = 2
84 # else:
85 # strong_beat_level = 1
86 # syncopation = WNBD.get_syncopation(seq, subdivision_seq, strong_beat_level, postbar_seq)
87 69
88 # else: 70 elem = Element("syncopation_results")
89 # print 'Error: undefined syncopation model.'
90 71
91 # return syncopation 72 for key, val in results.items():
73 child = Element(key)
74 child.text = str(val)
75 elem.append(child)
92 76
93 # def syncopation_all(rhythm, model, timesig, subdivision_seq = None, weight_seq = None, L_max = 5, strong_beat_level = None): 77 ElementTree(elem).write(outputFilename)
94 # syncopation = 0
95 # # Chope rhythm into seq
96 # # ...
97 78
98 # for (seq_perbar in seq):
99 # sync_perbar = syncopation_perbar(seq_perbar,model, timesig, subdivision_seq, weight_seq, L_max, strong_beat_level)
100 # if sync_perbar != None:
101 # syncopation = syncopation + sync_perbar
102 79
103 # return syncopation
104