Mercurial > hg > syncopation-dataset
comparison Syncopation models/parameter_setter.py @ 19:9030967a05f8
Refactored parameter_setter, basic_functions. Halfway fixing parameter argument in LHL model.
author | csong <csong@eecs.qmul.ac.uk> |
---|---|
date | Fri, 03 Apr 2015 22:57:27 +0100 |
parents | c2843ef4de2c |
children | b959c2acb927 |
comparison
equal
deleted
inserted
replaced
18:43fa04812800 | 19:9030967a05f8 |
---|---|
3 Institution: Centre for Digital Music, Queen Mary University of London | 3 Institution: Centre for Digital Music, Queen Mary University of London |
4 ''' | 4 ''' |
5 | 5 |
6 # Set the parameters: subdivision_seq, weight_seq, L_max, strong_beat_level | 6 # Set the parameters: subdivision_seq, weight_seq, L_max, strong_beat_level |
7 | 7 |
8 L_max = 5 | |
9 | |
10 # {'key': time-signature} : | 8 # {'key': time-signature} : |
11 # {'value': [subdivision_seq, theoretical beat-level represented by index in the subdivision_seq list]} | 9 # {'value': [subdivision_seq, theoretical beat-level represented by index in the subdivision_seq list]} |
12 timesigBase = { | 10 timeSignatureBase = { |
13 '2/2': [[1,2,2,2,2],1], | 11 '2/2': [[1,2,2,2,2],1], |
14 '3/2': [[1,3,2,2,2],1], | 12 '3/2': [[1,3,2,2,2],1], |
15 '4/2': [[1,2,2,2,2],1], | 13 '4/2': [[1,2,2,2,2],1], |
16 '2/4': [[1,2,2,2,2],1], | 14 '2/4': [[1,2,2,2,2],1], |
17 '3/4': [[1,3,2,2,2],1], | 15 '3/4': [[1,3,2,2,2],1], |
23 '6/8': [[1,2,3,2,2],1], | 21 '6/8': [[1,2,3,2,2],1], |
24 '9/8': [[1,3,3,2,2],1], | 22 '9/8': [[1,3,3,2,2],1], |
25 '12/8':[[1,2,2,3,2],2], | 23 '12/8':[[1,2,2,3,2],2], |
26 } | 24 } |
27 | 25 |
28 | 26 def add_time_signature(timeSignature, subdivisionSequence, beatLevel): |
29 def addTimesig(timesig, subdivision_seq, beat_level): | 27 if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel): |
30 if isTSValid(timesig,subdivision_seq,beat_level): | 28 if timeSignature in timesigBase: |
31 if timesig in timesigBase: | |
32 print 'This time-signature is existed already.' | 29 print 'This time-signature is existed already.' |
33 else: | 30 else: |
34 timesigBase[timesig] = [subdivision_seq, beat_level] | 31 timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel] |
35 writeTimesig() | 32 write_time_signature() |
36 | 33 |
37 def updateTimesig(timesig, subdivision_seq, beat_level): | 34 def update_time_signature(timeSignature, subdivisionSequence, beatLevel): |
38 if isTSValid(timesig,subdivision_seq,beat_level): | 35 if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel): |
39 if timesig in timesigBase: | 36 if timeSignature in timeSignatureBase: |
40 print 'Original settings for', timesig, ':',timesigBase[timesig] | 37 print 'Original settings for ', timeSignature, ':',timeSignatureBase[timeSignature] |
41 timesigBase[timesig] = [subdivision_seq, beat_level] | 38 timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel] |
42 print 'Changed into:',timesigBase[timesig] | 39 print 'Changed into:',timeSignatureBase[timeSignature] |
43 writeTimesig() | 40 write_time_signature() |
44 | 41 |
45 def isTSValid(timesig, subdivision_seq, beat_level): | 42 def is_time_signature_valid(timeSignature, subdivisionSequence, beatLevel): |
46 isValid = False | 43 isValid = False |
47 if ('/' not in timesig) or (not timesig.split('/')[0].isdigit()) or (not timesig.split('/')[1].isdigit()): | 44 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.' | 45 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)]: | 46 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].' | 47 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): | 48 elif beatLevel >= len(subdivisionSequence): |
52 print 'Error: beat-level exceeds the range of subdivision sequence list.' | 49 print 'Error: beat-level exceeds the range of subdivision sequence list.' |
53 else: | 50 else: |
54 isValid = True | 51 isValid = True |
55 return isValid | 52 return isValid |
56 | 53 |
57 def writeTimesig(): | 54 def write_time_signature(): |
58 import cPickle as pickle | 55 import cPickle as pickle |
59 timesigFile = open('TimeSignature.pkl', 'wb') | 56 timeSigFile = open('TimeSignature.pkl', 'wb') |
60 pickle.dump(timesigBase, timesigFile) | 57 pickle.dump(timeSignatureBase, timeSigFile) |
61 timesigFile.close() | 58 timeSigFile.close() |
62 | 59 |
63 def readTimesig(): | 60 def read_time_signature(): |
64 import cPickle as pickle | 61 import cPickle as pickle |
65 timesigFile = open('TimeSignature.pkl','rb') | 62 timeSigFile = open('TimeSignature.pkl','rb') |
66 data = pickle.load(timesigFile) | 63 data = pickle.load(timeSigFile) |
67 return data | 64 return data |
68 timesigFile.close() | 65 timeSigFile.close() |
69 | 66 |
70 def viewTimesigBase(): | 67 def print_time_signature_base(): |
71 data = readTimesig() | 68 data = read_time_signature() |
72 for timesig, settings in data.items(): | 69 for timeSignature, settings in data.items(): |
73 print timesig, settings | 70 print timeSignature, settings |
74 | 71 |
75 def set_L_max(number): | 72 # def get_subdivision_seq(timeSignature): |
76 L_max = number | 73 # if timeSignature in readTimesig(): |
74 # return timesigBase[timesig][0] | |
75 # else: | |
76 # print 'Error: the subdivision sequence for this time-signature is not defined.' | |
77 # return None | |
77 | 78 |
78 def get_subdivision_seq(timesig): | 79 # def get_beat_level(timesig): |
79 if timesig in readTimesig(): | 80 # if timesig in readTimesig(): |
80 return timesigBase[timesig][0] | 81 # return timesigBase[timesig][1] |
81 else: | 82 # else: |
82 print 'Error: the subdivision sequence for this time-signature is not defined.' | 83 # print 'Error: the subdivision sequence for this time-signature is not defined.' |
83 return None | 84 # 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 |