comparison Syncopation models/basic_functions.py @ 1:b2da092dc2e0

The consolidated syncopation software. Have finished individual model and basic functions. Need to revise the coding in main.py, and add rhythm-input interface.
author Chunyang Song <csong@eecs.qmul.ac.uk>
date Sun, 05 Oct 2014 21:52:41 +0100
parents
children
comparison
equal deleted inserted replaced
0:76ce27beba95 1:b2da092dc2e0
1 # This python file is a collection of basic functions that are used in the syncopation models.
2
3 import math
4
5 # The concatenation function is used to concatenate two sequences.
6 def concatenate(seq1,seq2):
7 return seq1+seq2
8
9 # The repetition function is to concatenate a sequence to itself for 'times' number of times.
10 def repeat(seq,times):
11 new_seq = list(seq)
12 if times >= 1:
13 for i in range(times-1):
14 new_seq = concatenate(new_seq,seq)
15 else:
16 #print 'Error: repetition times needs to be no less than 1.'
17 new_seq = []
18 return new_seq
19
20 # The subdivision function is to equally subdivide a sequence into 'divisor' number of segments.
21 def subdivide(seq,divisor):
22 subSeq = []
23 if len(seq) % divisor != 0:
24 print 'Error: rhythmic sequence cannot be equally subdivided.'
25 else:
26 n = len(seq) / divisor
27 start , end = 0, n
28 for i in range(divisor):
29 subSeq.append(seq[start : end])
30 start = end
31 end = end + n
32 return subSeq
33
34
35 # The ceiling function is to round each number inside a sequence up to its nearest integer.
36 def ceiling(seq):
37 seq_ceil = []
38 for s in seq:
39 seq_ceil.append(int(math.ceil(s)))
40 return seq_ceil
41
42 # The find_divisor function returns a list of all possible divisors for a length of sequence.
43 def find_divisor(number):
44 divisors = [1]
45 for i in range(2,number+1):
46 if number%i ==0:
47 divisors.append(i)
48 return divisors
49
50 # The find_divisor function returns a list of all possible divisors for a length of sequence.
51 def find_prime_factors(number):
52 prime_factors = find_divisor(number)
53
54 def is_prime(num):
55 if num < 2:
56 return False
57 if num == 2:
58 return True
59 else:
60 for div in range(2,num):
61 if num % div == 0:
62 return False
63 return True
64
65 for i in range(len(prime_factors)-1,0,-1):
66 if is_prime(prime_factors[i]) == False:
67 del prime_factors[i]
68
69 return prime_factors
70
71 # The min_timeSpan function searches for the shortest possible time-span representation for a sequence.
72 def get_min_timeSpan(seq):
73 min_ts = [1]
74 for d in find_divisor(len(seq)):
75 segments = subdivide(seq,d)
76 if len(segments)!=0:
77 del min_ts[:]
78 for s in segments:
79 min_ts.append(s[0])
80 if sum(min_ts) == sum(seq):
81 break
82 return min_ts
83
84 # get_note_indices returns all the indices of all the notes in this sequence
85 def get_note_indices(seq):
86 note_indices = []
87
88 for index in range(len(seq)):
89 if seq[index] != 0:
90 note_indices.append(index)
91
92 return note_indices
93
94 # The get_H returns a sequence of metrical weight for a certain metrical level (horizontal),
95 # given the sequence of metrical weights in a hierarchy (vertical) and a sequence of subdivisions.
96 def get_H(weight_seq,subdivision_seq, level):
97 H = []
98 #print len(weight_seq), len(subdivision_seq), level
99 if (level <= len(subdivision_seq)-1) & (level <= len(weight_seq)-1):
100 if level == 0:
101 H = repeat([weight_seq[0]],subdivision_seq[0])
102 else:
103 H_pre = get_H(weight_seq,subdivision_seq,level-1)
104 for h in H_pre:
105 H = concatenate(H, concatenate([h], repeat([weight_seq[level]],subdivision_seq[level]-1)))
106 else:
107 print 'Error: a subdivision factor or metrical weight is not defined for the request metrical level.'
108 return H
109
110 # The get_subdivision_seq function returns the subdivision sequence of several common time-signatures defined by GTTM,
111 # or ask for the top three level of subdivision_seq manually set by the user.
112 def get_subdivision_seq(timesig, L_max):
113 subdivision_seq = []
114
115 if timesig == '2/4' or timesig == '4/4':
116 subdivision_seq = [1,2,2]
117 elif timesig == '3/4':
118 subdivision_seq = [1,3,2]
119 elif timesig == '6/8':
120 subdivision_seq = [1,2,3]
121 elif timesig == '9/8':
122 subdivision_seq = [1,3,3]
123 elif timesig == '12/8':
124 subdivision_seq = [1,4,3]
125 elif timesig == '5/4':
126 subdivision_seq = [1,5,2]
127 elif timesig == '7/4':
128 subdivision_seq = [1,7,2]
129 elif timesig == '11/4':
130 subdivision_seq = [1,11,2]
131 else:
132 print 'Undefined time-signature. Please indicate subdivision sequence for this requested time-signature, e.g. [1,2,2] for 4/4 meter.'
133 for i in range(3):
134 s = int(input('Enter the subdivision factor at metrical level '+str(i)+':'))
135 subdivision_seq.append(s)
136
137 if L_max > 2:
138 subdivision_seq = subdivision_seq + [2]*(L_max-2)
139 else:
140 subdivision_seq = subdivision_seq[0:L_max+1]
141
142 return subdivision_seq
143
144 # The split_by_bar function seperates the score representation of rhythm by bar lines,
145 # resulting in a list representingbar-by-bar rhythm sequence,
146 # e.g. rhythm = ['|',[ts1,td1,v1], [ts2,td2,v2], '|',[ts3,td3,v3],'|'...]
147 # rhythm_bybar = [ [ [ts1,td1,v1], [ts2,td2,v2] ], [ [ts3,td3,v3] ], [...]]
148 # def split_by_bar(rhythm):
149 # rhythm_bybar = []
150 # bar_index = []
151 # for index in range(len(rhythm)):
152 # if rhythm[index] == '|':
153
154 # return rhythm_bybar
155
156 # def yseq_to_vseq(yseq):
157 # vseq = []
158
159 # return vseq
160
161
162 # # testing
163 # print find_prime_factors(10)