annotate Syncopation models/KTH.py @ 21:b6daddeefda9

working on KTH
author csong <csong@eecs.qmul.ac.uk>
date Tue, 07 Apr 2015 23:16:13 +0100
parents b959c2acb927
children 2dbc09ca8013
rev   line source
csong@1 1 '''
csong@1 2 Author: Chunyang Song
csong@1 3 Institution: Centre for Digital Music, Queen Mary University of London
csong@1 4
csong@1 5 '''
csong@1 6
csong@20 7 from basic_functions import get_min_timeSpan, get_note_indices, repeat
csong@1 8
csong@1 9 # To find the nearest power of 2 equal to or less than the given number
csong@20 10 def round_down_power_2(number):
csong@1 11 i = 0
csong@1 12 if number > 0:
csong@1 13 while pow(2,i) > number or number >= pow(2,i+1):
csong@1 14 i = i+1
csong@1 15 power2 = pow(2,i)
csong@1 16 else:
csong@1 17 print 'Error: numbers that are less than 1 cannot be rounded down to its nearest power of two.'
csong@1 18 power2 = None
csong@1 19 return power2
csong@1 20
csong@1 21 # To find the nearest power of 2 equal to or more than the given number
csong@20 22 def round_up_power_2(number):
csong@1 23 i = 0
csong@1 24 while pow(2,i) < number:
csong@1 25 i = i + 1
csong@1 26 return pow(2,i)
csong@1 27
csong@1 28 # To examine whether start_time is 'off-beat'
csong@20 29 def start(startTime, c_n):
csong@1 30 s = 0
csong@20 31 if startTime % c_n != 0:
csong@1 32 s = 2
csong@1 33 return s
csong@1 34
csong@1 35 # To examine whether end_time is 'off-beat'
csong@20 36 def end(endTime, c_n):
csong@1 37 s = 0
csong@20 38 if endTime % c_n != 0:
csong@1 39 s = 1
csong@1 40 return s
csong@1 41
csong@21 42 def get_syncopation(bar, parameters):
csong@21 43 syncopation = None
csong@1 44
csong@21 45 # KTH only deals with simple-duple meter where the number of beats per bar is a power of two.
csong@21 46 numerator = bar.get_time_signature().get_numerator()
csong@21 47 if numerator != round_down_power_2(numerator):
csong@21 48 print 'Warning: KTH model detects non simple-duple meter so returning None.'
csong@21 49 else:
csong@21 50 # retrieve note-sequence and next bar's note-sequence
csong@21 51 noteSequence = bar.get_note_sequence()
csong@21 52 nextbarNoteSequence = None
csong@21 53 if bar.get_next_bar() != None:
csong@21 54 nextbarNoteSequence = bar.get_next_bar().get_note_sequence()
csong@1 55
csong@21 56 # find the delta_t so that the time-span (in ticks) is minimized and the note durations are represented by possible smallest numbers
csong@21 57 # delta_t is the greatest common divisor of all the note durations
csong@21 58
csong@21 59
csong@21 60
csong@21 61
csong@21 62 # # To calculate syncopation value of the sequence in the given time-signature.
csong@21 63 # def get_syncopation(seq, timesig, postbar_seq):
csong@21 64 # syncopation = 0
csong@21 65
csong@21 66 # numerator = int(timesig.split("/")[0])
csong@21 67 # if numerator == round_down_power_2(numerator): # if is a binary time-signature
csong@21 68 # # converting to minimum time-span format
csong@21 69 # seq = get_min_timeSpan(seq)
csong@21 70 # if postbar_seq != None:
csong@21 71 # postbar_seq = get_min_timeSpan(postbar_seq)
csong@21 72
csong@21 73 # # sf is a stretching factor matching rhythm sequence and meter, as Keith defines the note duration as a multiple of 1/(2^d) beats where d is number of metrical level
csong@21 74 # sf = round_up_power_2(len(seq))
csong@1 75
csong@21 76 # # retrieve all the indices of all the notes in this sequence
csong@21 77 # note_indices = get_note_indices(seq)
csong@1 78
csong@21 79 # for i in range(len(note_indices)):
csong@21 80 # # Assuming start_time is the index of this note, end_time is the index of the following note
csong@21 81 # start_time = note_indices[i]*sf/float(len(seq))
csong@1 82
csong@21 83 # if i == len(note_indices)-1: # if this is the last note, end_time is the index of the following note in the next bar
csong@21 84 # if postbar_seq != None and postbar_seq != repeat([0],len(postbar_seq)):
csong@21 85 # next_index = get_note_indices(postbar_seq)[0]+len(seq)
csong@21 86 # end_time = next_index*sf/float(len(seq))
csong@21 87 # else: # or if the next bar is none or full rest, end_time is the end of this sequence.
csong@21 88 # end_time = sf
csong@21 89 # else:
csong@21 90 # end_time = note_indices[i+1]*sf/float(len(seq))
csong@1 91
csong@21 92 # duration = end_time - start_time
csong@21 93 # c_n = round_down_power_2(duration)
csong@21 94 # syncopation = syncopation + start(start_time,c_n) + end(end_time,c_n)
csong@21 95 # else:
csong@21 96 # print 'Error: KTH model can only deal with binary time-signature, e.g. 2/4 and 4/4. '
csong@21 97 # syncopation = None
csong@1 98
csong@21 99 # return syncopation