annotate Syncopation models/parameter_setter.py @ 14:6b6f8e3d7262

updating lhl and prs
author csong <csong@eecs.qmul.ac.uk>
date Fri, 03 Apr 2015 17:10:57 +0100
parents c2843ef4de2c
children 9030967a05f8
rev   line source
csong@9 1 '''
csong@9 2 Author: Chunyang Song
csong@9 3 Institution: Centre for Digital Music, Queen Mary University of London
csong@9 4 '''
csong@9 5
csong@9 6 # Set the parameters: subdivision_seq, weight_seq, L_max, strong_beat_level
csong@9 7
csong@9 8 L_max = 5
csong@9 9
csong@9 10 # {'key': time-signature} :
csong@9 11 # {'value': [subdivision_seq, theoretical beat-level represented by index in the subdivision_seq list]}
csong@9 12 timesigBase = {
csong@9 13 '2/2': [[1,2,2,2,2],1],
csong@9 14 '3/2': [[1,3,2,2,2],1],
csong@9 15 '4/2': [[1,2,2,2,2],1],
csong@9 16 '2/4': [[1,2,2,2,2],1],
csong@9 17 '3/4': [[1,3,2,2,2],1],
csong@9 18 '4/4': [[1,2,2,2,2],2],
csong@9 19 '5/4': [[1,5,2,2,2],1],
csong@9 20 '7/4': [[1,7,2,2,2],1],
csong@9 21 '3/8': [[1,3,2,2,2],1],
csong@9 22 '5/8': [[1,5,2,2,2],1],
csong@9 23 '6/8': [[1,2,3,2,2],1],
csong@9 24 '9/8': [[1,3,3,2,2],1],
csong@9 25 '12/8':[[1,2,2,3,2],2],
csong@9 26 }
csong@9 27
csong@9 28
csong@9 29 def addTimesig(timesig, subdivision_seq, beat_level):
csong@9 30 if isTSValid(timesig,subdivision_seq,beat_level):
csong@9 31 if timesig in timesigBase:
csong@9 32 print 'This time-signature is existed already.'
csong@9 33 else:
csong@9 34 timesigBase[timesig] = [subdivision_seq, beat_level]
csong@9 35 writeTimesig()
csong@9 36
csong@9 37 def updateTimesig(timesig, subdivision_seq, beat_level):
csong@9 38 if isTSValid(timesig,subdivision_seq,beat_level):
csong@9 39 if timesig in timesigBase:
csong@9 40 print 'Original settings for', timesig, ':',timesigBase[timesig]
csong@9 41 timesigBase[timesig] = [subdivision_seq, beat_level]
csong@9 42 print 'Changed into:',timesigBase[timesig]
csong@9 43 writeTimesig()
csong@9 44
csong@9 45 def isTSValid(timesig, subdivision_seq, beat_level):
csong@9 46 isValid = False
csong@9 47 if ('/' not in timesig) or (not timesig.split('/')[0].isdigit()) or (not timesig.split('/')[1].isdigit()):
csong@9 48 print 'Error: invalid time-signature. Please indicate in the form of fraction, e.g. 4/4, 6/8 or 3/4.'
csong@9 49 elif subdivision_seq != [s for s in subdivision_seq if isinstance(s,int)]:
csong@9 50 print 'Error: invalid subdivision sequence. Please indicate in the form of list of numbers, e.g [1,2,2,2,2].'
csong@9 51 elif beat_level >= len(subdivision_seq):
csong@9 52 print 'Error: beat-level exceeds the range of subdivision sequence list.'
csong@9 53 else:
csong@9 54 isValid = True
csong@9 55 return isValid
csong@9 56
csong@9 57 def writeTimesig():
csong@9 58 import cPickle as pickle
csong@9 59 timesigFile = open('TimeSignature.pkl', 'wb')
csong@9 60 pickle.dump(timesigBase, timesigFile)
csong@9 61 timesigFile.close()
csong@9 62
csong@9 63 def readTimesig():
csong@9 64 import cPickle as pickle
csong@9 65 timesigFile = open('TimeSignature.pkl','rb')
csong@9 66 data = pickle.load(timesigFile)
csong@9 67 return data
csong@9 68 timesigFile.close()
csong@9 69
csong@9 70 def viewTimesigBase():
csong@9 71 data = readTimesig()
csong@9 72 for timesig, settings in data.items():
csong@9 73 print timesig, settings
csong@9 74
csong@9 75 def set_L_max(number):
csong@9 76 L_max = number
csong@9 77
csong@9 78 def get_subdivision_seq(timesig):
csong@9 79 if timesig in readTimesig():
csong@9 80 return timesigBase[timesig][0]
csong@9 81 else:
csong@9 82 print 'Error: the subdivision sequence for this time-signature is not defined.'
csong@9 83 return None
csong@9 84
csong@9 85 def get_beat_level(timesig):
csong@9 86 if timesig in readTimesig():
csong@9 87 return timesigBase[timesig][1]
csong@9 88 else:
csong@9 89 print 'Error: the subdivision sequence for this time-signature is not defined.'
csong@9 90 return None