comparison Syncopation models/basic_functions.py @ 26:d9d22e6f396d

fixed SG!
author csong <csong@eecs.qmul.ac.uk>
date Sun, 12 Apr 2015 15:53:58 +0100
parents df1e7c378ee0
children 5de1cb45c145
comparison
equal deleted inserted replaced
23:df1e7c378ee0 26:d9d22e6f396d
76 else: 76 else:
77 for odd in range(3, int(math.sqrt(number) + 1), 2): 77 for odd in range(3, int(math.sqrt(number) + 1), 2):
78 if number % odd == 0: 78 if number % odd == 0:
79 isPrime = False 79 isPrime = False
80 return isPrime 80 return isPrime
81
82 # upsample a velocity sequence to certain length, e.g. [1,1] to [1,0,0,0,1,0,0,0]
83 def upsample_velocity_sequence(velocitySequence, length):
84 upsampledVelocitySequence = [0]*length
85 if length%len(velocitySequence) != 0:
86 print 'Error: the velocity sequence can only be upsampled to the interger times of its length.'
87 else:
88 scalingFactor = length/len(velocitySequence)
89 for index in range(len(velocitySequence)):
90 upsampledVelocitySequence[index*scalingFactor] = velocitySequence[index]
91 return upsampledVelocitySequence
92
81 93
82 # convert a velocity sequence to its minimum time-span representation 94 # convert a velocity sequence to its minimum time-span representation
83 def velocity_sequence_to_min_timespan(velocitySequence): 95 def velocity_sequence_to_min_timespan(velocitySequence):
84 minTimeSpanVelocitySeq = [1] 96 minTimeSpanVelocitySeq = [1]
85 for divisors in find_divisor(len(velocitySequence)): 97 for divisors in find_divisor(len(velocitySequence)):
116 note.duration = note.duration/delta_t 128 note.duration = note.duration/delta_t
117 129
118 return noteSequence 130 return noteSequence
119 131
120 132
121
122 # get_note_indices returns all the indices of all the notes in this velocity_sequence 133 # get_note_indices returns all the indices of all the notes in this velocity_sequence
123 def get_note_indices(velocitySequence): 134 def get_note_indices(velocitySequence):
124 noteIndices = [] 135 noteIndices = []
125 136
126 for index in range(len(velocitySequence)): 137 for index in range(len(velocitySequence)):
157 For monorhythms, all prime factors of the length of minimum time-span representation of this sequence are 168 For monorhythms, all prime factors of the length of minimum time-span representation of this sequence are
158 elements of its subdivision_seq, otherwise it is polyrhythm; 169 elements of its subdivision_seq, otherwise it is polyrhythm;
159 e.g. prime_factors of polyrhythm 100100101010 in 4/4 is [2,3] but subdivision_seq = [1,2,2] for 4/4 170 e.g. prime_factors of polyrhythm 100100101010 in 4/4 is [2,3] but subdivision_seq = [1,2,2] for 4/4
160 ''' 171 '''
161 rhythmCategory = 'mono' 172 rhythmCategory = 'mono'
162 for f in find_prime_factors(len(get_min_timeSpan(velocitySequence))): 173 for f in find_prime_factors(len(velocity_sequence_to_min_timespan(velocitySequence))):
163 if not (f in subdivisionSequence): 174 if not (f in subdivisionSequence):
164 rhythmCategory = 'poly' 175 rhythmCategory = 'poly'
165 break 176 break
166 return rhythmCategory 177 return rhythmCategory
167 178