annotate Syncopation models/basic_functions.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@2 1 # This python file is a collection of basic functions that are used in the syncopation models.
csong@2 2
csong@2 3 import math
csong@2 4
csong@2 5 # The concatenation function is used to concatenate two sequences.
csong@2 6 def concatenate(seq1,seq2):
csong@2 7 return seq1+seq2
csong@2 8
csong@2 9 # The repetition function is to concatenate a sequence to itself for 'times' number of times.
csong@2 10 def repeat(seq,times):
csong@2 11 new_seq = list(seq)
csong@2 12 if times >= 1:
csong@2 13 for i in range(times-1):
csong@2 14 new_seq = concatenate(new_seq,seq)
csong@2 15 else:
csong@2 16 #print 'Error: repetition times needs to be no less than 1.'
csong@2 17 new_seq = []
csong@2 18 return new_seq
csong@2 19
csong@2 20 # The subdivision function is to equally subdivide a sequence into 'divisor' number of segments.
csong@2 21 def subdivide(seq,divisor):
csong@2 22 subSeq = []
csong@2 23 if len(seq) % divisor != 0:
csong@2 24 print 'Error: rhythmic sequence cannot be equally subdivided.'
csong@2 25 else:
csong@2 26 n = len(seq) / divisor
csong@2 27 start , end = 0, n
csong@2 28 for i in range(divisor):
csong@2 29 subSeq.append(seq[start : end])
csong@2 30 start = end
csong@2 31 end = end + n
csong@2 32 return subSeq
csong@2 33
csong@2 34
csong@2 35 # The ceiling function is to round each number inside a sequence up to its nearest integer.
csong@2 36 def ceiling(seq):
csong@2 37 seq_ceil = []
csong@2 38 for s in seq:
csong@2 39 seq_ceil.append(int(math.ceil(s)))
csong@2 40 return seq_ceil
csong@2 41
csong@2 42 # The find_divisor function returns a list of all possible divisors for a length of sequence.
csong@2 43 def find_divisor(number):
csong@2 44 divisors = [1]
csong@2 45 for i in range(2,number+1):
csong@2 46 if number%i ==0:
csong@2 47 divisors.append(i)
csong@2 48 return divisors
csong@2 49
csong@2 50 # The find_divisor function returns a list of all possible divisors for a length of sequence.
csong@2 51 def find_prime_factors(number):
csong@20 52 primeFactors = find_divisor(number)
csong@2 53
csong@20 54 # remove 1 because 1 is not prime number
csong@20 55 del primeFactors[0]
csong@2 56
csong@20 57 # reversely traverse all the divisors list and once find a non-prime then delete
csong@20 58 for i in range(len(primeFactors)-1,0,-1):
csong@20 59 # print primeFactors[i], is_prime(primeFactors[i])
csong@20 60 if not is_prime(primeFactors[i]):
csong@20 61 del primeFactors[i]
csong@2 62
csong@20 63 return primeFactors
csong@20 64
csong@20 65 def is_prime(number):
csong@20 66 isPrime = True
csong@20 67 # 0 or 1 is not prime numbers
csong@20 68 if number < 2:
csong@20 69 isPrime = False
csong@20 70 # 2 is the only even prime number
csong@20 71 elif number == 2:
csong@20 72 pass
csong@20 73 # all the other even numbers are non-prime
csong@20 74 elif number % 2 == 0:
csong@20 75 isPrime = False
csong@20 76 else:
csong@20 77 for odd in range(3, int(math.sqrt(number) + 1), 2):
csong@20 78 if number % odd == 0:
csong@20 79 isPrime = False
csong@20 80 return isPrime
csong@2 81
csong@2 82 # The min_timeSpan function searches for the shortest possible time-span representation for a sequence.
csong@2 83 def get_min_timeSpan(seq):
csong@20 84 minTimeSpan = [1]
csong@2 85 for d in find_divisor(len(seq)):
csong@2 86 segments = subdivide(seq,d)
csong@2 87 if len(segments)!=0:
csong@20 88 del minTimeSpan[:]
csong@2 89 for s in segments:
csong@20 90 minTimeSpan.append(s[0])
csong@20 91 if sum(minTimeSpan) == sum(seq):
csong@2 92 break
csong@20 93 return minTimeSpan
csong@2 94
csong@2 95 # get_note_indices returns all the indices of all the notes in this sequence
csong@20 96 def get_note_indices(sequence):
csong@20 97 noteIndices = []
csong@2 98
csong@20 99 for index in range(len(sequence)):
csong@20 100 if sequence[index] != 0:
csong@20 101 noteIndices.append(index)
csong@2 102
csong@20 103 return noteIndices
csong@2 104
csong@2 105 # The get_H returns a sequence of metrical weight for a certain metrical level (horizontal),
csong@2 106 # given the sequence of metrical weights in a hierarchy (vertical) and a sequence of subdivisions.
csong@19 107 def get_H(weightSequence,subdivisionSequence, level):
csong@2 108 H = []
csong@2 109 #print len(weight_seq), len(subdivision_seq), level
csong@19 110 if (level <= len(subdivisionSequence)-1) and (level <= len(weightSequence)-1):
csong@2 111 if level == 0:
csong@19 112 H = repeat([weightSequence[0]],subdivisionSequence[0])
csong@2 113 else:
csong@19 114 H_pre = get_H(weightSequence,subdivisionSequence,level-1)
csong@2 115 for h in H_pre:
csong@19 116 H = concatenate(H, concatenate([h], repeat([weightSequence[level]],subdivisionSequence[level]-1)))
csong@2 117 else:
csong@2 118 print 'Error: a subdivision factor or metrical weight is not defined for the request metrical level.'
csong@2 119 return H
csong@2 120
csong@21 121 def calculate_time_span_ticks(numerator, denominator, ticksPerQuarter):
csong@21 122 return (numerator * ticksPerQuarter *4) / denominator
csong@21 123
csong@19 124 # # The get_subdivision_seq function returns the subdivision sequence of several common time-signatures defined by GTTM,
csong@19 125 # # or ask for the top three level of subdivision_seq manually set by the user.
csong@19 126 # def get_subdivision_seq(timesig, L_max):
csong@19 127 # subdivision_seq = []
csong@2 128
csong@19 129 # if timesig == '2/4' or timesig == '4/4':
csong@19 130 # subdivision_seq = [1,2,2]
csong@19 131 # elif timesig == '3/4' or timesig == '3/8':
csong@19 132 # subdivision_seq = [1,3,2]
csong@19 133 # elif timesig == '6/8':
csong@19 134 # subdivision_seq = [1,2,3]
csong@19 135 # elif timesig == '9/8':
csong@19 136 # subdivision_seq = [1,3,3]
csong@19 137 # elif timesig == '12/8':
csong@19 138 # subdivision_seq = [1,4,3]
csong@19 139 # elif timesig == '5/4' or timesig == '5/8':
csong@19 140 # subdivision_seq = [1,5,2]
csong@19 141 # elif timesig == '7/4' or timesig == '7/8':
csong@19 142 # subdivision_seq = [1,7,2]
csong@19 143 # elif timesig == '11/4' or timesig == '11/8':
csong@19 144 # subdivision_seq = [1,11,2]
csong@19 145 # else:
csong@19 146 # print 'Time-signature',timesig,'is undefined. Please indicate subdivision sequence for this requested time-signature, e.g. [1,2,2] for 4/4 meter.'
csong@19 147 # for i in range(3):
csong@19 148 # s = int(input('Enter the subdivision factor at metrical level '+str(i)+':'))
csong@19 149 # subdivision_seq.append(s)
csong@2 150
csong@19 151 # if L_max > 2:
csong@19 152 # subdivision_seq = subdivision_seq + [2]*(L_max-2)
csong@19 153 # else:
csong@19 154 # subdivision_seq = subdivision_seq[0:L_max+1]
csong@2 155
csong@19 156 # return subdivision_seq
csong@2 157
csong@9 158
csong@13 159 def get_rhythm_category(velocitySequence, subdivisionSequence):
csong@13 160 '''
csong@13 161 The get_rhythm_category function is used to detect rhythm category: monorhythm or polyrhythm.
csong@13 162 For monorhythms, all prime factors of the length of minimum time-span representation of this sequence are
csong@13 163 elements of its subdivision_seq, otherwise it is polyrhythm;
csong@13 164 e.g. prime_factors of polyrhythm 100100101010 in 4/4 is [2,3] but subdivision_seq = [1,2,2] for 4/4
csong@13 165 '''
csong@13 166 rhythmCategory = 'mono'
csong@13 167 for f in find_prime_factors(len(get_min_timeSpan(velocitySequence))):
csong@13 168 if not (f in subdivisionSequence):
csong@13 169 rhythmCategory = 'poly'
csong@9 170 break
csong@13 171 return rhythmCategory
csong@13 172
csong@13 173 def string_to_sequence(inputString):
csong@13 174 return map(int, inputString.split(','))
csong@9 175
csong@9 176
csong@2 177 # The split_by_bar function seperates the score representation of rhythm by bar lines,
csong@2 178 # resulting in a list representingbar-by-bar rhythm sequence,
csong@2 179 # e.g. rhythm = ['|',[ts1,td1,v1], [ts2,td2,v2], '|',[ts3,td3,v3],'|'...]
csong@2 180 # rhythm_bybar = [ [ [ts1,td1,v1], [ts2,td2,v2] ], [ [ts3,td3,v3] ], [...]]
csong@2 181 # def split_by_bar(rhythm):
csong@2 182 # rhythm_bybar = []
csong@2 183 # bar_index = []
csong@2 184 # for index in range(len(rhythm)):
csong@2 185 # if rhythm[index] == '|':
csong@2 186
csong@2 187 # return rhythm_bybar
csong@2 188
csong@2 189 # def yseq_to_vseq(yseq):
csong@2 190 # vseq = []
csong@2 191
csong@2 192 # return vseq
csong@2 193
csong@2 194
csong@2 195 # # testing
csong@20 196 # print find_prime_factors(10)
csong@20 197 # print find_prime_factors(2)
csong@20 198 # print find_prime_factors(12)
csong@20 199
csong@20 200
csong@20 201 # print is_prime(1) # False
csong@20 202 # print is_prime(2) # True
csong@20 203 # print is_prime(3) # True
csong@20 204 # print is_prime(29) # True
csong@20 205 # print is_prime(345) # False
csong@20 206 # print is_prime(999979) # True
csong@20 207 # print is_prime(999981) # False