comparison Syncopation models/synpy/parameter_setter.py @ 45:6e9154fc58df

moving the code files to the synpy package directory
author christopherh <christopher.harte@eecs.qmul.ac.uk>
date Thu, 23 Apr 2015 23:52:04 +0100
parents
children e71028851131
comparison
equal deleted inserted replaced
44:144460f34b5e 45:6e9154fc58df
1 '''
2 Author: Chunyang Song
3 Institution: Centre for Digital Music, Queen Mary University of London
4 '''
5
6 # Set the parameters: time-signature, subdivision-sequence, strong-beat-level; Lmax; weight-sequence
7 # Important condition: Lmax needs to be no less than the length of subdivision-sequence and the length of weight-sequence
8
9
10 # {'key': time-signature} :
11 # {'value': [subdivision-sequence, theoretical beat-level represented by index in the subdivision-sequence list]}
12 timeSignatureBase = {
13 '2/2': [[1,2,2,2,2,2],1],
14 '3/2': [[1,3,2,2,2,2],1],
15 '4/2': [[1,2,2,2,2,2],1],
16 '2/4': [[1,2,2,2,2,2],1],
17 '3/4': [[1,3,2,2,2,2],1],
18 '4/4': [[1,2,2,2,2,2],2],
19 '5/4': [[1,5,2,2,2,2],1],
20 '7/4': [[1,7,2,2,2,2],1],
21 '3/8': [[1,3,2,2,2,2],1],
22 '5/8': [[1,5,2,2,2,2],1],
23 '6/8': [[1,2,3,2,2,2],1],
24 '9/8': [[1,3,3,2,2,2],1],
25 '12/8':[[1,2,2,3,2,2],2],
26 }
27
28
29 def add_time_signature(timeSignature, subdivisionSequence, beatLevel):
30 if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel):
31 if timeSignature in timesigBase:
32 print 'This time-signature is existed already.'
33 else:
34 timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel]
35 write_time_signature()
36
37 def update_time_signature(timeSignature, subdivisionSequence, beatLevel):
38 if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel):
39 if timeSignature in timeSignatureBase:
40 print 'Original settings for ', timeSignature, ':',timeSignatureBase[timeSignature]
41 timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel]
42 print 'Changed into:',timeSignatureBase[timeSignature]
43 write_time_signature()
44
45 def is_time_signature_valid(timeSignature, subdivisionSequence, beatLevel):
46 isValid = False
47 if ('/' not in timeSignature) or (not timeSignature.split('/')[0].isdigit()) or (not timeSignature.split('/')[1].isdigit()):
48 print 'Error: invalid time-signature. Please indicate in the form of fraction, e.g. 4/4, 6/8 or 3/4.'
49 elif subdivisionSequence != [s for s in subdivisionSequence if isinstance(s,int)]:
50 print 'Error: invalid subdivision sequence. Please indicate in the form of list of numbers, e.g [1,2,2,2,2].'
51 elif beatLevel >= len(subdivisionSequence):
52 print 'Error: beat-level exceeds the range of subdivision sequence list.'
53 else:
54 isValid = True
55 return isValid
56
57 def write_time_signature():
58 import cPickle as pickle
59 timeSigFile = open('TimeSignature.pkl', 'wb')
60 pickle.dump(timeSignatureBase, timeSigFile)
61 timeSigFile.close()
62
63 def read_time_signature():
64 import cPickle as pickle
65 timeSigFile = open('TimeSignature.pkl','rb')
66 data = pickle.load(timeSigFile)
67 return data
68 timeSigFile.close()
69
70 def print_time_signature_base():
71 data = read_time_signature()
72 for timeSignature, settings in data.items():
73 print timeSignature, settings
74
75
76 def are_parameters_valid(Lmax, weightSequence, subdivisionSequence):
77
78 # is_Lmax_valid() checks:
79 # 1. if Lmax is a non-negative integer
80 # 2. if Lmax is higher than the length of weightSequence and subdivisionSequence
81 def is_Lmax_valid():
82 isValid = False
83 if isinstance(Lmax,int) and Lmax > 0:
84 if Lmax <= len(subdivisionSequence)-1:
85 if Lmax <= len(weightSequence)-1:
86 isValid = True
87 else:
88 print 'Error: Lmax exceeds the length of weight-sequence. Either reduce Lmax, or provide a new weight-sequence whose length is greater or equal to Lmax.'
89 else:
90 print 'Error: Lmax exceeds the length of subdivision-sequence. Either reduce Lmax, or extend subdivision-sequence through updating time-signature (refer to update_time_signature function).'
91 else:
92 print 'Error: Lmax needs to be a positive integer.'
93 return isValid
94
95 # is_weight_sequence_valid() checks:
96 # 1. weightSequence is a list of numbers
97 # 2. the length of weightSequence is no less than Lmax
98 def is_weight_sequence_valid():
99 isValid = False
100 if isinstance(weightSequence,list) and weightSequence == [i for i in weightSequence if isinstance(i,int)]:
101 if len(weightSequence) >= Lmax:
102 isValid = True
103 else:
104 print 'Error: the length of weight-sequence needs to be greater or equal to Lmax.'
105 else:
106 print 'Error: the weight-sequence needs to be a list of integers.'
107 return isValid
108
109
110 if is_weight_sequence_valid() and is_Lmax_valid():
111 return True
112 else:
113 return False