annotate Syncopation models/syncopation.py @ 23:df1e7c378ee0

fixed KTH, and WNBD
author csong <csong@eecs.qmul.ac.uk>
date Sun, 12 Apr 2015 13:06:17 +0100
parents b6daddeefda9
children 5de1cb45c145
rev   line source
csong@9 1 '''
csong@9 2 Author: Chunyang Song
csong@9 3 Institution: Centre for Digital Music, Queen Mary University of London
csong@9 4
csong@9 5 '''
csong@9 6
csong@21 7 def sync_perbar_permodel (model, bar, parameters):
csong@21 8 return model.get_syncopation(bar, parameters)
csong@9 9
csong@23 10 def syncopation_barlist_permodel(model, barlist, parameters):
csong@23 11 total = 0
csong@23 12 numberOfNotes = 0
csong@23 13 for bar in barlist:
csong@23 14 if sync_perbar_permodel(model, bar, parameters) != None:
csong@23 15 total += sync_perbar_permodel(model, bar, parameters)
csong@23 16 numberOfNotes += sum(bar.get_binary_sequence())
csong@23 17 else:
csong@23 18 print 'Bar %d cannot be measured, returning None.' % barlist.index(bar)
csong@23 19
csong@23 20 if model is WNBD:
csong@23 21 total = (float) total/ numberOfNotes
csong@9 22
csong@21 23 # 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):
csong@21 24 # syncopation = None
csong@9 25
csong@21 26 # if seq == None or model == None:
csong@21 27 # print 'Error: please indicate rhythm sequence and syncopation model.'
csong@21 28
csong@21 29 # elif timesig == None and subdivision_seq == None:
csong@21 30 # print 'Error: please indicate either time signature or subdivision sequence.'
csong@9 31
csong@21 32 # else:
csong@21 33 # while subdivision_seq == None:
csong@21 34 # from basic_functions import get_subdivision_seq
csong@21 35 # subdivision_seq = get_subdivision_seq(timesig, L_max)
csong@9 36
csong@21 37 # # The get_rhythm_category function is used to detect rhythm category: monorhythm or polyrhythm.
csong@21 38 # # For monorhythms, all prime factors of the length of minimum time-span representation of this sequence are
csong@21 39 # # elements of its subdivision_seq, otherwise it is polyrhythm;
csong@21 40 # # e.g. prime_factors of polyrhythm 100100101010 in 4/4 is [2,3] but subdivision_seq = [1,2,2] for 4/4
csong@21 41 # def get_rhythm_category():
csong@21 42 # rhythm_category = 'mono'
csong@21 43 # from basic_functions import get_min_timeSpan, find_prime_factors
csong@21 44 # for f in find_prime_factors(len(get_min_timeSpan(seq))):
csong@21 45 # if not (f in subdivision_seq):
csong@21 46 # rhythm_category = 'poly'
csong@21 47 # break
csong@21 48 # return rhythm_category
csong@9 49
csong@21 50 # rhythm_category = get_rhythm_category()
csong@9 51
csong@21 52 # if model == 'LHL':
csong@21 53 # import LHL
csong@21 54 # if weight_seq == None:
csong@21 55 # weight_seq = range(0,-L_max,-1)
csong@21 56 # syncopation = LHL.get_syncopation(seq, subdivision_seq, weight_seq, prebar_seq, rhythm_category)
csong@21 57 # elif model == 'PRS':
csong@21 58 # import PRS
csong@21 59 # syncopation = PRS.get_syncopation(seq, subdivision_seq, postbar_seq, rhythm_category)
csong@21 60 # elif model == 'TMC':
csong@21 61 # import TMC
csong@21 62 # if weight_seq == None:
csong@21 63 # weight_seq = range(L_max+1,0,-1)
csong@21 64 # syncopation = TMC.get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category)
csong@21 65 # elif model == 'SG':
csong@21 66 # import SG
csong@21 67 # if weight_seq == None:
csong@21 68 # weight_seq = range(L_max+1)
csong@21 69 # syncopation = SG.get_syncopation(seq, subdivision_seq, weight_seq, L_max, rhythm_category)
csong@21 70 # elif model == 'KTH':
csong@21 71 # import KTH
csong@21 72 # syncopation = KTH.get_syncopation(seq, timesig, postbar_seq)
csong@21 73 # elif model == 'TOB':
csong@21 74 # import TOB
csong@21 75 # syncopation = TOB.get_syncopation(seq)
csong@21 76 # elif model == 'WNBD':
csong@21 77 # import WNBD
csong@21 78 # if strong_beat_level == None:
csong@21 79 # if timesig == '4/4':
csong@21 80 # strong_beat_level = 2
csong@21 81 # else:
csong@21 82 # strong_beat_level = 1
csong@21 83 # syncopation = WNBD.get_syncopation(seq, subdivision_seq, strong_beat_level, postbar_seq)
csong@9 84
csong@21 85 # else:
csong@21 86 # print 'Error: undefined syncopation model.'
csong@9 87
csong@21 88 # return syncopation
csong@9 89
csong@9 90 # def syncopation_all(rhythm, model, timesig, subdivision_seq = None, weight_seq = None, L_max = 5, strong_beat_level = None):
csong@9 91 # syncopation = 0
csong@9 92 # # Chope rhythm into seq
csong@9 93 # # ...
csong@9 94
csong@9 95 # for (seq_perbar in seq):
csong@9 96 # sync_perbar = syncopation_perbar(seq_perbar,model, timesig, subdivision_seq, weight_seq, L_max, strong_beat_level)
csong@9 97 # if sync_perbar != None:
csong@9 98 # syncopation = syncopation + sync_perbar
csong@9 99
csong@9 100 # return syncopation
csong@9 101
csong@9 102
csong@9 103 ### TESTING
csong@9 104 # clave = [1,0,0,1,0,0,1,0,0,0,1,0,1,0,0,0]
csong@9 105 # bf = [0,0,0,1,0,0,0,0,0,0,1,0]
csong@9 106 # rhythm = [0,1,0,1,0,1,0,1]
csong@9 107 # classic1 = [1,0,1,1]*3 + [1,0,0,0]
csong@9 108 # classic2 = [1,0,0,1]*3 + [1,0,0,0]
csong@9 109 # shiko = [1,0,1,1,0,1,1,0]
csong@9 110 # rumba = [1,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0]
csong@9 111 # soukous = [1,0,0,1,0,0,1,0,0,0,1,1,0,0,0,0]
csong@9 112 # gahu = [1,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0]
csong@9 113 # bossanova = [1,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0]
csong@9 114
csong@9 115 # classic12 = [1,0,0,1,1,1,1,0,0,1,1,1]
csong@9 116 # soli = [1,0,1,0,1,0,1,0,1,1,0,1]
csong@9 117
csong@9 118 # print sync_perbar(seq = clave, model = 'WNBD', timesig = '4/4')