comparison Syncopation models/parameter_setter.py @ 9:c2843ef4de2c

changing filenames to Python conventions
author csong
date Fri, 03 Apr 2015 11:41:01 +0100
parents
children 9030967a05f8
comparison
equal deleted inserted replaced
8:2c5df6a4a22f 9:c2843ef4de2c
1 '''
2 Author: Chunyang Song
3 Institution: Centre for Digital Music, Queen Mary University of London
4 '''
5
6 # Set the parameters: subdivision_seq, weight_seq, L_max, strong_beat_level
7
8 L_max = 5
9
10 # {'key': time-signature} :
11 # {'value': [subdivision_seq, theoretical beat-level represented by index in the subdivision_seq list]}
12 timesigBase = {
13 '2/2': [[1,2,2,2,2],1],
14 '3/2': [[1,3,2,2,2],1],
15 '4/2': [[1,2,2,2,2],1],
16 '2/4': [[1,2,2,2,2],1],
17 '3/4': [[1,3,2,2,2],1],
18 '4/4': [[1,2,2,2,2],2],
19 '5/4': [[1,5,2,2,2],1],
20 '7/4': [[1,7,2,2,2],1],
21 '3/8': [[1,3,2,2,2],1],
22 '5/8': [[1,5,2,2,2],1],
23 '6/8': [[1,2,3,2,2],1],
24 '9/8': [[1,3,3,2,2],1],
25 '12/8':[[1,2,2,3,2],2],
26 }
27
28
29 def addTimesig(timesig, subdivision_seq, beat_level):
30 if isTSValid(timesig,subdivision_seq,beat_level):
31 if timesig in timesigBase:
32 print 'This time-signature is existed already.'
33 else:
34 timesigBase[timesig] = [subdivision_seq, beat_level]
35 writeTimesig()
36
37 def updateTimesig(timesig, subdivision_seq, beat_level):
38 if isTSValid(timesig,subdivision_seq,beat_level):
39 if timesig in timesigBase:
40 print 'Original settings for', timesig, ':',timesigBase[timesig]
41 timesigBase[timesig] = [subdivision_seq, beat_level]
42 print 'Changed into:',timesigBase[timesig]
43 writeTimesig()
44
45 def isTSValid(timesig, subdivision_seq, beat_level):
46 isValid = False
47 if ('/' not in timesig) or (not timesig.split('/')[0].isdigit()) or (not timesig.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 subdivision_seq != [s for s in subdivision_seq 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 beat_level >= len(subdivision_seq):
52 print 'Error: beat-level exceeds the range of subdivision sequence list.'
53 else:
54 isValid = True
55 return isValid
56
57 def writeTimesig():
58 import cPickle as pickle
59 timesigFile = open('TimeSignature.pkl', 'wb')
60 pickle.dump(timesigBase, timesigFile)
61 timesigFile.close()
62
63 def readTimesig():
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 viewTimesigBase():
71 data = readTimesig()
72 for timesig, settings in data.items():
73 print timesig, settings
74
75 def set_L_max(number):
76 L_max = number
77
78 def get_subdivision_seq(timesig):
79 if timesig in readTimesig():
80 return timesigBase[timesig][0]
81 else:
82 print 'Error: the subdivision sequence for this time-signature is not defined.'
83 return None
84
85 def get_beat_level(timesig):
86 if timesig in readTimesig():
87 return timesigBase[timesig][1]
88 else:
89 print 'Error: the subdivision sequence for this time-signature is not defined.'
90 return None